-
-
Save zoffixznet/8c3e1a873e1b1a4ce2985f09c83bc444 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 Grammar::ErrorReporting; | |
grammar Time::Grammar does Grammar::ErrorReporting { | |
token TOP { [ <!before $> <time-op> ]+ } | |
token time-op { <digit> <.ws>? <unit> <.ws>? } | |
token digit { <:N>+ || <.error('Incorrect digit')> } | |
proto token unit {*} | |
token unit:sym<mins> { 'min' | 'mins' } | |
token unit:sym<hrs> { 'hr' | 'hrs' } | |
} | |
class Time::Actions { | |
method TOP ($/) { make $<time-op>».made.sum } | |
method time-op ($/) { make $<digit> * $<unit>.made } | |
method unit:sym<mins> ($/) { make 1 } | |
method unit:sym<hrs> ($/) { make 60 } | |
} | |
say Time::Grammar.parse('1hrs 30 mins', actions => Time::Actions.new).made; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment