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