|
17 | 17 |
|
18 | 18 | /** Builds the {@link GrammarBundle} for the PPL language from the generated ANTLR lexer/parser. */ |
19 | 19 | public class PPLGrammarBundleBuilder { |
20 | | - |
21 | | - // Keep in sync with the antlr4 version in ppl/build.gradle. |
22 | | - private static final String ANTLR_VERSION = "4.13.2"; |
| 20 | + private static final String ANTLR_VERSION = getAntlrVersion(); |
23 | 21 | private static final String BUNDLE_VERSION = "1.0"; |
24 | 22 |
|
| 23 | + private static String getAntlrVersion() { |
| 24 | + Package antlrPackage = org.antlr.v4.runtime.RuntimeMetaData.class.getPackage(); |
| 25 | + String version = antlrPackage.getImplementationVersion(); |
| 26 | + return version != null ? version : "unknown"; |
| 27 | + } |
| 28 | + |
25 | 29 | public GrammarBundle build() { |
26 | 30 | OpenSearchPPLLexer lexer = new OpenSearchPPLLexer(CharStreams.fromString("")); |
27 | 31 | CommonTokenStream tokens = new CommonTokenStream(lexer); |
28 | 32 | OpenSearchPPLParser parser = new OpenSearchPPLParser(tokens); |
29 | 33 |
|
30 | | - // ATNSerializer re-serializes the ATN into the int[] format expected by antlr4ng. |
31 | | - // Do not use lexer.getSerializedATN().chars().toArray() — that yields raw UTF-16 char values |
32 | | - // which cause "state type 65535 is not valid" errors in the frontend deserializer. |
33 | 34 | int[] lexerATN = new ATNSerializer(lexer.getATN()).serialize().toArray(); |
34 | 35 | int[] parserATN = new ATNSerializer(parser.getATN()).serialize().toArray(); |
35 | 36 |
|
@@ -60,8 +61,6 @@ public GrammarBundle build() { |
60 | 61 | private static String computeGrammarHash(int[] lexerATN, int[] parserATN) { |
61 | 62 | try { |
62 | 63 | MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
63 | | - // ANTLR4 serialized ATN values are bounded to 16 bits (unicode char range), so hashing |
64 | | - // 2 bytes per element captures the full value without loss. |
65 | 64 | for (int v : lexerATN) { |
66 | 65 | digest.update((byte) (v >> 8)); |
67 | 66 | digest.update((byte) v); |
|
0 commit comments