| 1 |
comment = '#' (~'\n' anything)* |
| 2 |
hspace = ' ' | '\t' | comment |
| 3 |
vspace = '\r\n' | '\r' | '\n' |
| 4 |
ws = (hspace | vspace | comment)* |
| 5 |
|
| 6 |
emptyline = hspace* vspace |
| 7 |
indentation = emptyline* hspace+ |
| 8 |
noindentation = emptyline* ~~~hspace |
| 9 |
|
| 10 |
number = ws |
| 11 |
('-' barenumber:x -> t.Exactly(-x, span=self.getSpan()) |
| 12 |
|barenumber:x -> t.Exactly(x, span=self.getSpan())) |
| 13 |
barenumber = '0' (('x'|'X') <hexdigit+>:hs -> int(hs, 16) |
| 14 |
|<octaldigit+>:ds -> int(ds, 8)) |
| 15 |
|<digit+>:ds -> int(ds) |
| 16 |
octaldigit = :x ?(x in '01234567' ) -> x |
| 17 |
hexdigit = :x ?(x in '0123456789ABCDEFabcdef') -> x |
| 18 |
|
| 19 |
escapedChar = '\\' ('n' -> "\n" |
| 20 |
|'r' -> "\r" |
| 21 |
|'t' -> "\t" |
| 22 |
|'b' -> "\b" |
| 23 |
|'f' -> "\f" |
| 24 |
|'"' -> '"' |
| 25 |
|'\'' -> "'" |
| 26 |
|'x' <hexdigit hexdigit>:d -> chr(int(d, 16)) |
| 27 |
|'\\' -> "\\") |
| 28 |
|
| 29 |
character = ws '\'' (~'\'' (escapedChar | anything))+:c |
| 30 |
ws '\'' -> t.Exactly(''.join(c), span=self.getSpan()) |
| 31 |
|
| 32 |
string = ws '"' (escapedChar | ~('"') anything)*:c |
| 33 |
ws '"' -> t.Token(''.join(c), span=self.getSpan()) |
| 34 |
|
| 35 |
name = <letter ('_' |letterOrDigit)*> |
| 36 |
|
| 37 |
args = ('(' !(self.applicationArgs(finalChar=')')):args ')' |
| 38 |
-> args |
| 39 |
| -> []) |
| 40 |
|
| 41 |
application = indentation? name:name args:args |
| 42 |
-> t.Apply(name, self.rulename, args, span=self.getSpan()) |
| 43 |
|
| 44 |
foreignApply = indentation? name:grammar_name '.' name:rule_name args:args |
| 45 |
-> t.ForeignApply(grammar_name, rule_name, self.rulename, args, span=self.getSpan()) |
| 46 |
|
| 47 |
traceable = !(self.startSpan()) |
| 48 |
( foreignApply |
| 49 |
| application |
| 50 |
| ruleValue |
| 51 |
| semanticPredicate |
| 52 |
| semanticAction |
| 53 |
| number:n !(self.isTree()) -> n |
| 54 |
| character |
| 55 |
| string) |
| 56 |
|
| 57 |
expr1 = traceable |
| 58 |
| ws '(' expr:e ws ')' -> e |
| 59 |
| ws '<' expr:e ws '>' |
| 60 |
-> t.ConsumedBy(e) |
| 61 |
| ws '[' expr?:e ws ']' !(self.isTree()) |
| 62 |
-> t.List(e) if e else t.List() |
| 63 |
|
| 64 |
expr2 = (ws '~' ('~' expr2:e -> t.Lookahead(e) |
| 65 |
| expr2:e -> t.Not(e) |
| 66 |
) |
| 67 |
|expr1) |
| 68 |
|
| 69 |
repeatTimes = (barenumber:x -> int(x)) | name |
| 70 |
|
| 71 |
expr3 = (expr2:e |
| 72 |
('*' -> t.Many(e) |
| 73 |
|'+' -> t.Many1(e) |
| 74 |
|'?' -> t.Optional(e) |
| 75 |
|customLabel:l -> t.Label(e, l) |
| 76 |
|'{' ws repeatTimes:start ws ( |
| 77 |
(',' ws repeatTimes:end ws '}' |
| 78 |
-> t.Repeat(start, end, e)) |
| 79 |
| ws '}' |
| 80 |
-> t.Repeat(start, start, e)) |
| 81 |
| -> e |
| 82 |
)):r |
| 83 |
(':' name:n -> t.Bind(n, r) |
| 84 |
| ':(' name:n (',' ws name)*:others ws ')' |
| 85 |
(-> [n] + others if others else n):n -> t.Bind(n, r) |
| 86 |
| -> r) |
| 87 |
|ws ':' name:n |
| 88 |
-> t.Bind(n, t.Apply("anything", self.rulename, [])) |
| 89 |
|
| 90 |
expr4 = expr3+:es -> es[0] if len(es) == 1 else t.And(es) |
| 91 |
|
| 92 |
expr = expr4:e (ws '|' expr4)*:es |
| 93 |
-> t.Or([e] + es) if es else e |
| 94 |
|
| 95 |
ruleValue = ws '->' -> self.ruleValueExpr(True) |
| 96 |
|
| 97 |
customLabel = (ws '^' ws '(' <(~')' anything)+>:e ')' -> e) ^ (customLabelException) |
| 98 |
|
| 99 |
semanticPredicate = ws '?(' -> self.semanticPredicateExpr() |
| 100 |
|
| 101 |
semanticAction = ws '!(' -> self.semanticActionExpr() |
| 102 |
|
| 103 |
ruleEnd = ((hspace* vspace+) | end) ^ (rule end) |
| 104 |
|
| 105 |
rulePart :requiredName = noindentation name:n ?(n == requiredName) |
| 106 |
!(setattr(self, "rulename", n)) |
| 107 |
expr4?:args |
| 108 |
(ws '=' expr:e ruleEnd |
| 109 |
-> t.And([args, e]) if args else e |
| 110 |
| ruleEnd -> args) |
| 111 |
|
| 112 |
rule = noindentation ~~(name:n) rulePart(n)+:rs -> t.Rule(n, t.Or(rs)) |
| 113 |
|
| 114 |
|
| 115 |
grammar = rule*:rs ws -> t.Grammar(self.name, self.tree_target, rs) |