Skip to content

Instantly share code, notes, and snippets.

@librasteve
Created January 2, 2024 14:54
Show Gist options
  • Save librasteve/d3f74d47185abe238beb39fa7acc76d3 to your computer and use it in GitHub Desktop.
Save librasteve/d3f74d47185abe238beb39fa7acc76d3 to your computer and use it in GitHub Desktop.
GrammarConsumptionExampleOK
use v6.d;
grammar Timestamp {
token TOP { <year><month><day>T<hours><minutes> }
token year { \d**4 }
token month { \d**2 }
token day { \d**2 }
token hours { \d**2 }
token minutes { \d**2 }
}
grammar Filename {
token TOP { <name>'-'<timestamp>'.'<extension> }
token name { <[a..z]>+ }
token extension { <[a..z]>**3 }
method timestamp { Timestamp.subparse(self.orig, :pos(self.to)) }
}
my \x = Filename.parse('foo-20240102T1234.bar');
say "Name: {x<name>}";
say "Extension: {x<extension>}";
say
"Timestamp:"
~ " {x<timestamp><day>}.{x<timestamp><month>}.{x<timestamp><year>}"
~ " {x<timestamp><hours>}:{x<timestamp><minutes>}";
# Prints:
# Name: foo
# Extension: bar
# Timestamp: 02.01.2024 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment