Skip to content

Instantly share code, notes, and snippets.

@dmajda
Created April 19, 2012 15:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmajda/2421891 to your computer and use it in GitHub Desktop.
Save dmajda/2421891 to your computer and use it in GitHub Desktop.
Simple query language parser
{
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