Move location-related code into location.js

This commit is contained in:
Marijn Haverbeke 2015-03-19 16:28:28 +01:00
parent fec42cb596
commit 7b05e660b4
3 changed files with 109 additions and 965 deletions

File diff suppressed because it is too large Load Diff

66
src/location.js Normal file
View File

@ -0,0 +1,66 @@
import Parser from "./state"
// These are used when `options.locations` is on, for the
// `startLoc` and `endLoc` properties.
export class Position {
constructor(line, col) {
this.line = line
this.column = col
}
offset(n) {
return new Position(this.line, this.column + n)
}
}
export class SourceLocation {
constructor(p, start, end) {
this.start = start
this.end = end
if (p.sourceFile !== null) this.source = p.sourceFile
}
}
// The `getLineInfo` function is mostly useful when the
// `locations` option is off (for performance reasons) and you
// want to find the line/column position for a given character
// offset. `input` should be the code string that the offset refers
// into.
export function getLineInfo(input, offset) {
for (let line = 1, cur = 0;;) {
lineBreak.lastIndex = cur
let match = lineBreak.exec(input)
if (match && match.index < offset) {
++line
cur = match.index + match[0].length
} else {
return new Position(line, offset - cur)
}
}
}
const pp = Parser.prototype
// This function is used to raise exceptions on parse errors. It
// takes an offset integer (into the current `input`) to indicate
// the location of the error, attaches the position to the end
// of the error message, and then raises a `SyntaxError` with that
// message.
pp.raise = function(pos, message) {
var loc = getLineInfo(this.input, pos);
message += " (" + loc.line + ":" + loc.column + ")";
var err = new SyntaxError(message);
err.pos = pos; err.loc = loc; err.raisedAt = this.pos;
throw err;
};
pp.curPosition = function() {
return new Position(this.curLine, this.pos - this.lineStart);
};
pp.markPosition = function() {
return this.options.locations ? [this.start, this.startLoc] : this.start;
};

View File

@ -31,20 +31,6 @@ export function isNewLine(code) {
// ## Tokenizer
// These are used when `options.locations` is on, for the
// `startLoc` and `endLoc` properties.
class Position {
constructor(line, col) {
this.line = line
this.column = col
}
offset(n) {
return new Position(this.line, this.column + n)
}
}
// Shorthand because we are going to be adding a _lot_ of methods to
// this.
const pp = Parser.prototype
@ -217,10 +203,6 @@ pp.skipSpace = function() {
}
};
pp.curPosition = function() {
return new Position(this.curLine, this.pos - this.lineStart);
};
// Called at the end of every token. Sets `end`, `val`, and
// maintains `context` and `exprAllowed`, and skips the space after
// the token, so that the next one's `start` will point at the
@ -704,21 +686,3 @@ pp.readWord = function() {
type = keywordTypes[word];
return this.finishToken(type, word);
};
// This function is used to raise exceptions on parse errors. It
// takes an offset integer (into the current `input`) to indicate
// the location of the error, attaches the position to the end
// of the error message, and then raises a `SyntaxError` with that
// message.
pp.raise = function(pos, message) {
var loc = getLineInfo(this.input, pos);
message += " (" + loc.line + ":" + loc.column + ")";
var err = new SyntaxError(message);
err.pos = pos; err.loc = loc; err.raisedAt = this.pos;
throw err;
};
pp.currentPos = function() {
return this.options.locations ? [this.start, this.startLoc] : this.start;
};