Skip to content

Commit 660e273

Browse files
committed
Read ANTLR version from runtime
Signed-off-by: Eric Wei <mengwei.eric@gmail.com>
1 parent 7ddc3f0 commit 660e273

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

ppl/src/main/java/org/opensearch/sql/ppl/autocomplete/PPLGrammarBundleBuilder.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@
1717

1818
/** Builds the {@link GrammarBundle} for the PPL language from the generated ANTLR lexer/parser. */
1919
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();
2321
private static final String BUNDLE_VERSION = "1.0";
2422

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+
2529
public GrammarBundle build() {
2630
OpenSearchPPLLexer lexer = new OpenSearchPPLLexer(CharStreams.fromString(""));
2731
CommonTokenStream tokens = new CommonTokenStream(lexer);
2832
OpenSearchPPLParser parser = new OpenSearchPPLParser(tokens);
2933

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.
3334
int[] lexerATN = new ATNSerializer(lexer.getATN()).serialize().toArray();
3435
int[] parserATN = new ATNSerializer(parser.getATN()).serialize().toArray();
3536

@@ -60,8 +61,6 @@ public GrammarBundle build() {
6061
private static String computeGrammarHash(int[] lexerATN, int[] parserATN) {
6162
try {
6263
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.
6564
for (int v : lexerATN) {
6665
digest.update((byte) (v >> 8));
6766
digest.update((byte) v);

0 commit comments

Comments
 (0)