dfm support

Issue #43 new
Sorien created an issue

code below is pegjs dfm grammar, https://pegjs.org/online can create parser that converts dfm to ast, it has been teste at 100+ files (Delphi 2009) in one of my company projects maybe it could help you somehow

#!

start = object

ws  = [ \r\t\n]*

object          = ws t:("object" / "inherited") ws a:Identifier ":" ws b:Type ws (Index ws)*
                ws c:(object / Property)*
                ws "end" { var r = {}; r[t] = {name:a, type:b, items:c}; return r};

Property       = ws a:QualifiedIdent ws "=" ws b:PropertyValue ws { return {property:{name:a, value:b}}};

PropertyValue  = String        /
                 Number        /
                 Float         /
                 Identifier    /
                 Set           /
                 List          /
                 ItemList      /
                 Binary;

ItemList       = "<" list:(Item*) ">" {return {list}};

Item           = ws "item"
                 item:( Property* )
                 ws "end" { return {item} };

Type           = Identifier;

Set            = "[" first:(Identifier*) rest:( "," ws Identifier )* "]" {  
                    var result = first[0] == undefined ? [] : [first[0]];   
                    result = result.concat(rest.map(function(e) { return e[2]; }));
                    return {set:result};
                  };

List           = "(" list:(ws String / ws Float)* ")" { 
                   var result = [];   
                    result = result.concat(list.map(function(e) { return e[1]; }));
                    return {list:result};
                  };

IdentList      = Identifier ( "," Identifier )*;

QualifiedIdent = text: [_a-zA-Z0-9.]+ {return text.join("")};

Index          = "[" Number "]";

Identifier     = text: [_a-zA-Z0-9\.]+ {return text.join("")};

String         = first:(StringLiteral+) rest:(ws "+" ws StringLiteral)* {
                   var result = [];   
                   result = result.concat(rest.map(function(e) { return e[e.length-1] }));
                   return "'" + first[0] + result.join("")  + "'";  
                 }

StringLiteral  = str:(("'"[^'"]*"'") / ("#"[0-9]+))+{ 
                   var result = [];   
                   result = result.concat(str.map(function(e) { 
                     return e[1] == undefined ? '' : e[2] == undefined ? String.fromCharCode(e[1].join("")) : e[1].join(""); 
                   }));
                   return result.join("");
                 };

Number         = numbers:(('-')?[0-9]+) {return parseInt(numbers.join(""), 16)};

Float          = mark:('-')? left:[0-9]+ "." right:[0-9]+ { return parseFloat( (mark == null ? '' : mark) + left.join("") + "." +   right.join("")); }

Binary         = "{" bin:(ws [A-F0-9]+)* "}" {
                    var result = [];   
                    result = result.concat(bin.map(function(e) { return e[1].join(""); }));
                    return {bin:result.join("")};
                  };                  

Comments (2)

  1. Log in to comment