5

I've created a simple program based on the Raku parsefile example provided here. The contents of my Raku file are:

grammar Names {
  token TOP  { [<name><.ws>]+ }
  token name { <:alpha>+ }
}                                                                               
                                                                                
say Names.parsefile('names.txt');

The contents of names.txt is below:

Andrea
John

Running this minimal program indicates a successful parse has been achieved:

「Andrea
John
」
 name => 「Andrea」
 name => 「John」

If I now add something which will not be parsed by the name token (i.e. 99999999):

Andrea
99999999
John

...running the program then outputs only Nil. Is there a way to modify the program so that I could still find that "Andrea" and "John" have been parsed by the name token successfully?

1 Answer 1

6

Probably comb, match or contains is better in your case.

my token name { <:alpha>+ };

say './raku/names.txt'.IO.comb(/<name>/);
say slurp('./raku/names.txt').match(/<name>/,:g);
say './raku/names.txt'.IO.words.grep: *.contains: /^<name>$/;
(Andrea John)
(「Andrea」
 name => 「Andrea」 「John」
 name => 「John」)
(Andrea John)

If you really want grammar, you could redefine <ws>.

grammar Names {
    rule TOP  { ^ <name> +  }
    token name { <:alpha>+ }
    token ws { <-alpha>* }
    #or more general  token ws { [<!before <name>>.]* }
}

say Names.parsefile: './raku/names.txt';
「Andrea
99999999
John
」
 name => 「Andrea」
 name => 「John」
Sign up to request clarification or add additional context in comments.

1 Comment

awesome answer btw

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.