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 * 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 * @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; 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; if (!isIdentifierStart(code, true)) return null;
i += code <= 0xffff ? 1 : 2; i += code <= 0xffff ? 1 : 2;
while (i < this.template.length) { 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; if (!isIdentifierChar(code, true)) break;
i += code <= 0xffff ? 1 : 2; i += code <= 0xffff ? 1 : 2;

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

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

Loading…
Cancel
Save