Skip to content

Instantly share code, notes, and snippets.

@gerdr
Created September 22, 2012 14:34
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 gerdr/3766337 to your computer and use it in GitHub Desktop.
Save gerdr/3766337 to your computer and use it in GitHub Desktop.
grammar Syntax {
token TOP { ^ <list> $ }
token list { <expression>+ % <.space>+ }
token expression {
<match=.atom>
| <match=.parens>
| <match=.squares>
}
token atom { <.alpha>+ | <.punct>+ | <.digit>+ }
token parens { '(' <list> ')' }
token squares { <expression> '[' <list> ']' }
}
my $actions = class {
method TOP($/) {
make $<list>.ast
}
method list($/) {
make $<expression>>>.ast
}
method expression($/) {
make $<match>.ast
}
method atom($/) {
make ~$/
}
method parens($/) {
make $<list>.ast
}
method squares($/) {
make $($<expression>.ast, $<list>.ast)
}
};
say Syntax.parse($_, :$actions).ast.perl given 'f[a b c]';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment