mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
Merge pull request #5559 from pawelngei/expr-substr-error
expr: different stderr with `expr "56" "substr"`
This commit is contained in:
commit
af021e0d4f
2 changed files with 22 additions and 4 deletions
|
@ -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;
|
||||
|
||||
|
@ -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
|
||||
|
@ -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)),
|
||||
|
||||
|
@ -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();
|
||||
|
|
|
@ -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
|
||||
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue