-
-
Save zoffixznet/c82217a616ad878620f71b35c8da5893 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 ws { \s* } | |
rule time-op { <digit> <unit> } | |
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