Move location-related code into location.js
This commit is contained in:
parent
fec42cb596
commit
7b05e660b4
972
src/index.js
972
src/index.js
File diff suppressed because it is too large
Load Diff
66
src/location.js
Normal file
66
src/location.js
Normal 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;
|
||||
};
|
||||
@ -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;
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user