UNPKG

dt-sql-parser

Version:

SQL Parsers for BigData, built with antlr4

126 lines (125 loc) 5.3 kB
import { TrinoSqlLexer } from '../../lib/trino/TrinoSqlLexer'; import { TrinoSqlParser } from '../../lib/trino/TrinoSqlParser'; import { BasicSQL } from '../common/basicSQL'; import { EntityContextType } from '../common/types'; import { TrinoEntityCollector } from './trinoEntityCollector'; import { TrinoErrorListener } from './trinoErrorListener'; import { TrinoSqlSplitListener } from './trinoSplitListener'; export { TrinoEntityCollector, TrinoSqlSplitListener }; export class TrinoSQL extends BasicSQL { constructor() { super(...arguments); this.preferredRules = new Set([ TrinoSqlParser.RULE_catalogRef, TrinoSqlParser.RULE_catalogNameCreate, TrinoSqlParser.RULE_schemaRef, TrinoSqlParser.RULE_schemaNameCreate, TrinoSqlParser.RULE_tableRef, TrinoSqlParser.RULE_tableNameCreate, TrinoSqlParser.RULE_viewRef, TrinoSqlParser.RULE_viewNameCreate, TrinoSqlParser.RULE_functionName, TrinoSqlParser.RULE_functionNameCreate, TrinoSqlParser.RULE_columnRef, TrinoSqlParser.RULE_columnNameCreate, ]); } createLexerFromCharStream(charStreams) { return new TrinoSqlLexer(charStreams); } createParserFromTokenStream(tokenStream) { return new TrinoSqlParser(tokenStream); } get splitListener() { return new TrinoSqlSplitListener(); } createErrorListener(_errorListener) { const parserContext = this; return new TrinoErrorListener(_errorListener, parserContext, this.preferredRules); } createEntityCollector(input, allTokens, caretTokenIndex) { return new TrinoEntityCollector(input, allTokens, caretTokenIndex); } processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) { const originalSyntaxSuggestions = []; const keywords = []; for (let candidate of candidates.rules) { const [ruleType, candidateRule] = candidate; const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset; const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1); let syntaxContextType = void 0; switch (ruleType) { case TrinoSqlParser.RULE_catalogRef: { syntaxContextType = EntityContextType.CATALOG; break; } case TrinoSqlParser.RULE_catalogNameCreate: { syntaxContextType = EntityContextType.CATALOG_CREATE; break; } case TrinoSqlParser.RULE_schemaRef: { syntaxContextType = EntityContextType.DATABASE; break; } case TrinoSqlParser.RULE_schemaNameCreate: { syntaxContextType = EntityContextType.DATABASE_CREATE; break; } case TrinoSqlParser.RULE_tableRef: { syntaxContextType = EntityContextType.TABLE; break; } case TrinoSqlParser.RULE_tableNameCreate: { syntaxContextType = EntityContextType.TABLE_CREATE; break; } case TrinoSqlParser.RULE_viewRef: { syntaxContextType = EntityContextType.VIEW; break; } case TrinoSqlParser.RULE_viewNameCreate: { syntaxContextType = EntityContextType.VIEW_CREATE; break; } case TrinoSqlParser.RULE_functionName: { syntaxContextType = EntityContextType.FUNCTION; break; } case TrinoSqlParser.RULE_functionNameCreate: { syntaxContextType = EntityContextType.FUNCTION_CREATE; break; } case TrinoSqlParser.RULE_columnNameCreate: { syntaxContextType = EntityContextType.COLUMN_CREATE; break; } case TrinoSqlParser.RULE_columnRef: { syntaxContextType = EntityContextType.COLUMN; break; } default: break; } if (syntaxContextType) { originalSyntaxSuggestions.push({ syntaxContextType, wordRanges: tokenRanges, }); } } for (let candidate of candidates.tokens) { const symbolicName = this._parser.vocabulary.getSymbolicName(candidate[0]); const displayName = this._parser.vocabulary.getDisplayName(candidate[0]); if (displayName && symbolicName && symbolicName.startsWith('KW_')) { const keyword = displayName.startsWith("'") && displayName.endsWith("'") ? displayName.slice(1, -1) : displayName; keywords.push(keyword); } } return { syntax: originalSyntaxSuggestions, keywords, }; } }