@rsconcept/domain 1.6.1 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{analyzer-camCAEkQ.d.ts → analyzer-QxWs73gP.d.ts} +2 -2
- package/dist/{ast-annotations-DiXmFraa.d.ts → ast-annotations-CtF7C9uK.d.ts} +2 -2
- package/dist/{calculator-BuVooqpb.d.ts → calculator-REeX2ZS1.d.ts} +2 -2
- package/dist/{error-CZXO5Y6a.d.ts → error-toW0TUHF.d.ts} +10 -1
- package/dist/index.d.ts +4 -4
- package/dist/library/rsengine.d.ts +1 -1
- package/dist/library/rsform-api.d.ts +1 -1
- package/dist/library/rsform.d.ts +1 -1
- package/dist/rslang/ast-annotations.d.ts +1 -1
- package/dist/rslang/error.d.ts +1 -1
- package/dist/rslang/error.js +9 -0
- package/dist/rslang/error.js.map +1 -1
- package/dist/rslang/eval/calculator.d.ts +1 -1
- package/dist/rslang/eval/evaluator.d.ts +1 -1
- package/dist/rslang/index.d.ts +4 -4
- package/dist/rslang/labels.d.ts +1 -1
- package/dist/rslang/labels.js +1 -1
- package/dist/rslang/labels.js.map +1 -1
- package/dist/rslang/parser/normalize.js +5 -1
- package/dist/rslang/parser/normalize.js.map +1 -1
- package/dist/rslang/parser/syntax-errors/index.d.ts +22 -0
- package/dist/rslang/parser/syntax-errors/index.js +2 -0
- package/dist/rslang/semantic/analyzer.d.ts +1 -1
- package/dist/rslang/semantic/analyzer.js +1 -1
- package/dist/rslang/semantic/type-auditor.d.ts +1 -1
- package/dist/rslang/semantic/value-auditor.d.ts +1 -1
- package/dist/syntax-errors-pCI6RicF.js +689 -0
- package/dist/syntax-errors-pCI6RicF.js.map +1 -0
- package/package.json +1 -1
- package/src/rslang/error.ts +9 -0
- package/src/rslang/labels.ts +1 -1
- package/src/rslang/parser/README.md +73 -0
- package/src/rslang/parser/normalize.ts +5 -1
- package/src/rslang/parser/parser.test.ts +92 -6
- package/src/rslang/parser/syntax-errors/README.md +61 -0
- package/src/rslang/parser/syntax-errors/ast-utils.ts +112 -0
- package/src/rslang/parser/syntax-errors/classify.ts +70 -0
- package/src/rslang/parser/syntax-errors/context.ts +25 -0
- package/src/rslang/parser/syntax-errors/error-builders.ts +28 -0
- package/src/rslang/parser/syntax-errors/expression-errors.ts +237 -0
- package/src/rslang/parser/syntax-errors/function-calls.ts +76 -0
- package/src/rslang/parser/syntax-errors/generators.ts +307 -0
- package/src/rslang/parser/syntax-errors/incomplete-formal.ts +76 -0
- package/src/rslang/parser/syntax-errors/incomplete-operands.ts +184 -0
- package/src/rslang/parser/syntax-errors/index.ts +102 -0
- package/src/rslang/parser/syntax-errors/trailing-slot.ts +45 -0
- package/dist/rslang/parser/syntax-errors.d.ts +0 -14
- package/dist/rslang/parser/syntax-errors.js +0 -311
- package/dist/rslang/parser/syntax-errors.js.map +0 -1
- package/src/rslang/parser/syntax-errors.ts +0 -511
|
@@ -1,511 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Module: Syntactic errors reporting.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { type AstNode, visitAstDFS } from '../../parsing';
|
|
6
|
-
import { annotateError } from '../ast-annotations';
|
|
7
|
-
import { type ErrorReporter, RSErrorCode, type RSErrorDescription } from '../error';
|
|
8
|
-
import { type TypeClass, TypeClass as TypeClassEnum } from '../semantic/typification';
|
|
9
|
-
|
|
10
|
-
import { Arguments, Filter, Function, Function_decl, Predicate, Variable } from './parser.terms';
|
|
11
|
-
import { TokenID } from './token';
|
|
12
|
-
|
|
13
|
-
/** Options for syntax error extraction. */
|
|
14
|
-
export interface SyntaxErrorOptions {
|
|
15
|
-
expected?: TypeClass;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/** Extracts syntax errors from an AST. */
|
|
19
|
-
export function extractSyntaxErrors(
|
|
20
|
-
ast: AstNode,
|
|
21
|
-
expression: string,
|
|
22
|
-
reporter: ErrorReporter,
|
|
23
|
-
annotateErrors: boolean = false,
|
|
24
|
-
options?: SyntaxErrorOptions
|
|
25
|
-
) {
|
|
26
|
-
const forbiddenErrors = extractForbiddenCharacterErrors(expression);
|
|
27
|
-
if (forbiddenErrors.length > 0) {
|
|
28
|
-
for (const error of forbiddenErrors) {
|
|
29
|
-
reporter(error);
|
|
30
|
-
if (annotateErrors) {
|
|
31
|
-
annotateError(ast, error.code, error.params);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const collected: RSErrorDescription[] = [];
|
|
38
|
-
const annotationTargets = new Map<RSErrorDescription, AstNode>();
|
|
39
|
-
const collect = (error: RSErrorDescription, target?: AstNode) => {
|
|
40
|
-
collected.push(error);
|
|
41
|
-
if (target !== undefined) {
|
|
42
|
-
annotationTargets.set(error, target);
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const bracketError = extractBracketErrors(expression);
|
|
47
|
-
if (bracketError !== null) {
|
|
48
|
-
collect(bracketError, ast);
|
|
49
|
-
}
|
|
50
|
-
const hasBracketErrors = bracketError !== null;
|
|
51
|
-
visitAstDFS(ast, node => extractInternal(node, expression, collect, hasBracketErrors, options));
|
|
52
|
-
|
|
53
|
-
for (const error of deduplicateErrors(collected)) {
|
|
54
|
-
reporter(error);
|
|
55
|
-
if (annotateErrors) {
|
|
56
|
-
const target = annotationTargets.get(error);
|
|
57
|
-
if (target !== undefined) {
|
|
58
|
-
annotateError(target, error.code, error.params);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// ====== Internals =========
|
|
65
|
-
function extractInternal(
|
|
66
|
-
node: AstNode,
|
|
67
|
-
expression: string,
|
|
68
|
-
collect: (error: RSErrorDescription, target: AstNode) => void,
|
|
69
|
-
ignoreUnknownErrors: boolean,
|
|
70
|
-
options?: SyntaxErrorOptions
|
|
71
|
-
) {
|
|
72
|
-
if (node.typeID !== TokenID.ERROR) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function emit(target: AstNode, code: RSErrorCode, params?: readonly string[]) {
|
|
77
|
-
collect({ code: code, from: target.from, to: target.to, params }, target);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const classified = classifyParseError(node, expression, options);
|
|
81
|
-
if (classified !== null) {
|
|
82
|
-
collect(classified, node);
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const parent = node.parent;
|
|
87
|
-
if (parent === null) {
|
|
88
|
-
if (!ignoreUnknownErrors) {
|
|
89
|
-
return emit(node, RSErrorCode.unknownSyntax);
|
|
90
|
-
}
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (parent.typeID === Variable) {
|
|
95
|
-
return emit(parent, RSErrorCode.expectedLocal);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (!ignoreUnknownErrors) {
|
|
99
|
-
emit(node.from === node.to ? parent : node, RSErrorCode.unknownSyntax);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function classifyParseError(
|
|
104
|
-
node: AstNode,
|
|
105
|
-
expression: string,
|
|
106
|
-
options?: SyntaxErrorOptions
|
|
107
|
-
): RSErrorDescription | null {
|
|
108
|
-
const incompleteFunctionDecl = detectIncompleteFunctionDecl(node, expression, options?.expected);
|
|
109
|
-
if (incompleteFunctionDecl !== null) {
|
|
110
|
-
return incompleteFunctionDecl;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const filterParen = detectFilterParenMismatch(node, expression);
|
|
114
|
-
if (filterParen !== null) {
|
|
115
|
-
return filterParen;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return detectGlobalFuncWithoutArgs(node, expression);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function detectIncompleteFunctionDecl(
|
|
122
|
-
node: AstNode,
|
|
123
|
-
expression: string,
|
|
124
|
-
expected?: TypeClass
|
|
125
|
-
): RSErrorDescription | null {
|
|
126
|
-
const funcDecl = findAncestor(node, isFunctionDeclNode);
|
|
127
|
-
if (funcDecl === null) {
|
|
128
|
-
return null;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const bodyChild = funcDecl.children[funcDecl.children.length - 1];
|
|
132
|
-
if (bodyChild?.typeID !== TokenID.ERROR || node !== bodyChild) {
|
|
133
|
-
return null;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const argsNode = funcDecl.children.find(child => child.typeID === Arguments || child.typeID === TokenID.NT_ARGUMENTS);
|
|
137
|
-
if (argsNode === undefined || subtreeHasError(argsNode)) {
|
|
138
|
-
return null;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (expression.slice(funcDecl.to).trim().length > 0) {
|
|
142
|
-
return null;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return {
|
|
146
|
-
code: incompleteFormalExpressionCode(expected),
|
|
147
|
-
from: bodyChild.from,
|
|
148
|
-
to: bodyChild.to
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function incompleteFormalExpressionCode(expected?: TypeClass): RSErrorCode {
|
|
153
|
-
switch (expected) {
|
|
154
|
-
case TypeClassEnum.function:
|
|
155
|
-
return RSErrorCode.expectedExpressionBody;
|
|
156
|
-
case TypeClassEnum.predicate:
|
|
157
|
-
case TypeClassEnum.logic:
|
|
158
|
-
return RSErrorCode.expectedLogicBody;
|
|
159
|
-
default:
|
|
160
|
-
return RSErrorCode.expectedFunctionBody;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function subtreeHasError(node: AstNode): boolean {
|
|
165
|
-
if (node.typeID === TokenID.ERROR) {
|
|
166
|
-
return true;
|
|
167
|
-
}
|
|
168
|
-
return node.children.some(subtreeHasError);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
function detectFilterParenMismatch(node: AstNode, expression: string): RSErrorDescription | null {
|
|
172
|
-
const filterNode = findAncestor(node, isFilterNode) ?? findRelatedNode(node, isFilterNode);
|
|
173
|
-
if (filterNode === null) {
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
const nextChar = expression[filterNode.to];
|
|
178
|
-
if (nextChar === '(') {
|
|
179
|
-
return {
|
|
180
|
-
code: RSErrorCode.invalidFilterSyntax,
|
|
181
|
-
from: filterNode.from,
|
|
182
|
-
to: filterNode.to + 1
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
return null;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
function detectGlobalFuncWithoutArgs(node: AstNode, expression: string): RSErrorDescription | null {
|
|
189
|
-
const funcNode =
|
|
190
|
-
findAncestor(node, n => isFunctionNode(n) || isPredicateNode(n)) ??
|
|
191
|
-
findRelatedNode(node, n => isFunctionNode(n) || isPredicateNode(n));
|
|
192
|
-
if (funcNode === null) {
|
|
193
|
-
return null;
|
|
194
|
-
}
|
|
195
|
-
return classifyFuncWithoutArgs(funcNode, expression);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function findRelatedNode(node: AstNode, predicate: (target: AstNode) => boolean): AstNode | null {
|
|
199
|
-
let current: AstNode | null = node.parent;
|
|
200
|
-
while (current !== null) {
|
|
201
|
-
for (const child of current.children) {
|
|
202
|
-
if (predicate(child)) {
|
|
203
|
-
return child;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
current = current.parent;
|
|
207
|
-
}
|
|
208
|
-
return null;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
function classifyFuncWithoutArgs(funcNode: AstNode, expression: string): RSErrorDescription | null {
|
|
212
|
-
const name = expression.slice(funcNode.from, funcNode.to);
|
|
213
|
-
const rest = expression.slice(funcNode.to);
|
|
214
|
-
|
|
215
|
-
const withoutArgs: RSErrorDescription = {
|
|
216
|
-
code: RSErrorCode.globalFuncWithoutArgs,
|
|
217
|
-
from: funcNode.from,
|
|
218
|
-
to: funcNode.to,
|
|
219
|
-
params: [name]
|
|
220
|
-
};
|
|
221
|
-
|
|
222
|
-
// Function with nothing (or only whitespace) after it is genuinely called without arguments.
|
|
223
|
-
if (/^\s*$/.test(rest)) {
|
|
224
|
-
return withoutArgs;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// Anything other than a bracketed argument form (e.g. parentheses or braces) is malformed call
|
|
228
|
-
// syntax: defer to the bracket/syntax error handling instead of reporting a missing-arguments error.
|
|
229
|
-
const openBracket = /^\s*\[/.exec(rest);
|
|
230
|
-
if (openBracket === null) {
|
|
231
|
-
return null;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const closeIdx = findMatchingCloseBracket(rest, openBracket[0].length - 1);
|
|
235
|
-
if (closeIdx >= 0) {
|
|
236
|
-
const inner = rest.slice(openBracket[0].length, closeIdx).trim();
|
|
237
|
-
if (inner.length === 0) {
|
|
238
|
-
return withoutArgs;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
return null;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
function findAncestor(node: AstNode, predicate: (node: AstNode) => boolean): AstNode | null {
|
|
246
|
-
let current: AstNode | null = node;
|
|
247
|
-
while (current !== null) {
|
|
248
|
-
if (predicate(current)) {
|
|
249
|
-
return current;
|
|
250
|
-
}
|
|
251
|
-
current = current.parent;
|
|
252
|
-
}
|
|
253
|
-
return null;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
function isFunctionDeclNode(node: AstNode): boolean {
|
|
257
|
-
return node.typeID === Function_decl || node.typeID === TokenID.NT_FUNC_DEFINITION;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
function isFunctionNode(node: AstNode): boolean {
|
|
261
|
-
return node.typeID === Function || node.typeID === TokenID.ID_FUNCTION;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
function isPredicateNode(node: AstNode): boolean {
|
|
265
|
-
return node.typeID === Predicate || node.typeID === TokenID.ID_PREDICATE;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
function isFilterNode(node: AstNode): boolean {
|
|
269
|
-
return node.typeID === Filter || node.typeID === TokenID.FILTER;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
function findMatchingCloseBracket(expression: string, openPos: number): number {
|
|
273
|
-
const open = expression[openPos];
|
|
274
|
-
if (open !== '[') {
|
|
275
|
-
return -1;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
let depth = 0;
|
|
279
|
-
for (let index = openPos; index < expression.length; index++) {
|
|
280
|
-
const symbol = expression[index];
|
|
281
|
-
if (symbol === '[') {
|
|
282
|
-
depth++;
|
|
283
|
-
} else if (symbol === ']') {
|
|
284
|
-
depth--;
|
|
285
|
-
if (depth === 0) {
|
|
286
|
-
return index;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
return -1;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
function deduplicateErrors(errors: RSErrorDescription[]): RSErrorDescription[] {
|
|
294
|
-
const result: RSErrorDescription[] = [];
|
|
295
|
-
|
|
296
|
-
for (const error of errors) {
|
|
297
|
-
const duplicate = result.some(
|
|
298
|
-
existing => existing.code === error.code && existing.from === error.from && existing.to === error.to
|
|
299
|
-
);
|
|
300
|
-
if (duplicate) {
|
|
301
|
-
continue;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
if (error.code !== RSErrorCode.unknownSyntax) {
|
|
305
|
-
for (let index = result.length - 1; index >= 0; index--) {
|
|
306
|
-
if (result[index].code === RSErrorCode.unknownSyntax && rangesOverlap(error, result[index])) {
|
|
307
|
-
result.splice(index, 1);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
result.push(error);
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
const specific = result.filter(error => error.code !== RSErrorCode.unknownSyntax);
|
|
316
|
-
return result.filter(
|
|
317
|
-
error => error.code !== RSErrorCode.unknownSyntax || !specific.some(item => rangesOverlap(error, item))
|
|
318
|
-
);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
function rangesOverlap(a: RSErrorDescription, b: RSErrorDescription): boolean {
|
|
322
|
-
return a.from < b.to && b.from < a.to;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
type OpenBracket = '(' | '[' | '{';
|
|
326
|
-
type CloseBracket = ')' | ']' | '}';
|
|
327
|
-
|
|
328
|
-
interface BracketFrame {
|
|
329
|
-
bracket: OpenBracket;
|
|
330
|
-
index: number;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
const ALLOWED_EXPRESSION_CHARS = buildAllowedExpressionChars();
|
|
334
|
-
|
|
335
|
-
function buildAllowedExpressionChars(): Set<string> {
|
|
336
|
-
const chars = new Set<string>();
|
|
337
|
-
for (const chunk of [
|
|
338
|
-
'0123456789',
|
|
339
|
-
'abcdefghijklmnopqrstuvwxyz',
|
|
340
|
-
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
341
|
-
'_',
|
|
342
|
-
'∅¬∀∃⇔⇒∨&ℬ+-*∪\\∆∩×∈∉⊆⊄⊂<>≥≤≠=,:;|[]{}()'
|
|
343
|
-
]) {
|
|
344
|
-
for (const symbol of chunk) {
|
|
345
|
-
chars.add(symbol);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
for (let codePoint = 0x03b1; codePoint <= 0x03c9; codePoint++) {
|
|
349
|
-
chars.add(String.fromCodePoint(codePoint));
|
|
350
|
-
}
|
|
351
|
-
return chars;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
function isAllowedExpressionChar(symbol: string): boolean {
|
|
355
|
-
return /\s/u.test(symbol) || ALLOWED_EXPRESSION_CHARS.has(symbol);
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
function extractForbiddenCharacterErrors(expression: string): RSErrorDescription[] {
|
|
359
|
-
const errors: RSErrorDescription[] = [];
|
|
360
|
-
let pos = 0;
|
|
361
|
-
|
|
362
|
-
while (pos < expression.length) {
|
|
363
|
-
const symbol = expression[pos];
|
|
364
|
-
if (isAllowedExpressionChar(symbol)) {
|
|
365
|
-
pos++;
|
|
366
|
-
continue;
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
const start = pos;
|
|
370
|
-
while (pos < expression.length && !isAllowedExpressionChar(expression[pos])) {
|
|
371
|
-
pos++;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
errors.push({
|
|
375
|
-
code: RSErrorCode.forbiddenCharacter,
|
|
376
|
-
from: start,
|
|
377
|
-
to: pos,
|
|
378
|
-
params: [expression.slice(start, pos)]
|
|
379
|
-
});
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
return errors;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
function extractBracketErrors(expression: string): RSErrorDescription | null {
|
|
386
|
-
const stack: BracketFrame[] = [];
|
|
387
|
-
|
|
388
|
-
for (let pos = 0; pos < expression.length; pos++) {
|
|
389
|
-
const symbol = expression[pos];
|
|
390
|
-
if (isOpenBracket(symbol)) {
|
|
391
|
-
if (isDoubleParenthesis(expression, pos)) {
|
|
392
|
-
return {
|
|
393
|
-
code: RSErrorCode.doubleParenthesis,
|
|
394
|
-
from: pos,
|
|
395
|
-
to: pos + 2
|
|
396
|
-
};
|
|
397
|
-
}
|
|
398
|
-
stack.push({ bracket: symbol, index: pos });
|
|
399
|
-
continue;
|
|
400
|
-
}
|
|
401
|
-
if (!isCloseBracket(symbol)) {
|
|
402
|
-
continue;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
const expectedOpen = closeToOpen(symbol);
|
|
406
|
-
const top = stack[stack.length - 1];
|
|
407
|
-
if (top === undefined) {
|
|
408
|
-
return {
|
|
409
|
-
code: RSErrorCode.missingOpenBracket,
|
|
410
|
-
from: pos,
|
|
411
|
-
to: pos + 1,
|
|
412
|
-
params: [symbol]
|
|
413
|
-
};
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
if (top.bracket !== expectedOpen) {
|
|
417
|
-
return {
|
|
418
|
-
code: RSErrorCode.bracketMismatch,
|
|
419
|
-
from: pos,
|
|
420
|
-
to: pos + 1,
|
|
421
|
-
params: [openToClose(top.bracket), symbol]
|
|
422
|
-
};
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
stack.pop();
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
if (stack.length > 0) {
|
|
429
|
-
const unclosed = stack[0];
|
|
430
|
-
return {
|
|
431
|
-
code: RSErrorCode.missingCloseBracket,
|
|
432
|
-
from: unclosed.index,
|
|
433
|
-
to: unclosed.index + 1,
|
|
434
|
-
params: [unclosed.bracket]
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
return null;
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
function isOpenBracket(symbol: string): symbol is OpenBracket {
|
|
442
|
-
return symbol === '(' || symbol === '[' || symbol === '{';
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
function isCloseBracket(symbol: string): symbol is CloseBracket {
|
|
446
|
-
return symbol === ')' || symbol === ']' || symbol === '}';
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
function openToClose(symbol: OpenBracket): CloseBracket {
|
|
450
|
-
switch (symbol) {
|
|
451
|
-
case '(':
|
|
452
|
-
return ')';
|
|
453
|
-
case '[':
|
|
454
|
-
return ']';
|
|
455
|
-
case '{':
|
|
456
|
-
return '}';
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
function closeToOpen(symbol: CloseBracket): OpenBracket {
|
|
461
|
-
switch (symbol) {
|
|
462
|
-
case ')':
|
|
463
|
-
return '(';
|
|
464
|
-
case ']':
|
|
465
|
-
return '[';
|
|
466
|
-
case '}':
|
|
467
|
-
return '{';
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
function isDoubleParenthesis(expression: string, pos: number): boolean {
|
|
472
|
-
const isOpenDoubleParenthesis = expression[pos] === '(' && expression[pos + 1] === '(';
|
|
473
|
-
if (!isOpenDoubleParenthesis) {
|
|
474
|
-
return false;
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
const outerClose = findMatchingCloseParen(expression, pos);
|
|
478
|
-
const innerClose = findMatchingCloseParen(expression, pos + 1);
|
|
479
|
-
if (outerClose < 0 || innerClose < 0) {
|
|
480
|
-
return false;
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
return outerClose === innerClose + 1;
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
function findMatchingCloseParen(expression: string, openPos: number): number {
|
|
487
|
-
if (expression[openPos] !== '(') {
|
|
488
|
-
return -1;
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
const stack: OpenBracket[] = [];
|
|
492
|
-
for (let index = openPos; index < expression.length; index++) {
|
|
493
|
-
const symbol = expression[index];
|
|
494
|
-
if (isOpenBracket(symbol)) {
|
|
495
|
-
stack.push(symbol);
|
|
496
|
-
continue;
|
|
497
|
-
}
|
|
498
|
-
if (!isCloseBracket(symbol)) {
|
|
499
|
-
continue;
|
|
500
|
-
}
|
|
501
|
-
const top = stack.pop();
|
|
502
|
-
if (top === undefined || top !== closeToOpen(symbol)) {
|
|
503
|
-
return -1;
|
|
504
|
-
}
|
|
505
|
-
if (stack.length === 0) {
|
|
506
|
-
return index;
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
return -1;
|
|
511
|
-
}
|