Skip to content

Instantly share code, notes, and snippets.

@jnaglick
Last active December 19, 2015 06:25
Show Gist options
  • Save jnaglick/77aa9d92be34f96ee816 to your computer and use it in GitHub Desktop.
Save jnaglick/77aa9d92be34f96ee816 to your computer and use it in GitHub Desktop.
PEG grammar
grammar SrchStrings
rule statement
expression
end
rule expression
or_expression / and_expression / not_expression / literal
end
rule or_expression
loperand:(and_expression / not_expression / literal) whitespace op:or_operator whitespace roperand:(or_expression / and_expression / not_expression / literal) <BinaryExpression>
end
rule and_expression
loperand:(not_expression / literal) whitespace op:and_operator whitespace roperand:(and_expression / not_expression / literal) <BinaryExpression>
end
rule not_expression
op:not_operator whitespace operand:(not_expression / literal) <UnaryExpression>
end
rule or_operator
'OR'
end
rule and_operator
'AND'
end
rule not_operator
'NOT'
end
rule literal
quoted_literal / unquoted_literal / grouping
end
rule quoted_literal
'"' [\s\w]+ '"' <Literal>
end
rule unquoted_literal
!(and_operator / or_operator) [\w]+ <Literal>
end
rule grouping
"(" whitespace? expression whitespace? ")" <Grouping>
end
rule whitespace
[\s]+
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment