Skip to content

Instantly share code, notes, and snippets.

@fdlk
Created January 4, 2017 23:58
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 fdlk/63bac558fd02ada151730dc3375bb243 to your computer and use it in GitHub Desktop.
Save fdlk/63bac558fd02ada151730dc3375bb243 to your computer and use it in GitHub Desktop.
rsql grammar for pegjs
/*
* 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]*
@fdlk
Copy link
Author

fdlk commented Jan 4, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment