Skip to content

Instantly share code, notes, and snippets.

@zoffixznet

zoffixznet/p6.p6 Secret

Created September 6, 2017 20:43
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/8c3e1a873e1b1a4ce2985f09c83bc444 to your computer and use it in GitHub Desktop.
Save zoffixznet/8c3e1a873e1b1a4ce2985f09c83bc444 to your computer and use it in GitHub Desktop.
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