Unwanted failure and FAILGOAL

[email protected] (Richard Hainsworth) Wed, 11 May 2016 13:45:25 +0800
Newsgroups perl.perl6.language
Message-ID <[email protected]>
I have the following in a grammar

     rule TOP        { ^ <statement>+ $ };

     rule statement  { <id> '=' <endvalue>
                      | { { self.panic($/, "Declaration syntax 
incorrect") } }
                     };

     rule endvalue   { <keyword> '(' ~ ')' <pairlist>
                      | { self.panic($/, "Invalid declaration.") }
                     }

The grammar parses a correct input file up until the end of the file. At 
that point even if there is no un-consumed input, there is an attempt to 
match <id>, which fails. The failure causes the panic with 'Declaration 
syntax'.

Am I missing something simple here?

I would have thought  (though this is only a very newbie assumption) 
that if the end of the input being sent to the grammar has been reached 
after the last <statement> has been matched, then there should be no 
reason for the parse method to try to match <statement> again, and if it 
fails to test for the end of input.

Abstractly, it seems to me to be a bit like the difference between 
testing for the truth of a condition before entering a loop, and testing 
for the truth after the loop.

In trying to find a way out of this, I went looking for some information 
about FAILGOAL. I could not find anything in the documentation or in the 
specifications.

Google provided me with some conversations about FAILGOAL, but nothing 
about how to use it. I do not know enough about the guts of Rakudo to 
know where to look.

Would it be possible for someone who knows about this to add something 
to the Documentation on Grammars?

(As I write this, I thought  may be I need a lookahead pattern in the 
TOP rule to ensure there is still input. But even so, that seems counter 
intuitive.)