-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumMethods.ts
278 lines (234 loc) · 8.17 KB
/
numMethods.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { MmlNode } from "mathjax-full/js/core/MmlTree/MmlNode";
import TexError from "mathjax-full/js/input/tex/TexError";
import TexParser from "mathjax-full/js/input/tex/TexParser";
import { displayOutputMml } from "./numDisplayMethods";
import { postProcessNumber } from "./numPostProcessMethods";
import { findOptions, IOptions } from "./options/options";
import { INumOptions, INumParseOptions } from "./options/numberOptions";
import { siunitxError } from "./error/errors";
export interface INumberPiece {
prefix: string;
sign: string;
whole: string;
decimal: string;
fractional: string;
exponentMarker: string;
exponentSign: string;
exponent: string;
uncertainty: Array<IUncertainty>;
}
export interface IUncertainty extends INumberPiece {
type: 'bracket' | 'pm'
completed: boolean; // mostly for uncertainty
}
export declare type CharNumFunction = (text: string, numPiece: INumberPiece) => void;
export const NumberPieceDefault: INumberPiece = {
prefix: '',
sign: '',
whole: '',
decimal: '',
fractional: '',
exponentMarker: '',
exponentSign: '',
exponent: '',
uncertainty: [] // this is temporary
};
const UncertaintyDefault: IUncertainty = {
...NumberPieceDefault,
type: 'pm',
completed: false
};
// Can't splat default otherwise array reference gets copied. Need to construct it freshly.
export function generateNumberPiece(): INumberPiece {
const piece = { ...NumberPieceDefault };
piece.uncertainty = new Array<IUncertainty>();
return piece;
}
export function pieceToNumber(piece: INumberPiece): number {
let build = piece.sign + piece.whole;
if (piece.fractional) {
build += '.' + piece.fractional;
}
if (piece.exponent) {
build += 'e' + piece.exponentSign + piece.exponent;
}
try {
let result = Number.parseFloat(build);
if (Number.isNaN(result)) {
result = 0;
}
return result;
} catch {
return 0;
}
}
// INumberPiece is built from left to right, so we're always working on the latest part... which could be uncertainty. So get the last piece.
function getLastNumPiece(numPiece: INumberPiece):INumberPiece{
if (numPiece.uncertainty.length > 0) {
return numPiece.uncertainty[numPiece.uncertainty.length - 1];
} else {
return numPiece;
}
}
function parseDigits(text: string, numPiece: INumberPiece) {
const num = getLastNumPiece(numPiece);
if (num.exponentMarker) {
num.exponent += text;
} else if (num.decimal) {
num.fractional += text;
} else {
num.whole += text;
}
}
function parseDecimals(text: string, numPiece: INumberPiece) {
const num = getLastNumPiece(numPiece);
num.decimal += text;
}
function parseComparators(text: string, numPiece: INumberPiece) {
const num = getLastNumPiece(numPiece);
if (num.prefix){
throw siunitxError.ComparatorAlreadySet(num.prefix, text);
}
num.prefix += text;
}
function parseExponentMarkers(text: string, numPiece: INumberPiece) {
//let numPiece: INumberPiece;
// if (numPiece.uncertainty.length > 0){
// num = numPiece.uncertainty[numPiece.uncertainty.length-1];
// } else {
// num = numPiece;
// }
numPiece.exponentMarker += text;
}
function parseSigns(text: string, numPiece: INumberPiece) {
const num = getLastNumPiece(numPiece);
if (num.exponentMarker) {
num.exponentSign += text;
} else {
num.sign += text;
}
}
function parseOpenUncertainty(text: string, numPiece: INumberPiece) {
const uncertainty: IUncertainty = { ...UncertaintyDefault, type: 'bracket' };
numPiece.uncertainty.push(uncertainty);
}
function parseCloseUncertainty(text: string, numPiece: INumberPiece) {
if (numPiece.uncertainty.length === 0) {
throw new TexError('50', 'Trying to close an uncertainty that doesn\'t exist.');
}
const uncertainty = numPiece.uncertainty[numPiece.uncertainty.length - 1];
if (uncertainty.completed) {
throw new TexError('51', 'Uncertainty was already closed.');
}
uncertainty.completed = true;
}
function parseUncertaintySigns(text: string, numPiece: INumberPiece) {
const uncertainty: IUncertainty = { ...UncertaintyDefault, type: 'pm' };
numPiece.uncertainty.push(uncertainty);
}
function parseIgnore() {
// do nothing
}
// using two types for output. Ex. \\pm is used both as sign and as an uncertainty. Need map of map for this one.
export function generateNumberMapping(options: INumParseOptions): Map<string, CharNumFunction> {
const parseMap = new Map<string, CharNumFunction>();
const matchMacrosOrChar = /\\(?:[a-zA-Z]+|[\uD800-\uDBFF].|.)|[\uD800-\uDBFF].|[^\s\\]/g;
for (const [key, method] of [
['input-comparators', parseComparators],
['input-signs', parseSigns],
['input-digits', parseDigits],
['input-decimal-markers', parseDecimals],
['input-open-uncertainty', parseOpenUncertainty],
['input-close-uncertainty', parseCloseUncertainty],
['input-uncertainty-signs', parseUncertaintySigns],
['input-exponent-markers', parseExponentMarkers],
['input-ignore', parseIgnore]
] as [string, CharNumFunction][]) {
const option = options[key];
if (option.match(/(?:^|[^\\])(?:\\\\)*\\$/)) {
throw siunitxError.BadOptionChars(key);
}
(option.match(matchMacrosOrChar) || []).forEach((c: string) => {
if (parseMap.has(c) && key === 'input-uncertainty-signs') {
const inputSigns = parseMap.get(c) as CharNumFunction;
const altMethod: CharNumFunction = function (macro, num) {
(num.whole === '' && num.decimal === '' ? inputSigns : parseUncertaintySigns)(macro, num);
}
parseMap.set(c, altMethod);
} else {
parseMap.set(c, method);
}
});
}
return parseMap;
}
export function parseNumber(parser: TexParser, text: string, options: INumOptions): INumberPiece {
const mapping = generateNumberMapping(options);
text = text.replace('<<', '\\ll')
.replace('>>', '\\gg')
.replace('<=', '\\le')
.replace('>=', '\\ge')
.replace('+-', '\\pm');
const num: INumberPiece = generateNumberPiece();
const subParser = new TexParser(text, parser.stack.env, parser.configuration);
subParser.i = 0;
// process character
// if '\', then read until next '\' or whitespace char
let token;
while (subParser.i < subParser.string.length) {
token = subParser.GetNext();
subParser.i++; // GetNext() does not advance position unless skipping whitespace
if (token === '\\') {
token += subParser.GetCS();
}
try {
mapping.get(token)(token, num);
} catch {
throw siunitxError.InvalidNumArgument(subParser.string);
}
}
if (!options["retain-explicit-decimal-marker"] && num.decimal && !num.fractional) {
num.decimal = '';
}
if (!options["retain-explicit-plus"] && num.sign === '+') {
num.sign = '';
}
// adding exponent to value check here. Without it, exponentials without a base won't stay negative. (-e10)
const value = +(num.whole + (num.decimal ? '.' : '') + num.fractional + (num.exponent === '' ? '' : 'e' + num.exponentSign + num.exponent));
if (value === 0 && !options["retain-negative-zero"] && num.sign === '-') {
num.sign = '';
}
if (!options["retain-zero-uncertainty"]) {
for (let i = num.uncertainty.length - 1; i >= 0; i--) {
const uncertaintyValue = +(num.uncertainty[i].whole + (num.uncertainty[i].decimal ? '.' : '') + num.uncertainty[i].fractional);
if (uncertaintyValue === 0) {
num.uncertainty.splice(i, 1);
}
}
}
return num;
}
export function processNumber(parser: TexParser): MmlNode {
const globalOptions: IOptions = { ...parser.options.siunitx as IOptions };
const localOptions = findOptions(parser, globalOptions);
Object.assign(globalOptions, localOptions);
let text = parser.GetArgument('num');
if (globalOptions["parse-numbers"]) {
// going to assume evaluate expression is processed first, THEN the result is parsed normally
if (globalOptions["evaluate-expression"]) {
// TODO Sanitize Evaluate Expression!
let expression = globalOptions.expression
expression = expression.replace('#1', text);
text = eval(expression).toString();
}
const num = parseNumber(parser, text, globalOptions);
postProcessNumber(parser, num, globalOptions);
//const displayResult = displayOutput(num, globalOptions);
const mmlNode = displayOutputMml(num, parser, globalOptions);
//const mml = (new TexParser(displayResult, parser.stack.env, parser.configuration)).mml();
return mmlNode;
} else {
const mml = (new TexParser(text, parser.stack.env, parser.configuration)).mml();
return mml;
}
}