add validation of parentheses balance.
This commit is contained in:
parent
fd1bb299d4
commit
33bc039803
|
@ -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 {
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue