Search code examples
antlrantlr4antlr4cs

Ignoring leading and tailing quotes in a string literal match


I want to match 'Foo' as Foo, not 'Foo'.

I have the following lexer rule:

STRING_LITERAL
 : '\'' ( ~'\'' | '\'\'' )* '\''
 ;

But it seems to match the quotes.

My visitor looks like this:

public override IFilterExpression VisitLiteral_value(MagicHubFilterParser.Literal_valueContext context) {
    return MakeExpression(context.GetText());
}

I know I can trim it at that point, but I suspect it would be quicker and cleaner to handle it on the lexer level, if possible.

What's the best way to do this?


Solution

  • As @CoronA suggests, it might be more canonical to do in the visitor. However, I did figure out how to do it with a parsing rule:

    stringBody : ( ~'\'' | '\'\'' )*;
    stringLiteral
      : '\'' body=stringBody '\''
      ;