Skip to content

Instantly share code, notes, and snippets.

@lucs
Last active December 14, 2015 16:20
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/4ecb7602b8e474a01c1e to your computer and use it in GitHub Desktop.
Save lucs/4ecb7602b8e474a01c1e to your computer and use it in GitHub Desktop.
use v6;
grammar Goo {
token ident { ^ ')'? 'A'+ $ }
token qwert { <ident> | 'yuiop' }
}
test('ident', |< AA )A >);
test('qwert', |< AA )A >);
sub test (Str $top, *@egs) {
for @egs -> $eg {
say (Goo.parse( $eg, :rule($top) ) ?? "OK " !! "Fail")
~ " $eg - $top";
}
}
=begin Desc
The above contrived program prints:
OK AA - ident
OK )A - ident
OK AA - qwert
Fail )A - qwert
Any of the following modifications allows all tests to pass:
- Replace 'ident' with something else, like 'glorp'.
- Remove the 'yuiop' alternation from <qwert>.
- Replace the ')' prefix with something like 'x' (in both the rule
and the sample data).
In each case, what's going on?
(It turns out I was unsuccessfully using an equally non-predefined
'ident' in a grammar I'm designing. It took me a couple of hours to
golf it down to this hopefully useful example.)
=end Desc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment