Advertisement
Guest User

Raku grammars composition

a guest
Jan 1st, 2024
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 0.75 KB | None | 0 0
  1. #! /usr/bin/env raku
  2.  
  3. use v6.d;
  4.  
  5. grammar Timestamp {
  6.     token TOP { <year><month><day>T<hours><minutes> }
  7.     token year { \d**4 }
  8.     token month { \d**2 }
  9.     token day { \d**2 }
  10.     token hours { \d**2 }
  11.     token minutes { \d**2 }
  12. }
  13.  
  14. grammar Filename {
  15.     token TOP { <name>'-'<timestamp>'.'<extension> }
  16.     token name { <[a..z]>+ }
  17.     token extension { <[a..z]>**3 }
  18.     method timestamp { Timestamp.subparse(self.orig, :pos(self.to)) }
  19. }
  20.  
  21. my \x = Filename.parse('foo-20240102T1234.bar');
  22. say "Name: {x<name>}";
  23. say "Extension: {x<extension>}";
  24. say
  25.     "Timestamp:"
  26.     ~ " {x<timestamp><day>}.{x<timestamp><month>}.{x<timestamp><year>}"
  27.     ~ " {x<timestamp><hours>}:{x<timestamp><minutes>}";
  28.  
  29. # Prints:
  30. #   Name: foo
  31. #   Extension: bar
  32. #   Timestamp: 02.01.2024 12:34
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement