Created
April 19, 2012 15:49
-
-
Save dmajda/2421891 to your computer and use it in GitHub Desktop.
Simple query language parser
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:Word tail:(AND? Word)* { | |
return makeNode("AND", head, tail); | |
} | |
Word = !OR !AND chars:[a-zA-Z0-9]+ _ { return chars.join(""); } | |
OR = "OR" _ | |
AND = "AND" _ | |
_ = [ \r\n\t]* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment