Skip to content

Instantly share code, notes, and snippets.

@zoffixznet

zoffixznet/p6.p6 Secret

Created September 6, 2017 20:11
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 zoffixznet/dd2252f167233b007dd9993bbb2986ad to your computer and use it in GitHub Desktop.
Save zoffixznet/dd2252f167233b007dd9993bbb2986ad to your computer and use it in GitHub Desktop.
use JSON::Fast;
use Grammar::ErrorReporting;
grammar Time::Grammar does Grammar::ErrorReporting {
token TOP { [<!before $> <time-op>]+ }
token time-op { <minutes> | <hours> }
regex minutes { <digit> <mins> }
regex hours { <digit> <hrs> }
token digit { <:N>+ || <.error('Incorrect digit')> }
token mins { 'min' | 'mins' }
token hrs { 'hr' | 'hrs' }
}
class Time::Actions {
method TOP($/) { make $<time-op>>>.made.sum }
method time-op($/) {
make {
$<minutes>.made,
$<hours>.made,
}
}
method minutes($/) { make $<digit>.made; }
method hours($/) { make $<digit>.made * 60; }
method digit($/) { make ~$/.Int; }
method unit($/) { make $/.Str; }
}
say Time::Grammar.parse('1hrs 30 mins', actions => Time::Actions.new).made;
die;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment