Created
January 1, 2024 18:41
-
-
Save librasteve/242125b222466561da7c30fe43c3f88c to your computer and use it in GitHub Desktop.
GrammarConsumptionExample2
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::Tracer; | |
grammar Timestamp { | |
token TOP{^<y><m><d>T<hr><mn>$} | |
token y {\d**4} | |
token m {\d**2} | |
token d {\d**2} | |
token hr {\d**2} | |
token mn {\d**2} | |
} | |
#say Timestamp.parse: '20130304T0102'; | |
my $ts-match; | |
grammar TimestampedFile { | |
token TOP{^<name> '-' <timestamp> '.' <ext>$} | |
token name{<[a..z]>+} | |
token timestamp { | |
<-[.]>+ <?{$ts-match = Timestamp.parse("$/")}> | |
} | |
token ext{<[a..z]>**3} | |
} | |
class TimestampedFileActions { | |
method timestamp($/) { | |
make $ts-match | |
} | |
} | |
my $res = TimestampedFile.parse('xxx-20130304T0102.csv', actions => TimestampedFileActions.new); | |
say ~$res<timestamp>.made<y>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment