react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / syntax-error / node_modules / acorn / src / location.js
80744 viewsimport {Parser} from "./state"1import {lineBreakG} from "./whitespace"23// These are used when `options.locations` is on, for the4// `startLoc` and `endLoc` properties.56export class Position {7constructor(line, col) {8this.line = line9this.column = col10}1112offset(n) {13return new Position(this.line, this.column + n)14}15}1617export class SourceLocation {18constructor(p, start, end) {19this.start = start20this.end = end21if (p.sourceFile !== null) this.source = p.sourceFile22}23}2425// The `getLineInfo` function is mostly useful when the26// `locations` option is off (for performance reasons) and you27// want to find the line/column position for a given character28// offset. `input` should be the code string that the offset refers29// into.3031export function getLineInfo(input, offset) {32for (let line = 1, cur = 0;;) {33lineBreakG.lastIndex = cur34let match = lineBreakG.exec(input)35if (match && match.index < offset) {36++line37cur = match.index + match[0].length38} else {39return new Position(line, offset - cur)40}41}42}4344const pp = Parser.prototype4546// This function is used to raise exceptions on parse errors. It47// takes an offset integer (into the current `input`) to indicate48// the location of the error, attaches the position to the end49// of the error message, and then raises a `SyntaxError` with that50// message.5152pp.raise = function(pos, message) {53let loc = getLineInfo(this.input, pos)54message += " (" + loc.line + ":" + loc.column + ")"55let err = new SyntaxError(message)56err.pos = pos; err.loc = loc; err.raisedAt = this.pos57throw err58}5960pp.curPosition = function() {61return new Position(this.curLine, this.pos - this.lineStart)62}636465