mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
expr: adapt error messages, revert most of #5559
This commit is contained in:
parent
2e77d99dd4
commit
c2bfb6a465
2 changed files with 26 additions and 20 deletions
|
@ -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()
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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