From f5e7db5400ed63fab2c172b42b17400867b78e52 Mon Sep 17 00:00:00 2001 From: Tomas Mirchev Date: Thu, 30 Oct 2025 05:11:30 +0200 Subject: [PATCH] remove root level - simpelr --- barg.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/barg.js b/barg.js index 43e8def..59b483c 100644 --- a/barg.js +++ b/barg.js @@ -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; }