simplify further

pull/14923/head
Rich Harris 2 years ago
parent 6914cc9464
commit a930f2cf7b

@ -213,11 +213,6 @@ export class Parser {
}
}
/** @param {number} i */
code_point_at(i) {
return /** @type {number} */ (this.template.codePointAt(i));
}
/**
* Search for a regex starting at the current index and return the result if it matches
* @param {RegExp} pattern Should have a ^ anchor at the start so the regex doesn't search past the beginning, resulting in worse performance
@ -234,13 +229,13 @@ export class Parser {
let i = this.index;
const code = this.code_point_at(i);
const code = /** @type {number} */ (this.template.codePointAt(i));
if (!isIdentifierStart(code, true)) return null;
i += code <= 0xffff ? 1 : 2;
while (i < this.template.length) {
const code = this.code_point_at(i);
const code = /** @type {number} */ (this.template.codePointAt(i));
if (!isIdentifierChar(code, true)) break;
i += code <= 0xffff ? 1 : 2;

@ -2,7 +2,6 @@
/** @import { Pattern } from 'estree' */
/** @import { Parser } from '../index.js' */
// @ts-expect-error acorn type definitions are borked in the release we use
import { isIdentifierStart } from 'acorn';
import {
is_bracket_open,
is_bracket_close,
@ -22,10 +21,9 @@ export default function read_pattern(parser) {
const start = parser.index;
let i = parser.index;
const code = parser.code_point_at(i);
const name = parser.read_identifier();
if (isIdentifierStart(code, true)) {
const name = /** @type {string} */ (parser.read_identifier());
if (name !== null) {
const annotation = read_type_annotation(parser);
return {
@ -41,29 +39,29 @@ export default function read_pattern(parser) {
};
}
if (!is_bracket_open(code)) {
if (!is_bracket_open(parser.template[i])) {
e.expected_pattern(i);
}
const bracket_stack = [code];
i += code <= 0xffff ? 1 : 2;
/** @type {string[]} */
const bracket_stack = [];
while (i < parser.template.length) {
const code = parser.code_point_at(i);
if (is_bracket_open(code)) {
bracket_stack.push(code);
} else if (is_bracket_close(code)) {
const popped = /** @type {number} */ (bracket_stack.pop());
if (!is_bracket_pair(popped, code)) {
e.expected_token(i, String.fromCharCode(/** @type {number} */ (get_bracket_close(popped))));
const char = parser.template[i];
if (is_bracket_open(char)) {
bracket_stack.push(char);
} else if (is_bracket_close(char)) {
const popped = /** @type {string} */ (bracket_stack.pop());
if (!is_bracket_pair(popped, char)) {
e.expected_token(i, /** @type {string} */ (get_bracket_close(popped)));
}
if (bracket_stack.length === 0) {
i += code <= 0xffff ? 1 : 2;
i += 1;
break;
}
}
i += code <= 0xffff ? 1 : 2;
i += 1;
}
parser.index = i;

@ -1,23 +1,23 @@
const SQUARE_BRACKET_OPEN = '['.charCodeAt(0);
const SQUARE_BRACKET_CLOSE = ']'.charCodeAt(0);
const CURLY_BRACKET_OPEN = '{'.charCodeAt(0);
const CURLY_BRACKET_CLOSE = '}'.charCodeAt(0);
const PARENTHESES_OPEN = '('.charCodeAt(0);
const PARENTHESES_CLOSE = ')'.charCodeAt(0);
const SQUARE_BRACKET_OPEN = '[';
const SQUARE_BRACKET_CLOSE = ']';
const CURLY_BRACKET_OPEN = '{';
const CURLY_BRACKET_CLOSE = '}';
const PARENTHESES_OPEN = '(';
const PARENTHESES_CLOSE = ')';
/** @param {number} code */
export function is_bracket_open(code) {
return code === SQUARE_BRACKET_OPEN || code === CURLY_BRACKET_OPEN;
/** @param {string} char */
export function is_bracket_open(char) {
return char === SQUARE_BRACKET_OPEN || char === CURLY_BRACKET_OPEN;
}
/** @param {number} code */
export function is_bracket_close(code) {
return code === SQUARE_BRACKET_CLOSE || code === CURLY_BRACKET_CLOSE;
/** @param {string} char */
export function is_bracket_close(char) {
return char === SQUARE_BRACKET_CLOSE || char === CURLY_BRACKET_CLOSE;
}
/**
* @param {number} open
* @param {number} close
* @param {string} open
* @param {string} close
*/
export function is_bracket_pair(open, close) {
return (
@ -26,14 +26,16 @@ export function is_bracket_pair(open, close) {
);
}
/** @param {number} open */
/** @param {string} open */
export function get_bracket_close(open) {
if (open === SQUARE_BRACKET_OPEN) {
return SQUARE_BRACKET_CLOSE;
}
if (open === CURLY_BRACKET_OPEN) {
return CURLY_BRACKET_CLOSE;
}
if (open === PARENTHESES_OPEN) {
return PARENTHESES_CLOSE;
}
@ -130,8 +132,7 @@ function count_leading_backslashes(string, search_start_index) {
* @returns {number | undefined} The index of the closing bracket, or undefined if not found.
*/
export function find_matching_bracket(template, index, open) {
const open_code = open.charCodeAt(0);
const close_code = get_bracket_close(open_code);
const close = get_bracket_close(open);
let brackets = 1;
let i = index;
while (brackets > 0 && i < template.length) {
@ -157,10 +158,10 @@ export function find_matching_bracket(template, index, open) {
continue;
}
default: {
const code = template.codePointAt(i);
if (code === open_code) {
const char = template[i];
if (char === open) {
brackets++;
} else if (code === close_code) {
} else if (char === close) {
brackets--;
}
if (brackets === 0) {

Loading…
Cancel
Save