react / wstein / node_modules / browserify / node_modules / syntax-error / node_modules / acorn / src / location.js
80550 viewsimport {Parser} from "./state"1import {lineBreakG} from "./whitespace"2import {deprecate} from "util"34// These are used when `options.locations` is on, for the5// `startLoc` and `endLoc` properties.67export class Position {8constructor(line, col) {9this.line = line10this.column = col11}1213offset(n) {14return new Position(this.line, this.column + n)15}16}1718export class SourceLocation {19constructor(p, start, end) {20this.start = start21this.end = end22if (p.sourceFile !== null) this.source = p.sourceFile23}24}2526// The `getLineInfo` function is mostly useful when the27// `locations` option is off (for performance reasons) and you28// want to find the line/column position for a given character29// offset. `input` should be the code string that the offset refers30// into.3132export function getLineInfo(input, offset) {33for (let line = 1, cur = 0;;) {34lineBreakG.lastIndex = cur35let match = lineBreakG.exec(input)36if (match && match.index < offset) {37++line38cur = match.index + match[0].length39} else {40return new Position(line, offset - cur)41}42}43}4445const pp = Parser.prototype4647// This function is used to raise exceptions on parse errors. It48// takes an offset integer (into the current `input`) to indicate49// the location of the error, attaches the position to the end50// of the error message, and then raises a `SyntaxError` with that51// message.5253pp.raise = function(pos, message) {54let loc = getLineInfo(this.input, pos)55message += " (" + loc.line + ":" + loc.column + ")"56let err = new SyntaxError(message)57err.pos = pos; err.loc = loc; err.raisedAt = this.pos58throw err59}6061pp.curPosition = function() {62return new Position(this.curLine, this.pos - this.lineStart)63}6465pp.markPosition = function() {66return this.options.locations ? [this.start, this.startLoc] : this.start67}686970