I'm having an ANTLR4 problem with mismatched input but can't solve it. I've found a lot of questions dealing with that, and the usually revolve around the lexer matching something else to the token, but I don't see it in my case.
I've got this lexer grammar:
FieldStart : '[' Definition ']' -> pushMode(INFIELD) ;
Definition : 'Element';
mode INFIELD;
FieldEnd : '[end]' -> popMode ;
ContentValue : ~[[]* ;
Which then runs on the following parser:
field : FieldStart ContentValue FieldEnd #Field_Found;
I simplified it to zoom in to the problem, but here's the point where I can't get any further.
I'm running that on the following input:
[Element]Va-lu*e[end]
and I get this output:
Type : 001 | FieldStart | [Element]
Type : 004 | ContentValue | Va-lu*e
Type : 003 | FieldEnd | [end]
Type : -001 | EOF | <EOF>
([] [Element] Va-lu*e [end])
I generated the output with C#, doing the following (shortened):
string tokens = "";
foreach (IToken CurrToken in TokenStream.GetTokens())
{
if (CurrToken.Type == -1)
{
tokens += "Type : " + CurrToken.Type.ToString("000") + " | " + "EOF" + " | " + CurrToken.Text + "\n";
}
else
{
tokens += "Type : " + CurrToken.Type.ToString("000") + " | " + Lexer.RuleNames[CurrToken.Type - 1] + " | " + CurrToken.Text + "\n";
}
}
tokens += "\n\n" + ParseTree.ToStringTree();
Upon parsing this via
IParseTree ParseTree = Parser.field();
I am presented this error:
"mismatched input 'Va-lu*e' expecting ContentValue"
I just don't find the error, can you help me here? I assume it's got something to do with the lexer mode, but from as far as I read it looks like the parser doesn't care (or know) about the modes.
Thanks!