How do I escape an escape character with ANTLR 4?

james.garriss

Many languages bound a string with some sort of quote, like this:

"Rob Malda is smart."

ANTLR 4 can match such a string with a lexer rule like this:

QuotedString : '"' .*? '"';

To use certain characters within the string, they must be escaped, perhaps like this:

"Rob \"Commander Taco\" Malda is smart."

ANTLR 4 can match this string as well;

EscapedString : '"' ('\\"|.)*? '"';

(taken from p96 of The Definitive ANTLR 4 Reference)

Here's my problem: Suppose that the character for escaping is the same character as the string delimiter. For example:

"Rob ""Commander Taco"" Malda is smart."

(This is perfectly legal in Powershell.)

What lexer rule would match this? I would think this would work:

EscapedString : '"' ('""'|.)*? '"';

But it doesn't. The lexer tokenizes the escape character " as the end of string delimiter.

Bart Kiers

Negate certain characters with the ~ operator:

EscapedString : '"' ( '""' | ~["] )* '"';

or, if there can't be line breaks in your string, do:

EscapedString : '"' ( '""' | ~["\r\n] )* '"';

You don't want to use the non-greedy operator, otherwise "" would never be consumed and "a""b" would be tokenized as "a" and "b" instead of a single token.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I escape a special Regex character?

From Dev

how do I sent escape character ^] through a spawned telnet session?

From Dev

How do I concatenate text that contains the escape character "\"

From Dev

How do I split a whitespace delimited string of tokens with an escape character?

From Dev

How do I escape a certain character in a variable in a batch file

From Dev

what is the best way to escape two character escape sequences in an ANTLR4 grammar?

From Dev

Do I need to escape dash character in regex?

From Dev

How do you conditionally check for an escape character?

From Dev

How can I escape character '&' in Path in XAML?

From Dev

How to escape the ' character in sas?

From Dev

How to escape a character in vba

From Dev

How to escape + character in java?

From Dev

How to ignore escape character ?

From Dev

How to escape the ' character in sas

From Dev

How to escape Illegal Character '|'

From Dev

.NET Regex: How do I include "end of string only" character escape in a bracket expression?

From Dev

How do I use string formatting with explicit character escape codes in Python?

From Dev

q/kdb: How do I break my code into lines. Is there a new line 'escape' character or something similar?

From Dev

How do I escape a '@' in a Dapper query?

From Dev

How do I escape a string for HTML?

From Dev

How do I escape curly braces {...} in powershell?

From Dev

How do I escape a "$" in bitbake/yocto?

From Dev

How do I escape POST body correctly?

From Dev

How do I escape quotes in URL?

From Dev

How do I escape the '{' and '}' characters in an AutoHotKey script?

From Dev

How do I concatenate an escape to a string?

From Dev

How do I escape quotes in a string variable?

From Dev

Telnet: How do I un-escape?

From Dev

In bash, how do I escape an exclamation mark?

Related Related

  1. 1

    How do I escape a special Regex character?

  2. 2

    how do I sent escape character ^] through a spawned telnet session?

  3. 3

    How do I concatenate text that contains the escape character "\"

  4. 4

    How do I split a whitespace delimited string of tokens with an escape character?

  5. 5

    How do I escape a certain character in a variable in a batch file

  6. 6

    what is the best way to escape two character escape sequences in an ANTLR4 grammar?

  7. 7

    Do I need to escape dash character in regex?

  8. 8

    How do you conditionally check for an escape character?

  9. 9

    How can I escape character '&' in Path in XAML?

  10. 10

    How to escape the ' character in sas?

  11. 11

    How to escape a character in vba

  12. 12

    How to escape + character in java?

  13. 13

    How to ignore escape character ?

  14. 14

    How to escape the ' character in sas

  15. 15

    How to escape Illegal Character '|'

  16. 16

    .NET Regex: How do I include "end of string only" character escape in a bracket expression?

  17. 17

    How do I use string formatting with explicit character escape codes in Python?

  18. 18

    q/kdb: How do I break my code into lines. Is there a new line 'escape' character or something similar?

  19. 19

    How do I escape a '@' in a Dapper query?

  20. 20

    How do I escape a string for HTML?

  21. 21

    How do I escape curly braces {...} in powershell?

  22. 22

    How do I escape a "$" in bitbake/yocto?

  23. 23

    How do I escape POST body correctly?

  24. 24

    How do I escape quotes in URL?

  25. 25

    How do I escape the '{' and '}' characters in an AutoHotKey script?

  26. 26

    How do I concatenate an escape to a string?

  27. 27

    How do I escape quotes in a string variable?

  28. 28

    Telnet: How do I un-escape?

  29. 29

    In bash, how do I escape an exclamation mark?

HotTag

Archive