|
| 1 | +import {CharStreams, CommonTokenStream} from 'antlr4ts'; |
| 2 | +import {AbstractParseTreeVisitor} from 'antlr4ts/tree/AbstractParseTreeVisitor'; |
| 3 | + |
| 4 | +import {AntlrInputErrorListener} from './parseAssetSelectionQuery'; |
| 5 | +import {SelectionAutoCompleteLexer} from '../selection/generated/SelectionAutoCompleteLexer'; |
| 6 | +import { |
| 7 | + SelectionAutoCompleteParser, |
| 8 | + UnmatchedValueContext, |
| 9 | +} from '../selection/generated/SelectionAutoCompleteParser'; |
| 10 | +import {SelectionAutoCompleteVisitor} from '../selection/generated/SelectionAutoCompleteVisitor'; |
| 11 | + |
| 12 | +class UnmatchedValueQueryVisitor |
| 13 | + extends AbstractParseTreeVisitor<void> |
| 14 | + implements SelectionAutoCompleteVisitor<void> |
| 15 | +{ |
| 16 | + public isUnmatchedValue: boolean; |
| 17 | + |
| 18 | + defaultResult() {} |
| 19 | + |
| 20 | + constructor(private query: string) { |
| 21 | + super(); |
| 22 | + this.isUnmatchedValue = false; |
| 23 | + } |
| 24 | + |
| 25 | + visitUnmatchedValue(ctx: UnmatchedValueContext) { |
| 26 | + if (ctx.start.startIndex === 0 && ctx.stop?.stopIndex === this.query.length - 1) { |
| 27 | + // If a single attribute expression is the entire query, it's not a complex |
| 28 | + this.isUnmatchedValue = true; |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +export const isUnmatchedValueQuery = (query: string) => { |
| 34 | + try { |
| 35 | + const lexer = new SelectionAutoCompleteLexer(CharStreams.fromString(query)); |
| 36 | + lexer.removeErrorListeners(); |
| 37 | + lexer.addErrorListener(new AntlrInputErrorListener()); |
| 38 | + |
| 39 | + const tokenStream = new CommonTokenStream(lexer); |
| 40 | + |
| 41 | + const parser = new SelectionAutoCompleteParser(tokenStream); |
| 42 | + parser.removeErrorListeners(); |
| 43 | + parser.addErrorListener(new AntlrInputErrorListener()); |
| 44 | + |
| 45 | + const tree = parser.start(); |
| 46 | + |
| 47 | + const visitor = new UnmatchedValueQueryVisitor(query); |
| 48 | + visitor.visit(tree); |
| 49 | + return visitor.isUnmatchedValue; |
| 50 | + } catch { |
| 51 | + return false; |
| 52 | + } |
| 53 | +}; |
0 commit comments