Created
January 4, 2017 23:58
-
-
Save fdlk/63bac558fd02ada151730dc3375bb243 to your computer and use it in GitHub Desktop.
rsql grammar for 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
/* | |
* RSQL parser | |
*/ | |
or = head:and tail:("," and)* { | |
if(tail.length == 0) return head; | |
var result = [head], i; | |
for (i = 0; i < tail.length; i++) { | |
result.push(tail[i][1]); | |
} | |
return { operator: "OR", operands: result}; | |
} | |
and = head:constraint tail:(";" constraint)* { | |
if(tail.length == 0) return head; | |
var result = [head], i; | |
for (i = 0; i < tail.length; i++) { | |
result.push(tail[i][1]); | |
} | |
return { operator: "AND", operands: result}; | |
} | |
constraint = group / comparison | |
group = "(" o:or ")" { return o;} | |
comparison = s:selector c:comparisonop a:arguments { | |
return {selector: s, comparison: c, arguments: a} | |
} | |
selector = unreservedstr | |
comparisonop = $compfiql / $compalt | |
compfiql = ( "=" alpha* / "!" ) "=" | |
compalt = ( ">" / "<" ) "=" | |
alpha = [a-z] / [A-Z] | |
arguments = "(" _ head:value _ tail:("," _ value _)* ")" { | |
var result = [head], i; | |
for (i = 0; i < tail.length; i++) { | |
result.push(tail[i][2]); | |
} | |
return result;} / value | |
value = unreservedstr / doublequoted / singlequoted | |
unreservedstr = $unreserved+ | |
singlequoted = [\'] v:(escaped / [^'\\])* [\'] {return v.join("")} | |
doublequoted = [\"] v:(escaped / [^"\\])* [\"] {return v.join("")} | |
reserved = $["'();,=!~<>] | |
unreserved = $[^"'();,=!~<> ] | |
escaped = "\\" c:allchars { return c; } | |
allchars = $. | |
_ "whitespace" = [ \t]* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For use with https://pegjs.org/online