mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #5578 from cakebaker/expr_fix
expr: adapt error messages, revert most of #5559
This commit is contained in:
commit
fff1302bdf
3 changed files with 32 additions and 22 deletions
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
use clap::{crate_version, Arg, ArgAction, Command};
|
use clap::{crate_version, Arg, ArgAction, Command};
|
||||||
use uucore::{
|
use uucore::{
|
||||||
error::{UResult, USimpleError},
|
error::{UResult, USimpleError, UUsageError},
|
||||||
format_usage, help_about, help_section, help_usage,
|
format_usage, help_about, help_section, help_usage,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -58,6 +58,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
.map(|v| v.into_iter().map(|s| s.as_ref()).collect::<Vec<_>>())
|
.map(|v| v.into_iter().map(|s| s.as_ref()).collect::<Vec<_>>())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
if token_strings.is_empty() {
|
||||||
|
return Err(UUsageError::new(2, "missing operand"));
|
||||||
|
}
|
||||||
|
|
||||||
match process_expr(&token_strings[..]) {
|
match process_expr(&token_strings[..]) {
|
||||||
Ok(expr_result) => print_expr_ok(&expr_result),
|
Ok(expr_result) => print_expr_ok(&expr_result),
|
||||||
Err(expr_error) => Err(USimpleError::new(2, &expr_error)),
|
Err(expr_error) => Err(USimpleError::new(2, &expr_error)),
|
||||||
|
|
|
@ -215,7 +215,7 @@ pub fn tokens_to_ast(
|
||||||
assert!(op_stack.is_empty());
|
assert!(op_stack.is_empty());
|
||||||
|
|
||||||
maybe_dump_rpn(&out_stack);
|
maybe_dump_rpn(&out_stack);
|
||||||
let result = ast_from_rpn(&mut out_stack, None);
|
let result = ast_from_rpn(&mut out_stack);
|
||||||
if out_stack.is_empty() {
|
if out_stack.is_empty() {
|
||||||
maybe_dump_ast(&result);
|
maybe_dump_ast(&result);
|
||||||
result
|
result
|
||||||
|
@ -254,13 +254,9 @@ fn maybe_dump_rpn(rpn: &TokenStack) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ast_from_rpn(rpn: &mut TokenStack, op_type: Option<&str>) -> Result<Box<AstNode>, String> {
|
fn ast_from_rpn(rpn: &mut TokenStack) -> Result<Box<AstNode>, String> {
|
||||||
match rpn.pop() {
|
match rpn.pop() {
|
||||||
None => Err(match op_type {
|
None => Err("syntax error (premature end of expression)".to_owned()),
|
||||||
Some(value) => format!("syntax error: unexpected argument {}", value.quote()),
|
|
||||||
None => "missing operand".to_owned(),
|
|
||||||
}),
|
|
||||||
|
|
||||||
Some((token_idx, Token::Value { value })) => Ok(AstNode::new_leaf(token_idx, &value)),
|
Some((token_idx, Token::Value { value })) => Ok(AstNode::new_leaf(token_idx, &value)),
|
||||||
|
|
||||||
Some((token_idx, Token::InfixOp { value, .. })) => {
|
Some((token_idx, Token::InfixOp { value, .. })) => {
|
||||||
|
@ -285,7 +281,7 @@ fn maybe_ast_node(
|
||||||
) -> Result<Box<AstNode>, String> {
|
) -> Result<Box<AstNode>, String> {
|
||||||
let mut operands = Vec::with_capacity(arity);
|
let mut operands = Vec::with_capacity(arity);
|
||||||
for _ in 0..arity {
|
for _ in 0..arity {
|
||||||
let operand = ast_from_rpn(rpn, Some(op_type))?;
|
let operand = ast_from_rpn(rpn)?;
|
||||||
operands.push(operand);
|
operands.push(operand);
|
||||||
}
|
}
|
||||||
operands.reverse();
|
operands.reverse();
|
||||||
|
@ -335,12 +331,24 @@ fn push_token_to_either_stack(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Token::PrefixOp { .. } | Token::ParOpen => {
|
Token::ParOpen => {
|
||||||
if out_stack.is_empty() {
|
if out_stack.is_empty() {
|
||||||
op_stack.push((token_idx, token.clone()));
|
op_stack.push((token_idx, token.clone()));
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(String::from("syntax error (operation should be prefix)"))
|
Err("syntax error: unexpected argument '('".to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Token::PrefixOp { value, .. } => {
|
||||||
|
if out_stack.is_empty() {
|
||||||
|
op_stack.push((token_idx, token.clone()));
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(format!(
|
||||||
|
"syntax error: unexpected argument {}",
|
||||||
|
value.quote()
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ fn test_no_arguments() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.fails()
|
.fails()
|
||||||
.code_is(2)
|
.code_is(2)
|
||||||
.stderr_only("expr: missing operand\n");
|
.usage_error("missing operand");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -112,7 +112,8 @@ fn test_parenthesis() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&["1", "(", ")"])
|
.args(&["1", "(", ")"])
|
||||||
.fails()
|
.fails()
|
||||||
.stderr_only("expr: syntax error (operation should be prefix)\n");
|
.code_is(2)
|
||||||
|
.stderr_only("expr: syntax error: unexpected argument '('\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -238,7 +239,8 @@ fn test_index() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&["αbcdef", "index", "α"])
|
.args(&["αbcdef", "index", "α"])
|
||||||
.fails()
|
.fails()
|
||||||
.stderr_only("expr: syntax error (operation should be prefix)\n");
|
.code_is(2)
|
||||||
|
.stderr_only("expr: syntax error: unexpected argument 'index'\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -256,7 +258,8 @@ fn test_length() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&["abcdef", "length"])
|
.args(&["abcdef", "length"])
|
||||||
.fails()
|
.fails()
|
||||||
.stderr_only("expr: syntax error (operation should be prefix)\n");
|
.code_is(2)
|
||||||
|
.stderr_only("expr: syntax error: unexpected argument 'length'\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -298,17 +301,12 @@ fn test_substr() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&["abc", "substr", "1", "1"])
|
.args(&["abc", "substr", "1", "1"])
|
||||||
.fails()
|
.fails()
|
||||||
.stderr_only("expr: syntax error (operation should be prefix)\n");
|
.code_is(2)
|
||||||
|
.stderr_only("expr: syntax error: unexpected argument 'substr'\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_substr() {
|
fn test_invalid_substr() {
|
||||||
new_ucmd!()
|
|
||||||
.args(&["56", "substr"])
|
|
||||||
.fails()
|
|
||||||
.code_is(2)
|
|
||||||
.stderr_only("expr: syntax error: unexpected argument 'substr'\n");
|
|
||||||
|
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&["substr", "abc", "0", "1"])
|
.args(&["substr", "abc", "0", "1"])
|
||||||
.fails()
|
.fails()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue