[Philadelphia-pm] syms in grammars
Brian Duggan
bduggan at matatu.org
Thu Apr 11 06:12:25 PDT 2019
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」
More information about the Philadelphia-pm
mailing list