latex-utensils
Version:
A LaTeX parser, a BibTeX parser, and utilities
1,583 lines (1,483 loc) • 246 kB
JavaScript
// Generated by Peggy 3.0.2.
//
// https://peggyjs.org/
"use strict";
function peg$subclass(child, parent) {
function C() { this.constructor = child; }
C.prototype = parent.prototype;
child.prototype = new C();
}
function peg$SyntaxError(message, expected, found, location) {
var self = Error.call(this, message);
// istanbul ignore next Check is a necessary evil to support older environments
if (Object.setPrototypeOf) {
Object.setPrototypeOf(self, peg$SyntaxError.prototype);
}
self.expected = expected;
self.found = found;
self.location = location;
self.name = "SyntaxError";
return self;
}
peg$subclass(peg$SyntaxError, Error);
function peg$padEnd(str, targetLength, padString) {
padString = padString || " ";
if (str.length > targetLength) { return str; }
targetLength -= str.length;
padString += padString.repeat(targetLength);
return str + padString.slice(0, targetLength);
}
peg$SyntaxError.prototype.format = function(sources) {
var str = "Error: " + this.message;
if (this.location) {
var src = null;
var k;
for (k = 0; k < sources.length; k++) {
if (sources[k].source === this.location.source) {
src = sources[k].text.split(/\r\n|\n|\r/g);
break;
}
}
var s = this.location.start;
var offset_s = (this.location.source && (typeof this.location.source.offset === "function"))
? this.location.source.offset(s)
: s;
var loc = this.location.source + ":" + offset_s.line + ":" + offset_s.column;
if (src) {
var e = this.location.end;
var filler = peg$padEnd("", offset_s.line.toString().length, ' ');
var line = src[s.line - 1];
var last = s.line === e.line ? e.column : line.length + 1;
var hatLen = (last - s.column) || 1;
str += "\n --> " + loc + "\n"
+ filler + " |\n"
+ offset_s.line + " | " + line + "\n"
+ filler + " | " + peg$padEnd("", s.column - 1, ' ')
+ peg$padEnd("", hatLen, "^");
} else {
str += "\n at " + loc;
}
}
return str;
};
peg$SyntaxError.buildMessage = function(expected, found) {
var DESCRIBE_EXPECTATION_FNS = {
literal: function(expectation) {
return "\"" + literalEscape(expectation.text) + "\"";
},
class: function(expectation) {
var escapedParts = expectation.parts.map(function(part) {
return Array.isArray(part)
? classEscape(part[0]) + "-" + classEscape(part[1])
: classEscape(part);
});
return "[" + (expectation.inverted ? "^" : "") + escapedParts.join("") + "]";
},
any: function() {
return "any character";
},
end: function() {
return "end of input";
},
other: function(expectation) {
return expectation.description;
}
};
function hex(ch) {
return ch.charCodeAt(0).toString(16).toUpperCase();
}
function literalEscape(s) {
return s
.replace(/\\/g, "\\\\")
.replace(/"/g, "\\\"")
.replace(/\0/g, "\\0")
.replace(/\t/g, "\\t")
.replace(/\n/g, "\\n")
.replace(/\r/g, "\\r")
.replace(/[\x00-\x0F]/g, function(ch) { return "\\x0" + hex(ch); })
.replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x" + hex(ch); });
}
function classEscape(s) {
return s
.replace(/\\/g, "\\\\")
.replace(/\]/g, "\\]")
.replace(/\^/g, "\\^")
.replace(/-/g, "\\-")
.replace(/\0/g, "\\0")
.replace(/\t/g, "\\t")
.replace(/\n/g, "\\n")
.replace(/\r/g, "\\r")
.replace(/[\x00-\x0F]/g, function(ch) { return "\\x0" + hex(ch); })
.replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x" + hex(ch); });
}
function describeExpectation(expectation) {
return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);
}
function describeExpected(expected) {
var descriptions = expected.map(describeExpectation);
var i, j;
descriptions.sort();
if (descriptions.length > 0) {
for (i = 1, j = 1; i < descriptions.length; i++) {
if (descriptions[i - 1] !== descriptions[i]) {
descriptions[j] = descriptions[i];
j++;
}
}
descriptions.length = j;
}
switch (descriptions.length) {
case 1:
return descriptions[0];
case 2:
return descriptions[0] + " or " + descriptions[1];
default:
return descriptions.slice(0, -1).join(", ")
+ ", or "
+ descriptions[descriptions.length - 1];
}
}
function describeFound(found) {
return found ? "\"" + literalEscape(found) + "\"" : "end of input";
}
return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found.";
};
function peg$parse(input, options) {
options = options !== undefined ? options : {};
var peg$FAILED = {};
var peg$source = options.grammarSource;
var peg$startRuleFunctions = { Root: peg$parseRoot, Preamble: peg$parsePreamble };
var peg$startRuleFunction = peg$parseRoot;
var peg$c0 = "begin{document}";
var peg$c1 = "%";
var peg$c2 = ".";
var peg$c3 = "label";
var peg$c4 = "ref";
var peg$c5 = "eqref";
var peg$c6 = "autoref";
var peg$c7 = "}";
var peg$c8 = "{";
var peg$c9 = "url";
var peg$c10 = "href";
var peg$c11 = "verb*";
var peg$c12 = "verb";
var peg$c13 = "verbatim";
var peg$c14 = "verbatim*";
var peg$c15 = "minted";
var peg$c16 = "lstlisting";
var peg$c17 = "comment";
var peg$c18 = "@";
var peg$c19 = "*";
var peg$c20 = "\\*";
var peg$c21 = "end";
var peg$c22 = "[";
var peg$c23 = "]";
var peg$c24 = "(";
var peg$c25 = ")";
var peg$c26 = "def";
var peg$c27 = "left";
var peg$c28 = "right";
var peg$c29 = "\\{";
var peg$c30 = "\\}";
var peg$c31 = "bigg";
var peg$c32 = "Bigg";
var peg$c33 = "big";
var peg$c34 = "Big";
var peg$c35 = ",";
var peg$c36 = "text";
var peg$c37 = "begin";
var peg$c38 = "equation*";
var peg$c39 = "equation";
var peg$c40 = "align*";
var peg$c41 = "align";
var peg$c42 = "alignat*";
var peg$c43 = "alignat";
var peg$c44 = "gather*";
var peg$c45 = "gather";
var peg$c46 = "multline*";
var peg$c47 = "multline";
var peg$c48 = "flalign*";
var peg$c49 = "flalign";
var peg$c50 = "math";
var peg$c51 = "displaymath";
var peg$c52 = "alignedat";
var peg$c53 = "aligned";
var peg$c54 = "cases*";
var peg$c55 = "cases";
var peg$c56 = "gathered";
var peg$c57 = "split";
var peg$c58 = "matrix";
var peg$c59 = "bmatrix";
var peg$c60 = "pmatrix";
var peg$c61 = "vmatrix";
var peg$c62 = "Bmatrix";
var peg$c63 = "Vmatrix";
var peg$c64 = "\\";
var peg$c65 = "$";
var peg$c66 = "&";
var peg$c67 = "#";
var peg$c68 = "^";
var peg$c69 = "_";
var peg$c70 = "\0";
var peg$c71 = "~";
var peg$c72 = "document";
var peg$c73 = "linebreak";
var peg$c74 = "newline";
var peg$c75 = "\n";
var peg$c76 = "\r\n";
var peg$c77 = "\r";
var peg$c78 = "\u2028";
var peg$c79 = "\u2029";
var peg$c80 = "par";
var peg$r0 = /^[ ]/;
var peg$r1 = /^[^\\%{}$&~\r\n\u2028\u2029#\^_\0 \t[\]]/;
var peg$r2 = /^[\\%{}$&~\r\n\u2028\u2029#\^_\0 \t[\]]/;
var peg$r3 = /^[^a-zA-Z]/;
var peg$r4 = /^[ $%#&{}_\-,\/@]/;
var peg$r5 = /^[()[\]|\/.]/;
var peg$r6 = /^[{}]/;
var peg$r7 = /^[rlm]/;
var peg$r8 = /^[a-zA-Z]/;
var peg$r9 = /^[0-9]/;
var peg$r10 = /^[.,;:\-*\/()!?=+<>[\]]/;
var peg$r11 = /^[ \t]/;
var peg$r12 = /^[^ \r\n\t%\u2028\u2029]/;
var peg$e0 = peg$literalExpectation("begin{document}", false);
var peg$e1 = peg$anyExpectation();
var peg$e2 = peg$classExpectation([" "], false, false);
var peg$e3 = peg$classExpectation(["\\", "%", "{", "}", "$", "&", "~", "\r", "\n", "\u2028", "\u2029", "#", "^", "_", "\0", " ", "\t", "[", "]"], true, false);
var peg$e4 = peg$classExpectation(["\\", "%", "{", "}", "$", "&", "~", "\r", "\n", "\u2028", "\u2029", "#", "^", "_", "\0", " ", "\t", "[", "]"], false, false);
var peg$e5 = peg$literalExpectation("%", false);
var peg$e6 = peg$literalExpectation(".", false);
var peg$e7 = peg$otherExpectation("special command");
var peg$e8 = peg$literalExpectation("label", false);
var peg$e9 = peg$literalExpectation("ref", false);
var peg$e10 = peg$literalExpectation("eqref", false);
var peg$e11 = peg$literalExpectation("autoref", false);
var peg$e12 = peg$literalExpectation("}", false);
var peg$e13 = peg$literalExpectation("{", false);
var peg$e14 = peg$literalExpectation("url", false);
var peg$e15 = peg$literalExpectation("href", false);
var peg$e16 = peg$literalExpectation("verb*", false);
var peg$e17 = peg$literalExpectation("verb", false);
var peg$e18 = peg$classExpectation([["a", "z"], ["A", "Z"]], true, false);
var peg$e19 = peg$literalExpectation("verbatim", false);
var peg$e20 = peg$literalExpectation("verbatim*", false);
var peg$e21 = peg$literalExpectation("minted", false);
var peg$e22 = peg$literalExpectation("lstlisting", false);
var peg$e23 = peg$literalExpectation("comment", false);
var peg$e24 = peg$literalExpectation("@", false);
var peg$e25 = peg$literalExpectation("*", false);
var peg$e26 = peg$literalExpectation("\\*", false);
var peg$e27 = peg$classExpectation([" ", "$", "%", "#", "&", "{", "}", "_", "-", ",", "/", "@"], false, false);
var peg$e28 = peg$literalExpectation("end", false);
var peg$e29 = peg$literalExpectation("[", false);
var peg$e30 = peg$literalExpectation("]", false);
var peg$e31 = peg$literalExpectation("(", false);
var peg$e32 = peg$literalExpectation(")", false);
var peg$e33 = peg$literalExpectation("def", false);
var peg$e34 = peg$literalExpectation("left", false);
var peg$e35 = peg$literalExpectation("right", false);
var peg$e36 = peg$classExpectation(["(", ")", "[", "]", "|", "/", "."], false, false);
var peg$e37 = peg$classExpectation(["{", "}"], false, false);
var peg$e38 = peg$literalExpectation("\\{", false);
var peg$e39 = peg$literalExpectation("\\}", false);
var peg$e40 = peg$literalExpectation("bigg", false);
var peg$e41 = peg$literalExpectation("Bigg", false);
var peg$e42 = peg$literalExpectation("big", false);
var peg$e43 = peg$literalExpectation("Big", false);
var peg$e44 = peg$classExpectation(["r", "l", "m"], false, false);
var peg$e45 = peg$literalExpectation(",", false);
var peg$e46 = peg$literalExpectation("text", false);
var peg$e47 = peg$literalExpectation("begin", false);
var peg$e48 = peg$literalExpectation("equation*", false);
var peg$e49 = peg$literalExpectation("equation", false);
var peg$e50 = peg$literalExpectation("align*", false);
var peg$e51 = peg$literalExpectation("align", false);
var peg$e52 = peg$literalExpectation("alignat*", false);
var peg$e53 = peg$literalExpectation("alignat", false);
var peg$e54 = peg$literalExpectation("gather*", false);
var peg$e55 = peg$literalExpectation("gather", false);
var peg$e56 = peg$literalExpectation("multline*", false);
var peg$e57 = peg$literalExpectation("multline", false);
var peg$e58 = peg$literalExpectation("flalign*", false);
var peg$e59 = peg$literalExpectation("flalign", false);
var peg$e60 = peg$literalExpectation("math", false);
var peg$e61 = peg$literalExpectation("displaymath", false);
var peg$e62 = peg$literalExpectation("alignedat", false);
var peg$e63 = peg$literalExpectation("aligned", false);
var peg$e64 = peg$literalExpectation("cases*", false);
var peg$e65 = peg$literalExpectation("cases", false);
var peg$e66 = peg$literalExpectation("gathered", false);
var peg$e67 = peg$literalExpectation("split", false);
var peg$e68 = peg$literalExpectation("matrix", false);
var peg$e69 = peg$literalExpectation("bmatrix", false);
var peg$e70 = peg$literalExpectation("pmatrix", false);
var peg$e71 = peg$literalExpectation("vmatrix", false);
var peg$e72 = peg$literalExpectation("Bmatrix", false);
var peg$e73 = peg$literalExpectation("Vmatrix", false);
var peg$e74 = peg$literalExpectation("\\", false);
var peg$e75 = peg$literalExpectation("$", false);
var peg$e76 = peg$literalExpectation("&", false);
var peg$e77 = peg$literalExpectation("#", false);
var peg$e78 = peg$literalExpectation("^", false);
var peg$e79 = peg$literalExpectation("_", false);
var peg$e80 = peg$literalExpectation("\0", false);
var peg$e81 = peg$classExpectation([["a", "z"], ["A", "Z"]], false, false);
var peg$e82 = peg$classExpectation([["0", "9"]], false, false);
var peg$e83 = peg$classExpectation([".", ",", ";", ":", "-", "*", "/", "(", ")", "!", "?", "=", "+", "<", ">", "[", "]"], false, false);
var peg$e84 = peg$literalExpectation("~", false);
var peg$e85 = peg$literalExpectation("document", false);
var peg$e86 = peg$literalExpectation("linebreak", false);
var peg$e87 = peg$literalExpectation("newline", false);
var peg$e88 = peg$literalExpectation("\n", false);
var peg$e89 = peg$literalExpectation("\r\n", false);
var peg$e90 = peg$literalExpectation("\r", false);
var peg$e91 = peg$literalExpectation("\u2028", false);
var peg$e92 = peg$literalExpectation("\u2029", false);
var peg$e93 = peg$classExpectation([" ", "\t"], false, false);
var peg$e94 = peg$otherExpectation("spaces");
var peg$e95 = peg$classExpectation([" ", "\r", "\n", "\t", "%", "\u2028", "\u2029"], true, false);
var peg$e96 = peg$literalExpectation("par", false);
var peg$f0 = function(x) {
const comment = commentMap ? Array.from(commentMap.values()) : undefined;
return { kind: "ast.root", content: x, comment };
};
var peg$f1 = function(e) { return e; };
var peg$f2 = function(x, rest) {
const comment = commentMap ? Array.from(commentMap.values()) : undefined;
return { kind: "ast.preamble", content: x, rest, comment };
};
var peg$f3 = function(x) {
return x;
};
var peg$f4 = function(c) {
timeKeeper && timeKeeper.check();
return { kind: "text.string", content: c, location: location() };
};
var peg$f5 = function(c) {
timeKeeper && timeKeeper.check();
return { kind: "text.string", content: c, location: location() };
};
var peg$f6 = function(c) {
return { kind: "space" };
};
var peg$f7 = function(x) {
return x;
};
var peg$f8 = function(c) {
if (options.enableMathCharacterLocation) {
return { kind: "math.character", content: c, location: location() };
} else {
return { kind: "math.character", content: c };
}
};
var peg$f9 = function(name, label) {
return { kind: "command.label", name, label: label.join(''), location: location() };
};
var peg$f10 = function(x) {
return { kind: "command.url", name: "url", url: x, location: location() };
};
var peg$f11 = function(arg, x, grp) {
return { kind: "command.href", name: "href", url: x, content: grp.content, arg: arg || undefined, location: location() };
};
var peg$f12 = function(name, e, end) {return end === e;};
var peg$f13 = function(name, e, x, end) {return end === e;};
var peg$f14 = function(name, e, x) {
return { kind: "verb", name, escape: e, content: x, location: location() };
};
var peg$f15 = function(x) {
return x;
};
var peg$f16 = function(x) {
return { kind: "env.verbatim", name: "verbatim", content: x, location: location() };
};
var peg$f17 = function(x) {
return { kind: "env.verbatim", name: "verbatim*", content: x, location: location() };
};
var peg$f18 = function(x) {
return x;
};
var peg$f19 = function(args, x) {
return { kind: "env.minted", name: "minted", args: args, content: x, location: location() };
};
var peg$f20 = function(x) {
return x;
};
var peg$f21 = function(arg, x) {
return { kind: "env.lstlisting", name: "lstlisting", arg: arg, content: x, location: location() };
};
var peg$f22 = function(x) {
return { kind: "env.comment", content: x, location: location() };
};
var peg$f23 = function(t) {return t;};
var peg$f24 = function(eq) {
return { kind: "inlineMath", content: eq, location: location() };
};
var peg$f25 = function(t) {return t;};
var peg$f26 = function(eq) {
return { kind: "inlineMath", content: eq, location: location() };
};
var peg$f27 = function(x) {return x;};
var peg$f28 = function(x) {
return { kind: "inlineMath", content: x, location: location() };
};
var peg$f29 = function(x) {return x;};
var peg$f30 = function(x) {
return { kind: "displayMath", content: x, location: location() };
};
var peg$f31 = function(x) {return x;};
var peg$f32 = function(x) {
return { kind: "displayMath", content: x, location: location() };
};
var peg$f33 = function(n, args) {
return { kind: "command", name: n, args: args, location: location() };
};
var peg$f34 = function(x) {
return x;
};
var peg$f35 = function(n) {
return { kind: "command", name: n, args: [], location: location() };
};
var peg$f36 = function(n) { return n + '*'; };
var peg$f37 = function(c) {
return { kind: "command", name: c, args: [], location: location() };
};
var peg$f38 = function(n, args) {
return { kind: "command", name: n, args: args, location: location() };
};
var peg$f39 = function(token, numArgs, grArg) {
return { kind: "command.def", token, args: numArgs.concat([grArg]), location: location() };
};
var peg$f40 = function(x) {
return x;
};
var peg$f41 = function(c) {return c;};
var peg$f42 = function(x) {
return { kind: "arg.group", content: x, location: location() };
};
var peg$f43 = function(x) {
return x;
};
var peg$f44 = function(c) {return c;};
var peg$f45 = function(x) {
return { kind: "arg.group", content: x, location: location() };
};
var peg$f46 = function(x) {
return x;
};
var peg$f47 = function(l, c) {return c;};
var peg$f48 = function(l, x, r) {
return { kind: "math.matching_delimiters", left: l, right: r, content: x, location: location() };
};
var peg$f49 = function(x) {
return x;
};
var peg$f50 = function(l, c) {return c;};
var peg$f51 = function(l, x, r) {
return { kind: "math.math_delimiters", lcommand: l, rcommand: r, left: "(", right: ")", content: x, location: location() };
};
var peg$f52 = function(l, c) {return c;};
var peg$f53 = function(l, x, r) {
return { kind: "math.math_delimiters", lcommand: l, rcommand: r, left: "[", right: "]", content: x, location: location() };
};
var peg$f54 = function(l, c) {return c;};
var peg$f55 = function(l, x, r) {
return { kind: "math.math_delimiters", lcommand: l, rcommand: r, left: "\\{", right: "\\}", content: x, location: location() };
};
var peg$f56 = function(x) {
return x;
};
var peg$f57 = function(x) {return x;};
var peg$f58 = function(body) {
return { kind: "arg.optional", content: body, location: location() };
};
var peg$f59 = function() { return { kind:"parbreak", location: location() }; };
var peg$f60 = function(c) { return { kind: "text.string", content: c, location: location() }; };
var peg$f61 = function() {
return { kind: "text.string", content: ",", location: location() };
};
var peg$f62 = function(x) {
return x;
};
var peg$f63 = function(name, args, x) {return x;};
var peg$f64 = function(name, args, body, n) { return name === n; };
var peg$f65 = function(name, args, body, n) {
return { kind: "env", name, args, content: body, location: location() };
};
var peg$f66 = function(x) {
return x;
};
var peg$f67 = function(name, x) {return x;};
var peg$f68 = function(name, body, n) { return name === n; };
var peg$f69 = function(name, body, n) {
return { kind: "env.math.align", name, args: [], content: body, location: location() };
};
var peg$f70 = function(name, x) {return x;};
var peg$f71 = function(name, body, n) { return name === n; };
var peg$f72 = function(name, body, n) {
return { kind: "env.math.aligned", name, args: [], content: body, location: location() };
};
var peg$f73 = function(x) {
return x;
};
var peg$f74 = function(arg) {
return { kind: "command.text", arg: arg, location: location() };
};
var peg$f75 = function(x) { return { kind: "comment", content: x, location: location() }; };
var peg$f76 = function(x) { return { kind: "comment", content: x, location: location() }; };
var peg$f77 = function() {
return { kind: "alignmentTab", location: location() };
};
var peg$f78 = function(x) {
return { kind: "superscript", arg: x, location: location() };
};
var peg$f79 = function() {
return { kind: "superscript", arg: undefined, location: location() };
};
var peg$f80 = function(x) {
return { kind: "subscript", arg: x, location: location() };
};
var peg$f81 = function() {
return { kind: "subscript", arg: undefined, location: location() };
};
var peg$f82 = function() {
return { kind: "ignore", location: location() };
};
var peg$f83 = function() {
return { kind: "activeCharacter", location: location() };
};
var peg$f84 = function(n) {
return { kind: "commandParameter", nargs: n, location: location() };
};
var peg$f85 = function(c) {
return c;
};
var peg$f86 = function(x) {
return x;
};
var peg$f87 = function(n, arg) {
return { kind: "linebreak", name: n, arg: arg || undefined, location: location() };
};
var peg$f88 = function(n, arg) {
return { kind: "linebreak", name: n, arg: arg || undefined, location: location() };
};
var peg$f89 = function(n) {
return { kind: "linebreak", name: n, arg: undefined, location: location() }
};
var peg$f90 = function() {
return { kind: "softbreak" };
};
var peg$f91 = function() {
return { kind: "space" };
};
var peg$f92 = function(c) {
if (options.enableComment) {
const loc = location();
const locJson = JSON.stringify(loc);
if (!commentMap.has(locJson)) {
commentMap.set(locJson, { kind: "comment", content: c, location: loc } );
}
}
};
var peg$f93 = function() {
return undefined;
};
var peg$f94 = function() {
return { kind: "parbreak", location: location() };
};
var peg$f95 = function() {
return { kind: "parbreak", location: location() };
};
var peg$currPos = 0;
var peg$savedPos = 0;
var peg$posDetailsCache = [{ line: 1, column: 1 }];
var peg$maxFailPos = 0;
var peg$maxFailExpected = [];
var peg$silentFails = 0;
var peg$result;
if ("startRule" in options) {
if (!(options.startRule in peg$startRuleFunctions)) {
throw new Error("Can't start parsing from rule \"" + options.startRule + "\".");
}
peg$startRuleFunction = peg$startRuleFunctions[options.startRule];
}
function text() {
return input.substring(peg$savedPos, peg$currPos);
}
function offset() {
return peg$savedPos;
}
function range() {
return {
source: peg$source,
start: peg$savedPos,
end: peg$currPos
};
}
function location() {
return peg$computeLocation(peg$savedPos, peg$currPos);
}
function expected(description, location) {
location = location !== undefined
? location
: peg$computeLocation(peg$savedPos, peg$currPos);
throw peg$buildStructuredError(
[peg$otherExpectation(description)],
input.substring(peg$savedPos, peg$currPos),
location
);
}
function error(message, location) {
location = location !== undefined
? location
: peg$computeLocation(peg$savedPos, peg$currPos);
throw peg$buildSimpleError(message, location);
}
function peg$literalExpectation(text, ignoreCase) {
return { type: "literal", text: text, ignoreCase: ignoreCase };
}
function peg$classExpectation(parts, inverted, ignoreCase) {
return { type: "class", parts: parts, inverted: inverted, ignoreCase: ignoreCase };
}
function peg$anyExpectation() {
return { type: "any" };
}
function peg$endExpectation() {
return { type: "end" };
}
function peg$otherExpectation(description) {
return { type: "other", description: description };
}
function peg$computePosDetails(pos) {
var details = peg$posDetailsCache[pos];
var p;
if (details) {
return details;
} else {
p = pos - 1;
while (!peg$posDetailsCache[p]) {
p--;
}
details = peg$posDetailsCache[p];
details = {
line: details.line,
column: details.column
};
while (p < pos) {
if (input.charCodeAt(p) === 10) {
details.line++;
details.column = 1;
} else {
details.column++;
}
p++;
}
peg$posDetailsCache[pos] = details;
return details;
}
}
function peg$computeLocation(startPos, endPos, offset) {
var startPosDetails = peg$computePosDetails(startPos);
var endPosDetails = peg$computePosDetails(endPos);
var res = {
source: peg$source,
start: {
offset: startPos,
line: startPosDetails.line,
column: startPosDetails.column
},
end: {
offset: endPos,
line: endPosDetails.line,
column: endPosDetails.column
}
};
if (offset && peg$source && (typeof peg$source.offset === "function")) {
res.start = peg$source.offset(res.start);
res.end = peg$source.offset(res.end);
}
return res;
}
function peg$fail(expected) {
if (peg$currPos < peg$maxFailPos) { return; }
if (peg$currPos > peg$maxFailPos) {
peg$maxFailPos = peg$currPos;
peg$maxFailExpected = [];
}
peg$maxFailExpected.push(expected);
}
function peg$buildSimpleError(message, location) {
return new peg$SyntaxError(message, null, null, location);
}
function peg$buildStructuredError(expected, found, location) {
return new peg$SyntaxError(
peg$SyntaxError.buildMessage(expected, found),
expected,
found,
location
);
}
function peg$parseRoot() {
var s0, s1, s2, s3;
s0 = peg$currPos;
s1 = peg$parseskip_space();
if (s1 !== peg$FAILED) {
s2 = [];
s3 = peg$parseElement();
while (s3 !== peg$FAILED) {
s2.push(s3);
s3 = peg$parseElement();
}
peg$savedPos = s0;
s0 = peg$f0(s2);
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
return s0;
}
function peg$parsePreamble() {
var s0, s1, s2, s3, s4, s5, s6, s7, s8, s9;
s0 = peg$currPos;
s1 = peg$parseskip_space();
if (s1 !== peg$FAILED) {
s2 = [];
s3 = peg$currPos;
s4 = peg$currPos;
peg$silentFails++;
s5 = peg$currPos;
s6 = peg$parseskip_space();
if (s6 !== peg$FAILED) {
s7 = peg$parseescape();
if (s7 !== peg$FAILED) {
if (input.substr(peg$currPos, 15) === peg$c0) {
s8 = peg$c0;
peg$currPos += 15;
} else {
s8 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e0); }
}
if (s8 !== peg$FAILED) {
s6 = [s6, s7, s8];
s5 = s6;
} else {
peg$currPos = s5;
s5 = peg$FAILED;
}
} else {
peg$currPos = s5;
s5 = peg$FAILED;
}
} else {
peg$currPos = s5;
s5 = peg$FAILED;
}
peg$silentFails--;
if (s5 === peg$FAILED) {
s4 = undefined;
} else {
peg$currPos = s4;
s4 = peg$FAILED;
}
if (s4 !== peg$FAILED) {
s5 = peg$parseElement();
if (s5 !== peg$FAILED) {
peg$savedPos = s3;
s3 = peg$f1(s5);
} else {
peg$currPos = s3;
s3 = peg$FAILED;
}
} else {
peg$currPos = s3;
s3 = peg$FAILED;
}
while (s3 !== peg$FAILED) {
s2.push(s3);
s3 = peg$currPos;
s4 = peg$currPos;
peg$silentFails++;
s5 = peg$currPos;
s6 = peg$parseskip_space();
if (s6 !== peg$FAILED) {
s7 = peg$parseescape();
if (s7 !== peg$FAILED) {
if (input.substr(peg$currPos, 15) === peg$c0) {
s8 = peg$c0;
peg$currPos += 15;
} else {
s8 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e0); }
}
if (s8 !== peg$FAILED) {
s6 = [s6, s7, s8];
s5 = s6;
} else {
peg$currPos = s5;
s5 = peg$FAILED;
}
} else {
peg$currPos = s5;
s5 = peg$FAILED;
}
} else {
peg$currPos = s5;
s5 = peg$FAILED;
}
peg$silentFails--;
if (s5 === peg$FAILED) {
s4 = undefined;
} else {
peg$currPos = s4;
s4 = peg$FAILED;
}
if (s4 !== peg$FAILED) {
s5 = peg$parseElement();
if (s5 !== peg$FAILED) {
peg$savedPos = s3;
s3 = peg$f1(s5);
} else {
peg$currPos = s3;
s3 = peg$FAILED;
}
} else {
peg$currPos = s3;
s3 = peg$FAILED;
}
}
s3 = peg$parseskip_space();
if (s3 !== peg$FAILED) {
s4 = peg$currPos;
s5 = peg$currPos;
s6 = peg$parseescape();
if (s6 !== peg$FAILED) {
if (input.substr(peg$currPos, 15) === peg$c0) {
s7 = peg$c0;
peg$currPos += 15;
} else {
s7 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e0); }
}
if (s7 !== peg$FAILED) {
s8 = [];
if (input.length > peg$currPos) {
s9 = input.charAt(peg$currPos);
peg$currPos++;
} else {
s9 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e1); }
}
while (s9 !== peg$FAILED) {
s8.push(s9);
if (input.length > peg$currPos) {
s9 = input.charAt(peg$currPos);
peg$currPos++;
} else {
s9 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e1); }
}
}
s6 = [s6, s7, s8];
s5 = s6;
} else {
peg$currPos = s5;
s5 = peg$FAILED;
}
} else {
peg$currPos = s5;
s5 = peg$FAILED;
}
if (s5 === peg$FAILED) {
s5 = null;
}
s4 = input.substring(s4, peg$currPos);
peg$savedPos = s0;
s0 = peg$f2(s2, s4);
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
return s0;
}
function peg$parseElement() {
var s0, s1, s2, s3;
s0 = peg$currPos;
s1 = peg$parseElement_p();
if (s1 !== peg$FAILED) {
s2 = [];
s3 = peg$parseskip_comment();
while (s3 !== peg$FAILED) {
s2.push(s3);
s3 = peg$parseskip_comment();
}
peg$savedPos = s0;
s0 = peg$f3(s1);
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
return s0;
}
function peg$parseElement_p() {
var s0, s1, s2, s3, s4, s5;
s0 = peg$parseTypicalWord();
if (s0 === peg$FAILED) {
s0 = peg$parseTypicalSpace();
if (s0 === peg$FAILED) {
s0 = peg$parseSpecialCommand();
if (s0 === peg$FAILED) {
s0 = peg$parsebreak();
if (s0 === peg$FAILED) {
s0 = peg$parseLinebreak();
if (s0 === peg$FAILED) {
s0 = peg$parseDefCommand();
if (s0 === peg$FAILED) {
s0 = peg$parseCommand();
if (s0 === peg$FAILED) {
s0 = peg$parseGroupIsolated();
if (s0 === peg$FAILED) {
s0 = peg$parseInlineMathShift();
if (s0 === peg$FAILED) {
s0 = peg$parseAlignmentTab();
if (s0 === peg$FAILED) {
s0 = peg$parseCommandParameterWithNumber();
if (s0 === peg$FAILED) {
s0 = peg$parseSuperscript();
if (s0 === peg$FAILED) {
s0 = peg$parseSubscript();
if (s0 === peg$FAILED) {
s0 = peg$parseActiveCharacter();
if (s0 === peg$FAILED) {
s0 = peg$parseignore();
if (s0 === peg$FAILED) {
s0 = peg$parseSoftbreak();
if (s0 === peg$FAILED) {
s0 = peg$currPos;
s1 = peg$currPos;
s2 = [];
s3 = peg$currPos;
s4 = peg$currPos;
peg$silentFails++;
s5 = peg$parsenoncharToken();
peg$silentFails--;
if (s5 === peg$FAILED) {
s4 = undefined;
} else {
peg$currPos = s4;
s4 = peg$FAILED;
}
if (s4 !== peg$FAILED) {
if (input.length > peg$currPos) {
s5 = input.charAt(peg$currPos);
peg$currPos++;
} else {
s5 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e1); }
}
if (s5 !== peg$FAILED) {
s4 = [s4, s5];
s3 = s4;
} else {
peg$currPos = s3;
s3 = peg$FAILED;
}
} else {
peg$currPos = s3;
s3 = peg$FAILED;
}
if (s3 !== peg$FAILED) {
while (s3 !== peg$FAILED) {
s2.push(s3);
s3 = peg$currPos;
s4 = peg$currPos;
peg$silentFails++;
s5 = peg$parsenoncharToken();
peg$silentFails--;
if (s5 === peg$FAILED) {
s4 = undefined;
} else {
peg$currPos = s4;
s4 = peg$FAILED;
}
if (s4 !== peg$FAILED) {
if (input.length > peg$currPos) {
s5 = input.charAt(peg$currPos);
peg$currPos++;
} else {
s5 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e1); }
}
if (s5 !== peg$FAILED) {
s4 = [s4, s5];
s3 = s4;
} else {
peg$currPos = s3;
s3 = peg$FAILED;
}
} else {
peg$currPos = s3;
s3 = peg$FAILED;
}
}
} else {
s2 = peg$FAILED;
}
if (s2 !== peg$FAILED) {
s1 = input.substring(s1, peg$currPos);
} else {
s1 = s2;
}
if (s1 !== peg$FAILED) {
peg$savedPos = s0;
s1 = peg$f4(s1);
}
s0 = s1;
if (s0 === peg$FAILED) {
s0 = peg$parseSpace();
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return s0;
}
function peg$parseTypicalWord() {
var s0, s1, s2, s3;
s0 = peg$currPos;
s1 = peg$currPos;
s2 = [];
s3 = peg$parsecharTokenOptm();
if (s3 !== peg$FAILED) {
while (s3 !== peg$FAILED) {
s2.push(s3);
s3 = peg$parsecharTokenOptm();
}
} else {
s2 = peg$FAILED;
}
if (s2 !== peg$FAILED) {
s1 = input.substring(s1, peg$currPos);
} else {
s1 = s2;
}
if (s1 !== peg$FAILED) {
s2 = peg$currPos;
peg$silentFails++;
s3 = peg$parsespecialTokenOptm();
peg$silentFails--;
if (s3 !== peg$FAILED) {
peg$currPos = s2;
s2 = undefined;
} else {
s2 = peg$FAILED;
}
if (s2 !== peg$FAILED) {
peg$savedPos = s0;
s0 = peg$f5(s1);
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
return s0;
}
function peg$parseTypicalSpace() {
var s0, s1, s2, s3;
s0 = peg$currPos;
s1 = peg$currPos;
s2 = [];
if (peg$r0.test(input.charAt(peg$currPos))) {
s3 = input.charAt(peg$currPos);
peg$currPos++;
} else {
s3 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e2); }
}
if (s3 !== peg$FAILED) {
while (s3 !== peg$FAILED) {
s2.push(s3);
if (peg$r0.test(input.charAt(peg$currPos))) {
s3 = input.charAt(peg$currPos);
peg$currPos++;
} else {
s3 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e2); }
}
}
} else {
s2 = peg$FAILED;
}
if (s2 !== peg$FAILED) {
s1 = input.substring(s1, peg$currPos);
} else {
s1 = s2;
}
if (s1 !== peg$FAILED) {
s2 = peg$currPos;
peg$silentFails++;
s3 = peg$parsecharTokenOptm();
peg$silentFails--;
if (s3 !== peg$FAILED) {
peg$currPos = s2;
s2 = undefined;
} else {
s2 = peg$FAILED;
}
if (s2 !== peg$FAILED) {
peg$savedPos = s0;
s0 = peg$f6(s1);
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
return s0;
}
function peg$parsecharTokenOptm() {
var s0;
if (peg$r1.test(input.charAt(peg$currPos))) {
s0 = input.charAt(peg$currPos);
peg$currPos++;
} else {
s0 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e3); }
}
return s0;
}
function peg$parsespecialTokenOptm() {
var s0;
if (peg$r2.test(input.charAt(peg$currPos))) {
s0 = input.charAt(peg$currPos);
peg$currPos++;
} else {
s0 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e4); }
}
return s0;
}
function peg$parseMathElement() {
var s0, s1, s2, s3;
s0 = peg$currPos;
s1 = peg$parseMathElement_p();
if (s1 !== peg$FAILED) {
s2 = [];
s3 = peg$parseskip_comment();
while (s3 !== peg$FAILED) {
s2.push(s3);
s3 = peg$parseskip_comment();
}
peg$savedPos = s0;
s0 = peg$f7(s1);
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
return s0;
}
function peg$parseMathElement_p() {
var s0, s1, s2, s3, s4;
s0 = peg$parseMathAlignedEnvironment();
if (s0 === peg$FAILED) {
s0 = peg$parseLinebreak();
if (s0 === peg$FAILED) {
s0 = peg$parseAmsmathTextCommand();
if (s0 === peg$FAILED) {
s0 = peg$parseSpecialCommand();
if (s0 === peg$FAILED) {
s0 = peg$parseMatchingDelimiters();
if (s0 === peg$FAILED) {
s0 = peg$parseMathematicalDelimiters();
if (s0 === peg$FAILED) {
s0 = peg$parseMathCommand();
if (s0 === peg$FAILED) {
s0 = peg$parseMathGroup();
if (s0 === peg$FAILED) {
s0 = peg$parseAlignmentTab();
if (s0 === peg$FAILED) {
s0 = peg$parseCommandParameterWithNumber();
if (s0 === peg$FAILED) {
s0 = peg$parseSuperscriptWithArg();
if (s0 === peg$FAILED) {
s0 = peg$parseSubscriptWithArg();
if (s0 === peg$FAILED) {
s0 = peg$parseActiveCharacter();
if (s0 === peg$FAILED) {
s0 = peg$parseignore();
if (s0 === peg$FAILED) {
s0 = peg$currPos;
s1 = peg$currPos;
s2 = peg$currPos;
s3 = peg$currPos;
peg$silentFails++;
s4 = peg$parsenonMathcharToken();
peg$silentFails--;
if (s4 === peg$FAILED) {
s3 = undefined;
} else {
peg$currPos = s3;
s3 = peg$FAILED;
}
if (s3 !== peg$FAILED) {
if (input.length > peg$currPos) {
s4 = input.charAt(peg$currPos);
peg$currPos++;
} else {
s4 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e1); }
}
if (s4 !== peg$FAILED) {
s3 = [s3, s4];
s2 = s3;
} else {
peg$currPos = s2;
s2 = peg$FAILED;
}
} else {
peg$currPos = s2;
s2 = peg$FAILED;
}
if (s2 !== peg$FAILED) {
s1 = input.substring(s1, peg$currPos);
} else {
s1 = s2;
}
if (s1 !== peg$FAILED) {
peg$savedPos = s0;
s1 = peg$f8(s1);
}
s0 = s1;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return s0;
}
function peg$parsenonMathcharToken() {
var s0;
s0 = peg$parsemathShift();
if (s0 === peg$FAILED) {
s0 = peg$parseescape();
}
return s0;
}
function peg$parsenoncharToken() {
var s0;
s0 = peg$parseescape();
if (s0 === peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 37) {
s0 = peg$c1;
peg$currPos++;
} else {
s0 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e5); }
}
if (s0 === peg$FAILED) {
s0 = peg$parsebeginGroup();
if (s0 === peg$FAILED) {
s0 = peg$parseendGroup();
if (s0 === peg$FAILED) {
s0 = peg$parsemathShift();
if (s0 === peg$FAILED) {
s0 = peg$parseAlignmentTab();
if (s0 === peg$FAILED) {
s0 = peg$parseActiveCharacter();
if (s0 === peg$FAILED) {
s0 = peg$parsenl();
if (s0 === peg$FAILED) {
s0 = peg$parsecommandParameter();
if (s0 === peg$FAILED) {
s0 = peg$parseSuperscript();
if (s0 === peg$FAILED) {
s0 = peg$parseSubscript();
if (s0 === peg$FAILED) {
s0 = peg$parseignore();
if (s0 === peg$FAILED) {
s0 = peg$parsesp();
if (s0 === peg$FAILED) {
s0 = peg$parseEOF();
}
}
}
}
}
}
}
}
}
}
}
}
}
return s0;
}
function peg$parsenumber() {
var s0, s1, s2, s3, s4, s5;
s0 = peg$currPos;
s1 = peg$currPos;
s2 = [];
s3 = peg$parsenum();
if (s3 !== peg$FAILED) {
while (s3 !== peg$FAILED) {
s2.push(s3);
s3 = peg$parsenum();
}
} else {
s2 = peg$FAILED;
}
if (s2 !== peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 46) {
s3 = peg$c2;
peg$currPos++;
} else {
s3 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e6); }
}
if (s3 !== peg$FAILED) {
s4 = [];
s5 = peg$parsenum();
if (s5 !== peg$FAILED) {
while (s5 !== peg$FAILED) {
s4.push(s5);
s5 = peg$parsenum();
}
} else {
s4 = peg$FAILED;
}
if (s4 !== peg$FAILED) {
s2 = [s2, s3, s4];
s1 = s2;
} else {
peg$currPos = s1;
s1 = peg$FAILED;
}
} else {
peg$currPos = s1;
s1 = peg$FAILED;
}
} else {
peg$currPos = s1;
s1 = peg$FAILED;
}
if (s1 !== peg$FAILED) {
s0 = input.substring(s0, peg$currPos);
} else {
s0 = s1;
}
if (s0 === peg$FAILED) {
s0 = peg$currPos;
s1 = peg$currPos;
if (input.charCodeAt(peg$currPos) === 46) {
s2 = peg$c2;
peg$currPos++;
} else {
s2 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e6); }
}
if (s2 !== peg$FAILED) {
s3 = [];
s4 = peg$parsenum();
if (s4 !== peg$FAILED) {
while (s4 !== peg$FAILED) {
s3.push(s4);
s4 = peg$parsenum();
}
} else {
s3 = peg$FAILED;
}
if (s3 !== peg$FAILED) {
s2 = [s2, s3];
s1 = s2;
} else {
peg$currPos = s1;
s1 = peg$FAILED;
}
} else {
peg$currPos = s1;
s1 = peg$FAILED;
}
if (s1 !== peg$FAILED) {
s0 = input.substring(s0, peg$currPos);
} else {
s0 = s1;
}
if (s0 === peg$FAILED) {
s0 = peg$currPos;
s1 = peg$currPos;
s2 = [];
s3 = peg$parsenum();
if (s3 !== peg$FAILED) {
while (s3 !== peg$FAILED) {
s2.push(s3);
s3 = peg$parsenum();
}
} else {
s2 = peg$FAILED;
}
if (s2 !== peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 46) {
s3 = peg$c2;
peg$currPos++;
} else {
s3 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e6); }
}
if (s3 !== peg$FAILED) {
s2 = [s2, s3];
s1 = s2;
} else {
peg$currPos = s1;
s1 = peg$FAILED;
}
} else {
peg$currPos = s1;
s1 = peg$FAILED;
}
if (s1 !== peg$FAILED) {
s0 = input.substring(s0, peg$currPos);
} else {
s0 = s1;
}
if (s0 === peg$FAILED) {
s0 = peg$currPos;
s1 = [];
s2 = peg$parsenum();
if (s2 !== peg$FAILED) {
while (s2 !== peg$FAILED) {
s1.push(s2);
s2 = peg$parsenum();
}
} else {
s1 = peg$FAILED;