|
|
|
import {Token} from "./Token"; |
|
|
|
import type {AnyParseNode} from "./parseNode"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ParseError { |
|
name: "ParseError"; |
|
position: number | void; |
|
|
|
length: number | void; |
|
|
|
rawMessage: string | void; |
|
|
|
|
|
constructor( |
|
message: string, |
|
token?: ?Token | AnyParseNode, |
|
): ParseError { |
|
let error = "KaTeX parse error: " + message; |
|
let start; |
|
let end; |
|
|
|
const loc = token && token.loc; |
|
if (loc && loc.start <= loc.end) { |
|
|
|
|
|
|
|
const input = loc.lexer.input; |
|
|
|
|
|
start = loc.start; |
|
end = loc.end; |
|
if (start === input.length) { |
|
error += " at end of input: "; |
|
} else { |
|
error += " at position " + (start + 1) + ": "; |
|
} |
|
|
|
|
|
const underlined = input.slice(start, end).replace(/[^]/g, "$&\u0332"); |
|
|
|
|
|
let left; |
|
if (start > 15) { |
|
left = "…" + input.slice(start - 15, start); |
|
} else { |
|
left = input.slice(0, start); |
|
} |
|
let right; |
|
if (end + 15 < input.length) { |
|
right = input.slice(end, end + 15) + "…"; |
|
} else { |
|
right = input.slice(end); |
|
} |
|
error += left + underlined + right; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
const self: ParseError = new Error(error); |
|
self.name = "ParseError"; |
|
|
|
self.__proto__ = ParseError.prototype; |
|
self.position = start; |
|
if (start != null && end != null) { |
|
self.length = end - start; |
|
} |
|
self.rawMessage = message; |
|
return self; |
|
} |
|
} |
|
|
|
|
|
ParseError.prototype.__proto__ = Error.prototype; |
|
|
|
export default ParseError; |
|
|