Created
April 23, 2012 12:56
-
-
Save thomasfr/2470772 to your computer and use it in GitHub Desktop.
Simple query language parser in PEGjs
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
{ | |
"type": "OR", | |
"terms": [ | |
{ | |
"type": "AND", | |
"terms": [ | |
"foo", | |
"bar" | |
] | |
}, | |
{ | |
"type": "AND", | |
"terms": [ | |
"baz", | |
{ | |
"type": "OR", | |
"terms": [ | |
"foo2", | |
"foo3", | |
{ | |
"type": "AND", | |
"terms": [ | |
"baz1", | |
"baz2" | |
] | |
} | |
] | |
} | |
] | |
} | |
] | |
} |
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
{ | |
function makeNode(type, head, tail) { | |
return tail.length > 0 | |
? { | |
type: type, | |
terms: [head].concat(tail.map(function(t) { return t[1]; })) | |
} | |
: head; | |
} | |
} | |
OrExpression | |
= head:AndExpression tail:(OR AndExpression)* { return makeNode("OR", head, tail); } | |
AndExpression | |
= head:primary tail:(AND? primary)* { return makeNode("AND", head, tail); } | |
primary | |
= Word | |
/ GROUP_START OrExpression:OrExpression GROUP_END { return OrExpression; } | |
Word | |
= !OR !AND !GROUP_START !GROUP_END chars:[^ \r\n\t,\(\)]+ _ { return chars.join(""); } | |
OR | |
= "OR" _ | |
/ "," _ | |
AND | |
= "AND" _ | |
GROUP_START | |
= _ "(" _ | |
GROUP_END | |
= _ ")" _ | |
_ | |
= [ \r\n\t]* |
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
foo bar, baz (foo2, foo3, (baz1 baz2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment