|
|
@ -1,41 +1,30 @@
|
|
|
|
import full_char_code_at from './full_char_code_at.js';
|
|
|
|
const SQUARE_BRACKET_OPEN = '[';
|
|
|
|
|
|
|
|
const SQUARE_BRACKET_CLOSE = ']';
|
|
|
|
|
|
|
|
const CURLY_BRACKET_OPEN = '{';
|
|
|
|
|
|
|
|
const CURLY_BRACKET_CLOSE = '}';
|
|
|
|
|
|
|
|
const PARENTHESES_OPEN = '(';
|
|
|
|
|
|
|
|
const PARENTHESES_CLOSE = ')';
|
|
|
|
|
|
|
|
|
|
|
|
const SQUARE_BRACKET_OPEN = '['.charCodeAt(0);
|
|
|
|
/** @param {string} char */
|
|
|
|
const SQUARE_BRACKET_CLOSE = ']'.charCodeAt(0);
|
|
|
|
export function is_bracket_open(char) {
|
|
|
|
const CURLY_BRACKET_OPEN = '{'.charCodeAt(0);
|
|
|
|
return char === SQUARE_BRACKET_OPEN || char === CURLY_BRACKET_OPEN;
|
|
|
|
const CURLY_BRACKET_CLOSE = '}'.charCodeAt(0);
|
|
|
|
|
|
|
|
const PARENTHESES_OPEN = '('.charCodeAt(0);
|
|
|
|
|
|
|
|
const PARENTHESES_CLOSE = ')'.charCodeAt(0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @param {number} code */
|
|
|
|
|
|
|
|
export function is_bracket_open(code) {
|
|
|
|
|
|
|
|
return code === SQUARE_BRACKET_OPEN || code === 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 {string} open */
|
|
|
|
* @param {number} open
|
|
|
|
|
|
|
|
* @param {number} close
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function is_bracket_pair(open, close) {
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
|
|
(open === SQUARE_BRACKET_OPEN && close === SQUARE_BRACKET_CLOSE) ||
|
|
|
|
|
|
|
|
(open === CURLY_BRACKET_OPEN && close === CURLY_BRACKET_CLOSE)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @param {number} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -132,8 +121,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 = full_char_code_at(open, 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) {
|
|
|
@ -159,10 +147,10 @@ export function find_matching_bracket(template, index, open) {
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
default: {
|
|
|
|
const code = full_char_code_at(template, 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) {
|
|
|
|