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