Skip to content

Instantly share code, notes, and snippets.

@agwind
Last active June 13, 2016 03:02
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/055b50c52026845c4b5d1c9b100f28d8 to your computer and use it in GitHub Desktop.
Save agwind/055b50c52026845c4b5d1c9b100f28d8 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 {
method TOP($/) {
$/.make: $<row>.made;
}
method row($/) {
#say $/.perl;
$/.make: *.ast;
}
method number($/) {
$/.make: %translate{$/.lc};
}
method period($/) {
$/.make: ',';
}
}
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