Skip to content

Commit

Permalink
Rewrites object to map.
Browse files Browse the repository at this point in the history
  • Loading branch information
zorkow committed Dec 2, 2023
1 parent 70a3b19 commit 15b1ee5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 52 deletions.
84 changes: 44 additions & 40 deletions ts/semantic_tree/semantic_mathml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ import * as SemanticUtil from './semantic_util.js';
import { MMLTAGS } from '../semantic_tree/semantic_util.js';

export class SemanticMathml extends SemanticAbstractParser<Element> {
private parseMap_: {
[key: string]: (p1: Element, p2: Element[]) => SemanticNode;
};
private parseMap_: Map<string, (p1: Element, p2: Element[]) => SemanticNode>;

/**
* Get an attribute from a node and provide a default if it does not exist. It
Expand Down Expand Up @@ -66,40 +64,39 @@ export class SemanticMathml extends SemanticAbstractParser<Element> {
*/
constructor() {
super('MathML');
this.parseMap_ = {
SEMANTICS: this.semantics_.bind(this),
MATH: this.rows_.bind(this),
MROW: this.rows_.bind(this),
MPADDED: this.rows_.bind(this),
MSTYLE: this.rows_.bind(this),
MFRAC: this.fraction_.bind(this),
MSUB: this.limits_.bind(this),
MSUP: this.limits_.bind(this),
MSUBSUP: this.limits_.bind(this),
MOVER: this.limits_.bind(this),
MUNDER: this.limits_.bind(this),
MUNDEROVER: this.limits_.bind(this),
MROOT: this.root_.bind(this),
MSQRT: this.sqrt_.bind(this),
MTABLE: this.table_.bind(this),
MLABELEDTR: this.tableLabeledRow_.bind(this),
MTR: this.tableRow_.bind(this),
MTD: this.tableCell_.bind(this),
MS: this.text_.bind(this),
MTEXT: this.text_.bind(this),
MSPACE: this.space_.bind(this),
'ANNOTATION-XML': this.text_.bind(this),
MI: this.identifier_.bind(this),
MN: this.number_.bind(this),
MO: this.operator_.bind(this),
MFENCED: this.fenced_.bind(this),
MENCLOSE: this.enclosed_.bind(this),
MMULTISCRIPTS: this.multiscripts_.bind(this),
ANNOTATION: this.empty_.bind(this),
NONE: this.empty_.bind(this),
MACTION: this.action_.bind(this)
};

this.parseMap_ = new Map([
[MMLTAGS.SEMANTICS, this.semantics_.bind(this)],
[MMLTAGS.MATH, this.rows_.bind(this)],
[MMLTAGS.MROW, this.rows_.bind(this)],
[MMLTAGS.MPADDED, this.rows_.bind(this)],
[MMLTAGS.MSTYLE, this.rows_.bind(this)],
[MMLTAGS.MFRAC, this.fraction_.bind(this)],
[MMLTAGS.MSUB, this.limits_.bind(this)],
[MMLTAGS.MSUP, this.limits_.bind(this)],
[MMLTAGS.MSUBSUP, this.limits_.bind(this)],
[MMLTAGS.MOVER, this.limits_.bind(this)],
[MMLTAGS.MUNDER, this.limits_.bind(this)],
[MMLTAGS.MUNDEROVER, this.limits_.bind(this)],
[MMLTAGS.MROOT, this.root_.bind(this)],
[MMLTAGS.MSQRT, this.sqrt_.bind(this)],
[MMLTAGS.MTABLE, this.table_.bind(this)],
[MMLTAGS.MLABELEDTR, this.tableLabeledRow_.bind(this)],
[MMLTAGS.MTR, this.tableRow_.bind(this)],
[MMLTAGS.MTD, this.tableCell_.bind(this)],
[MMLTAGS.MS, this.text_.bind(this)],
[MMLTAGS.MTEXT, this.text_.bind(this)],
[MMLTAGS.MSPACE, this.space_.bind(this)],
[MMLTAGS.ANNOTATIONXML, this.text_.bind(this)],
[MMLTAGS.MI, this.identifier_.bind(this)],
[MMLTAGS.MN, this.number_.bind(this)],
[MMLTAGS.MO, this.operator_.bind(this)],
[MMLTAGS.MFENCED, this.fenced_.bind(this)],
[MMLTAGS.MENCLOSE, this.enclosed_.bind(this)],
[MMLTAGS.MMULTISCRIPTS, this.multiscripts_.bind(this)],
[MMLTAGS.ANNOTATION, this.empty_.bind(this)],
[MMLTAGS.NONE, this.empty_.bind(this)],
[MMLTAGS.MACTION, this.action_.bind(this)],
]);
const meaning = {
type: SemanticType.IDENTIFIER,
role: SemanticRole.NUMBERSET,
Expand Down Expand Up @@ -131,12 +128,19 @@ export class SemanticMathml extends SemanticAbstractParser<Element> {
public parse(mml: Element) {
SemanticProcessor.getInstance().setNodeFactory(this.getFactory());
const children = DomUtil.toArray(mml.childNodes);
const tag = DomUtil.tagName(mml);
const func = this.parseMap_[tag];
const tag = DomUtil.tagName(mml) as MMLTAGS;
const func = this.parseMap_.get(tag);
const newNode = (func ? func : this.dummy_.bind(this))(mml, children);
SemanticUtil.addAttributes(newNode, mml);
if (
['MATH', 'MROW', 'MPADDED', 'MSTYLE', 'SEMANTICS', 'MACTION'].indexOf(
[
MMLTAGS.MATH,
MMLTAGS.MROW,
MMLTAGS.MPADDED,
MMLTAGS.MSTYLE,
MMLTAGS.SEMANTICS,
MMLTAGS.MACTION
].indexOf(
tag
) !== -1
) {
Expand Down
24 changes: 12 additions & 12 deletions ts/semantic_tree/semantic_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ export default class SemanticProcessor {
};

private static readonly MML_TO_LIMIT_: { [key: string]: BoundsType } = {
MSUB: { type: SemanticType.LIMLOWER, length: 1 },
MUNDER: { type: SemanticType.LIMLOWER, length: 1 },
MSUP: { type: SemanticType.LIMUPPER, length: 1 },
MOVER: { type: SemanticType.LIMUPPER, length: 1 },
MSUBSUP: { type: SemanticType.LIMBOTH, length: 2 },
MUNDEROVER: { type: SemanticType.LIMBOTH, length: 2 }
[MMLTAGS.MSUB]: { type: SemanticType.LIMLOWER, length: 1 },
[MMLTAGS.MUNDER]: { type: SemanticType.LIMLOWER, length: 1 },
[MMLTAGS.MSUP]: { type: SemanticType.LIMUPPER, length: 1 },
[MMLTAGS.MOVER]: { type: SemanticType.LIMUPPER, length: 1 },
[MMLTAGS.MSUBSUP]: { type: SemanticType.LIMBOTH, length: 2 },
[MMLTAGS.MUNDEROVER]: { type: SemanticType.LIMBOTH, length: 2 }
};

/**
Expand All @@ -66,12 +66,12 @@ export default class SemanticProcessor {
*
*/
private static readonly MML_TO_BOUNDS_: { [key: string]: BoundsType } = {
MSUB: { type: SemanticType.SUBSCRIPT, length: 1, accent: false },
MSUP: { type: SemanticType.SUPERSCRIPT, length: 1, accent: false },
MSUBSUP: { type: SemanticType.SUBSCRIPT, length: 2, accent: false },
MUNDER: { type: SemanticType.UNDERSCORE, length: 1, accent: true },
MOVER: { type: SemanticType.OVERSCORE, length: 1, accent: true },
MUNDEROVER: { type: SemanticType.UNDERSCORE, length: 2, accent: true }
[MMLTAGS.MSUB]: { type: SemanticType.SUBSCRIPT, length: 1, accent: false },
[MMLTAGS.MSUP]: { type: SemanticType.SUPERSCRIPT, length: 1, accent: false },
[MMLTAGS.MSUBSUP]: { type: SemanticType.SUBSCRIPT, length: 2, accent: false },
[MMLTAGS.MUNDER]: { type: SemanticType.UNDERSCORE, length: 1, accent: true },
[MMLTAGS.MOVER]: { type: SemanticType.OVERSCORE, length: 1, accent: true },
[MMLTAGS.MUNDEROVER]: { type: SemanticType.UNDERSCORE, length: 2, accent: true }
};

private static readonly CLASSIFY_FUNCTION_: { [key: string]: string } = {
Expand Down

0 comments on commit 15b1ee5

Please sign in to comment.