Categories: Uncategorized

[Parser][PEG]Try to use PEG.js for rich config file


Lately, I’m interested in PEG(Parsing Expression Grammar) which can treat
wider grammar than CFG(Context Free Grammar).
Then, I make new language for config file with it. PEG.js which generates parser for PEG is published on github.
I use it.
http://pegjs.majda.cz/ The language’s grammar which I make is like following.
start
= tree

tree
= stat:statement? { return stat; }

statement
= stat:if_statement { return stat.toString(); }
/ stat:proc_statement { return stat.toString(); }

if_statement
= fac:if_factor 'Y' edge branch1:proc_statement '\n'
'N' '\n' edge branch2:statement
{ return branch2.toString().match(/if/) ?
"if (" + fac + ")\n\t" + branch1 + '\nelse ' + branch2
: 'if (' + fac + ')\n\t' + branch1 + '\nelse\n\t' + branch2;}

proc_statement
= fac1:proc_factor [-|]+ fac2:proc_statement { return fac1 + '.then(' + fac2.slice(0,fac2.length-1) + ');'; }
/ factor:proc_factor { return factor + ';'; }

proc_factor
= '[' procexp:expression ']' { return procexp; }
if_factor
= '<' ifexp:expression '>' { return ifexp; }

expression
= elem:element ' ' cdr:arguments { return elem.toString() + '(' +cdr + ')'; }
/ elem:element { return elem.toString(); }

arguments
= elem:element ',' arg:arguments { return elem.toString() + ',' + arg; }
/ elem:element { return elem.toString(); }

element
= characters:[a-zA-Z0-9-_."]+ { return characters.join('') }

edge
= symbols:[-|\n]+ { return symbols.join('') }
The parser can analyze like following sentences.
<hasTwitterAccount id>Y---[getTweet id]---[outputTweets]
N
|
|
<hasYoutubeAccount id>Y-----[youtube_search keyword,30]
N
|
|
|
|
|
[alert "nothing"]
This project is published on github. The link is this.
zuqqhi2