brush_parser/
test_command.rs

1//! Parser for shell test commands.
2
3use crate::{ast, error};
4
5/// Parses a test command expression.
6///
7/// # Arguments
8///
9/// * `input` - The test command expression to parse, in string form.
10pub fn parse(input: &[String]) -> Result<ast::TestExpr, error::TestCommandParseError> {
11    let input: Vec<_> = input.iter().map(|s| s.as_str()).collect();
12
13    let expr = test_command::full_expression(input.as_slice())
14        .map_err(error::TestCommandParseError::TestCommand)?;
15
16    Ok(expr)
17}
18
19peg::parser! {
20    grammar test_command<'a>() for [&'a str] {
21        pub(crate) rule full_expression() -> ast::TestExpr =
22            end() { ast::TestExpr::False } /
23            e:one_arg_expr() end() { e } /
24            e:two_arg_expr() end()  { e } /
25            e:three_arg_expr() end()  { e } /
26            e:four_arg_expr() end()  { e } /
27            expression()
28
29        rule one_arg_expr() -> ast::TestExpr =
30            [s] { ast::TestExpr::Literal(s.to_owned()) }
31
32        rule two_arg_expr() -> ast::TestExpr =
33            ["!"] e:one_arg_expr() { ast::TestExpr::Not(Box::from(e)) } /
34            op:unary_op() [s] { ast::TestExpr::UnaryTest(op, s.to_owned()) } /
35            [_] [_] { ast::TestExpr::False }
36
37        rule three_arg_expr() -> ast::TestExpr =
38            [left] ["-a"] [right] { ast::TestExpr::And(Box::from(ast::TestExpr::Literal(left.to_owned())), Box::from(ast::TestExpr::Literal(right.to_owned()))) } /
39            [left] ["-o"] [right] { ast::TestExpr::Or(Box::from(ast::TestExpr::Literal(left.to_owned())), Box::from(ast::TestExpr::Literal(right.to_owned()))) } /
40            [left] op:binary_op() [right] { ast::TestExpr::BinaryTest(op, left.to_owned(), right.to_owned()) } /
41            ["!"] e:two_arg_expr() { ast::TestExpr::Not(Box::from(e)) } /
42            ["("] e:one_arg_expr() [")"] { e } /
43            [_] [_] [_] { ast::TestExpr::False }
44
45        rule four_arg_expr() -> ast::TestExpr =
46            ["!"] e:three_arg_expr() { ast::TestExpr::Not(Box::from(e)) }
47
48        rule expression() -> ast::TestExpr = precedence! {
49            left:(@) ["-a"] right:@ { ast::TestExpr::And(Box::from(left), Box::from(right)) }
50            left:(@) ["-o"] right:@ { ast::TestExpr::Or(Box::from(left), Box::from(right)) }
51            --
52            ["("] e:expression() [")"] { ast::TestExpr::Parenthesized(Box::from(e)) }
53            --
54            ["!"] e:@ { ast::TestExpr::Not(Box::from(e)) }
55            --
56            [left] op:binary_op() [right] { ast::TestExpr::BinaryTest(op, left.to_owned(), right.to_owned()) }
57            --
58            op:unary_op() [operand] { ast::TestExpr::UnaryTest(op, operand.to_owned()) }
59            --
60            [s] { ast::TestExpr::Literal(s.to_owned()) }
61        }
62
63        rule unary_op() -> ast::UnaryPredicate =
64            ["-a"] { ast::UnaryPredicate::FileExists } /
65            ["-b"] { ast::UnaryPredicate::FileExistsAndIsBlockSpecialFile } /
66            ["-c"] { ast::UnaryPredicate::FileExistsAndIsCharSpecialFile } /
67            ["-d"] { ast::UnaryPredicate::FileExistsAndIsDir } /
68            ["-e"] { ast::UnaryPredicate::FileExists } /
69            ["-f"] { ast::UnaryPredicate::FileExistsAndIsRegularFile } /
70            ["-g"] { ast::UnaryPredicate::FileExistsAndIsSetgid } /
71            ["-h"] { ast::UnaryPredicate::FileExistsAndIsSymlink } /
72            ["-k"] { ast::UnaryPredicate::FileExistsAndHasStickyBit } /
73            ["-n"] { ast::UnaryPredicate::StringHasNonZeroLength } /
74            ["-o"] { ast::UnaryPredicate::ShellOptionEnabled } /
75            ["-p"] { ast::UnaryPredicate::FileExistsAndIsFifo } /
76            ["-r"] { ast::UnaryPredicate::FileExistsAndIsReadable } /
77            ["-s"] { ast::UnaryPredicate::FileExistsAndIsNotZeroLength } /
78            ["-t"] { ast::UnaryPredicate::FdIsOpenTerminal } /
79            ["-u"] { ast::UnaryPredicate::FileExistsAndIsSetuid } /
80            ["-v"] { ast::UnaryPredicate::ShellVariableIsSetAndAssigned } /
81            ["-w"] { ast::UnaryPredicate::FileExistsAndIsWritable } /
82            ["-x"] { ast::UnaryPredicate::FileExistsAndIsExecutable } /
83            ["-z"] { ast::UnaryPredicate::StringHasZeroLength } /
84            ["-G"] { ast::UnaryPredicate::FileExistsAndOwnedByEffectiveGroupId } /
85            ["-L"] { ast::UnaryPredicate::FileExistsAndIsSymlink } /
86            ["-N"] { ast::UnaryPredicate::FileExistsAndModifiedSinceLastRead } /
87            ["-O"] { ast::UnaryPredicate::FileExistsAndOwnedByEffectiveUserId } /
88            ["-R"] { ast::UnaryPredicate::ShellVariableIsSetAndNameRef } /
89            ["-S"] { ast::UnaryPredicate::FileExistsAndIsSocket }
90
91        rule binary_op() -> ast::BinaryPredicate =
92            ["=="]  { ast::BinaryPredicate::StringExactlyMatchesString } /
93            ["-ef"] { ast::BinaryPredicate::FilesReferToSameDeviceAndInodeNumbers } /
94            ["-eq"] { ast::BinaryPredicate::ArithmeticEqualTo } /
95            ["-ge"] { ast::BinaryPredicate::ArithmeticGreaterThanOrEqualTo } /
96            ["-gt"] { ast::BinaryPredicate::ArithmeticGreaterThan } /
97            ["-le"] { ast::BinaryPredicate::ArithmeticLessThanOrEqualTo } /
98            ["-lt"] { ast::BinaryPredicate::ArithmeticLessThan } /
99            ["-ne"] { ast::BinaryPredicate::ArithmeticNotEqualTo } /
100            ["-nt"] { ast::BinaryPredicate::LeftFileIsNewerOrExistsWhenRightDoesNot } /
101            ["-ot"] { ast::BinaryPredicate::LeftFileIsOlderOrDoesNotExistWhenRightDoes } /
102            ["="]   { ast::BinaryPredicate::StringExactlyMatchesString } /
103            ["!="]  { ast::BinaryPredicate::StringDoesNotExactlyMatchString } /
104            ["<"]   { ast::BinaryPredicate::LeftSortsBeforeRight } /
105            [">"]   { ast::BinaryPredicate::LeftSortsAfterRight }
106
107        rule end() = ![_]
108    }
109}