Search code examples
parsingantlrlexer

Antlr dynamic end of token capture with target-language function?


I have a grammar that will allow entering in a 'native' expression in the form of @dialect(...). However, depending on the "dialect" value we will parse the inside of the expression differently. Let me give a few examples:

= @postgres(')'''--comment) + 2
= 3 + @mysql(`my ide)ntifier`) + 4

I would like to actually insert a custom function to determine where we should close the parentheses based on what dialect we are in. Is there a way to do this? I have shown the parser that I am trying to do in a conceptual manner below:

grammar Native;

root: '=' expr ('+' expr)*;
expr: NUMBER | native_expr;
native_expr: '@' NATIVE_DIALECT '(' customJsFunctionToGetToEndOfToken();

NATIVE_DIALECT: ('postgres' | 'mysql');
NUMBER: [0-9]+;

The customJsFunctionToGetToEndOfToken would 'eat' all of those tokens and advance to the closing character of a ). In other words, the action itself would consume characters to create the token. is this possible to do in ANTLR? If so, what would be an example of how I could do this?


Solution

  • In other words, the action itself would consume characters to create the token. is this possible to do in ANTLR?

    No, that is not possible. Inside parser rules, you cannot influence the creation of tokens. However, my answer in your other question already creates the correct "closing parenthesis token", so perhaps this question is no longer needed?