Skip to content

Instantly share code, notes, and snippets.

@agwind
Last active June 13, 2016 03:10
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 agwind/deaad9b98b77efd6747890fc2049cb79 to your computer and use it in GitHub Desktop.
Save agwind/deaad9b98b77efd6747890fc2049cb79 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl6
use v6;
#use Grammar::Debugger;
grammar TEST {
token TOP {
<row>
}
token row {
\[ (<number> | <period> ) ** 0..* \]
}
token period {
\.
}
token number {
'one' | 'two' | 'three' | 'four' | 'five'
}
};
my %translate =
'one' => 1,
'two' => 2,
'three' => 3,
'four' => 4,
'five' => 5,
;
class TEST::Action {
has Str $!rowstr = '';
method TOP($/) {
$/.make: $<row>.made;
#say $<row>;
}
method row($/) {
$/.make: ~$!rowstr;
$!rowstr = '';
}
method number($/) {
$!rowstr = $!rowstr ~ %translate{$/.lc};
}
method period($/) {
$!rowstr = $!rowstr ~ ','
}
}
my $string = "[one.two.three.two]";
my $action = TEST::Action.new();
my $output = TEST.parse($string, :actions($action)).ast;
say $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment