Skip to content

Instantly share code, notes, and snippets.

@bioduds
Created June 9, 2017 13:04
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/f4acfdc1294684243976a4205c7413b5 to your computer and use it in GitHub Desktop.
Save bioduds/f4acfdc1294684243976a4205c7413b5 to your computer and use it in GitHub Desktop.
How to get the array?
#### SO, I have this grammar
unit package EC::Grammars:auth<github:bioduds>;
grammar ADDR {
rule TOP { <IDENTIFIER> <TIMESTAMP> <LINKER> <ADDRESS>+ }
token IDENTIFIER { 'Right now, ' }
rule TIMESTAMP { <WEEK-DAY> <MONTH> <DAY> <TIME> <PLACE> <YEAR> }
token WEEK-DAY { \S+ }
token MONTH { \S+ }
token DAY { \d+ }
token TIME { \d+ \: \d+ \: \d+ }
token PLACE { \S+ }
token YEAR { \d+ }
token LINKER { 'I have' }
token ADDRESS { \d+ \. \d+ \. \d+ \. \d+ }
}
class ADDR::Actions {
method TOP ($/) { say "ADDR went through!" }
method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
method TIMESTAMP ($/) { say "Got TIMESTAMP " ~ $/; }
method LINKER ($/) { say "Got LINKER" ~ $/; }
method ADDRESS ($/) {
say "Got ADDRESSES " ~ $/;
for $/ -> $addr {
say $addr;
}
}
}
#### being called from
use v6;
use lib 'lib/grammars';
use message;
say "Running Test...";
my $addr = EC::Grammars::ADDR.new.parse(
'Right now, Sex Jun 9 09:23:35 BRT 2017 I have 154.54.78.96 56.21.147.10',
:actions( EC::Grammars::ADDR::Actions.new )
);
#### BUT I'M ONLY GETTING THE FIRST IP ADDRESS, HOW CAN I GET ALL THE LIST? THANKS
@MARTIMM
Copy link

MARTIMM commented Jun 9, 2017

You need to allow for whitespace between the addresses so you must specify the first rule differently;

rule TOP { <IDENTIFIER> <TIMESTAMP> <LINKER> [ <ADDRESS> ]+

Also by checking the returned Match object in $addr you will see that Any is returned and this means that the parse failed somewhere. In this case before the second address because you saw the first one.

Success
Marcel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment