mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 19:17:43 +00:00
test: fmt + requested change
This commit is contained in:
parent
bf94e8fff1
commit
11fd56c2be
3 changed files with 17 additions and 25 deletions
|
@ -29,14 +29,7 @@ impl std::fmt::Display for ParseError {
|
||||||
/// Implement UError trait for ParseError to make it easier to return useful error codes from main().
|
/// Implement UError trait for ParseError to make it easier to return useful error codes from main().
|
||||||
impl uucore::error::UError for ParseError {
|
impl uucore::error::UError for ParseError {
|
||||||
fn code(&self) -> i32 {
|
fn code(&self) -> i32 {
|
||||||
match self {
|
2
|
||||||
Self::Expected(_) => 2,
|
|
||||||
Self::MissingArgument(_) => 2,
|
|
||||||
Self::ExtraArgument(_) => 2,
|
|
||||||
Self::UnknownOperator(_) => 2,
|
|
||||||
Self::ExpectedValue => 2,
|
|
||||||
Self::InvalidInteger(_) => 2,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,13 +7,12 @@
|
||||||
|
|
||||||
// spell-checker:ignore (grammar) BOOLOP STRLEN FILETEST FILEOP INTOP STRINGOP ; (vars) LParen StrlenOp
|
// spell-checker:ignore (grammar) BOOLOP STRLEN FILETEST FILEOP INTOP STRINGOP ; (vars) LParen StrlenOp
|
||||||
|
|
||||||
use std::ffi::OsString;
|
use std::ffi::{OsStr, OsString};
|
||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
|
|
||||||
use super::error::{ParseError, ParseResult};
|
use super::error::{ParseError, ParseResult};
|
||||||
|
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
use uucore::error::UResult;
|
|
||||||
|
|
||||||
/// 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)]
|
||||||
|
@ -98,16 +97,16 @@ impl std::fmt::Display for Symbol {
|
||||||
/// Format a Symbol for printing
|
/// Format a Symbol for printing
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let s = match &self {
|
let s = match &self {
|
||||||
Self::LParen => OsString::from("("),
|
Self::LParen => OsStr::new("("),
|
||||||
Self::Bang => OsString::from("!"),
|
Self::Bang => OsStr::new("!"),
|
||||||
Self::BoolOp(s)
|
Self::BoolOp(s)
|
||||||
| Self::Literal(s)
|
| Self::Literal(s)
|
||||||
| Self::Op(Operator::String(s))
|
| Self::Op(Operator::String(s))
|
||||||
| Self::Op(Operator::Int(s))
|
| Self::Op(Operator::Int(s))
|
||||||
| Self::Op(Operator::File(s))
|
| Self::Op(Operator::File(s))
|
||||||
| Self::UnaryOp(UnaryOperator::StrlenOp(s))
|
| Self::UnaryOp(UnaryOperator::StrlenOp(s))
|
||||||
| Self::UnaryOp(UnaryOperator::FiletestOp(s)) => s.clone(),
|
| Self::UnaryOp(UnaryOperator::FiletestOp(s)) => OsStr::new(s),
|
||||||
Self::None => OsString::from("None"),
|
Self::None => OsStr::new("None"),
|
||||||
};
|
};
|
||||||
write!(f, "{}", s.quote())
|
write!(f, "{}", s.quote())
|
||||||
}
|
}
|
||||||
|
@ -159,7 +158,7 @@ impl Parser {
|
||||||
fn expect(&mut self, value: &str) -> ParseResult<()> {
|
fn expect(&mut self, value: &str) -> ParseResult<()> {
|
||||||
match self.next_token() {
|
match self.next_token() {
|
||||||
Symbol::Literal(s) if s == value => Ok(()),
|
Symbol::Literal(s) if s == value => Ok(()),
|
||||||
_ => Err(ParseError::Expected(value.into())),
|
_ => Err(ParseError::Expected(value.quote().to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -424,11 +423,11 @@ impl Parser {
|
||||||
|
|
||||||
/// 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) -> UResult<()> {
|
fn parse(&mut self) -> ParseResult<()> {
|
||||||
self.expr()?;
|
self.expr()?;
|
||||||
|
|
||||||
match self.tokens.next() {
|
match self.tokens.next() {
|
||||||
Some(token) => Err(ParseError::ExtraArgument(token.quote().to_string()).into()),
|
Some(token) => Err(ParseError::ExtraArgument(token.quote().to_string())),
|
||||||
None => Ok(()),
|
None => Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -436,7 +435,7 @@ impl Parser {
|
||||||
|
|
||||||
/// Parse the token stream `args`, returning a `Symbol` stack representing the
|
/// Parse the token stream `args`, returning a `Symbol` stack representing the
|
||||||
/// operations to perform in postfix order.
|
/// operations to perform in postfix order.
|
||||||
pub fn parse(args: Vec<OsString>) -> UResult<Vec<Symbol>> {
|
pub fn parse(args: Vec<OsString>) -> ParseResult<Vec<Symbol>> {
|
||||||
let mut p = Parser::new(args);
|
let mut p = Parser::new(args);
|
||||||
p.parse()?;
|
p.parse()?;
|
||||||
Ok(p.stack)
|
Ok(p.stack)
|
||||||
|
|
|
@ -12,6 +12,7 @@ pub(crate) mod error;
|
||||||
mod parser;
|
mod parser;
|
||||||
|
|
||||||
use clap::{crate_version, Command};
|
use clap::{crate_version, Command};
|
||||||
|
use error::{ParseError, ParseResult};
|
||||||
use parser::{parse, Operator, Symbol, UnaryOperator};
|
use parser::{parse, Operator, Symbol, UnaryOperator};
|
||||||
use std::ffi::{OsStr, OsString};
|
use std::ffi::{OsStr, OsString};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
@ -20,7 +21,6 @@ use std::os::unix::fs::MetadataExt;
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
use uucore::error::{UResult, USimpleError};
|
use uucore::error::{UResult, USimpleError};
|
||||||
use uucore::format_usage;
|
use uucore::format_usage;
|
||||||
use error::{ParseError, ParseResult};
|
|
||||||
|
|
||||||
const USAGE: &str = "\
|
const USAGE: &str = "\
|
||||||
{} EXPRESSION
|
{} EXPRESSION
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue