-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve errorListener msg (#281)
* feat: add mysql errorListener and commonErrorListener * feat: improve other sql error msg * feat: support i18n for error msg * feat: add all sql errorMsg unit test * feat: update locale file and change i18n funtion name * test: upate error unit test
- Loading branch information
Showing
27 changed files
with
1,310 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const zh_CN = { | ||
stmtInComplete: '语句不完整', | ||
noValidPosition: '在此位置无效', | ||
expecting: ',期望', | ||
unfinishedMultilineComment: '未完成的多行注释', | ||
unfinishedDoubleQuoted: '未完成的双引号字符串字变量', | ||
unfinishedSingleQuoted: '未完成的单引号字符串字变量', | ||
unfinishedTickQuoted: '未完成的反引号引用字符串字变量', | ||
noValidInput: '没有有效的输入', | ||
newObj: '一个新的对象', | ||
existingObj: '一个存在的对象', | ||
new: '一个新的', | ||
existing: '一个存在的', | ||
orKeyword: '或者一个关键字', | ||
keyword: '一个关键字', | ||
missing: '缺少', | ||
at: '在', | ||
or: '或者', | ||
}; | ||
|
||
const en_US: typeof zh_CN = { | ||
stmtInComplete: 'Statement is incomplete', | ||
noValidPosition: 'is not valid at this position', | ||
expecting: ', expecting ', | ||
unfinishedMultilineComment: 'Unfinished multiline comment', | ||
unfinishedDoubleQuoted: 'Unfinished double quoted string literal', | ||
unfinishedSingleQuoted: 'Unfinished single quoted string literal', | ||
unfinishedTickQuoted: 'Unfinished back tick quoted string literal', | ||
noValidInput: 'is no valid input at all', | ||
newObj: 'a new object', | ||
existingObj: 'an existing object', | ||
new: 'a new ', | ||
existing: 'an existing ', | ||
orKeyword: ' or a keyword', | ||
keyword: 'a keyword', | ||
missing: 'missing ', | ||
at: ' at ', | ||
or: ' or ', | ||
}; | ||
|
||
const i18n = { | ||
zh_CN, | ||
en_US, | ||
}; | ||
|
||
export { i18n }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { LOCALE_TYPE } from './types'; | ||
import { i18n } from '../../locale/locale'; | ||
|
||
/** | ||
* transform message to locale language | ||
* @param message error msg | ||
* @param locale language setting | ||
*/ | ||
function transform(message: string, locale: LOCALE_TYPE) { | ||
const regex = /{([^}]+)}/g; | ||
return message.replace( | ||
regex, | ||
(_, key: keyof (typeof i18n)[typeof locale]) => i18n[locale][key] || '' | ||
); | ||
} | ||
|
||
export { transform }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { CodeCompletionCore } from 'antlr4-c3'; | ||
import { ErrorListener, ParseErrorListener } from '../common/parseErrorListener'; | ||
import { Parser, Token } from 'antlr4ng'; | ||
import { FlinkSqlParser } from '../../lib/flink/FlinkSqlParser'; | ||
import { LOCALE_TYPE } from '../common/types'; | ||
|
||
export class FlinkErrorListener extends ParseErrorListener { | ||
private preferredRules: Set<number>; | ||
|
||
private objectNames: Map<number, string> = new Map([ | ||
[FlinkSqlParser.RULE_catalogPath, 'catalog'], | ||
[FlinkSqlParser.RULE_catalogPathCreate, 'catalog'], | ||
[FlinkSqlParser.RULE_databasePath, 'database'], | ||
[FlinkSqlParser.RULE_databasePathCreate, 'database'], | ||
[FlinkSqlParser.RULE_tablePath, 'table'], | ||
[FlinkSqlParser.RULE_tablePathCreate, 'table'], | ||
[FlinkSqlParser.RULE_viewPath, 'view'], | ||
[FlinkSqlParser.RULE_viewPathCreate, 'view'], | ||
[FlinkSqlParser.RULE_functionName, 'function'], | ||
[FlinkSqlParser.RULE_functionNameCreate, 'function'], | ||
[FlinkSqlParser.RULE_columnName, 'column'], | ||
[FlinkSqlParser.RULE_columnNameCreate, 'column'], | ||
]); | ||
|
||
constructor(errorListener: ErrorListener, preferredRules: Set<number>, locale: LOCALE_TYPE) { | ||
super(errorListener, locale); | ||
this.preferredRules = preferredRules; | ||
} | ||
|
||
public getExpectedText(parser: Parser, token: Token) { | ||
let expectedText = ''; | ||
|
||
let currentContext = parser.context ?? undefined; | ||
while (currentContext?.parent) { | ||
currentContext = currentContext.parent; | ||
} | ||
|
||
const core = new CodeCompletionCore(parser); | ||
core.preferredRules = this.preferredRules; | ||
const candidates = core.collectCandidates(token.tokenIndex, currentContext); | ||
|
||
if (candidates.rules.size) { | ||
const result: string[] = []; | ||
// get expectedText as collect rules first | ||
for (const candidate of candidates.rules) { | ||
const [ruleType] = candidate; | ||
const name = this.objectNames.get(ruleType); | ||
switch (ruleType) { | ||
case FlinkSqlParser.RULE_databasePath: | ||
case FlinkSqlParser.RULE_tablePath: | ||
case FlinkSqlParser.RULE_viewPath: | ||
case FlinkSqlParser.RULE_functionName: | ||
case FlinkSqlParser.RULE_columnName: | ||
case FlinkSqlParser.RULE_catalogPath: { | ||
result.push(`{existing}${name}`); | ||
break; | ||
} | ||
case FlinkSqlParser.RULE_databasePathCreate: | ||
case FlinkSqlParser.RULE_tablePathCreate: | ||
case FlinkSqlParser.RULE_functionNameCreate: | ||
case FlinkSqlParser.RULE_viewPathCreate: | ||
case FlinkSqlParser.RULE_columnNameCreate: | ||
case FlinkSqlParser.RULE_catalogPathCreate: { | ||
result.push(`{new}${name}`); | ||
break; | ||
} | ||
} | ||
} | ||
expectedText = result.join('{or}'); | ||
} | ||
if (candidates.tokens.size) { | ||
expectedText += expectedText ? '{orKeyword}' : '{keyword}'; | ||
} | ||
return expectedText; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.