My Lua grammar currently cannot handle function definitions whose parameter list ends in the var arg operator, like this one:
function foo(x, y, ...) print("hello") end
The relevant terminals are parameterList and identifierList:
parameterList.production = identifierList + (@comma + @varArgOperator).Optional()
identifierList.production = @identifier + (@comma + @identifier).ZeroOrMore()
When parsing inside the identifierList production hits the var arg operator, it terminates unsuccessfully because it's not an identifier.
I tried using this CanMatchCallback for identifierList:
private bool CanMatchIdentifierList(IParserState state)
{
var tokenReader = state.TokenReader;
tokenReader.Push();
try
{
if (tokenReader.LookAheadToken.Id != LuaTokenId.Identifier)
{
return false;
}
tokenReader.Advance();
while (tokenReader.LookAheadToken.Id == LuaTokenId.Comma)
{
tokenReader.Advance();
if (tokenReader.LookAheadToken.Id != LuaTokenId.Identifier)
{
return false;
}
else
{
tokenReader.Advance();
}
}
}
finally
{
tokenReader.Pop();
}
return true;
}
However, that results in no identifierList match at all, not a partial one up to the comma before the var arg operator.
I also tried using an OnError callback that returns the Ignore result. When I advance the token reader inside of it, parsing is successful, but the var arg operator is passed over entirely. If I only return the Ignore result, I still get no identifierList match.
How do I correctly handle this scenario so that the parameterList match goes up until the last comma before the var arg operator when it's present?