tweak-parser
Rich Harris 2 days ago
parent 0d2a8ecbc2
commit 92ebcaec99

@ -10,6 +10,7 @@ import read_options from './read/options.js';
import { is_reserved } from '../../../utils.js';
import { disallow_children } from '../2-analyze/visitors/shared/special-element.js';
import * as state from '../../state.js';
import { find_end } from './utils/find.js';
/** @param {number} cc */
function is_whitespace(cc) {
@ -214,6 +215,33 @@ export class Parser {
return match[0];
}
advance() {
let i = this.index;
let source = this.template;
while (i < source.length) {
const code = source.charCodeAt(i);
if (is_whitespace(code)) {
i += 1;
continue;
}
if (code === 47) {
const next = source.charCodeAt(i + 1);
if (next === 47 || next === 42) {
i = find_end(source, next === 42 ? '*/' : '\n', i);
continue;
}
}
break;
}
this.index = i;
}
allow_whitespace() {
while (
this.index < this.template.length &&

@ -1,9 +1,6 @@
/** @import { Expression, Pattern, ObjectPattern, Property, Identifier, RestElement, ArrayPattern } from 'estree' */
/** @import { Parser } from '../index.js' */
import { match_bracket } from '../utils/bracket.js';
import { parse_expression_at } from '../acorn.js';
import { regex_not_newline_characters } from '../../patterns.js';
import * as e from '../../../errors.js';
import { get_loc } from '../../../state.js';
/**
@ -51,7 +48,7 @@ function read_object_pattern(parser) {
};
while (true) {
parser.allow_whitespace();
parser.advance();
const start = parser.index;
@ -61,7 +58,7 @@ function read_object_pattern(parser) {
}
if (parser.eat('...')) {
parser.allow_whitespace();
parser.advance();
const argument = parser.read_identifier();
properties.push({
@ -72,7 +69,7 @@ function read_object_pattern(parser) {
loc: get_loc(start, argument.end)
});
parser.allow_whitespace();
parser.advance();
break;
}
@ -96,17 +93,17 @@ function read_object_pattern(parser) {
kind: 'init'
};
parser.allow_whitespace();
parser.advance();
if (parser.eat(':', computed)) {
property.value = read_pattern(parser);
property.shorthand = false;
}
parser.allow_whitespace();
parser.advance();
if (parser.eat('=')) {
parser.allow_whitespace();
parser.advance();
const start = parser.index;
let right = /** @type {Expression} */ (
@ -138,14 +135,14 @@ function read_object_pattern(parser) {
properties.push(property);
parser.allow_whitespace();
parser.advance();
if (!parser.eat(',')) {
break;
}
}
parser.allow_whitespace();
parser.advance();
parser.eat('}', true);
pattern.end = parser.index;
@ -175,7 +172,7 @@ function read_array_pattern(parser) {
};
while (true) {
parser.allow_whitespace();
parser.advance();
const start = parser.index;
@ -185,7 +182,7 @@ function read_array_pattern(parser) {
}
if (parser.eat('...')) {
parser.allow_whitespace();
parser.advance();
const argument = parser.read_identifier();
elements.push({
@ -196,17 +193,17 @@ function read_array_pattern(parser) {
loc: get_loc(start, argument.end)
});
parser.allow_whitespace();
parser.advance();
break;
}
let element = read_pattern(parser);
parser.allow_whitespace();
parser.advance();
if (parser.eat('=')) {
parser.allow_whitespace();
parser.advance();
const start = parser.index;
let right = /** @type {Expression} */ (
@ -238,14 +235,14 @@ function read_array_pattern(parser) {
elements.push(element);
parser.allow_whitespace();
parser.advance();
if (!parser.eat(',')) {
break;
}
}
parser.allow_whitespace();
parser.advance();
parser.eat(']', true);
pattern.end = parser.index;

@ -1,4 +1,5 @@
import * as e from '../../../errors.js';
import { find_end } from './find.js';
/**
* @param {string} source
@ -47,21 +48,6 @@ export function match_bracket(source, start, open, close, offset = 0) {
e.unexpected_eof(source.length);
}
/**
* @param {string} haystack
* @param {string} needle
* @param {number} start
*/
function find_end(haystack, needle, start) {
const i = haystack.indexOf(needle, start);
if (i === -1) {
e.unexpected_eof(haystack.length);
}
return i + needle.length;
}
/**
* Returns true if there are an odd number of backslashes directly before {@link index}
*

@ -0,0 +1,14 @@
/**
* @param {string} haystack
* @param {string} needle
* @param {number} start
*/
export function find_end(haystack, needle, start) {
const i = haystack.indexOf(needle, start);
if (i === -1) {
e.unexpected_eof(haystack.length);
}
return i + needle.length;
}
Loading…
Cancel
Save