@@ -3,10 +3,11 @@ import { ExtensionContext } from 'vscode';
33import { FlinkSQLLexer } from './FlinkSQLLexer' ; // 导入生成的词法分析器
44import { FlinkSQLParser } from './FlinkSQLParser' ; // 导入生成的解析器
55import { ANTLRInputStream , CommonTokenStream } from 'antlr4ts' ;
6- import { MyFlinkSQLVisitor } from './Grammar' ;
7- import { FlinkSQLReferenceProvider } from './Reference' ;
8- import { FlinkSQLRenameProvider } from './Rename' ;
9- import { ParserErrorListener , RecognitionException , Recognizer } from 'antlr4ts' ;
6+ import { MyFlinkSQLVisitor } from './Grammar' ;
7+ import { FlinkSQLReferenceProvider } from './Reference' ;
8+ import { FlinkSQLRenameProvider } from './Rename' ;
9+ import { ATNSimulator } from 'antlr4ts/atn/ATNSimulator'
10+ import { Token , ParserErrorListener , RecognitionException , Recognizer } from 'antlr4ts' ;
1011const sqlFormatter = require ( 'sql-formatter-plus' ) ;
1112
1213interface Config {
@@ -82,14 +83,26 @@ function updateFeatureStatus() {
8283 diagnosticCollection . clear ( ) ;
8384
8485 // 使用生成的词法分析器和解析器进行语法检查
85- const inputStream = new ANTLRInputStream ( event . getText ( ) ) ;
86+ const sourceText = event . getText ( ) ;
87+ const inputStream = new ANTLRInputStream ( sourceText ) ;
8688 const lexer = new FlinkSQLLexer ( inputStream ) ;
8789 const tokenStream = new CommonTokenStream ( lexer ) ;
8890 const parser = new FlinkSQLParser ( tokenStream ) ;
8991 parser . removeErrorListeners ( ) ;
9092 parser . addErrorListener ( {
91- syntaxError : ( recognizer : Recognizer < any , any > , offendingSymbol : any , line : number , charPositionInLine : number , msg : string , e : RecognitionException | undefined ) : void => {
92- vscode . window . showErrorMessage ( "Parser flink sql error. line: " + line + " position: " + charPositionInLine + " msg: " + msg ) ;
93+ syntaxError : ( recognizer : Recognizer < Token , ATNSimulator > , offendingSymbol : Token | undefined , line : number , startPosition : number , msg : string , e : RecognitionException | undefined ) : void => {
94+ let endPosition = startPosition + 1 ;
95+ if ( offendingSymbol != undefined && offendingSymbol . text != undefined
96+ && offendingSymbol && offendingSymbol . text !== null ) {
97+ endPosition = startPosition + offendingSymbol . text . length ;
98+ const splitedText = sourceText . split ( '\n' )
99+ const errorLine = splitedText [ line - 1 ]
100+ vscode . window . showErrorMessage ( "Parse error. line: " + line + " start position: "
101+ + startPosition + " end position: " + endPosition + " near the input: ' " + errorLine + " ' msg: " + msg ) ;
102+ } else {
103+ vscode . window . showErrorMessage ( "Parse error. line: " + line + " start position: "
104+ + startPosition + " end position: " + endPosition + " msg: " + msg ) ;
105+ }
93106 } ,
94107 } )
95108 parser . compileParseTreePattern
@@ -101,7 +114,7 @@ function updateFeatureStatus() {
101114 visitor . visit ( parseTree ) ;
102115 const errors = visitor . getErrors ( ) ;
103116 errors . forEach ( error => {
104- vscode . window . showInformationMessage ( "Visitor flink sql error. error: " + error ) ;
117+ vscode . window . showInformationMessage ( "Check error. error: " + error ) ;
105118 } )
106119 } ) ;
107120 }
0 commit comments