mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
test: Remplaced panic in favor of ParseError
This commit is contained in:
parent
23289e66d2
commit
9fb18bb08c
1 changed files with 74 additions and 48 deletions
|
@ -11,7 +11,6 @@ use std::ffi::OsString;
|
||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
|
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
use uucore::show_error;
|
|
||||||
|
|
||||||
/// Represents one of the binary comparison operators for strings, integers, or files
|
/// Represents one of the binary comparison operators for strings, integers, or files
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
@ -40,6 +39,25 @@ pub enum Symbol {
|
||||||
None,
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents an error encountered while parsing a test expression
|
||||||
|
pub enum ParseError {
|
||||||
|
MissingArgument(OsString),
|
||||||
|
Expected(OsString),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A Result type for parsing test expressions
|
||||||
|
type ParseResult<T> = Result<T, ParseError>;
|
||||||
|
|
||||||
|
/// Display an error message for a ParseError
|
||||||
|
impl std::fmt::Display for ParseError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::Expected(s) => write!(f, "expected {}", s.to_string_lossy()),
|
||||||
|
Self::MissingArgument(s) => write!(f, "missing argument for {}", s.to_string_lossy()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Symbol {
|
impl Symbol {
|
||||||
/// Create a new Symbol from an OsString.
|
/// Create a new Symbol from an OsString.
|
||||||
///
|
///
|
||||||
|
@ -133,16 +151,10 @@ impl Parser {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Consume the next token & verify that it matches the provided value.
|
/// Consume the next token & verify that it matches the provided value.
|
||||||
///
|
fn expect(&mut self, value: &str) -> ParseResult<()> {
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the next token does not match the provided value.
|
|
||||||
///
|
|
||||||
/// TODO: remove panics and convert Parser to return error messages.
|
|
||||||
fn expect(&mut self, value: &str) {
|
|
||||||
match self.next_token() {
|
match self.next_token() {
|
||||||
Symbol::Literal(s) if s == value => (),
|
Symbol::Literal(s) if s == value => Ok(()),
|
||||||
_ => panic!("expected ‘{value}’"),
|
_ => Err(ParseError::Expected(value.into())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,25 +174,27 @@ impl Parser {
|
||||||
/// Parse an expression.
|
/// Parse an expression.
|
||||||
///
|
///
|
||||||
/// EXPR → TERM | EXPR BOOLOP EXPR
|
/// EXPR → TERM | EXPR BOOLOP EXPR
|
||||||
fn expr(&mut self) {
|
fn expr(&mut self) -> ParseResult<()> {
|
||||||
if !self.peek_is_boolop() {
|
if !self.peek_is_boolop() {
|
||||||
self.term();
|
self.term()?;
|
||||||
}
|
}
|
||||||
self.maybe_boolop();
|
self.maybe_boolop()?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a term token and possible subsequent symbols: "(", "!", UOP,
|
/// Parse a term token and possible subsequent symbols: "(", "!", UOP,
|
||||||
/// literal, or None.
|
/// literal, or None.
|
||||||
fn term(&mut self) {
|
fn term(&mut self) -> ParseResult<()> {
|
||||||
let symbol = self.next_token();
|
let symbol = self.next_token();
|
||||||
|
|
||||||
match symbol {
|
match symbol {
|
||||||
Symbol::LParen => self.lparen(),
|
Symbol::LParen => self.lparen()?,
|
||||||
Symbol::Bang => self.bang(),
|
Symbol::Bang => self.bang()?,
|
||||||
Symbol::UnaryOp(_) => self.uop(symbol),
|
Symbol::UnaryOp(_) => self.uop(symbol),
|
||||||
Symbol::None => self.stack.push(symbol),
|
Symbol::None => self.stack.push(symbol),
|
||||||
literal => self.literal(literal),
|
literal => self.literal(literal)?,
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a (possibly) parenthesized expression.
|
/// Parse a (possibly) parenthesized expression.
|
||||||
|
@ -192,7 +206,7 @@ impl Parser {
|
||||||
/// * when followed by a binary operator that is not _itself_ interpreted
|
/// * when followed by a binary operator that is not _itself_ interpreted
|
||||||
/// as a literal
|
/// as a literal
|
||||||
///
|
///
|
||||||
fn lparen(&mut self) {
|
fn lparen(&mut self) -> ParseResult<()> {
|
||||||
// Look ahead up to 3 tokens to determine if the lparen is being used
|
// Look ahead up to 3 tokens to determine if the lparen is being used
|
||||||
// as a grouping operator or should be treated as a literal string
|
// as a grouping operator or should be treated as a literal string
|
||||||
let peek3: Vec<Symbol> = self
|
let peek3: Vec<Symbol> = self
|
||||||
|
@ -204,13 +218,13 @@ impl Parser {
|
||||||
|
|
||||||
match peek3.as_slice() {
|
match peek3.as_slice() {
|
||||||
// case 1: lparen is a literal when followed by nothing
|
// case 1: lparen is a literal when followed by nothing
|
||||||
[] => self.literal(Symbol::LParen.into_literal()),
|
[] => {
|
||||||
|
self.literal(Symbol::LParen.into_literal())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
// case 2: error if end of stream is `( <any_token>`
|
// case 2: error if end of stream is `( <any_token>`
|
||||||
[symbol] => {
|
[symbol] => Err(ParseError::MissingArgument(format!("{symbol:#?}").into())),
|
||||||
show_error!("missing argument after ‘{:?}’", symbol);
|
|
||||||
std::process::exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// case 3: `( uop <any_token> )` → parenthesized unary operation;
|
// case 3: `( uop <any_token> )` → parenthesized unary operation;
|
||||||
// this case ensures we don’t get confused by `( -f ) )`
|
// this case ensures we don’t get confused by `( -f ) )`
|
||||||
|
@ -218,43 +232,49 @@ impl Parser {
|
||||||
[Symbol::UnaryOp(_), _, Symbol::Literal(s)] if s == ")" => {
|
[Symbol::UnaryOp(_), _, Symbol::Literal(s)] if s == ")" => {
|
||||||
let symbol = self.next_token();
|
let symbol = self.next_token();
|
||||||
self.uop(symbol);
|
self.uop(symbol);
|
||||||
self.expect(")");
|
self.expect(")")?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// case 4: binary comparison of literal lparen, e.g. `( != )`
|
// case 4: binary comparison of literal lparen, e.g. `( != )`
|
||||||
[Symbol::Op(_), Symbol::Literal(s)] | [Symbol::Op(_), Symbol::Literal(s), _]
|
[Symbol::Op(_), Symbol::Literal(s)] | [Symbol::Op(_), Symbol::Literal(s), _]
|
||||||
if s == ")" =>
|
if s == ")" =>
|
||||||
{
|
{
|
||||||
self.literal(Symbol::LParen.into_literal());
|
self.literal(Symbol::LParen.into_literal())?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// case 5: after handling the prior cases, any single token inside
|
// case 5: after handling the prior cases, any single token inside
|
||||||
// parentheses is a literal, e.g. `( -f )`
|
// parentheses is a literal, e.g. `( -f )`
|
||||||
[_, Symbol::Literal(s)] | [_, Symbol::Literal(s), _] if s == ")" => {
|
[_, Symbol::Literal(s)] | [_, Symbol::Literal(s), _] if s == ")" => {
|
||||||
let symbol = self.next_token();
|
let symbol = self.next_token();
|
||||||
self.literal(symbol);
|
self.literal(symbol)?;
|
||||||
self.expect(")");
|
self.expect(")")?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// case 6: two binary ops in a row, treat the first op as a literal
|
// case 6: two binary ops in a row, treat the first op as a literal
|
||||||
[Symbol::Op(_), Symbol::Op(_), _] => {
|
[Symbol::Op(_), Symbol::Op(_), _] => {
|
||||||
let symbol = self.next_token();
|
let symbol = self.next_token();
|
||||||
self.literal(symbol);
|
self.literal(symbol)?;
|
||||||
self.expect(")");
|
self.expect(")")?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// case 7: if earlier cases didn’t match, `( op <any_token>…`
|
// case 7: if earlier cases didn’t match, `( op <any_token>…`
|
||||||
// indicates binary comparison of literal lparen with
|
// indicates binary comparison of literal lparen with
|
||||||
// anything _except_ ")" (case 4)
|
// anything _except_ ")" (case 4)
|
||||||
[Symbol::Op(_), _] | [Symbol::Op(_), _, _] => {
|
[Symbol::Op(_), _] | [Symbol::Op(_), _, _] => {
|
||||||
self.literal(Symbol::LParen.into_literal());
|
self.literal(Symbol::LParen.into_literal())?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, lparen indicates the start of a parenthesized
|
// Otherwise, lparen indicates the start of a parenthesized
|
||||||
// expression
|
// expression
|
||||||
_ => {
|
_ => {
|
||||||
self.expr();
|
self.expr()?;
|
||||||
self.expect(")");
|
self.expect(")")?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -276,7 +296,7 @@ impl Parser {
|
||||||
/// * `! str BOOLOP str`: negate the entire Boolean expression
|
/// * `! str BOOLOP str`: negate the entire Boolean expression
|
||||||
/// * `! str BOOLOP EXPR BOOLOP EXPR`: negate the value of the first `str` term
|
/// * `! str BOOLOP EXPR BOOLOP EXPR`: negate the value of the first `str` term
|
||||||
///
|
///
|
||||||
fn bang(&mut self) {
|
fn bang(&mut self) -> ParseResult<()> {
|
||||||
match self.peek() {
|
match self.peek() {
|
||||||
Symbol::Op(_) | Symbol::BoolOp(_) => {
|
Symbol::Op(_) | Symbol::BoolOp(_) => {
|
||||||
// we need to peek ahead one more token to disambiguate the first
|
// we need to peek ahead one more token to disambiguate the first
|
||||||
|
@ -289,14 +309,14 @@ impl Parser {
|
||||||
Symbol::Op(_) | Symbol::None => {
|
Symbol::Op(_) | Symbol::None => {
|
||||||
// op is literal
|
// op is literal
|
||||||
let op = self.next_token().into_literal();
|
let op = self.next_token().into_literal();
|
||||||
self.literal(op);
|
self.literal(op)?;
|
||||||
self.stack.push(Symbol::Bang);
|
self.stack.push(Symbol::Bang);
|
||||||
}
|
}
|
||||||
// case 2: `<! as literal> OP str [BOOLOP EXPR]`.
|
// case 2: `<! as literal> OP str [BOOLOP EXPR]`.
|
||||||
_ => {
|
_ => {
|
||||||
// bang is literal; parsing continues with op
|
// bang is literal; parsing continues with op
|
||||||
self.literal(Symbol::Bang.into_literal());
|
self.literal(Symbol::Bang.into_literal())?;
|
||||||
self.maybe_boolop();
|
self.maybe_boolop()?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -317,32 +337,34 @@ impl Parser {
|
||||||
match peek4.as_slice() {
|
match peek4.as_slice() {
|
||||||
// we peeked ahead 4 but there were only 3 tokens left
|
// we peeked ahead 4 but there were only 3 tokens left
|
||||||
[Symbol::Literal(_), Symbol::BoolOp(_), Symbol::Literal(_)] => {
|
[Symbol::Literal(_), Symbol::BoolOp(_), Symbol::Literal(_)] => {
|
||||||
self.expr();
|
self.expr()?;
|
||||||
self.stack.push(Symbol::Bang);
|
self.stack.push(Symbol::Bang);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
self.term();
|
self.term()?;
|
||||||
self.stack.push(Symbol::Bang);
|
self.stack.push(Symbol::Bang);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Peek at the next token and parse it as a BOOLOP or string literal,
|
/// Peek at the next token and parse it as a BOOLOP or string literal,
|
||||||
/// as appropriate.
|
/// as appropriate.
|
||||||
fn maybe_boolop(&mut self) {
|
fn maybe_boolop(&mut self) -> ParseResult<()> {
|
||||||
if self.peek_is_boolop() {
|
if self.peek_is_boolop() {
|
||||||
let symbol = self.next_token();
|
let symbol = self.next_token();
|
||||||
|
|
||||||
// BoolOp by itself interpreted as Literal
|
// BoolOp by itself interpreted as Literal
|
||||||
if let Symbol::None = self.peek() {
|
if let Symbol::None = self.peek() {
|
||||||
self.literal(symbol.into_literal());
|
self.literal(symbol.into_literal())?;
|
||||||
} else {
|
} else {
|
||||||
self.boolop(symbol);
|
self.boolop(symbol)?;
|
||||||
self.maybe_boolop();
|
self.maybe_boolop()?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a Boolean expression.
|
/// Parse a Boolean expression.
|
||||||
|
@ -350,13 +372,14 @@ impl Parser {
|
||||||
/// Logical and (-a) has higher precedence than or (-o), so in an
|
/// Logical and (-a) has higher precedence than or (-o), so in an
|
||||||
/// expression like `foo -o '' -a ''`, the and subexpression is evaluated
|
/// expression like `foo -o '' -a ''`, the and subexpression is evaluated
|
||||||
/// first.
|
/// first.
|
||||||
fn boolop(&mut self, op: Symbol) {
|
fn boolop(&mut self, op: Symbol) -> ParseResult<()> {
|
||||||
if op == Symbol::BoolOp(OsString::from("-a")) {
|
if op == Symbol::BoolOp(OsString::from("-a")) {
|
||||||
self.term();
|
self.term()?;
|
||||||
} else {
|
} else {
|
||||||
self.expr();
|
self.expr()?;
|
||||||
}
|
}
|
||||||
self.stack.push(op);
|
self.stack.push(op);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a (possible) unary argument test (string length or file
|
/// Parse a (possible) unary argument test (string length or file
|
||||||
|
@ -375,7 +398,7 @@ impl Parser {
|
||||||
|
|
||||||
/// Parse a string literal, optionally followed by a comparison operator
|
/// Parse a string literal, optionally followed by a comparison operator
|
||||||
/// and a second string literal.
|
/// and a second string literal.
|
||||||
fn literal(&mut self, token: Symbol) {
|
fn literal(&mut self, token: Symbol) -> ParseResult<()> {
|
||||||
self.stack.push(token.into_literal());
|
self.stack.push(token.into_literal());
|
||||||
|
|
||||||
// EXPR → str OP str
|
// EXPR → str OP str
|
||||||
|
@ -383,18 +406,21 @@ impl Parser {
|
||||||
let op = self.next_token();
|
let op = self.next_token();
|
||||||
|
|
||||||
match self.next_token() {
|
match self.next_token() {
|
||||||
Symbol::None => panic!("missing argument after {op:?}"),
|
Symbol::None => {
|
||||||
|
return Err(ParseError::MissingArgument(format!("{op:#?}").into()));
|
||||||
|
}
|
||||||
token => self.stack.push(token.into_literal()),
|
token => self.stack.push(token.into_literal()),
|
||||||
}
|
}
|
||||||
|
|
||||||
self.stack.push(op);
|
self.stack.push(op);
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parser entry point: parse the token stream `self.tokens`, storing the
|
/// Parser entry point: parse the token stream `self.tokens`, storing the
|
||||||
/// resulting `Symbol` stack in `self.stack`.
|
/// resulting `Symbol` stack in `self.stack`.
|
||||||
fn parse(&mut self) -> Result<(), String> {
|
fn parse(&mut self) -> Result<(), String> {
|
||||||
self.expr();
|
self.expr().map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
match self.tokens.next() {
|
match self.tokens.next() {
|
||||||
Some(token) => Err(format!("extra argument {}", token.quote())),
|
Some(token) => Err(format!("extra argument {}", token.quote())),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue