(message, // The error message token)
| 213 | */ |
| 214 | var ParseError = // Error position based on passed-in Token or ParseNode. |
| 215 | function ParseError(message, // The error message |
| 216 | token) // An object providing position information |
| 217 | { |
| 218 | this.position = void 0; |
| 219 | var error = "KaTeX parse error: " + message; |
| 220 | var start; |
| 221 | var loc = token && token.loc; |
| 222 | |
| 223 | if (loc && loc.start <= loc.end) { |
| 224 | // If we have the input and a position, make the error a bit fancier |
| 225 | // Get the input |
| 226 | var input = loc.lexer.input; // Prepend some information |
| 227 | |
| 228 | start = loc.start; |
| 229 | var end = loc.end; |
| 230 | |
| 231 | if (start === input.length) { |
| 232 | error += " at end of input: "; |
| 233 | } else { |
| 234 | error += " at position " + (start + 1) + ": "; |
| 235 | } // Underline token in question using combining underscores |
| 236 | |
| 237 | |
| 238 | var underlined = input.slice(start, end).replace(/[^]/g, "$&\u0332"); // Extract some context from the input and add it to the error |
| 239 | |
| 240 | var left; |
| 241 | |
| 242 | if (start > 15) { |
| 243 | left = "…" + input.slice(start - 15, start); |
| 244 | } else { |
| 245 | left = input.slice(0, start); |
| 246 | } |
| 247 | |
| 248 | var right; |
| 249 | |
| 250 | if (end + 15 < input.length) { |
| 251 | right = input.slice(end, end + 15) + "…"; |
| 252 | } else { |
| 253 | right = input.slice(end); |
| 254 | } |
| 255 | |
| 256 | error += left + underlined + right; |
| 257 | } // Some hackery to make ParseError a prototype of Error |
| 258 | // See http://stackoverflow.com/a/8460753 |
| 259 | |
| 260 | |
| 261 | var self = new Error(error); |
| 262 | self.name = "ParseError"; // $FlowFixMe |
| 263 | |
| 264 | self.__proto__ = ParseError.prototype; // $FlowFixMe |
| 265 | |
| 266 | self.position = start; |
| 267 | return self; |
| 268 | }; // $FlowFixMe More hackery |
| 269 | |
| 270 | |
| 271 | ParseError.prototype.__proto__ = Error.prototype; |
nothing calls this directly
no outgoing calls
no test coverage detected