/dynamic-hash.p6 Secret
Created
August 6, 2017 14:01
Star
You must be signed in to star a gist
Dynamic hash variables in a grammar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| my $valid = Q:to/EOF/; | |
| a = 1 | |
| b = 2 | |
| { | |
| c = a, 5, b | |
| } | |
| EOF | |
| my $invalid = Q:to/EOF/; | |
| a = 1 | |
| { | |
| b = 2 | |
| } | |
| c = a, 5, b | |
| EOF | |
| grammar VariableLists { | |
| token TOP { | |
| :my %*SYMBOLS; | |
| <statement>* | |
| } | |
| token ws { <!ww> \h* } | |
| token statement { | |
| | <declaration> | |
| | <block> | |
| } | |
| rule declaration { | |
| <identifier> | |
| { %*SYMBOLS{ $<identifier> } := True } | |
| '=' <termlist> | |
| \n | |
| } | |
| rule block { | |
| '{' \n* | |
| :my %*SYMBOLS = CALLER::<%*SYMBOLS>; | |
| <statement>* | |
| '}' \n* | |
| } | |
| rule termlist { <term> * % ',' } | |
| token term { <variable> | <number> } | |
| token variable { | |
| <identifier> | |
| <?{ %*SYMBOLS{ $<identifier> } }> | |
| } | |
| token number { \d+ } | |
| token identifier { <:alpha> \w* } | |
| } | |
| use Test; | |
| ok VariableLists.parse($valid); | |
| nok VariableLists.parse($invalid), 'Undeclared variables cause parse fail';; | |
| done-testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment