Skip to content

Instantly share code, notes, and snippets.

Created October 12, 2015 20:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/ee46fc609a75b8054ec0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl6
use Grammar::Tracer;
# use Grammar::Debugger;
grammar J-NUMERIC-LITERAL {
rule TOP { <numeric-atom>* %% \s+ } # vector of atoms
token numeric-atom { <decimal-number> ( b <[0..9]+[A..Z]>+ )? x? } # based constants, e.g. 16bFFFF
token decimal-number { <complex-number> (<[px]> <complex-number> )? } # π- and e-notation, e.g. 2p1, 1e0.5
token complex-number { <rational> ( (j|ar|ad) <rational> )? } # e.g. 1j1, 2ar3, 4ad5
token rational { <scientific> ( r <scientific> )? } # e.g. 1r2, 22r7
token scientific { <floating-point> ( e _?<nn-integer> )? } # e.g. 1.234e_56
token floating-point { _?(_|<nn-integer>( '.' <nn-integer> )?) | '_.' } # _1.234, Nan (_.), +Inf (_), -Inf (__)
token nn-integer { <[0..9]>+ } # non-negative ints
}
sub MAIN(Str $s) {
my Match $m = J-NUMERIC-LITERAL.parse($s);
say "Numbers: {$m.hash.join(',')}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment