| 1 |
#TreeTransformer |
| 2 |
Exactly(:x) -> [t.Match(x)] |
| 3 |
Token(:x) -> [t.Match(x)] |
| 4 |
Many(@x) -> [t.Choice(len(x) + 2)] + x + [t.Commit(-len(x) - 1)] |
| 5 |
Many1(@x) -> x + [t.Choice(len(x) + 2)] + x + [t.Commit(-len(x) - 1)] |
| 6 |
Repeat(:min :max @x) |
| 7 |
-> [t.Python(repr(int(min))), t.Push(), t.Python(repr(int(max))), |
| 8 |
t.Push(), t.RepeatChoice(len(x) + 2)] + x + [t.Commit(-len(x) - 1)] |
| 9 |
Optional(@x) -> [t.Choice(len(x) + 2)] + x + [t.Commit(2), t.Python("None")] |
| 10 |
# Right-associate Or() as needed. Note that Or() can have a list of a single |
| 11 |
# element. |
| 12 |
Or(@xs) = ?(len(xs) == 1) transform(xs[0]) |
| 13 |
| ?(len(xs) == 2) transform(t.Or(xs[0], xs[1])) |
| 14 |
| transform(t.Or(xs[0], t.Or(xs[1:]))) |
| 15 |
Or(@left @right) |
| 16 |
-> [t.Choice(len(left) + 2)] + left + [t.Commit(len(right) + 1)] + right |
| 17 |
Not(@x) -> [t.Choice(len(x) + 3)] + x + [t.Commit(1), t.Fail()] |
| 18 |
Lookahead(:x) = transform(t.Not(t.Not(x))) |
| 19 |
And(@xs) -> sum(xs, []) |
| 20 |
Bind(:name @x) -> x + [t.Bind(name)] |
| 21 |
Predicate(@x) -> x + [t.Predicate()] |
| 22 |
Action(:x) -> [t.Python(x.data)] |
| 23 |
Python(:x) -> [t.Python(x.data)] |
| 24 |
List(@x) -> [t.Descend()] + x + [t.Ascend()] |
| 25 |
ConsumedBy(@x) -> [t.StartSlice()] + x + [t.EndSlice()] |
| 26 |
|
| 27 |
pushes :xs -> [inner for x in xs for inner in [x[0], t.Push()]] |
| 28 |
Apply("super" :code @args) pushes(args):xs -> xs + [t.SuperCall(code)] |
| 29 |
Apply(:rule :code @args) pushes(args):xs -> xs + [t.Call(rule)] |
| 30 |
ForeignApply(:grammar :rule :code @args) pushes(args):xs -> (xs + |
| 31 |
[t.ForeignCall(grammar, rule)]) |
| 32 |
|
| 33 |
Rule(:name @xs) -> t.Rule(name, xs) |
| 34 |
Grammar(:name :tree @rules) -> t.Grammar(name, tree, rules) |