| Brian Duggan on 11 Apr 2019 06:12:48 -0700 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
| [Philadelphia-pm] syms in grammars |
Hi Folks,
Thanks to those who came yesterday for the clinic :-)
Following up on the conversation from dinner -- here's
an example of using syms to simplify alternation --
Before:
grammar g {
rule TOP { 1 <op> 2 }
token op { <x> | <y> }
token x { x }
token y { y | Y }
}
class a {
method op($/) {
say "op is " ~ ( $<x> // $<y> ) # we want to avoid this
}
}
After:
grammar g {
rule TOP { 1 <op> 2 }
proto token op { <...> }
token op:sym<x> { x }
token op:sym<y> { y | Y }
}
class a {
method op:sym<x> ($/) { say 'op is x'; }
method op:sym<y> ($/) { say 'op is y or Y'; }
}
my $actions = a.new;
say g.parse('1 x 2', :$actions);
say '---';
say g.parse('1 y 2', :$actions);
say '---';
say g.parse('1 Y 2', :$actions);
which produces
op is x
「1 x 2」
op => 「x」
---
op is y or Y
「1 y 2」
op => 「y」
---
op is y or Y
「1 Y 2」
op => 「Y」
_______________________________________________
Philadelphia-pm mailing list
Philadelphia-pm@pm.org
https://mail.pm.org/mailman/listinfo/philadelphia-pm