-
-
Save zoffixznet/dd2252f167233b007dd9993bbb2986ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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