1
Fork 0
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:
Sylvestre Ledru 2023-11-22 14:56:42 +01:00 committed by GitHub
commit af021e0d4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View file

@ -13,6 +13,7 @@
use num_bigint::BigInt; use num_bigint::BigInt;
use num_traits::Zero; use num_traits::Zero;
use onig::{Regex, RegexOptions, Syntax}; use onig::{Regex, RegexOptions, Syntax};
use uucore::display::Quotable;
use crate::tokens::Token; use crate::tokens::Token;
@ -214,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); let result = ast_from_rpn(&mut out_stack, None);
if out_stack.is_empty() { if out_stack.is_empty() {
maybe_dump_ast(&result); maybe_dump_ast(&result);
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() { 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)), 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> { ) -> 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)?; let operand = ast_from_rpn(rpn, Some(op_type))?;
operands.push(operand); operands.push(operand);
} }
operands.reverse(); operands.reverse();

View file

@ -6,6 +6,14 @@
use crate::common::util::TestScenario; use crate::common::util::TestScenario;
#[test]
fn test_no_arguments() {
new_ucmd!()
.fails()
.code_is(2)
.stderr_only("expr: missing operand\n");
}
#[test] #[test]
fn test_simple_values() { fn test_simple_values() {
// null or 0 => EXIT_VALUE == 1 // null or 0 => EXIT_VALUE == 1
@ -295,6 +303,12 @@ fn test_substr() {
#[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()