Skip to content

Commit b61cb9a

Browse files
committed
Add startLocation option
FEATURE: The new `startLocation` option can now be used to tell the parser the line and column the parse starts at (mostly useful with `parseExpressionAt`). Issue '#1448
1 parent c8d515c commit b61cb9a

4 files changed

Lines changed: 24 additions & 3 deletions

File tree

acorn/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ required):
137137
one-based line and zero-based column numbers in `{line, column}`
138138
form. Default is `false`.
139139

140+
- **startLocation**: An optional `{line, column}` object to use for
141+
the start of the parse. This is mostly useful when using
142+
`parseExpressionAt` with `locations: true`, to prevent the parser
143+
from having to determine the line position at the start position.
144+
140145
- **onToken**: If a function is passed for this option, each found
141146
token will be passed in same format as tokens returned from
142147
`tokenizer().getToken()`.

acorn/src/acorn.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,14 @@ export interface Options {
693693
*/
694694
locations?: boolean
695695

696+
/**
697+
* Pass an optional `{line, column}` object to use for the start of
698+
* the parse. This is mostly useful when using `parseExpressionAt`
699+
* with `locations: true`, to prevent the parser from having to
700+
* determine the line position at the start position.
701+
*/
702+
startLocation?: {line: number, column: number}
703+
696704
/**
697705
* a callback that will cause Acorn to call that export function with object in the same
698706
* format as tokens returned from `tokenizer().getToken()`. Note

acorn/src/options.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ export const defaultOptions = {
6060
// line being 1-based and column 0-based) will be attached to the
6161
// nodes.
6262
locations: false,
63+
// Pass an optional `{line, column}` object to use for the start of
64+
// the parse. This is mostly useful when using `parseExpressionAt`
65+
// with `locations: true`, to prevent the parser from having to
66+
// determine the line position at the start position.
67+
startLocation: null,
6368
// A function can be passed as `onToken` option, which will
6469
// cause Acorn to call that function with object in the same
6570
// format as tokens returned from `tokenizer().getToken()`. Note

acorn/src/state.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@ export class Parser {
3232
// Set up token state
3333

3434
// The current position of the tokenizer in the input.
35+
this.pos = startPos || 0
3536
this.curLine = 1
36-
if (startPos) {
37-
this.pos = startPos
37+
if (options.startLocation) {
38+
this.lineStart = this.pos - options.startLocation.column
39+
this.curLine = options.startLocation.line
40+
} else if (startPos) {
3841
this.lineStart = this.input.lastIndexOf("\n", startPos - 1) + 1
3942
if (this.options.locations)
4043
this.curLine = this.input.slice(0, this.lineStart).split(lineBreak).length
4144
} else {
42-
this.pos = this.lineStart = 0
45+
this.lineStart = 0
4346
}
4447

4548
// Properties of the current token:

0 commit comments

Comments
 (0)