-
-
Save lucasbuchala/8644f12767b70cdaebb19fe02c4e8f0b 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
grammar G { | |
token TOP { <line>* } | |
rule line { '[' <dt> ']' <desc> } | |
rule dt { <date> <time> } | |
token date { (\d**4) '-' (\d\d) '-' (\d\d) } | |
token time { (\d\d) ':' (\d\d) } | |
token desc { | |
| 'Guard #' (\d+) ' begins shift' | |
| 'falls asleep' | |
| 'wakes up' | |
} | |
} | |
enum GuardEvent <BeginsShift FallsAsleep WakesUp>; | |
class GuardLog { | |
has DateTime $.dt; | |
has GuardEvent $.desc; | |
has $.id; | |
method gist { "[$!dt $!id $!desc]" } | |
} | |
class A { | |
has $!current-id; | |
method TOP($/) { | |
make $<line>».made; | |
} | |
method line($/) { | |
my $dt = $<dt>.made; | |
my $desc = $<desc>.made; | |
make GuardLog.new( | |
dt => $dt, | |
desc => $desc, | |
id => $!current-id, | |
); | |
} | |
method desc($/) { | |
my $s = ~$/; | |
given $s { | |
when 'wakes up' { make WakesUp } | |
when 'falls asleep' { make FallsAsleep } | |
default { | |
$!current-id = $/[0].Int; | |
make BeginsShift; | |
} | |
}; | |
} | |
method dt($/) { | |
make DateTime.new( | |
year => $<date>[0], | |
month => $<date>[1], | |
day => $<date>[2], | |
hour => $<time>[0], | |
minute => $<time>[1], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment