-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser.ts
More file actions
111 lines (99 loc) · 3.09 KB
/
Copy pathparser.ts
File metadata and controls
111 lines (99 loc) · 3.09 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import type { EditorState } from "@codemirror/state";
import type { Option } from "node-sql-parser";
import { lazy } from "../utils.js";
import type { SqlParseError, SqlParseResult, SqlParser } from "./types.js";
interface NodeSqlParserOptions {
getParserOptions?: (state: EditorState) => Option;
}
/**
* A SQL parser wrapper around node-sql-parser with enhanced error handling
* and validation capabilities for CodeMirror integration.
*
* @example Custom dialect
* ```ts
* import { NodeSqlParser } from "@marimo-team/codemirror-sql";
*
* const myParser = new NodeSqlParser({
* getParserOptions: (state) => ({
* dialect: getDialect(state),
* parseOptions: {
* includeLocations: true,
* },
* }),
* });
* ```
*/
export class NodeSqlParser implements SqlParser {
private opts: NodeSqlParserOptions;
constructor(opts: NodeSqlParserOptions = {}) {
this.opts = opts;
}
/**
* Lazy import of the node-sql-parser package and create a new Parser instance.
*/
private getParser = lazy(async () => {
const { Parser } = await import("node-sql-parser");
return new Parser();
});
async parse(sql: string, opts: { state: EditorState }): Promise<SqlParseResult> {
try {
const parserOptions = this.opts.getParserOptions?.(opts.state);
const parser = await this.getParser();
const ast = parser.astify(sql, parserOptions);
return {
success: true,
errors: [],
ast,
};
} catch (error: unknown) {
const parseError = this.extractErrorInfo(error, sql);
return {
success: false,
errors: [parseError],
};
}
}
private extractErrorInfo(error: unknown, _sql: string): SqlParseError {
let line = 1;
let column = 1;
const message = (error as Error)?.message || "SQL parsing error";
const errorObj = error as {
location?: { start?: { line: number; column: number } };
hash?: { line: number; loc?: { first_column: number } };
};
if (errorObj?.location) {
line = errorObj.location.start?.line || 1;
column = errorObj.location.start?.column || 1;
} else if (errorObj?.hash) {
line = errorObj.hash.line || 1;
column = errorObj.hash.loc?.first_column || 1;
} else {
const lineMatch = message.match(/line (\d+)/i);
const columnMatch = message.match(/column (\d+)/i);
if (lineMatch?.[1]) {
line = parseInt(lineMatch[1], 10);
}
if (columnMatch?.[1]) {
column = parseInt(columnMatch[1], 10);
}
}
return {
message: this.cleanErrorMessage(message),
line: Math.max(1, line),
column: Math.max(1, column),
severity: "error" as const,
};
}
private cleanErrorMessage(message: string): string {
return message
.replace(/^Error: /, "")
.replace(/Expected .* but .* found\./i, (match) =>
match.replace(/but .* found/, "found unexpected token"),
)
.trim();
}
async validateSql(sql: string, opts: { state: EditorState }): Promise<SqlParseError[]> {
const result = await this.parse(sql, opts);
return result.errors;
}
}