Skip to content

expr: check prefix operation #5566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/uu/expr/src/syntax_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,12 @@ fn push_token_to_either_stack(
}

Token::PrefixOp { .. } | Token::ParOpen => {
op_stack.push((token_idx, token.clone()));
Ok(())
if out_stack.is_empty() {
op_stack.push((token_idx, token.clone()));
Ok(())
} else {
Err(String::from("syntax error (operation should be prefix)"))
}
}

Token::ParClose => move_till_match_paren(out_stack, op_stack),
Expand Down
20 changes: 20 additions & 0 deletions tests/by-util/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ fn test_parenthesis() {
.args(&["(", "1", "+", "1", ")", "*", "2"])
.succeeds()
.stdout_only("4\n");

new_ucmd!()
.args(&["1", "(", ")"])
.fails()
.stderr_only("expr: syntax error (operation should be prefix)\n");
}

#[test]
Expand Down Expand Up @@ -221,6 +226,11 @@ fn test_index() {
.args(&["index", "αbcdef_f", "f"])
.succeeds()
.stdout_only("6\n");

new_ucmd!()
.args(&["αbcdef", "index", "α"])
.fails()
.stderr_only("expr: syntax error (operation should be prefix)\n");
}

#[test]
Expand All @@ -234,6 +244,11 @@ fn test_length() {
.args(&["length", "abcdef"])
.succeeds()
.stdout_only("6\n");

new_ucmd!()
.args(&["abcdef", "length"])
.fails()
.stderr_only("expr: syntax error (operation should be prefix)\n");
}

#[test]
Expand Down Expand Up @@ -271,6 +286,11 @@ fn test_substr() {
.args(&["substr", "abc", "1", "1"])
.succeeds()
.stdout_only("a\n");

new_ucmd!()
.args(&["abc", "substr", "1", "1"])
.fails()
.stderr_only("expr: syntax error (operation should be prefix)\n");
}

#[test]
Expand Down