Skip to content

Instantly share code, notes, and snippets.

@bioduds
Created June 10, 2017 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bioduds/cff61d5071e53e3b1ada177c9c4cc107 to your computer and use it in GitHub Desktop.
Save bioduds/cff61d5071e53e3b1ada177c9c4cc107 to your computer and use it in GitHub Desktop.
reuse grammar in grammar?
## reusable grammars
grammar TIMESTAMP {
rule TOP { <WEEK-DAY> <MONTH> <DAY> <TIME> <PLACE> <PLACE> <YEAR> }
token WEEK-DAY { \S+ }
token MONTH { \S+ }
token DAY { \d+ }
token TIME { \d+ \: \d+ \: \d+ }
token PLACE { \S+ }
token YEAR { \d+ }
}
##
grammar VERSION {
rule TOP { <GREETING> <NODE> <VERSION> <TIMESTAMP> }
token GREETING { 'Hello,' | 'Hi,' | 'Good Morning,' }
token NODE { 'I am ' ( \d+ \. \d+ \. \d+ \. \d+ ) }
token VERSION { 'and my version is ' ( \d+ \. \d+ \. \d+ ) }
# TIMESTAMP HERE, HOW?
}
class VERSION::Actions {
has $.timestamp;
has $.node;
has $.version;
method TOP ($/) {
self!respond;
say "VERACK::Greetings done!";
}
method GREETING ($/) {
say 'Has greeting!';
my $proc = run 'date', :out, :err;
$!timestamp = $proc.out.slurp: :close;
}
method NODE ($/) { say 'Has NODE ' ~ $/[0]; $!node = $/[0]; }
method VERSION ($/) { say 'Has VERSION ' ~ $/[0]; $!version = $/[0]; }
method !respond {
say 'Responding to ' ~ $!node ~ ' with version ' ~ $!version ~ ' on ' ~ $!timestamp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment