| 1 |
comment = '#' (~'\n' anything)* |
| 2 |
hspace = ' ' | '\t' | comment |
| 3 |
vspace = '\r\n' | '\r' | '\n' |
| 4 |
ws = (hspace | vspace | comment)* |
| 5 |
|
| 6 |
number = ws ('-' barenumber:x -> t.Exactly(-x) |
| 7 |
|barenumber:x -> t.Exactly(x)) |
| 8 |
barenumber = '0' (('x'|'X') <hexdigit+>:hs -> int(hs, 16) |
| 9 |
|<octaldigit+>:ds -> int(ds, 8)) |
| 10 |
|<digit+>:ds -> int(ds) |
| 11 |
octaldigit = :x ?(x in '01234567' ) -> x |
| 12 |
hexdigit = :x ?(x in '0123456789ABCDEFabcdef') -> x |
| 13 |
|
| 14 |
escapedChar = '\\' ('n' -> "\n" |
| 15 |
|'r' -> "\r" |
| 16 |
|'t' -> "\t" |
| 17 |
|'b' -> "\b" |
| 18 |
|'f' -> "\f" |
| 19 |
|'"' -> '"' |
| 20 |
|'\'' -> "'" |
| 21 |
|'\\' -> "\\") |
| 22 |
|
| 23 |
character = ws '\'' (escapedChar | anything):c ws '\'' -> t.Exactly(c) |
| 24 |
|
| 25 |
string = ws '"' (escapedChar | ~('"') anything)*:c '"' -> t.Exactly(''.join(c)) |
| 26 |
|
| 27 |
name = <letter (letterOrDigit | '_')*> |
| 28 |
application = (ws '<' ws name:name |
| 29 |
(' ' !(self.applicationArgs(finalChar='>')):args '>' |
| 30 |
-> t.Apply(name, self.rulename, args) |
| 31 |
|ws '>' |
| 32 |
-> t.Apply(name, self.rulename, []))) |
| 33 |
|
| 34 |
expr1 = (application |
| 35 |
|ruleValue |
| 36 |
|semanticPredicate |
| 37 |
|semanticAction |
| 38 |
|number:n !(self.isTree()) -> n |
| 39 |
|character |
| 40 |
|string |
| 41 |
|ws '(' expr:e ws ')' -> e |
| 42 |
|(ws '[' expr:e ws ']' !(self.isTree()) |
| 43 |
-> t.List(e))) |
| 44 |
|
| 45 |
expr2 = (ws '~' ('~' expr2:e |
| 46 |
-> t.Lookahead(e) |
| 47 |
|expr2:e -> t.Not(e)) |
| 48 |
|expr1) |
| 49 |
|
| 50 |
expr3 = expr2:e |
| 51 |
('*' -> t.Many(e) |
| 52 |
|'+' -> t.Many1(e) |
| 53 |
|'?' -> t.Optional(e) |
| 54 |
| -> e):r |
| 55 |
(':' name:n -> t.Bind(n, r) |
| 56 |
| -> r) |
| 57 |
| (ws ':' name:n -> t.Bind(n, t.Apply("anything", self.rulename, []))) |
| 58 |
|
| 59 |
expr4 = expr3*:es -> t.And(es) |
| 60 |
|
| 61 |
expr = expr4:e (ws '|' expr4)*:es |
| 62 |
-> t.Or([e] + es) |
| 63 |
|
| 64 |
ruleValue = ws '=>' -> self.ruleValueExpr(False) |
| 65 |
|
| 66 |
semanticPredicate = ws '?(' -> self.semanticPredicateExpr() |
| 67 |
|
| 68 |
semanticAction = ws '!(' -> self.semanticActionExpr() |
| 69 |
|
| 70 |
ruleEnd = (hspace* vspace+) | end |
| 71 |
rulePart :requiredName = ws name:n ?(n == requiredName) |
| 72 |
!(setattr(self, "rulename", n)) |
| 73 |
expr4:args |
| 74 |
(ws '::=' expr:e ruleEnd |
| 75 |
-> t.And([args, e]) |
| 76 |
| ruleEnd -> args) |
| 77 |
rule = (ws ~~(name:n) rulePart(n):r |
| 78 |
(rulePart(n)+:rs -> t.Rule(n, t.Or([r] + rs)) |
| 79 |
| -> t.Rule(n, r))) |
| 80 |
|
| 81 |
grammar = rule*:rs ws -> t.Grammar(self.name, self.tree_target, rs) |