@@ -10,6 +10,8 @@ import {
10
10
InputMismatchException ,
11
11
NoViableAltException ,
12
12
} from 'antlr4ng' ;
13
+ import { LOCALE_TYPE } from './types' ;
14
+ import { transformToI18n } from './transformToI18n' ;
13
15
14
16
/**
15
17
* Converted from {@link SyntaxError}.
@@ -46,8 +48,10 @@ export type ErrorListener = (parseError: ParseError, originalError: SyntaxError)
46
48
47
49
export abstract class ParseErrorListener implements ANTLRErrorListener {
48
50
private _errorListener : ErrorListener ;
51
+ private locale : LOCALE_TYPE ;
49
52
50
- constructor ( errorListener : ErrorListener ) {
53
+ constructor ( errorListener : ErrorListener , locale : LOCALE_TYPE = 'en_US' ) {
54
+ this . locale = locale ;
51
55
this . _errorListener = errorListener ;
52
56
}
53
57
@@ -88,20 +92,32 @@ export abstract class ParseErrorListener implements ANTLRErrorListener {
88
92
// handle missing or unwanted tokens.
89
93
message = msg ;
90
94
if ( msg . includes ( 'extraneous' ) ) {
91
- message = `'${ wrongText } ' is not valid at this position ${
92
- expectedText . length ? `, expecting ${ expectedText } ` : ''
95
+ message = `'${ wrongText } ' {noValidPosition} ${
96
+ expectedText . length ? `{ expecting} ${ expectedText } ` : ''
93
97
} `;
94
98
}
99
+ if ( msg . includes ( 'missing' ) ) {
100
+ const regex = / m i s s i n g \s + ' ( [ ^ ' ] + ) ' / ;
101
+ const match = msg . match ( regex ) ;
102
+ message = `{missing}` ;
103
+ if ( match ) {
104
+ const missKeyword = match [ 1 ] ;
105
+ message += `'${ missKeyword } '` ;
106
+ } else {
107
+ message += `{keyword}` ;
108
+ }
109
+ message += `{at}'${ wrongText } '` ;
110
+ }
95
111
} else {
96
112
// handle mismatch exception or no viable alt exception
97
113
if ( e instanceof InputMismatchException || e instanceof NoViableAltException ) {
98
114
if ( isEof ) {
99
- message = `statement is incomplete ` ;
115
+ message = `{stmtInComplete} ` ;
100
116
} else {
101
- message = `'${ wrongText } ' is not valid at this position ` ;
117
+ message = `'${ wrongText } ' {noValidPosition} ` ;
102
118
}
103
119
if ( expectedText . length > 0 ) {
104
- message += `, expecting ${ expectedText } ` ;
120
+ message += `{ expecting} ${ expectedText } ` ;
105
121
}
106
122
} else {
107
123
message = msg ;
@@ -117,24 +133,25 @@ export abstract class ParseErrorListener implements ANTLRErrorListener {
117
133
) ;
118
134
switch ( text [ 0 ] ) {
119
135
case '/' :
120
- message = 'Unfinished multiline comment ' ;
136
+ message = '{unfinishedMultilineComment} ' ;
121
137
break ;
122
138
case '"' :
123
- message = 'Unfinished double quoted string literal ' ;
139
+ message = '{unfinishedDoubleQuoted} ' ;
124
140
break ;
125
141
case "'" :
126
- message = 'Unfinished single quoted string literal ' ;
142
+ message = '{unfinishedSingleQuoted} ' ;
127
143
break ;
128
144
case '`' :
129
- message = 'Unfinished back tick quoted string literal ' ;
145
+ message = '{unfinishedTickQuoted} ' ;
130
146
break ;
131
147
132
148
default :
133
- message = '"' + text + '" is no valid input at all ' ;
149
+ message = '"' + text + '" {noValidInput} ' ;
134
150
break ;
135
151
}
136
152
}
137
153
}
154
+ message = transformToI18n ( message , this . locale ) ;
138
155
let endCol = charPositionInLine + 1 ;
139
156
if ( offendingSymbol && offendingSymbol . text !== null ) {
140
157
endCol = charPositionInLine + offendingSymbol . text . length ;
@@ -146,7 +163,7 @@ export abstract class ParseErrorListener implements ANTLRErrorListener {
146
163
endLine : line ,
147
164
startColumn : charPositionInLine + 1 ,
148
165
endColumn : endCol + 1 ,
149
- message : message ,
166
+ message,
150
167
} ,
151
168
{
152
169
e,
0 commit comments