Skip to content

Commit bb29871

Browse files
committed
Rewrites error messages in stack items into a static object.
1 parent 30e42a2 commit bb29871

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

mathjax3-ts/input/tex/StackItem.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ export abstract class BaseItem extends MmlStack implements StackItem {
341341
* A list of basic errors.
342342
* @type {{[key: string]: string[]}}
343343
*/
344-
protected errors: {[key: string]: string[]} = {
344+
protected static errors: {[key: string]: string[]} = {
345345
// @test ExtraOpenMissingClose
346346
end: ['MissingBeginExtraEnd', 'Missing \\begin{%1} or extra \\end{%1}'],
347347
// @test ExtraCloseMissingOpen
@@ -452,10 +452,10 @@ export abstract class BaseItem extends MmlStack implements StackItem {
452452
// @test Ampersand-error
453453
throw new TexError('Misplaced', 'Misplaced %1', item.getName());
454454
}
455-
if (item.isClose && this.errors[item.kind]) {
455+
if (item.isClose && this.getErrors(item.kind)) {
456456
// @test ExtraOpenMissingClose, ExtraCloseMissingOpen,
457457
// MissingLeftExtraRight, MissingBeginExtraEnd
458-
const [id, message] = this.errors[item.kind];
458+
const [id, message] = this.getErrors(item.kind);
459459
throw new TexError(id, message, item.getName());
460460
}
461461
if (!item.isFinal) {
@@ -500,4 +500,17 @@ export abstract class BaseItem extends MmlStack implements StackItem {
500500
return this.kind + '[' + this.nodes.join('; ') + ']';
501501
}
502502

503+
504+
/**
505+
* Get error messages for a particular types of stack items. This reads error
506+
* messages from the static errors object, which can be extended in
507+
* subclasses.
508+
* @param {string} kind The stack item type.
509+
* @return {string[]} The list of arguments for the TeXError.
510+
*/
511+
public getErrors(kind: string): string[] {
512+
const CLASS = (this.constructor as typeof BaseItem);
513+
return (CLASS.errors || {})[kind] || BaseItem.errors[kind];
514+
}
515+
503516
}

mathjax3-ts/input/tex/TexParser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ export default class TexParser {
179179
* Parses the current input string.
180180
*/
181181
public Parse() {
182-
let c, n;
182+
let c: string;
183+
let n: number;
183184
while (this.i < this.string.length) {
184185
c = this.string.charAt(this.i++);
185186
n = c.charCodeAt(0);

mathjax3-ts/input/tex/base/BaseItems.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ export class StopItem extends BaseItem {
112112
*/
113113
export class OpenItem extends BaseItem {
114114

115+
115116
/**
116117
* @override
117118
*/
118-
constructor(factory: StackItemFactory) {
119-
super(factory);
119+
protected static errors = Object.assign(Object.create(BaseItem.errors), {
120120
// @test ExtraOpenMissingClose
121-
this.errors['stop'] = ['ExtraOpenMissingClose',
122-
'Extra open brace or missing close brace'];
123-
}
121+
'stop': ['ExtraOpenMissingClose',
122+
'Extra open brace or missing close brace']
123+
});
124124

125125
/**
126126
* @override
@@ -212,18 +212,17 @@ export class SubsupItem extends BaseItem {
212212
/**
213213
* @override
214214
*/
215-
constructor(factory: StackItemFactory, ...nodes: MmlNode[]) {
216-
super(factory, ...nodes);
215+
protected static errors = Object.assign(Object.create(BaseItem.errors), {
217216
// @test MissingScript Sub, MissingScript Sup
218-
this.errors['stop'] = ['MissingScript',
219-
'Missing superscript or subscript argument'];
217+
'stop': ['MissingScript',
218+
'Missing superscript or subscript argument'],
220219
// @test MissingOpenForSup
221-
this.errors['sup'] = ['MissingOpenForSup',
222-
'Missing open brace for superscript'];
220+
'sup': ['MissingOpenForSup',
221+
'Missing open brace for superscript'],
223222
// @test MissingOpenForSub
224-
this.errors['sub'] = ['MissingOpenForSub',
225-
'Missing open brace for subscript'];
226-
}
223+
'sub': ['MissingOpenForSub',
224+
'Missing open brace for subscript']
225+
});
227226

228227
/**
229228
* @override
@@ -263,7 +262,7 @@ export class SubsupItem extends BaseItem {
263262
}
264263
if (super.checkItem(item)[1]) {
265264
// @test Brace Superscript Error, MissingOpenForSup, MissingOpenForSub
266-
const error = this.errors[['', 'sub', 'sup'][position]];
265+
const error = this.getErrors(['', 'sub', 'sup'][position]);
267266
throw new TexError(error[0], error[1], ...error.splice(2));
268267
}
269268
}
@@ -347,15 +346,22 @@ export class OverItem extends BaseItem {
347346
*/
348347
export class LeftItem extends BaseItem {
349348

349+
/**
350+
* @override
351+
*/
352+
protected static errors = Object.assign(Object.create(BaseItem.errors), {
353+
// @test ExtraLeftMissingRight
354+
'stop': ['ExtraLeftMissingRight',
355+
'Extra \\left or missing \\right']
356+
});
357+
358+
350359
/**
351360
* @override
352361
*/
353362
constructor(factory: StackItemFactory) {
354363
super(factory);
355-
this.setProperty('delim', '('),
356-
// @test ExtraLeftMissingRight
357-
this.errors['stop'] = ['ExtraLeftMissingRight',
358-
'Extra \\left or missing \\right'];
364+
this.setProperty('delim', '(');
359365
}
360366

361367
/**

0 commit comments

Comments
 (0)