Skip to content

Instantly share code, notes, and snippets.

@uasi
Created December 6, 2009 02:14
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 uasi/249996 to your computer and use it in GitHub Desktop.
Save uasi/249996 to your computer and use it in GitHub Desktop.
grammar MyInteger {
token TOP {
| 0b(<[01]>+) {*} #= binary
| \d+ {*} #= decimal
}
}
class Twice {
multi method TOP($/, $tag) {
my $text = ~$/;
# Don't do :2($text) since :2('0b1') == 23.
# I'm not sure it's a feature or not.
$text = :2(~$/[0]) if $tag eq 'binary';
make $text * 2;
}
# This multi method will never be called
# since every rule has ``#= tag''.
#
# multi method TOP($/) { }
}
say MyInteger.parse('21', :action(Twice.new)).ast; # 42
say MyInteger.parse('0b10101', :action(Twice.new)).ast; # 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment