Skip to content

Instantly share code, notes, and snippets.

@ungrim97
Created October 29, 2015 14:48
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 ungrim97/7c0d17e1846cd002377d to your computer and use it in GitHub Desktop.
Save ungrim97/7c0d17e1846cd002377d to your computer and use it in GitHub Desktop.
Grammar
role Grammar::IETF::ABNF::RFC5234_Core is Grammar {
token ALPHA { <[\x[41]..\x[5A]]> | <[\x[61]..\x[7A]]> }
token BIT { 0 | 1 }
token CHAR { <[\x[01]..\x[7F]]> }
token CR { \x[0D] }
token CRLF { [<.CR> <.LF>] }
token CTL { <[\x[00]..\x[1F]]> | \x[7F] }
token DIGIT { <[\x[30]..\x[39]]> }
token DQUOTE { \x[22] }
token HEXDIG { <.DIGIT> | <[a..fA..F]> }
token HTAB { \x[09] }
token LF { \x[0A] }
token LWSP { [<.WSP> | <.CRLF> <.WSP>]* }
token OCTET { <[\x[00]..\x[FF]]> }
token SP { \x[20] }
token VCHAR { <[\x[21]..\x[7E]]> }
token WSP { <SP> | <HTAB> }
}
grammar Grammar::IETF::ABNF::RFC5234 does Grammar::IETF::ABNF::RFC5234_Core {
rule TOP { ^ <rulelist> $ }
token rulelist { [<rule> | [.<c-wsp>* <.c-nl>]]+ }
token rule { <rulename> <defined-as> <elements> <.c-nl> }
token c-wsp { <.WSP> | [<c-nl> <.WSP>] }
token c-nl { <.comment> | <.CRLF> }
token comment { ';' [<.WSP> | <.VCHAR>]* <.CRLF> }
token alternation { <.concatenation> [<.c-wsp>* '/' <.c-wsp>* <.concatenation>]* }
token concatenation { <.repetition> [<.c-wsp>+ <.repetition>]* }
token repetition { <.repeat>? <.element> }
token elements { <.alternation> <.c-wsp>* }
token rulename { <.ALPHA> [<.ALPHA> | <.DIGIT> | '-']* }
token defined-as { <.c-wsp>* ['=' | '=/'] <.c-wsp>* }
token repeat { <.DIGIT>+ | [<.DIGIT>* '*' <.DIGIT>*] }
token element {
<.rulename> | <.group> | <.option> | <.char-val> | <.num-val> | <.prose-val>
}
token group { '(' <.c-wsp>* <.alternation> <.c-wsp>* ')' }
token option { '[' <.c-wsp>* <.alternation> <.c-wsp>* ']' }
token char-val { <.DQUOTE> [ <[\x[20] \x[21] \x[23]..\x[7E] ]> ]* <.DQUOTE> }
token num-val { '%' [<.bin-val> | <.dec-val> | <.hex-val>] }
token bin-val { <[bB]> <.BIT>+ [ ['.' <.BIT>+ ]+ | ['-' <.BIT>+ ] ]? }
token dec-val { <[dD]> <.DIGIT>+ [ ['.' <.DIGIT>+ ]+ | ['-' <.DIGIT>+ ] ]? }
token hex-val { <[xX]> <.HEXDIG>+ [ ['.' <.HEXDIG>+ ]+ | ['-' <.HEXDIG>+ ] ]? }
token prose-val { '<' [ <[\x[20]..\x[3D]]> | <[\x[3F]..\x[7E]]> ]* '>' }
}
use v6;
use Test;
use Grammar::IETF::ABNF::RFC5234;
subtest {
my $match = Grammar::IETF::ABNF::RFC5234.parse('2*2 foo', :rule<repetition>);
$match.say;
}, 'repetition';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment