Skip to content

Instantly share code, notes, and snippets.

Created April 10, 2017 19:05
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 anonymous/f563aee0183ff39fe4c6cc90ff0afc5b to your computer and use it in GitHub Desktop.
Save anonymous/f563aee0183ff39fe4c6cc90ff0afc5b to your computer and use it in GitHub Desktop.
#use Grammar::Debugger;
### Package : rakudo-star-ver-arch-build-tag.ext
grammar Pkg {
token TOP { <ws> <name> <ver>? <arch>? <build>? <tag>? <ext>? }
regex name { <alnum>+ [ '-' <alnum>+ ]* <!before '.'> } # using regex as name may contain "-"
token ver { '-' <(<digit>+ ['.' <digit>+]+)> }
proto token arch { * } # look-around asssertions for "arch" token doesn't work, Why!
token arch:sym<x86_64> { '-' <(<sym>)> }
token arch:sym<i386> { '-' <(<sym>)> }
#token build { <?after <arch>> '-' <(<digit>+)> } # does not work, but <?after 'x86_64'> works
token build { '-' <(<digit>+)> }
token tag { '-' <(<alnum>+)> } # "tag" is optional in the package name.
#token ext { <?after [<build> | <tag>] > '.' <('pkg')> } # "ext" should come after ("build" or "tag") only
token ext { '.' <('pkg')> }
}
### Issues ###
# grammar should fail to parse if a token matched in wrong location (for example: if arch token come before version token)
# rakudo-star-ver-arch.ext should fail, (ext only comes after build or tag tokens), however, grammar successfully parse it
# if I addede look-around assertions grammar doesn't match at all or other tests fail.
my @tests = (
# [ 'string to test', matches?, message ]
[ 'rakudo-star', True, 'name' ],
[ 'rakudo-star-0.7', True, 'name-ver' ],
[ 'rakudo-star-0.7-x86_64', True, 'name-ver-arch' ],
[ 'rakudo-star-0.7-x86_64-0', True, 'name-ver-arch-build' ],
[ 'rakudo-star-0.7-x86_64-0.pkg', True, 'name-ver-arch-build-ext' ],
[ 'rakudo-star-0.7-x86_64-0-tag', True, 'name-ver-arch-build-tag' ],
[ 'rakudo-star-0.7-x86_64-0-tag.pkg', True, 'name-ver-arch-build-tag.ext' ],
[ 'rakudo-star-0.7-0', False, 'build must come after arch' ],
[ 'rakudo-star-0.7.pkg', False, 'ext must come after build or tag' ],
);
for @tests -> [$string, $should-parse, $message] {
my $m = Pkg.parse($string);
my $result = ?$m; # make boolean
$result = !$result if $should-parse === False;
say "[$result]\tt$string $message";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment