Skip to content

Instantly share code, notes, and snippets.

@MattOates
Created July 22, 2013 23:46
Show Gist options
  • Save MattOates/6058680 to your computer and use it in GitHub Desktop.
Save MattOates/6058680 to your computer and use it in GitHub Desktop.
Playing with roles of tokens for grammars in Perl6.
#!/usr/bin/env perl6
use v6;
role PhoneNumber {
proto token phone-num {*}
token phone-num:sym<gb> {
[<[0]>*]\s*[2<[03489]>]\s*[\d**4]\s*[\d**4] # 02x [eight-digit local number]
|
[<[0]>*]\s*[11<[3..8]>]\s*[\d**3]\s*[\d**4] # 011x [seven-digit local number]
|
[<[0]>*]\s*[1<[2..9]>1]\s*[\d**3]\s*[\d**4] # 01x1 [seven-digit local number]
|
[<[0]>*]\s*[1<[2..9]><[0|2..9]>\d]\s*[\d ** 5..6] # 01xxx [mostly six-digit local numbers] (but not 01x1 codes)
}
}
role ScientificNotation {
token sci-num {
[\d+]\.?[\d*]<[eE]>[<[\-+]>?\d+]
}
}
grammar MyParser does ScientificNotation does PhoneNumber {
rule TOP {
<seconds-since>','<contact>
}
rule seconds-since {
<sci-num>
}
rule contact {
<phone-num>
}
}
my Str $string = "1.3e-10,01245554844";
my $match = MyParser.parse($string);
say $match.perl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment