1+ import type { EditorState } from "@codemirror/state" ;
2+ import type { Option } from "node-sql-parser" ;
13import { lazy } from "../utils.js" ;
4+ import type { SqlParseError , SqlParseResult , SqlParser } from "./types.js" ;
25
3- /**
4- * Represents a SQL parsing error with location information
5- */
6- export interface SqlParseError {
7- /** Error message describing the issue */
8- message : string ;
9- /** Line number where the error occurred (1-indexed) */
10- line : number ;
11- /** Column number where the error occurred (1-indexed) */
12- column : number ;
13- /** Severity level of the error */
14- severity : "error" | "warning" ;
15- }
16-
17- /**
18- * Result of parsing a SQL statement
19- */
20- export interface SqlParseResult {
21- /** Whether parsing was successful */
22- success : boolean ;
23- /** Array of parsing errors, if any */
24- errors : SqlParseError [ ] ;
25- /** The parsed AST if successful */
26- ast ?: unknown ;
6+ interface NodeSqlParserOptions {
7+ getParserOptions ?: ( state : EditorState ) => Option ;
278}
289
2910/**
3011 * A SQL parser wrapper around node-sql-parser with enhanced error handling
3112 * and validation capabilities for CodeMirror integration.
13+ *
14+ * @example Custom dialect
15+ * ```ts
16+ * import { NodeSqlParser } from "@marimo-team/codemirror-sql";
17+ *
18+ * const myParser = new NodeSqlParser({
19+ * getParserOptions: (state) => ({
20+ * dialect: getDialect(state),
21+ * parseOptions: {
22+ * includeLocations: true,
23+ * },
24+ * }),
25+ * });
26+ * ```
3227 */
33- export class SqlParser {
28+ export class NodeSqlParser implements SqlParser {
29+ private opts : NodeSqlParserOptions ;
30+
31+ constructor ( opts : NodeSqlParserOptions = { } ) {
32+ this . opts = opts ;
33+ }
34+
3435 /**
3536 * Lazy import of the node-sql-parser package and create a new Parser instance.
3637 */
@@ -39,10 +40,11 @@ export class SqlParser {
3940 return new Parser ( ) ;
4041 } ) ;
4142
42- async parse ( sql : string ) : Promise < SqlParseResult > {
43+ async parse ( sql : string , opts : { state : EditorState } ) : Promise < SqlParseResult > {
4344 try {
45+ const parserOptions = this . opts . getParserOptions ?.( opts . state ) ;
4446 const parser = await this . getParser ( ) ;
45- const ast = parser . astify ( sql ) ;
47+ const ast = parser . astify ( sql , parserOptions ) ;
4648
4749 return {
4850 success : true ,
@@ -102,8 +104,8 @@ export class SqlParser {
102104 . trim ( ) ;
103105 }
104106
105- async validateSql ( sql : string ) : Promise < SqlParseError [ ] > {
106- const result = await this . parse ( sql ) ;
107+ async validateSql ( sql : string , opts : { state : EditorState } ) : Promise < SqlParseError [ ] > {
108+ const result = await this . parse ( sql , opts ) ;
107109 return result . errors ;
108110 }
109111}
0 commit comments