(message, // The error message token)
| 88 | class ParseError { |
| 89 | // Error position based on passed-in Token or ParseNode. |
| 90 | constructor(message, // The error message |
| 91 | token) // An object providing position information |
| 92 | { |
| 93 | this.position = void 0; |
| 94 | let error = "KaTeX parse error: " + message; |
| 95 | let start; |
| 96 | const loc = token && token.loc; |
| 97 | |
| 98 | if (loc && loc.start <= loc.end) { |
| 99 | // If we have the input and a position, make the error a bit fancier |
| 100 | // Get the input |
| 101 | const input = loc.lexer.input; // Prepend some information |
| 102 | |
| 103 | start = loc.start; |
| 104 | const end = loc.end; |
| 105 | |
| 106 | if (start === input.length) { |
| 107 | error += " at end of input: "; |
| 108 | } else { |
| 109 | error += " at position " + (start + 1) + ": "; |
| 110 | } // Underline token in question using combining underscores |
| 111 | |
| 112 | |
| 113 | const underlined = input.slice(start, end).replace(/[^]/g, "$&\u0332"); // Extract some context from the input and add it to the error |
| 114 | |
| 115 | let left; |
| 116 | |
| 117 | if (start > 15) { |
| 118 | left = "…" + input.slice(start - 15, start); |
| 119 | } else { |
| 120 | left = input.slice(0, start); |
| 121 | } |
| 122 | |
| 123 | let right; |
| 124 | |
| 125 | if (end + 15 < input.length) { |
| 126 | right = input.slice(end, end + 15) + "…"; |
| 127 | } else { |
| 128 | right = input.slice(end); |
| 129 | } |
| 130 | |
| 131 | error += left + underlined + right; |
| 132 | } // Some hackery to make ParseError a prototype of Error |
| 133 | // See http://stackoverflow.com/a/8460753 |
| 134 | |
| 135 | |
| 136 | const self = new Error(error); |
| 137 | self.name = "ParseError"; // $FlowFixMe |
| 138 | |
| 139 | self.__proto__ = ParseError.prototype; // $FlowFixMe |
| 140 | |
| 141 | self.position = start; |
| 142 | return self; |
| 143 | } |
| 144 | |
| 145 | } // $FlowFixMe More hackery |
| 146 |
nothing calls this directly
no outgoing calls
no test coverage detected