Last active
July 25, 2018 10:31
-
-
Save daffl/878d4b60eb45e3a19e2b to your computer and use it in GitHub Desktop.
An expression grammar for PEGJS
This file contains hidden or 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
start = (text / enclosedexpression)* | |
text = | |
characters:$((!open) c:any)+ { | |
return { | |
type: 'text', | |
value: characters | |
} | |
} | |
enclosedexpression = | |
open ws* e:expression ws* close { return e; } | |
open = "{{" | |
close = "}}" | |
any = . | |
expression = | |
main:path args:parameter* truthy:truthy falsy:falsy { | |
return { | |
type: 'expression', | |
path: main.value, | |
args: args, | |
truthy: truthy || null, | |
falsy: falsy || null | |
} | |
} | |
truthy = | |
ws+ "?" ws+ arg:argument { return arg; } | |
/ "" | |
falsy = | |
ws+ ":" ws+ arg:argument { return arg; } | |
/ "" | |
parameter = ws+ a:argument { return a; } | |
argument = path / string | |
path = | |
first:variable rest:("." s:variable { return s; })* { | |
return { | |
type: 'path', | |
value: [first].concat(rest) | |
}; | |
} | |
string "string" = | |
doublequote text:(doublequote_character*) doublequote { | |
return { type: 'string', value: text.join('') }; | |
} | |
/ singlequote text:(singlequote_character*) singlequote { | |
return { type: 'string', value: text.join('') }; | |
} | |
doublequote_character = | |
(!doublequote) c:character { return c; } | |
singlequote_character = | |
(!singlequote) c:character { return c; } | |
character = | |
unescaped | |
/ escape_sequence | |
escape_sequence "escape sequence" = escape_character sequence:( | |
doublequote | |
/ singlequote | |
/ "\\" | |
/ "/" | |
/ "b" { return "\b"; } | |
/ "f" { return "\f"; } | |
/ "n" { return "\n"; } | |
/ "r" { return "\r"; } | |
/ "t" { return "\t"; } | |
/ "u" digits:$(HEXDIG HEXDIG HEXDIG HEXDIG) { | |
return String.fromCharCode(parseInt(digits, 16)); | |
} | |
) | |
{ return sequence; } | |
variable = $([0-9a-zA-Z_\$]+) | |
escape_character = "\\" | |
doublequote "double quote" = '"' | |
singlequote "single quote" = "'" | |
unescaped = [\x20-\x21\x23-\x5B\x5D-\u10FFFF] | |
HEXDIG = [0-9a-f]i | |
ws "whitespace" = [ \t] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment