tweak-parser
Rich Harris 3 months ago
parent 2459947858
commit 37e79aec96

@ -1,18 +1,17 @@
/** @import { Expression } from 'estree' */
/** @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';
import { match_bracket } from '../utils/bracket.js';
/**
* @param {Parser} parser
* @param {string} [opening_token]
* @param {string} [open]
* @returns {Expression}
*/
export function get_loose_identifier(parser, opening_token) {
export function get_loose_identifier(parser, open = '{') {
// 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 ?? '{');
const end = match_bracket(parser.template, parser.index, open, open === '{' ? '}' : ')', 1) - 1;
parser.index = end;

@ -66,25 +66,6 @@ function count_leading_backslashes(string, search_start_index) {
return count;
}
/**
* Finds the corresponding closing bracket, ignoring brackets found inside comments, strings, or regex expressions.
* @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} The index of the closing bracket
*/
export function find_matching_bracket(template, index, open) {
const close = default_brackets[open];
return match_bracket(template, index, open, close, 1) - 1;
}
/** @type {Record<string, string>} */
const default_brackets = {
'{': '}',
'(': ')',
'[': ']'
};
/**
* @param {string} source
* @param {number} start

Loading…
Cancel
Save