|
8 | 8 | import java.nio.charset.StandardCharsets; |
9 | 9 | import java.security.MessageDigest; |
10 | 10 | import java.security.NoSuchAlgorithmException; |
| 11 | +import java.util.ArrayList; |
11 | 12 | import java.util.Arrays; |
| 13 | +import java.util.HashSet; |
| 14 | +import java.util.LinkedHashMap; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.Set; |
12 | 18 | import org.antlr.v4.runtime.CharStreams; |
13 | 19 | import org.antlr.v4.runtime.CommonTokenStream; |
14 | 20 | import org.antlr.v4.runtime.Vocabulary; |
@@ -52,9 +58,136 @@ public GrammarBundle build() { |
52 | 58 | .startRuleIndex(resolveStartRuleIndex(parser.getRuleNames())) |
53 | 59 | .literalNames(literalNames) |
54 | 60 | .symbolicNames(symbolicNames) |
| 61 | + .tokenDictionary(buildTokenDictionary(vocabulary)) |
| 62 | + .ignoredTokens(buildIgnoredTokens()) |
| 63 | + .rulesToVisit(buildRulesToVisit(parser.getRuleNames())) |
55 | 64 | .build(); |
56 | 65 | } |
57 | 66 |
|
| 67 | + /** |
| 68 | + * Build the token dictionary — semantic name → token type ID mapping. Uses lexer constants |
| 69 | + * since token type IDs are defined by the lexer. The frontend autocomplete enrichment uses |
| 70 | + * these to identify tokens like SPACE, PIPE, SOURCE by name. |
| 71 | + */ |
| 72 | + private static Map<String, Integer> buildTokenDictionary(Vocabulary vocabulary) { |
| 73 | + Map<String, Integer> dict = new LinkedHashMap<>(); |
| 74 | + // SPACE token may not exist in this grammar (whitespace may be implicitly skipped). |
| 75 | + // Resolve by searching symbolic names; use -1 if not found. |
| 76 | + dict.put("WHITESPACE", OpenSearchPPLLexer.WHITESPACE); |
| 77 | + dict.put("FROM", OpenSearchPPLLexer.FROM); |
| 78 | + dict.put("OPENING_BRACKET", OpenSearchPPLLexer.LT_PRTHS); |
| 79 | + dict.put("CLOSING_BRACKET", OpenSearchPPLLexer.RT_PRTHS); |
| 80 | + dict.put("SEARCH", OpenSearchPPLLexer.SEARCH); |
| 81 | + dict.put("SOURCE", OpenSearchPPLLexer.SOURCE); |
| 82 | + dict.put("PIPE", OpenSearchPPLLexer.PIPE); |
| 83 | + dict.put("ID", OpenSearchPPLLexer.ID); |
| 84 | + dict.put("EQUAL", OpenSearchPPLLexer.EQUAL); |
| 85 | + dict.put("IN", OpenSearchPPLLexer.IN); |
| 86 | + dict.put("COMMA", OpenSearchPPLLexer.COMMA); |
| 87 | + dict.put("BACKTICK_QUOTE", OpenSearchPPLLexer.BQUOTA_STRING); |
| 88 | + dict.put("DOT", OpenSearchPPLLexer.DOT); |
| 89 | + return dict; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Build the list of token type IDs to ignore for autocomplete. Mirrors the frontend |
| 94 | + * getIgnoredTokens() logic: explicitly ignore AS/IN, then ignore two contiguous token ranges |
| 95 | + * minus operatorsToInclude. |
| 96 | + * |
| 97 | + * <p>Range 1 (relevance/internal tokens): MATCH .. ERROR_RECOGNITION — covers relevance |
| 98 | + * functions, search parameters, span literals, IDs, quoted strings, and error tokens. |
| 99 | + * |
| 100 | + * <p>Range 2 (keywords/functions/operators): CASE .. CAST — covers CASE/ELSE, IN, EXISTS, |
| 101 | + * NOT/OR/AND/XOR, TRUE/FALSE, REGEXP, datetime parts, data type keywords, punctuation, |
| 102 | + * aggregate functions, math/text/date functions, and CAST. |
| 103 | + * |
| 104 | + * <p>Tokens in {@code operatorsToInclude} are kept as suggestions even if they fall within |
| 105 | + * an ignored range. |
| 106 | + */ |
| 107 | + private static int[] buildIgnoredTokens() { |
| 108 | + // Verify range boundaries match expected token IDs. If the grammar changes and |
| 109 | + // shifts token ordinals, these assertions surface the problem at build time. |
| 110 | + assert OpenSearchPPLParser.MATCH == 427 |
| 111 | + : "MATCH token ID shifted — update ignored range start"; |
| 112 | + assert OpenSearchPPLParser.ERROR_RECOGNITION == 488 |
| 113 | + : "ERROR_RECOGNITION token ID shifted — update ignored range end"; |
| 114 | + assert OpenSearchPPLParser.CASE == 142 |
| 115 | + : "CASE token ID shifted — update ignored range start"; |
| 116 | + assert OpenSearchPPLParser.CAST == 387 |
| 117 | + : "CAST token ID shifted — update ignored range end"; |
| 118 | + |
| 119 | + Set<Integer> operatorsToInclude = new HashSet<>(Arrays.asList( |
| 120 | + OpenSearchPPLParser.PIPE, OpenSearchPPLParser.EQUAL, OpenSearchPPLParser.COMMA, |
| 121 | + OpenSearchPPLParser.NOT_EQUAL, OpenSearchPPLParser.LESS, OpenSearchPPLParser.NOT_LESS, |
| 122 | + OpenSearchPPLParser.GREATER, OpenSearchPPLParser.NOT_GREATER, |
| 123 | + OpenSearchPPLParser.OR, OpenSearchPPLParser.AND, |
| 124 | + OpenSearchPPLParser.LT_PRTHS, OpenSearchPPLParser.RT_PRTHS, |
| 125 | + OpenSearchPPLParser.SPAN, |
| 126 | + OpenSearchPPLParser.MATCH, OpenSearchPPLParser.MATCH_PHRASE, |
| 127 | + OpenSearchPPLParser.MATCH_BOOL_PREFIX, OpenSearchPPLParser.MATCH_PHRASE_PREFIX, |
| 128 | + OpenSearchPPLParser.SQUOTA_STRING |
| 129 | + )); |
| 130 | + |
| 131 | + List<Integer> ignored = new ArrayList<>(); |
| 132 | + ignored.add(OpenSearchPPLParser.AS); |
| 133 | + ignored.add(OpenSearchPPLParser.IN); |
| 134 | + |
| 135 | + // Range 1: MATCH .. ERROR_RECOGNITION |
| 136 | + for (int i = OpenSearchPPLParser.MATCH; i <= OpenSearchPPLParser.ERROR_RECOGNITION; i++) { |
| 137 | + if (!operatorsToInclude.contains(i)) { |
| 138 | + ignored.add(i); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // Range 2: CASE .. CAST |
| 143 | + for (int i = OpenSearchPPLParser.CASE; i <= OpenSearchPPLParser.CAST; i++) { |
| 144 | + if (!operatorsToInclude.contains(i)) { |
| 145 | + ignored.add(i); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return ignored.stream().mapToInt(Integer::intValue).toArray(); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Build the list of parser rule indices for CodeCompletionCore preferredRules. |
| 154 | + * These rules trigger semantic suggestions (suggest fields, tables, functions, etc.). |
| 155 | + * |
| 156 | + * @throws IllegalStateException if any expected rule name is not found in the parser grammar |
| 157 | + */ |
| 158 | + private static int[] buildRulesToVisit(String[] ruleNames) { |
| 159 | + List<String> ruleNamesToVisit = Arrays.asList( |
| 160 | + "statsFunctionName", "takeAggFunction", "integerLiteral", "decimalLiteral", |
| 161 | + "keywordsCanBeId", "renameClasue", "qualifiedName", "tableQualifiedName", |
| 162 | + "wcQualifiedName", "positionFunctionName", "searchableKeyWord", "stringLiteral", |
| 163 | + "searchCommand", "searchComparisonOperator", "comparisonOperator", "sqlLikeJoinType" |
| 164 | + ); |
| 165 | + |
| 166 | + List<String> ruleNamesList = Arrays.asList(ruleNames); |
| 167 | + int[] indices = new int[ruleNamesToVisit.size()]; |
| 168 | + for (int i = 0; i < ruleNamesToVisit.size(); i++) { |
| 169 | + String name = ruleNamesToVisit.get(i); |
| 170 | + int idx = ruleNamesList.indexOf(name); |
| 171 | + if (idx < 0) { |
| 172 | + throw new IllegalStateException( |
| 173 | + "Parser rule '" + name + "' not found in grammar — " |
| 174 | + + "was it renamed or removed from OpenSearchPPLParser.g4?"); |
| 175 | + } |
| 176 | + indices[i] = idx; |
| 177 | + } |
| 178 | + return indices; |
| 179 | + } |
| 180 | + |
| 181 | + /** Resolve a token type ID from the vocabulary by symbolic name. Returns -1 if not found. */ |
| 182 | + private static int resolveTokenType(Vocabulary vocabulary, String name) { |
| 183 | + for (int i = 0; i <= vocabulary.getMaxTokenType(); i++) { |
| 184 | + if (name.equals(vocabulary.getSymbolicName(i))) { |
| 185 | + return i; |
| 186 | + } |
| 187 | + } |
| 188 | + return -1; |
| 189 | + } |
| 190 | + |
58 | 191 | private static int resolveStartRuleIndex(String[] ruleNames) { |
59 | 192 | int idx = Arrays.asList(ruleNames).indexOf("root"); |
60 | 193 | return Math.max(idx, 0); |
|
0 commit comments