catgirl-calculus/src/tokenize.mjs

75 lines
997 B
JavaScript

import Line from "./Line.mjs";
export function tokenize_line (line) {
const tree = [];
const stack = [];
let node = tree;
let token = "";
function push (value) { node.push(value); }
function push_token () {
if (token.length > 0) {
push(token);
token = "";
}
}
const chars = line.split("");
for (const c of chars) {
if (is_parens_open(c)) {
push_token();
stack.push(node);
node.push([]);
node = node[node.length - 1];
continue;
}
if (is_parens_close(c)) {
if (stack.length === 0) {
return {
valid: false,
};
}
push_token();
node = stack.pop();
continue;
}
if (is_whitespace(c)) {
push_token();
continue;
}
token += c;
}
if (stack.length > 0) {
return {
valid: false,
};
}
push_token();
return {
valid: true,
tokens: tree,
};
}
function is_parens_open (c) {
return c === "(";
}
function is_parens_close (c) {
return c === ")";
}
function is_whitespace (c) {
return /\s/.test(c);
}