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