Skip to content

Conversation

@DavidGregory084
Copy link

This PR ports the action templates functionality from antlr/antlr4#4345 to ANTLR5.

This enables users to write cross-target grammars which make use of actions, by enabling users to provide action templates as StringTemplate .stg group files on the command line.

By providing different action templates for each target language, users can provide a different implementation of the action logic for each target.

Java.stg:

normalizerImports() ::= <<
import java.text.Normalizer;
import java.text.Normalizer.Form;
>>
normalize(s) ::= <<Normalizer.normalize(<s>, Form.NFKC)>>
getText() ::= <<getText()>>
setText(s) ::= <<setText(<s>);>>

Javascript.stg:

normalizerImports() ::= ""
normalize(s) ::= <<<s>.normalize("NFKC")>>
getText() ::= "this.text"
setText(s) ::= "this.text = <s>"

The example below is the motivating example for me - I have a grammar that I'd like to use in both a JVM-based compiler and a VS Code extension:

Example.g4:

lexer grammar Example;

@lexer::header {
<normalizerImports()>
}

ID : (ID_START ID_CONTINUE* | '_' ID_CONTINUE+) { <setText(normalize(getText()))> } ;
ID_START : [\p{XID_Start}] ;
ID_CONTINUE: [\p{XID_Continue}] ;
WS : (' '|'\n') -> skip ;

@DavidGregory084
Copy link
Author

Hey @ericvergnaud is there anything I can do to move things along here?
Is there another approach that would be more suitable given the discussions in #32?

@ericvergnaud
Copy link
Contributor

ericvergnaud commented Mar 21, 2025

Hi @DavidGregory084
Thanks for this. Sorry for the very long delay.
IMHO, this is definitely a good feature for antlr4.
For antlr5, it's still unclear what the 'production' language features will look like, because calling them from WebAssembly will come with constraints and performance concerns.
I appreciate these comments go somewhat counter-clockwise to my comments in antlr/antlr4#4345 and I apologize for this.
I like the feature, but how it applies might be different from how it would in antlr4 (if accepted by Ter).
So I guess patience is required.

n.b. plus, you may have noticed that antlr5 development is not currently progressing much, due to unavailability of contributors.

@DavidGregory084
Copy link
Author

@ericvergnaud I am very happy to help if I can although my free time is a bit unpredictable.
I have recent Kotlin experience and I am a user of ANTLR in Mina, although I'm not super familiar with the internals of ANTLR.
FWIW, I tend to agree that this approach is probably not a great fit for ANTLR5, as discussed a little bit here and here.
I'd love to know your thoughts on the approach described in that second comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants