Skip to content

Commit

Permalink
Flatten "size" ParseNode and also simplify its uses. (#1554)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcianx authored and ylemkimon committed Aug 6, 2018
1 parent 5b6ffd7 commit f0b9a34
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 42 deletions.
7 changes: 2 additions & 5 deletions src/Parser.js
Expand Up @@ -793,11 +793,8 @@ export default class Parser {
return newArgument({
type: "size",
mode: this.mode,
value: {
type: "size",
value: data,
isBlank: isBlank,
},
value: data,
isBlank,
}, res);
}

Expand Down
7 changes: 4 additions & 3 deletions src/environments/array.js
Expand Up @@ -16,6 +16,7 @@ import type Parser from "../Parser";
import type {ParseNode, AnyParseNode} from "../parseNode";
import type {StyleStr} from "../types";
import type {HtmlBuilder, MathMLBuilder} from "../defineFunction";
import type {Measurement} from "../units";

// Data stored in the ParseNode associated with the environment.
type AlignSpec = { type: "separator", separator: string } | {
Expand All @@ -32,7 +33,7 @@ export type ArrayEnvNodeData = {|
cols?: AlignSpec[],
arraystretch: number,
body: AnyParseNode[][], // List of rows in the (2D) array.
rowGaps: (?ParseNode<"size">)[],
rowGaps: (?Measurement)[],
hLinesBeforeRow: Array<boolean[]>,
|};
// Same as above but with some fields not yet filled.
Expand All @@ -44,7 +45,7 @@ type ArrayEnvNodeDataIncomplete = {|
// Before these fields are filled.
arraystretch?: number,
body?: AnyParseNode[][],
rowGaps?: (?ParseNode<"size">)[],
rowGaps?: (?Measurement)[],
hLinesBeforeRow?: Array<boolean[]>,
|};

Expand Down Expand Up @@ -241,7 +242,7 @@ const htmlBuilder: HtmlBuilder<"array"> = function(group, options) {
const rowGap = group.value.rowGaps[r];
let gap = 0;
if (rowGap) {
gap = calculateSize(rowGap.value.value, options);
gap = calculateSize(rowGap, options);
if (gap > 0) { // \@argarraycr
gap += arstrutDepth;
if (depth < gap) {
Expand Down
12 changes: 6 additions & 6 deletions src/functions/cr.js
Expand Up @@ -23,7 +23,7 @@ defineFunction({
allowedInText: true,
},

handler: ({parser, funcName}, args, optArgs) => {
handler({parser, funcName}, args, optArgs) {
const size = optArgs[0];
const newRow = (funcName === "\\cr");
let newLine = false;
Expand All @@ -42,14 +42,14 @@ defineFunction({
mode: parser.mode,
newLine,
newRow,
size: size && assertNodeType(size, "size"),
size: size && assertNodeType(size, "size").value,
};
},

// The following builders are called only at the top level,
// not within tabular/array environments.

htmlBuilder: (group, options) => {
htmlBuilder(group, options) {
if (group.newRow) {
throw new ParseError(
"\\cr valid only within a tabular/array environment");
Expand All @@ -59,19 +59,19 @@ defineFunction({
span.classes.push("newline");
if (group.size) {
span.style.marginTop =
calculateSize(group.size.value.value, options) + "em";
calculateSize(group.size, options) + "em";
}
}
return span;
},

mathmlBuilder: (group, options) => {
mathmlBuilder(group, options) {
const node = new mathMLTree.MathNode("mspace");
if (group.newLine) {
node.setAttribute("linebreak", "newline");
if (group.size) {
node.setAttribute("height",
calculateSize(group.size.value.value, options) + "em");
calculateSize(group.size, options) + "em");
}
}
return node;
Expand Down
14 changes: 6 additions & 8 deletions src/functions/genfrac.js
Expand Up @@ -5,6 +5,7 @@ import delimiter from "../delimiter";
import mathMLTree from "../mathMLTree";
import Style from "../Style";
import {assertNodeType, assertAtomFamily, checkNodeType} from "../parseNode";
import {assert} from "../utils";

import * as html from "../buildHTML";
import * as mml from "../buildMathML";
Expand Down Expand Up @@ -357,7 +358,7 @@ defineFunction({
greediness: 6,
argTypes: ["math", "math", "size", "text", "math", "math"],
},
handler: ({parser}, args) => {
handler({parser}, args) {
const numer = args[4];
const denom = args[5];

Expand All @@ -381,13 +382,13 @@ defineFunction({
const barNode = assertNodeType(args[2], "size");
let hasBarLine;
let barSize = null;
if (barNode.value.isBlank) {
if (barNode.isBlank) {
// \genfrac acts differently than \above.
// \genfrac treats an empty size group as a signal to use a
// standard bar size. \above would see size = 0 and omit the bar.
hasBarLine = true;
} else {
barSize = barNode.value.value;
barSize = barNode.value;
hasBarLine = barSize.number > 0;
}

Expand Down Expand Up @@ -432,12 +433,11 @@ defineFunction({
infix: true,
},
handler({parser, funcName, token}, args) {
const sizeNode = assertNodeType(args[0], "size");
return {
type: "infix",
mode: parser.mode,
replaceWith: "\\\\abovefrac",
sizeNode,
size: assertNodeType(args[0], "size").value,
token,
};
},
Expand All @@ -452,11 +452,9 @@ defineFunction({
},
handler: ({parser, funcName}, args) => {
const numer = args[0];
const infixNode = assertNodeType(args[1], "infix");
const sizeNode = assertNodeType(infixNode.sizeNode, "size");
const barSize = assert(assertNodeType(args[1], "infix").size);
const denom = args[2];

const barSize = sizeNode.value.value;
const hasBarLine = barSize.number > 0;
return {
type: "genfrac",
Expand Down
12 changes: 6 additions & 6 deletions src/functions/kern.js
Expand Up @@ -17,16 +17,16 @@ defineFunction({
argTypes: ["size"],
allowedInText: true,
},
handler: ({parser, funcName}, args) => {
handler({parser, funcName}, args) {
const size = assertNodeType(args[0], "size");
if (parser.settings.strict) {
const mathFunction = (funcName[1] === 'm'); // \mkern, \mskip
const muUnit = (size.value.value.unit === 'mu');
const muUnit = (size.value.unit === 'mu');
if (mathFunction) {
if (!muUnit) {
parser.settings.reportNonstrict("mathVsTextUnits",
`LaTeX's ${funcName} supports only mu units, ` +
`not ${size.value.value.unit} units`);
`not ${size.value.unit} units`);
}
if (parser.mode !== "math") {
parser.settings.reportNonstrict("mathVsTextUnits",
Expand All @@ -42,13 +42,13 @@ defineFunction({
return {
type: "kern",
mode: parser.mode,
dimension: size.value.value,
dimension: size.value,
};
},
htmlBuilder: (group, options) => {
htmlBuilder(group, options) {
return buildCommon.makeGlue(group.dimension, options);
},
mathmlBuilder: (group, options) => {
mathmlBuilder(group, options) {
const dimension = calculateSize(group.dimension, options);
return new mathMLTree.SpaceNode(dimension);
},
Expand Down
6 changes: 3 additions & 3 deletions src/functions/raisebox.js
Expand Up @@ -18,7 +18,7 @@ defineFunction({
allowedInText: true,
},
handler({parser}, args) {
const amount = assertNodeType(args[0], "size");
const amount = assertNodeType(args[0], "size").value;
const body = args[1];
return {
type: "raisebox",
Expand All @@ -44,7 +44,7 @@ defineFunction({
size: 6, // simulate \normalsize
};
const body = sizing.htmlBuilder(sizedText, options);
const dy = calculateSize(group.dy.value.value, options);
const dy = calculateSize(group.dy, options);
return buildCommon.makeVList({
positionType: "shift",
positionData: -dy,
Expand All @@ -54,7 +54,7 @@ defineFunction({
mathmlBuilder(group, options) {
const node = new mathMLTree.MathNode(
"mpadded", [mml.buildGroup(group.body, options)]);
const dy = group.dy.value.value.number + group.dy.value.value.unit;
const dy = group.dy.number + group.dy.unit;
node.setAttribute("voffset", dy);
return node;
},
Expand Down
6 changes: 3 additions & 3 deletions src/functions/rule.js
Expand Up @@ -20,9 +20,9 @@ defineFunction({
return {
type: "rule",
mode: parser.mode,
shift: shift && assertNodeType(shift, "size").value.value,
width: width.value.value,
height: height.value.value,
shift: shift && assertNodeType(shift, "size").value,
width: width.value,
height: height.value,
};
},
htmlBuilder(group, options) {
Expand Down
13 changes: 5 additions & 8 deletions src/parseNode.js
Expand Up @@ -78,11 +78,8 @@ type ParseNodeTypes = {
type: "size",
mode: Mode,
loc?: ?SourceLocation,
value: {|
type: "size",
value: Measurement,
isBlank: boolean,
|},
value: Measurement,
isBlank: boolean,
|},
"styling": {|
type: "styling",
Expand Down Expand Up @@ -199,7 +196,7 @@ type ParseNodeTypes = {
loc?: ?SourceLocation,
newRow: boolean,
newLine: boolean,
size: ?ParseNode<"size">,
size: ?Measurement,
|},
"delimsizing": {|
type: "delimsizing",
Expand Down Expand Up @@ -272,7 +269,7 @@ type ParseNodeTypes = {
mode: Mode,
loc?: ?SourceLocation,
replaceWith: string,
sizeNode?: ParseNode<"size">,
size?: Measurement,
token: ?Token,
|},
"kern": {|
Expand Down Expand Up @@ -358,7 +355,7 @@ type ParseNodeTypes = {
type: "raisebox",
mode: Mode,
loc?: ?SourceLocation,
dy: ParseNode<"size">,
dy: Measurement,
body: AnyParseNode,
|},
"rule": {|
Expand Down

0 comments on commit f0b9a34

Please sign in to comment.