mmendez.semantic
mmendez.semantic

Reputation: 244

Antlr4 how to avoid syntax errors with anything between quotes rule?

I'm trying to build my own dsl in order to create custom rules to match a given json object.

for this purpose I've created so far two kind of rules with the following grammar:

grammar RuleGrammar;

def: 'Def(' jsonrule ')';
jsonrule: regex|composite;
regex: '"' code '"';
composite: '[' jsonrule ('&&'jsonrule)* ']';
code: ANY+;

ANY: ( '\\"' | .);

WS: [ \t\r\n]+ -> skip();

This grammar fails with a syntax error when the "code" of the regex contains characters '[' or ']', such as:

Def("[a-zA-Z0-9]+")
line 1:5 extraneous input '[' expecting ANY

I see this has to do with the definition of the composite rule, which has ']' in it.

Is there a way to avoid the syntax error without escaping the brackets in code?

Upvotes: 0

Views: 209

Answers (1)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51430

regex and code should be lexer rules. Besides, code is greedy, so it'll probably consume too much input.

Write the REGEX rule like that instead:

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

If you want an explanation for the error you get, it's because the [ character is an implicitly defined token, as you used it in the composite rule. It doesn't get recognized as ANY because of the lexer priority rules.

Upvotes: 1

Related Questions