Created
December 6, 2009 02:14
-
-
Save uasi/249996 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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