Skip to content

Instantly share code, notes, and snippets.

@lucs
Last active May 30, 2020 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucs/b6b5e77126b895512dacd4bc02a2b498 to your computer and use it in GitHub Desktop.
Save lucs/b6b5e77126b895512dacd4bc02a2b498 to your computer and use it in GitHub Desktop.
# In a grammar, how do I reuse a proto'ed definition? Here for
# example, I want <ld> to use some <elem>:
#use Grammar::Debugger;
grammar Foo {
proto token elem {*}
token elem:sym<let> { <[a..z]> }
token elem:sym<dig> { <[0..9]> }
token anys { <elem>+ }
# A <let> followed by a <dig>. None of these work. Some fail
# to match, some fail to compile.
# token ld { <let> <dig> }
# token ld { sym<let> sym<dig> }
# token ld { elem:sym<let> elem:sym<dig> }
# token ld { elem<let> elem<dig> }
token TOP { <anys> | \( <ld> \) }
};
# Matches <TOP> with <anys>.
#say Foo.parse('bar42');
# Can't get this to match <TOP> with parenthesized <ld>.
say Foo.parse('(k9)');
=finish
- --------------------------------------------------------------------
Two solutions were suggested by ShimmerFairy#raku@freenode.net.irc
1. Factor out the names of the required tokens:
token let { <[a..z]> }
token dig { <[0..9]> }
token elem:sym<let> { <let> }
token elem:sym<dig> { <dig> }
token ld { <let> <dig> }
2. Define <ld> like this:
token ld { <elem:sym<let>> <elem:sym<dig>> }
@tbrowder
Copy link

Did you try escaping the left paren? Inside double or single quotes?

@lucs
Copy link
Author

lucs commented May 30, 2020

Tom, it turns out the parentheses were not a problem. I actually needed to fix my token definitions, and ShimmerFairy pointed out a couple of ways to do it, shown in the current revised version of the gist.

@tbrowder
Copy link

tbrowder commented May 30, 2020

Hm, great. The sym thing is something I've avoided as hard to understand, but I'm at a point in my own project I need to buckle down and study it more closely. @ShimmerFairy has a lot of Raku experience and it would be nice if she could add more to our docs about the solution and more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment