Antlr4 and Antlr-ng follow a predictable naming scheme for source code in generated files by using the names declared in the .g4 files. It is followed consistently across all targets.
There are two exceptions to this rule. One is when there are "symbol conflicts", which were never considered systematically until a solution was added to version 4.10. Antlr4 handles this problem by looking up rule names in a table, then generating mutated names. The second is for the Go target and file names. Antlr4 generates file names in snake_case for Go.
While this solution solved the problem of having the generated code work in any target, it introduced a new problem: generating source code with an unpredictable naming scheme. The grammars-v4 repo carefully avoids "symbol conflict" issues and the unpredictable renaming solution that was chosen for version 4.10 by renaming all rule names so that it works on all targets.
For the Go target, it doesn't matter what the file name is because Go's import doesn't require a file name, just the directory where the generated files reside. Rust is different because it uses pub mod ...; syntax.
Examples of grammars and naming for Antlr4/Antlr-ng:
- For Antlr 4.13.2/Java/JSON.g4 with combined gramar name JSON and start rule json, the parser is JSONParser defined in JSONParser.java and the start rule json.
- For Antlr 4.13.2/CSharp/JSON.g4 with combined gramar name JSON and start rule json, the parser is JSONParser defined in JSONParser.cs with start rule json. The naming is Identical across the Java and CSharp targets.
- For Antlr 4.13.2/CSharp/JavaParser.g4 with parser grammar name JavaParser and start rule compilationUnit, the parser is JSONParser defined in JSONParser.cs and start rule compilationUnit.
- For Antlr 4.13.2/Go/AbbParser.g4 with parser grammar name abbParser and start rule module_, the parser is abbParser defined in abb_parser.go (renamed) and start rule module_.
In comparison, Antlr-rust-gen mutates the names of declarations and generated files to PascalCase and snake_case unconditionally.
Examples of grammars and naming for Antlr-rust-gen:
- For Antlr-rust-gen/JSON.g4 with combined gramar name JSON and start rule json, the parser is JsonParser (renamed) defined in json_parser.rs (renamed) and start rule json.
- For Antlr-rust-gen/JavaParser.g4 with parser grammar name JavaParser and start rule compilationUnit, the parser is JavaParser defined in java_parser.rs (renamed) and the start rule compilation_unit (renamed).
- For Antlr-rust-gen/AbbParser.g4 with parser grammar name abbParser and start rule module_, the parser is AbbParser (renamed) defined in abb_parser.rs (renamed) and the start rule module (renamed).
Why does this matter? We should have predictable, consistent naming for generated declarations and file names. Trash, which generates driver applications for Antlr4 grammars for any target (now including Antlr-rust-gen), requires code to implement similar renaming that Antlr-rust-gen does for Antlr-rust-runtime templates, i.e., more code bloat.
Antlr4 and Antlr-ng follow a predictable naming scheme for source code in generated files by using the names declared in the .g4 files. It is followed consistently across all targets.
There are two exceptions to this rule. One is when there are "symbol conflicts", which were never considered systematically until a solution was added to version 4.10. Antlr4 handles this problem by looking up rule names in a table, then generating mutated names. The second is for the Go target and file names. Antlr4 generates file names in snake_case for Go.
While this solution solved the problem of having the generated code work in any target, it introduced a new problem: generating source code with an unpredictable naming scheme. The grammars-v4 repo carefully avoids "symbol conflict" issues and the unpredictable renaming solution that was chosen for version 4.10 by renaming all rule names so that it works on all targets.
For the Go target, it doesn't matter what the file name is because Go's
importdoesn't require a file name, just the directory where the generated files reside. Rust is different because it usespub mod ...;syntax.Examples of grammars and naming for Antlr4/Antlr-ng:
In comparison, Antlr-rust-gen mutates the names of declarations and generated files to PascalCase and snake_case unconditionally.
Examples of grammars and naming for Antlr-rust-gen:
Why does this matter? We should have predictable, consistent naming for generated declarations and file names. Trash, which generates driver applications for Antlr4 grammars for any target (now including Antlr-rust-gen), requires code to implement similar renaming that Antlr-rust-gen does for Antlr-rust-runtime templates, i.e., more code bloat.