|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.executor.autocomplete; |
| 7 | + |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.security.MessageDigest; |
| 10 | +import java.security.NoSuchAlgorithmException; |
| 11 | +import lombok.extern.log4j.Log4j2; |
| 12 | +import org.antlr.v4.runtime.Lexer; |
| 13 | +import org.antlr.v4.runtime.Parser; |
| 14 | +import org.antlr.v4.runtime.Vocabulary; |
| 15 | + |
| 16 | +/** |
| 17 | + * Utility class for extracting ANTLR grammar artifacts (ATN, vocabulary, rule names) from generated |
| 18 | + * parser/lexer classes. |
| 19 | + * |
| 20 | + * <p>This class handles the low-level details of: |
| 21 | + * |
| 22 | + * <ul> |
| 23 | + * <li>Converting ANTLR's Java String ATN format to int[] for JSON transfer |
| 24 | + * <li>Extracting vocabulary (literal and symbolic names) |
| 25 | + * <li>Extracting rule names via public ANTLR APIs |
| 26 | + * <li>Computing grammar hash for versioning |
| 27 | + * </ul> |
| 28 | + * |
| 29 | + * <p>Language-specific builders (PPL, SQL) use this class to build their autocomplete bundles. |
| 30 | + */ |
| 31 | +@Log4j2 |
| 32 | +public class GrammarArtifactBuilder { |
| 33 | + |
| 34 | + /** |
| 35 | + * Extract literal names from vocabulary. |
| 36 | + * |
| 37 | + * <p>Returns array where index = token type, value = literal token (with quotes) or null. |
| 38 | + * |
| 39 | + * <p>Example: ["<INVALID>", "'search'", "'where'", "'|'", null, ...] |
| 40 | + * |
| 41 | + * @param vocabulary Parser vocabulary |
| 42 | + * @return Array of literal names |
| 43 | + */ |
| 44 | + public static String[] extractLiteralNames(Vocabulary vocabulary) { |
| 45 | + int maxTokenType = vocabulary.getMaxTokenType(); |
| 46 | + String[] names = new String[maxTokenType + 1]; |
| 47 | + |
| 48 | + for (int i = 0; i <= maxTokenType; i++) { |
| 49 | + String literal = vocabulary.getLiteralName(i); |
| 50 | + // Keep nulls as nulls (no literal representation) |
| 51 | + names[i] = literal; |
| 52 | + } |
| 53 | + |
| 54 | + log.debug("Extracted {} literal names", names.length); |
| 55 | + return names; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Extract symbolic names from vocabulary. |
| 60 | + * |
| 61 | + * <p>Returns array where index = token type, value = symbolic token name or null. |
| 62 | + * |
| 63 | + * <p>Example: ["<INVALID>", "SEARCH", "WHERE", "PIPE", "ID", ...] |
| 64 | + * |
| 65 | + * @param vocabulary Parser vocabulary |
| 66 | + * @return Array of symbolic names |
| 67 | + */ |
| 68 | + public static String[] extractSymbolicNames(Vocabulary vocabulary) { |
| 69 | + int maxTokenType = vocabulary.getMaxTokenType(); |
| 70 | + String[] names = new String[maxTokenType + 1]; |
| 71 | + |
| 72 | + for (int i = 0; i <= maxTokenType; i++) { |
| 73 | + String symbolic = vocabulary.getSymbolicName(i); |
| 74 | + // Keep nulls as nulls (no symbolic name) |
| 75 | + names[i] = symbolic; |
| 76 | + } |
| 77 | + |
| 78 | + log.debug("Extracted {} symbolic names", names.length); |
| 79 | + return names; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Extract rule names from parser. |
| 84 | + * |
| 85 | + * <p>Parser.getRuleNames() is public API. |
| 86 | + * |
| 87 | + * @param parser Parser instance |
| 88 | + * @return Array of rule names |
| 89 | + */ |
| 90 | + public static String[] extractParserRuleNames(Parser parser) { |
| 91 | + String[] ruleNames = parser.getRuleNames(); |
| 92 | + log.debug("Extracted {} parser rule names", ruleNames.length); |
| 93 | + return ruleNames; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Extract rule names from lexer. |
| 98 | + * |
| 99 | + * <p>Lexer.getRuleNames() is public API (Lexer extends Recognizer). |
| 100 | + * |
| 101 | + * @param lexer Lexer instance |
| 102 | + * @return Array of lexer rule names |
| 103 | + */ |
| 104 | + public static String[] extractLexerRuleNames(Lexer lexer) { |
| 105 | + String[] ruleNames = lexer.getRuleNames(); |
| 106 | + log.debug("Extracted {} lexer rule names", ruleNames.length); |
| 107 | + return ruleNames; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Extract channel names from lexer. |
| 112 | + * |
| 113 | + * <p>ANTLR 4.x exposes channel names via getChannelNames() method in generated lexers. |
| 114 | + * This method dynamically extracts the actual channel names from the lexer instance. |
| 115 | + * |
| 116 | + * @param lexer Lexer instance |
| 117 | + * @return Array of channel names |
| 118 | + */ |
| 119 | + public static String[] extractChannelNames(Lexer lexer) { |
| 120 | + String[] channelNames = lexer.getChannelNames(); |
| 121 | + log.debug("Extracted {} channel names from lexer", channelNames.length); |
| 122 | + return channelNames; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Extract mode names from lexer. |
| 127 | + * |
| 128 | + * <p>ANTLR 4.x exposes mode names via getModeNames() method in generated lexers. |
| 129 | + * This method dynamically extracts the actual mode names from the lexer instance. |
| 130 | + * |
| 131 | + * @param lexer Lexer instance |
| 132 | + * @return Array of mode names |
| 133 | + */ |
| 134 | + public static String[] extractModeNames(Lexer lexer) { |
| 135 | + String[] modeNames = lexer.getModeNames(); |
| 136 | + log.debug("Extracted {} mode names from lexer", modeNames.length); |
| 137 | + return modeNames; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Compute grammar hash from ATN data (recommended). |
| 142 | + * |
| 143 | + * <p>This method hashes the serialized ATN arrays directly, which: |
| 144 | + * |
| 145 | + * <ul> |
| 146 | + * <li>Always available at runtime (no classpath dependencies) |
| 147 | + * <li>Reflects the actual artifact being served |
| 148 | + * <li>Changes when grammar changes (ATN structure changes) |
| 149 | + * </ul> |
| 150 | + * |
| 151 | + * @param lexerATN Serialized lexer ATN as int array |
| 152 | + * @param parserATN Serialized parser ATN as int array |
| 153 | + * @param antlrVersion ANTLR tool version (e.g., "4.13.2") |
| 154 | + * @return Hash string in format "sha256:abc123..." |
| 155 | + */ |
| 156 | + public static String computeGrammarHash(int[] lexerATN, int[] parserATN, String antlrVersion) { |
| 157 | + try { |
| 158 | + MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
| 159 | + |
| 160 | + // Hash lexer ATN data |
| 161 | + for (int value : lexerATN) { |
| 162 | + digest.update((byte) (value >> 8)); |
| 163 | + digest.update((byte) value); |
| 164 | + } |
| 165 | + |
| 166 | + // Hash parser ATN data |
| 167 | + for (int value : parserATN) { |
| 168 | + digest.update((byte) (value >> 8)); |
| 169 | + digest.update((byte) value); |
| 170 | + } |
| 171 | + |
| 172 | + // Hash ANTLR version to detect generator changes |
| 173 | + digest.update(antlrVersion.getBytes(StandardCharsets.UTF_8)); |
| 174 | + |
| 175 | + // Compute hash |
| 176 | + byte[] hashBytes = digest.digest(); |
| 177 | + String result = "sha256:" + bytesToHex(hashBytes); |
| 178 | + |
| 179 | + log.info("Computed grammar hash from ATN data: {}", result); |
| 180 | + return result; |
| 181 | + |
| 182 | + } catch (NoSuchAlgorithmException e) { |
| 183 | + // SHA-256 is required by Java specification, this should never happen |
| 184 | + throw new IllegalStateException("SHA-256 algorithm not available", e); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + |
| 189 | + /** |
| 190 | + * Convert byte array to hex string. |
| 191 | + * |
| 192 | + * @param bytes Input bytes |
| 193 | + * @return Hex string (lowercase) |
| 194 | + */ |
| 195 | + private static String bytesToHex(byte[] bytes) { |
| 196 | + StringBuilder sb = new StringBuilder(bytes.length * 2); |
| 197 | + for (byte b : bytes) { |
| 198 | + sb.append(String.format("%02x", b & 0xFF)); |
| 199 | + } |
| 200 | + return sb.toString(); |
| 201 | + } |
| 202 | +} |
0 commit comments