-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.ts
47 lines (40 loc) · 1.86 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { CharStreams, CommonTokenStream } from "antlr4ts";
import { SqlBaseLexer } from "./SqlBaseLexer";
import { SqlBaseParser } from "./SqlBaseParser";
import { UppercaseCharStream } from "./UppercaseCharStream";
import { Cursor } from "./Cursor";
import _ from "lodash";
import { CompletionParserOptions, isCompletionOptions, SqlCompletionParseTree } from "./SqlCompletionParseTree";
import { LineageParserOptions, SqlLineageParseTree } from "./SqlLineageParseTree";
const defaultCursor = new Cursor("_CURSOR_");
function parse(sql: string, options: CompletionParserOptions): SqlCompletionParseTree;
function parse(sql: string, options?: LineageParserOptions): SqlLineageParseTree;
function parse(sql: string, options?: LineageParserOptions | CompletionParserOptions | undefined) {
const doubleQuotedIdentifier = options?.doubleQuotedIdentifier ?? false;
if (options !== undefined && isCompletionOptions(options)) {
sql = defaultCursor.insertAt(sql, options.cursorPosition);
}
const inputStream = new UppercaseCharStream(CharStreams.fromString(sql));
const lexer = new SqlBaseLexer(inputStream);
lexer.doublequoted_identifier = doubleQuotedIdentifier;
const tokens = new CommonTokenStream(lexer);
const parser = new SqlBaseParser(tokens);
parser.doublequoted_identifier = doubleQuotedIdentifier;
parser.buildParseTree = true;
parser.removeErrorListeners();
if (options !== undefined && isCompletionOptions(options)) {
return new SqlCompletionParseTree(parser.statement(), defaultCursor);
} else {
return new SqlLineageParseTree(parser.statement());
}
}
const antlr = {
/**
* Parses SQL text and builds parse tree suitable for further analysis and operations.
* @param sql SQL text
* @param options Options affecting parsing
* @returns Parsed SQL tree object with the number of possible operations
*/
parse
};
export default antlr;