diff --git a/Lexer.js b/Lexer.js index c4cc026..7839e0e 100644 --- a/Lexer.js +++ b/Lexer.js @@ -1,3 +1,5 @@ +var ParseError = require("./ParseError"); + // The main lexer class function Lexer(input) { this._input = input; @@ -62,7 +64,8 @@ Lexer.prototype.lex = function(pos) { } // We didn't match any of the tokens, so throw an error. - throw "Unexpected character: '" + input[0] + "' at position " + pos; + throw new ParseError("Unexpected character: '" + input[0] + + "' at position " + pos); }; module.exports = Lexer; diff --git a/ParseError.js b/ParseError.js new file mode 100644 index 0000000..6025976 --- /dev/null +++ b/ParseError.js @@ -0,0 +1,7 @@ +function ParseError(message) { + this.message = "TeX parse error: " + message; +} + +ParseError.prototype = Error.prototype; + +module.exports = ParseError; diff --git a/Parser.js b/Parser.js index 6361e66..b97fbea 100644 --- a/Parser.js +++ b/Parser.js @@ -1,6 +1,8 @@ var Lexer = require("./Lexer"); var utils = require("./utils"); +var ParseError = require("./ParseError"); + // Main Parser class function Parser() { }; @@ -22,7 +24,8 @@ function ParseNode(type, value) { // appropriate error otherwise. var expect = function(result, type) { if (result.type !== type) { - throw "Failed parsing: Expected '" + type + "', got '" + result.type + "'"; + throw new ParseError( + "Expected '" + type + "', got '" + result.type + "'"); } }; @@ -76,7 +79,7 @@ Parser.prototype.parseSuperscript = function(pos) { return group; } else { // Throw an error if we didn't find a group - throw "Parse error: Couldn't find group after '^'"; + throw new ParseError("Couldn't find group after '^'"); } } else if (sup.type === "'") { var pos = sup.position; @@ -98,7 +101,7 @@ Parser.prototype.parseSubscript = function(pos) { return group; } else { // Throw an error if we didn't find a group - throw "Parse error: Couldn't find group after '_'"; + throw new ParseError("Couldn't find group after '_'"); } } else { return null; @@ -343,7 +346,8 @@ Parser.prototype.parseNucleus = function(pos) { {color: nucleus.type.slice(1), value: atoms}), group.position); } else { - throw "Parse error: Expected group after '" + nucleus.text + "'"; + throw new ParseError( + "Expected group after '" + nucleus.text + "'"); } } else if (nucleus.type === "\\llap" || nucleus.type === "\\rlap") { // If this is an llap or rlap, parse its argument and return @@ -353,7 +357,8 @@ Parser.prototype.parseNucleus = function(pos) { new ParseNode(nucleus.type.slice(1), group.result), group.position); } else { - throw "Parse error: Expected group after '" + nucleus.text + "'"; + throw new ParseError( + "Expected group after '" + nucleus.text + "'"); } } else if (nucleus.type === "\\dfrac" || nucleus.type === "\\frac" || nucleus.type === "\\tfrac") { @@ -370,12 +375,12 @@ Parser.prototype.parseNucleus = function(pos) { }), denom.position); } else { - throw "Parse error: Expected denominator after '" + - nucleus.type + "'"; + throw new ParseError("Expected denominator after '" + + nucleus.type + "'"); } } else { - throw "Parse error: Expected numerator after '" + nucleus.type + - "'"; + throw new ParseError("Parse error: Expected numerator after '" + + nucleus.type + "'"); } } else if (funcToType[nucleus.type]) { // Otherwise if this is a no-argument function, find the type it diff --git a/katex.js b/katex.js index c42c501..10b899d 100644 --- a/katex.js +++ b/katex.js @@ -1,4 +1,5 @@ var Style = require("./Style"); +var ParseError = require("./ParseError"); var parseTree = require("./parseTree"); var utils = require("./utils"); @@ -240,23 +241,19 @@ var clearNode = function(node) { } }; -var process = function(toParse, baseElem) { - try { - var tree = parseTree(toParse); - } catch (e) { - console.error(e); - return false; - } +var process = function(toParse, baseNode) { + var tree = parseTree(toParse); var style = Style.TEXT; var expression = buildExpression(style, /* color: */ "", tree); var span = makeSpan(style.cls(), expression); + var katexNode = makeSpan("katex", [span]); - clearNode(baseElem); - baseElem.appendChild(span); - return true; + clearNode(baseNode); + baseNode.appendChild(katexNode); }; module.exports = { - process: process + process: process, + ParseError: ParseError }; diff --git a/static/index.html b/static/index.html index 5d7118d..a00ade2 100644 --- a/static/index.html +++ b/static/index.html @@ -9,6 +9,6 @@
- +