Created
January 15, 2014 09:04
-
-
Save joeriks/8433033 to your computer and use it in GitHub Desktop.
domain type DSL (peg grammar) version 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// do not use result cache, nor line and column tracking | |
/* sample: | |
Project | |
Id :key | |
Name | |
Write project name:label | |
You need to state the project name:err | |
Address | |
SomeValue :num | |
The idea is to create a json that is easy to use for code generation later. | |
*/ | |
{ var indentStack = [], indent = ""; } | |
start | |
= (INDENT? l:NAMESPACE { return l; })+ | |
NAMESPACEHEAD = c:[a-zA-Z.]+ " "* {return c.join("");} | |
NAMESPACE | |
= SAMEDENT type:(!EOL typeHead:NAMESPACEHEAD {return {"nameSpace":typeHead}}) EOL* | |
children:( INDENT c:TYPE* DEDENT { return c; })? | |
{ | |
if (children) type["types"]=children; | |
return type; } | |
TYPEHEAD = c:[a-zA-Z]+ " "* {return c.join("");} | |
TYPE | |
= SAMEDENT type:(!EOL typeHead:TYPEHEAD {return {"typeName":typeHead}}) EOL* | |
children:( INDENT c:MEMBER* DEDENT { return c; })? | |
{ | |
if (children) type["members"]=children; | |
return type; } | |
MEMBERHEAD = c:[a-zA-Z]+ " "* t:(":" t:MEMBERTYPE{return t})? {return {name:c.join(""),type:t==""?"string":t}} | |
MEMBERTYPE = type:('key'/'number'/'string') {return type;} | |
MEMBER | |
= SAMEDENT memberHead:(!EOL memberhead:MEMBERHEAD {return memberhead}) EOL* | |
children:( INDENT c:DETAIL* DEDENT { return c; })? | |
{ | |
if (children) memberHead["details"] = children; | |
return memberHead } | |
DETAILHEAD = c:[a-zA-Z ]* t:(":" t:DETAILTYPE{return t})? {return {name:c.join(""),type:t}} | |
DETAILTYPE = type:('label'/'err') {return type;} | |
DETAIL | |
= SAMEDENT detailHead:(!EOL detailHead:DETAILHEAD {return detailHead}) EOL? | |
{return detailHead} | |
EOL | |
= "\r\n" / "\n" / "\r" | |
SAMEDENT | |
= i:[ \t|" "]* &{ return i.join("") === indent; } | |
INDENT | |
= i:[ \t|" "]+ &{ return i.length > indent.length; } | |
{ | |
indentStack.push(indent); | |
indent = i.join(""); | |
pos = offset; | |
} | |
DEDENT | |
= { indent = indentStack.pop(); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some.NameSpace | |
Project | |
Id:key | |
Name | |
Write project name :label | |
You need to state the project name:err | |
Address | |
Person | |
Id:key | |
Name | |
Other.Namespace | |
Vehicle | |
Id:key | |
Name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://peg.arcanis.fr/1PvOax/17/