remove root level - simpelr

This commit is contained in:
Tomas Mirchev 2025-10-30 05:11:30 +02:00
parent cbea6cc4dc
commit f5e7db5400

22
barg.js
View File

@ -60,9 +60,9 @@ function parse_input(spec, input) {
const schema = {};
const alias_map = {};
let position = [null, 0];
let level = 1;
let commands = ["_"];
let position = [0];
let level = 0;
let commands = [];
function ref(...rest) {
return commands.slice(0, level).concat(rest).join(".");
@ -99,12 +99,13 @@ function parse_input(spec, input) {
// Control
commands.push(name);
level += 1;
position[level] = 0;
position.push(0);
break;
}
case "end": {
level -= 1;
commands.pop();
position.pop();
break;
}
case "argument": {
@ -159,9 +160,9 @@ function parse_input(spec, input) {
}
}
commands = ["_"];
level = 1;
position = [null, 0];
commands = [];
level = 0;
position = [0];
}
function next_token_value() {
@ -174,7 +175,7 @@ function parse_input(spec, input) {
}
generate_schema();
// log({ schema, alias_map });
log({ schema, alias_map });
while (tokens.length > 0) {
const token = take_one(tokens);
@ -182,7 +183,7 @@ function parse_input(spec, input) {
// Bubble up arguments -> Loop over all command levels starting by the latest
let found = false;
for (level = commands.length; level >= 1; level--) {
for (level = position.length - 1; level >= 0; level--) {
let entry_ref = get_entry_ref(token);
// Check for rest argument
@ -194,7 +195,7 @@ function parse_input(spec, input) {
entry_ref = get_entry_ref(token);
}
if (!entry_ref && level === commands.length && token_type === "unk") {
if (!entry_ref && level === position.length - 1 && token_type === "unk") {
// Positional arguments are valid only for the latest command
entry_ref = get_entry_ref(`pos::${position[level]}`);
if (entry_ref) {
@ -261,7 +262,6 @@ function parse_input(spec, input) {
}
}
commands.shift();
log({ commands, values });
return values;
}