0

I define some String tokens like this in ANTLR4, with some exceptions surely properly handled in Java:

STRINGLIT: '"'('\\'[bfrnt\\"]|~[\n"EOF])*'"';

ILLEGAL_ESC: '"'(('\\'[bfrnt\\"]|~[\n\\"EOF]))*('\\'(~[bfrnt\\"]|EOF))
    {if (true) throw new bkool.parser.IllegalEscape(getText());};

UNCLOSED_STRING: '"'('\\'[bfrnt\\"]|[\n\\"EOF])*
    {if (true) throw new bkool.parser.UncloseString(getText());};

Then I tested with some cases, with:

"This is a string"
"String with legal escape \\"
"Legal \\n"
"Illegal \"
"Illegal \n"

No exceptions are thrown. Then with some other cases:

"This is a string
"String with legal escape \\"
"Legal \\n"
"Illegal \"
"Illegal \n"

Then it ends up with:

Unclosed string: "

The exceptions are handled by printing the respective improper string with the name of exception

I have been struggling with them for a day and now I'm stuck with it. What is still not okay with my ANTLR definitions?

necroface
  • 3,365
  • 10
  • 46
  • 70
  • 1
    Take a look at [my answer here](http://stackoverflow.com/a/24892371/3764814) for some lexer error handling tips (basically: validate *after* lexing). Also `[\n"EOF]` doesn't mean what you think. `EOF` is a separate token altogether. – Lucas Trzesniewski Sep 23 '15 at 11:19
  • Actually, I have solved this a week ago, anyway, thank you :D – necroface Sep 23 '15 at 11:30
  • Now I have another problem, could you please help me with it? http://stackoverflow.com/questions/32738669/antlr4-mismatched-input-expecting – necroface Sep 23 '15 at 11:46

0 Answers0