Skip to content

Commit

Permalink
feat(parser): enable line/column location information to each node
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed Jun 23, 2019
1 parent 1fe699a commit 75c43c7
Show file tree
Hide file tree
Showing 11 changed files with 7,658 additions and 812 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ The second argument allows you to specify various options:
| `globalReturn` | Allow `return` in the global scope |
| `impliedStrict` | Enable strict mode (*initial enforcement*) |
| `lexical` | Enable lexical binding and scope tracking |
| `loc` | Enable line/column location information to each node |
| `module` | Allow parsing with module goal |
| `next` | Allow parsing with `ESNext` features |
| `parenthesizedExpr` | Enable non-standard parenthesized expression node |
Expand Down
21 changes: 20 additions & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ export interface ParserState {
column: number;
tokenIndex: number;
startIndex: number;
startColumn: number;
startLine: number;
columnOffset: number;
lineOffset: number;
end: number;
token: Token;
tokenValue: any;
Expand Down Expand Up @@ -376,12 +380,27 @@ export function finishNode<T extends Node>(
parser: ParserState,
context: Context,
start: number,
line: number,
column: number,
node: T,
): T {
if (context & Context.OptionsRanges) {
node.start = start;
node.end = parser.startIndex;
}
}

if (context & Context.OptionsLoc) {
node.loc = {
start: {
line,
column,
},
end: {
line: parser.startLine,
column: parser.startColumn,
}
};
}

return node;
}
2 changes: 1 addition & 1 deletion src/lexer/comments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nextCodePoint, CharTypes, CharFlags, ScannerState, consumeLineFeed, advanceNewline } from './';
import { nextCodePoint, CharTypes, CharFlags, ScannerState, advanceNewline } from './';
import { Chars } from '../chars';
import { ParserState, Flags } from '../common';
import { report, Errors } from '../errors';
Expand Down
2 changes: 1 addition & 1 deletion src/lexer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export {
scanUnicodeEscapeValue
} from './identifier';
export { scanString } from './string';
export { scanNumber } from './numeric';
export { scanNumber, NumberKind } from './numeric';
export { scanTemplate, scanTemplateTail } from './template';
export { scanRegularExpression } from './regexp';
5 changes: 2 additions & 3 deletions src/lexer/numeric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,12 @@ export function scanNumber(parser: ParserState, context: Context, isFloat: 0 | 1
if (isFloat) {
// scan subsequent decimal digits
let digit = 9;
while (digit >= 0 && CharTypes[parser.nextCP] & CharFlags.Decimal) {
while (digit >= 0 && CharTypes[nextCodePoint(parser)] & CharFlags.Decimal) {
value = 10 * value + (parser.nextCP - Chars.Zero);
nextCodePoint(parser);
--digit;
}

if (digit >= 0 && parser.nextCP !== Chars.Period && !isIdentifierStart(parser.nextCP)) {
if (digit >= 0 && !isIdentifierStart(parser.nextCP) && parser.nextCP !== Chars.Period) {
if (context & Context.OptionsRaw) parser.tokenRaw = parser.source.slice(parser.tokenIndex, parser.index);
parser.tokenValue = value;
return Token.NumericLiteral;
Expand Down
7 changes: 5 additions & 2 deletions src/lexer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ export const TokenLookup = [
export function nextToken(parser: ParserState, context: Context): void {
parser.flags &= ~Flags.NewLine;
parser.startIndex = parser.index;
parser.startColumn = parser.column;
parser.startLine = parser.line;
parser.token = scanSingleToken(parser, context, ScannerState.None);
}

Expand All @@ -184,7 +186,8 @@ export function scanSingleToken(parser: ParserState, context: Context, state: Sc

while (parser.index < parser.end) {
parser.tokenIndex = parser.index;

parser.columnOffset = parser.column;
parser.lineOffset = parser.line;
const first = parser.nextCP;

if (first <= 0x7e) {
Expand Down Expand Up @@ -230,7 +233,7 @@ export function scanSingleToken(parser: ParserState, context: Context, state: Sc

// Look for a decimal number.
case Token.NumericLiteral:
return scanNumber(parser, context, 0);
return scanNumber(parser, context, /* isFloat */ 0);

// Look for a string or a template string.
case Token.StringLiteral:
Expand Down
Loading

0 comments on commit 75c43c7

Please sign in to comment.