tweak-parser
Rich Harris 2 months ago
parent ebb1954419
commit a8d79ffbd8

@ -2,26 +2,22 @@
/** @import { Parser } from '../index.js' */
import { parse_expression_at } from '../acorn.js';
import { find_matching_bracket } from '../utils/bracket.js';
import * as e from '../../../errors.js';
/**
* @param {Parser} parser
* @param {string} [opening_token]
* @returns {Expression | undefined}
* @returns {Expression}
*/
export function get_loose_identifier(parser, opening_token) {
// Find the next } and treat it as the end of the expression
const start = parser.index;
const end = find_matching_bracket(parser.template, parser.index, opening_token ?? '{');
if (end) {
const start = parser.index;
parser.index = end;
// We don't know what the expression is and signal this by returning an empty identifier
return {
type: 'Identifier',
start,
end,
name: ''
};
}
parser.index = end;
// We don't know what the expression is and signal this by returning an empty identifier
return { type: 'Identifier', start, end, name: '' };
}
/**
@ -36,10 +32,7 @@ export default function read_expression(parser, opening_token) {
// If we are in an each loop we need the error to be thrown in cases like
// `as { y = z }` so we still throw and handle the error there
if (parser.loose) {
const expression = get_loose_identifier(parser, opening_token);
if (expression) {
return expression;
}
return get_loose_identifier(parser, opening_token);
}
throw err;

@ -12,8 +12,6 @@ import { match_bracket } from '../utils/bracket.js';
const regex_whitespace_with_closing_curly_brace = /\s*}/y;
const pointy_bois = { '<': '>' };
/** @param {Parser} parser */
export default function tag(parser) {
const start = parser.index;
@ -103,7 +101,8 @@ function open(parser) {
}
if (end <= start) {
if (parser.loose && (expression = get_loose_identifier(parser))) {
if (parser.loose) {
expression = get_loose_identifier(parser);
break;
}

@ -89,7 +89,7 @@ function count_leading_backslashes(string, search_start_index) {
* @param {string} template The string to search.
* @param {number} index The index to begin the search at.
* @param {string} open The opening bracket (ex: `'{'` will search for `'}'`).
* @returns {number | undefined} The index of the closing bracket, or undefined if not found.
* @returns {number} The index of the closing bracket
*/
export function find_matching_bracket(template, index, open) {
const close = default_brackets[open];
@ -131,7 +131,8 @@ export function find_matching_bracket(template, index, open) {
}
}
}
return undefined;
e.unexpected_eof(template.length);
}
/** @type {Record<string, string>} */
@ -141,8 +142,6 @@ const default_brackets = {
'[': ']'
};
const default_close = new Set(Object.values(default_brackets));
/**
* @param {Parser} parser
* @param {number} start

Loading…
Cancel
Save