Skip to content

Instantly share code, notes, and snippets.

@librasteve
Created January 1, 2024 22:36
Show Gist options
  • Save librasteve/ff7deab0e4e046c6388d10514be3b4e1 to your computer and use it in GitHub Desktop.
Save librasteve/ff7deab0e4e046c6388d10514be3b4e1 to your computer and use it in GitHub Desktop.
GrammarConsumptionExample3
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 {
\w+ <?{$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