Skip to content

Commit

Permalink
Merge pull request uutils#5559 from pawelngei/expr-substr-error
Browse files Browse the repository at this point in the history
expr: different stderr with `expr "56" "substr"`
  • Loading branch information
sylvestre committed Nov 22, 2023
2 parents 2fdc782 + 8b650a7 commit af021e0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/uu/expr/src/syntax_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use num_bigint::BigInt;
use num_traits::Zero;
use onig::{Regex, RegexOptions, Syntax};
use uucore::display::Quotable;

use crate::tokens::Token;

Expand Down Expand Up @@ -214,7 +215,7 @@ pub fn tokens_to_ast(
assert!(op_stack.is_empty());

maybe_dump_rpn(&out_stack);
let result = ast_from_rpn(&mut out_stack);
let result = ast_from_rpn(&mut out_stack, None);
if out_stack.is_empty() {
maybe_dump_ast(&result);
result
Expand Down Expand Up @@ -253,9 +254,12 @@ fn maybe_dump_rpn(rpn: &TokenStack) {
}
}

fn ast_from_rpn(rpn: &mut TokenStack) -> Result<Box<AstNode>, String> {
fn ast_from_rpn(rpn: &mut TokenStack, op_type: Option<&str>) -> Result<Box<AstNode>, String> {
match rpn.pop() {
None => Err("syntax error (premature end of expression)".to_owned()),
None => Err(match op_type {
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)),

Expand All @@ -281,7 +285,7 @@ fn maybe_ast_node(
) -> Result<Box<AstNode>, String> {
let mut operands = Vec::with_capacity(arity);
for _ in 0..arity {
let operand = ast_from_rpn(rpn)?;
let operand = ast_from_rpn(rpn, Some(op_type))?;
operands.push(operand);
}
operands.reverse();
Expand Down
14 changes: 14 additions & 0 deletions tests/by-util/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

use crate::common::util::TestScenario;

#[test]
fn test_no_arguments() {
new_ucmd!()
.fails()
.code_is(2)
.stderr_only("expr: missing operand\n");
}

#[test]
fn test_simple_values() {
// null or 0 => EXIT_VALUE == 1
Expand Down Expand Up @@ -295,6 +303,12 @@ fn test_substr() {

#[test]
fn test_invalid_substr() {
new_ucmd!()
.args(&["56", "substr"])
.fails()
.code_is(2)
.stderr_only("expr: syntax error: unexpected argument 'substr'\n");

new_ucmd!()
.args(&["substr", "abc", "0", "1"])
.fails()
Expand Down

0 comments on commit af021e0

Please sign in to comment.