add validation of parentheses balance.

This commit is contained in:
trans_soup 2023-11-11 16:17:28 +01:00
parent fd1bb299d4
commit 33bc039803
2 changed files with 26 additions and 2 deletions

View File

@ -36,7 +36,15 @@ export function parse_line (line) {
};
}
const tokens = tokenize_line(line);
const tokenized_result = tokenize_line(line);
if (!tokenized_result.valid) {
return {
type: Line.Invalid,
};
}
const { tokens } = tokenized_result;
if (tokens.length === 0) {
return {

View File

@ -28,6 +28,12 @@ export function tokenize_line (line) {
}
if (is_parens_close(c)) {
if (stack.length === 0) {
return {
valid: false,
};
}
push_token();
node = stack.pop();
continue;
@ -41,8 +47,18 @@ export function tokenize_line (line) {
token += c;
}
if (stack.length > 0) {
return {
valid: false,
};
}
push_token();
return tree;
return {
valid: true,
tokens: tree,
};
}
function is_parens_open (c) {