How to exclude "and \ in ANTLR 4 string matching?

I have the following line that I want to combine with the rule, stringLiteral:

"D:\\Downloads\\Java\\MyFile" 

And my grammar is a file: String.g4, as shown below:

 grammar String; fragment HexDigit : ('0'..'9'|'a'..'f'|'A'..'F') ; stringLiteral : '"' ( EscapeSequence | XXXXX )* '"' ; fragment EscapeSequence : '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\') | UnicodeEscape | OctalEscape ; fragment OctalEscape : '\\' ('0'..'3') ('0'..'7') ('0'..'7') | '\\' ('0'..'7') ('0'..'7') | '\\' ('0'..'7') ; fragment UnicodeEscape : '\\' 'u' HexDigit HexDigit HexDigit HexDigit ; 

What should I put in XXXXX to match any character that is not \ or "?

I tried the following and it all does not work:

 ~['\\'"'] ~['\\'\"'] ~["\] ~[\"\\] ~('\"'|'\\') ~[\\\"] 

I am using ANTLRWorks 2 to try this. Errors are as follows:

 D:\Downloads\ANTLR\String.g4 line 26:5 mismatched character '<EOF>' expecting '"' error(50): D:\Downloads\ANTLR\String.g4:26:5: syntax error: '<EOF>' came as a complete surprise to me while looking for rule element 
+4
source share
1 answer

Inside the character class, you only need to avoid the backslash:

The following is illegal , it eludes ] :

 [\] 

The following matches the backslash:

 [\\] 

The following is a quote:

 ["] 

And the following combinations: backslash or quote:

 [\\"] 

In v4 style, your grammar might look like this:

 grammar String; /* other rules */ StringLiteral : '"' ( EscapeSequence | ~[\\"] )* '"' ; fragment HexDigit : [0-9a-fA-F] ; fragment EscapeSequence : '\\' [btnfr"'\\] | UnicodeEscape | OctalEscape ; fragment OctalEscape : '\\' [0-3] [0-7] [0-7] | '\\' [0-7] [0-7] | '\\' [0-7] ; fragment UnicodeEscape : '\\' 'u' HexDigit HexDigit HexDigit HexDigit ; 

Note: you cannot use fragments inside parser rules: StringLiteral must be a lexer rule!

+4
source

Source: https://habr.com/ru/post/1483935/


All Articles