partial broken fix for optional parameters (mostly message and reproxy commands)

This commit is contained in:
dusk 2025-11-27 00:40:53 +00:00
parent 81e0cebb8e
commit 3c59ad62bd
No known key found for this signature in database
6 changed files with 71 additions and 53 deletions

View file

@ -51,21 +51,31 @@ pub fn parse_command(
let mut matched_tokens: Vec<(Tree, (Token, TokenMatchResult, usize))> = Vec::new();
let mut filtered_tokens: Vec<Token> = Vec::new();
loop {
// println!(
// "possible: {:?}",
// local_tree
// .possible_tokens()
// .filter(|t| filtered_tokens.contains(t))
// .collect::<Vec<_>>()
// );
let next = next_token(
local_tree
.possible_tokens()
.filter(|t| !filtered_tokens.contains(t)),
&input,
current_pos,
);
// println!("next: {:?}", next);
let mut possible_tokens = local_tree
.possible_tokens()
.filter(|t| !filtered_tokens.contains(t))
// .filter(|t| {
// if !filtered_tokens.is_empty() {
// !matches!(t, Token::Parameter(param) if param.is_optional())
// } else {
// true
// }
// })
.collect::<Vec<_>>();
// sort so parameters come last
// we always want to test values first
// parameters that parse the remainder come last (otherwise they would always match)
possible_tokens.sort_by(|a, b| match (a, b) {
(Token::Parameter(param), _) if param.is_remainder() => std::cmp::Ordering::Greater,
(_, Token::Parameter(param)) if param.is_remainder() => std::cmp::Ordering::Less,
(Token::Parameter(_), Token::Parameter(_)) => std::cmp::Ordering::Equal,
(Token::Parameter(_), _) => std::cmp::Ordering::Greater,
(_, Token::Parameter(_)) => std::cmp::Ordering::Less,
_ => std::cmp::Ordering::Equal,
});
println!("possible: {:?}", possible_tokens);
let next = next_token(possible_tokens.iter().cloned(), &input, current_pos);
println!("next: {:?}", next);
match &next {
Some((found_token, result, new_pos)) => {
match &result {
@ -76,6 +86,11 @@ pub fn parse_command(
));
}
TokenMatchResult::ParameterMatchError { input: raw, msg } => {
if matches!(found_token, Token::Parameter(param) if param.is_skip())
&& possible_tokens.len() > 1
{
continue;
}
return Err(format!(
"Parameter `{raw}` in command `{prefix}{input}` could not be parsed: {msg}."
));
@ -112,6 +127,7 @@ pub fn parse_command(
.pop()
.and_then(|m| matches!(m.1, (Token::Parameter(_), _, _)).then_some(m))
{
println!("redoing previous branch: {:?}", match_next.0);
local_tree = match_tree;
filtered_tokens.push(match_next.0);
continue;

View file

@ -76,16 +76,10 @@ impl Token {
name: param.name().into(),
value: matched,
},
Err(err) => {
if param.is_skip() {
return None;
} else {
TokenMatchResult::ParameterMatchError {
input: input.into(),
msg: err,
}
}
}
Err(err) => TokenMatchResult::ParameterMatchError {
input: input.into(),
msg: err,
},
}),
// don't add a _ match here!
}

View file

@ -1,6 +1,6 @@
use ordermap::OrderMap;
use crate::{command::Command, token::Token};
use crate::{command::Command, parameter::Skip, token::Token};
#[derive(Debug, Clone)]
pub struct TreeBranch {
@ -21,7 +21,17 @@ impl TreeBranch {
pub fn register_command(&mut self, command: Command) {
let mut current_branch = self;
// iterate over tokens in command
for token in command.tokens.clone() {
for (index, token) in command.tokens.clone().into_iter().enumerate() {
// if the token is an optional parameter, register rest of the tokens to a separate branch
// this allows optional parameters to work if they are not the last token
if matches!(token, Token::Parameter(ref param) if param.is_optional())
&& index < command.tokens.len() - 1
{
current_branch.register_command(Command {
tokens: command.tokens[index + 1..].to_vec(),
..command.clone()
});
}
// recursively get or create a sub-branch for each token
current_branch = current_branch
.branches
@ -36,7 +46,7 @@ impl TreeBranch {
self.current_command.clone()
}
pub fn possible_tokens(&self) -> impl Iterator<Item = &Token> {
pub fn possible_tokens(&self) -> impl Iterator<Item = &Token> + Clone {
self.branches.keys()
}