0

I need to parse SRGS files written in ABNF format. I need to catch missing enclosing double quote character, ex:

public $sdsid300035= $<sdsid300101>s number "hee;  // missing enclosing " 
public $sdsid300036= $<sdsid300101>'s number "that's";

I've this lexer rule:

 DOUBLE_QUOTED_CHARACTERS: '\"' ~(['\"'])*? '\"'

It matches valid input, like: "some text". But how to write a rule for detecting and throwing error for missing enclosing double quote character like in an example ?

Here is the link to implementation of this rule in ANTLR v3: ANTLR4 lexer rule with @init block

Community
  • 1
  • 1
Adrian
  • 183
  • 1
  • 11

2 Answers2

0

One way is to use a mode to isolate the syntax of what constitutes a valid string:

StrStart: '"' -> pushMode(strMode) ;

mode strMode;
ValidStringChar: [a-zA-Z ... ]+ ;
StrEnd:          '"'           -> popMode;
InvalidStrChar1: '>'           -> popMode, type(RAngle);
InvalidStrChar2: ';'           -> popMode, type(Semi);
...

When the parser fails to find an alternative in the case of an unterminated string, the Parser's ANTLRErrorStrategy can be used to define how to deal with the error. This answer provides a bit more detail on using error strategies. Rather that just throwing an error, you have the option to potentially 'warn and recover' from the parser error.

Community
  • 1
  • 1
GRosenberg
  • 5,843
  • 2
  • 19
  • 23
0

I've implemented this using altrenatives, see:

doubleQuotedCharacters
 : DOUBLE_QUOTED_CHARACTERS
 | WHITE_SPACES_IN_DOUBLE_QUOTE     {notifyErrorListeners("Illegal empty quotes\"\"!");}
 | MISSING_CLOSING_DOUBLE_QT        {notifyErrorListeners("Missing closing double quote!");}
;

WHITE_SPACES_IN_DOUBLE_QUOTE
: '\"' WS* '\"' -> channel(HIDDEN) 
;                

MISSING_CLOSING_DOUBLE_QT
:   '\"' ~('\"')*?
;

DOUBLE_QUOTED_CHARACTERS
: '\"' ~('\"' | '\n'|'\r' )*? '\"'           
{  setText(getText().substring(1, getText().length()-1));}
;

I works fine for strings in double quotes which are not divited in multiple lines.

Adrian
  • 183
  • 1
  • 11