Unable to write a grammar in perl6 for parsing lines with special characters

  • A+

I have the code in: https://gist.github.com/ravbell/d94b37f1a346a1f73b5a827d9eaf7c92

use v6; #use Grammar::Tracer;   grammar invoice {      token ws { /h*};     token super-word {/S+};     token super-phrase { <super-word> [/h  <super-word>]*}     token line {^^ /h* [ <super-word> /h+]* <super-word>* /n};      token invoice-prelude-start {^^'Invoice Summary'/n}     token invoice-prelude-end {<line> <?before 'Start Invoice Details'/n>};      rule invoice-prelude {         <invoice-prelude-start>         <line>*?         <invoice-prelude-end>         <line>     } }  multi sub MAIN(){       my $t = q :to/EOQ/;      Invoice Summary     asd fasdf     asdfasdf     asd 123-fasdf $1234.00     qwe {rq} [we-r_q] we     Start Invoice Details      EOQ       say $t;     say invoice.parse($t,:rule<invoice-prelude>); }  multi sub MAIN('test'){     use Test;     ok invoice.parse('Invoice Summary' ~ "/n", rule => <invoice-prelude-start>);      ok invoice.parse('asdfa {sf} asd-[fasdf] #werwerw'~"/n", rule => <line>);     ok invoice.parse('asdfawerwerw'~"/n", rule => <line>);      ok invoice.subparse('fasdff;kjaf asdf asderwret'~"/n"~'Start Invoice Details'~"/n",rule => <invoice-prelude-end>);     ok invoice.parse('fasdff;kjaf asdf asderwret'~"/n"~'Start Invoice Details'~"/n",rule => <invoice-prelude-end>);     done-testing; } 

I have not been able to figure out why the parse on the rule <invoice-prelude> fails with a Nil. Note that even .subparse also fails.

The tests for the individual tokens are passing as you can see by running MAIN with 'test' argument (except ofcourse the .parse on <invoice-prelude> fails because it does not the full string).

What should be modified in the rule <invoice-prelude> so that the whole string $t in MAIN() can be parsed correctly?


Note that there is a hidden space at the end of the last line in the $t string:

my $t = q :to/EOQ/;      Invoice Summary     asd fasdf     asdfasdf     asd 123-fasdf $1234.00     qwe {rq} [we-r_q] we     Start Invoice Details␣   <-- Space at the end of the line     EOQ 

This makes the <invoice-prelude-end> token fail since it contains the a lookahead regexp <?before 'Start Invoice Details'/n>. This lookahead does not include a possible space at the end of the line (due to the explicit newline character /n at the end of the lookahead). Hence, the <invoice-prelude> rule cannot match either.

A quick fix is to remove the space at the end of the line Start Invoice Details.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: