brush_parser/
arithmetic.rs

1//! Parser for shell arithmetic expressions.
2
3use crate::ast;
4use crate::error;
5
6/// Parses a shell arithmetic expression.
7///
8/// # Arguments
9///
10/// * `input` - The arithmetic expression to parse, in string form.
11pub fn parse(input: &str) -> Result<ast::ArithmeticExpr, error::WordParseError> {
12    cacheable_parse(input.to_owned())
13}
14
15#[cached::proc_macro::cached(size = 64, result = true)]
16fn cacheable_parse(input: String) -> Result<ast::ArithmeticExpr, error::WordParseError> {
17    tracing::debug!(target: "arithmetic", "parsing arithmetic expression: '{input}'");
18    arithmetic::full_expression(input.as_str()).map_err(error::WordParseError::ArithmeticExpression)
19}
20
21peg::parser! {
22    grammar arithmetic() for str {
23        pub(crate) rule full_expression() -> ast::ArithmeticExpr =
24            ![_] { ast::ArithmeticExpr::Literal(0) } /
25            _ e:expression() _ { e }
26
27        pub(crate) rule expression() -> ast::ArithmeticExpr = precedence!{
28            x:(@) _ "," _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Comma, Box::new(x), Box::new(y)) }
29            --
30            x:lvalue() _ "*=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::Multiply, x, Box::new(y)) }
31            x:lvalue() _ "/=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::Divide, x, Box::new(y)) }
32            x:lvalue() _ "%=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::Modulo, x, Box::new(y)) }
33            x:lvalue() _ "+=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::Add, x, Box::new(y)) }
34            x:lvalue() _ "-=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::Subtract, x, Box::new(y)) }
35            x:lvalue() _ "<<=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::ShiftLeft, x, Box::new(y)) }
36            x:lvalue() _ ">>=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::ShiftRight, x, Box::new(y)) }
37            x:lvalue() _ "&=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::BitwiseAnd, x, Box::new(y)) }
38            --
39            x:lvalue() _ "=" _ y:(@) { ast::ArithmeticExpr::Assignment(x, Box::new(y)) }
40            --
41            x:@ _ "?" _ y:expression() _ ":" _ z:(@) { ast::ArithmeticExpr::Conditional(Box::new(x), Box::new(y), Box::new(z)) }
42            --
43            x:(@) _ "||" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::LogicalOr, Box::new(x), Box::new(y)) }
44            --
45            x:(@) _ "&&" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::LogicalAnd, Box::new(x), Box::new(y)) }
46            --
47            x:(@) _ "|" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::BitwiseOr, Box::new(x), Box::new(y)) }
48            --
49            x:(@) _ "^" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::BitwiseXor, Box::new(x), Box::new(y)) }
50            --
51            x:(@) _ "&" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::BitwiseAnd, Box::new(x), Box::new(y)) }
52            --
53            x:(@) _ "==" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Equals, Box::new(x), Box::new(y)) }
54            x:(@) _ "!=" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::NotEquals, Box::new(x), Box::new(y)) }
55            --
56            x:(@) _ "<" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::LessThan, Box::new(x), Box::new(y)) }
57            x:(@) _ ">" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::GreaterThan, Box::new(x), Box::new(y)) }
58            x:(@) _ "<=" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::LessThanOrEqualTo, Box::new(x), Box::new(y)) }
59            x:(@) _ ">=" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::GreaterThanOrEqualTo, Box::new(x), Box::new(y)) }
60            --
61            x:(@) _ "<<" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::ShiftLeft, Box::new(x), Box::new(y)) }
62            x:(@) _ ">>" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::ShiftRight, Box::new(x), Box::new(y)) }
63            --
64            x:(@) _ "+" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Add, Box::new(x), Box::new(y)) }
65            x:(@) _ "-" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Subtract, Box::new(x), Box::new(y)) }
66            --
67            x:(@) _ "*" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Multiply, Box::new(x), Box::new(y)) }
68            x:(@) _ "%" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Modulo, Box::new(x), Box::new(y)) }
69            x:(@) _ "/" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Divide, Box::new(x), Box::new(y)) }
70            --
71            x:@ _ "**" _ y:(@) { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Power, Box::new(x), Box::new(y)) }
72            --
73            "!" x:(@) { ast::ArithmeticExpr::UnaryOp(ast::UnaryOperator::LogicalNot, Box::new(x)) }
74            "~" x:(@) { ast::ArithmeticExpr::UnaryOp(ast::UnaryOperator::BitwiseNot, Box::new(x)) }
75            --
76            "++" x:lvalue() { ast::ArithmeticExpr::UnaryAssignment(ast::UnaryAssignmentOperator::PrefixIncrement, x) }
77            "--" x:lvalue() { ast::ArithmeticExpr::UnaryAssignment(ast::UnaryAssignmentOperator::PrefixDecrement, x) }
78            --
79            x:lvalue() "++" { ast::ArithmeticExpr::UnaryAssignment(ast::UnaryAssignmentOperator::PostfixIncrement, x) }
80            x:lvalue() "--" { ast::ArithmeticExpr::UnaryAssignment(ast::UnaryAssignmentOperator::PostfixDecrement, x) }
81            --
82            "+" x:(@) { ast::ArithmeticExpr::UnaryOp(ast::UnaryOperator::UnaryPlus, Box::new(x)) }
83            "-" x:(@) { ast::ArithmeticExpr::UnaryOp(ast::UnaryOperator::UnaryMinus, Box::new(x)) }
84            --
85            n:literal_number() { ast::ArithmeticExpr::Literal(n) }
86            l:lvalue() { ast::ArithmeticExpr::Reference(l) }
87            "(" _ expr:expression() _ ")" { expr }
88        }
89
90        rule lvalue() -> ast::ArithmeticTarget =
91            name:variable_name() "[" index:expression() "]" {
92                ast::ArithmeticTarget::ArrayElement(name.to_owned(), Box::new(index))
93            } /
94            name:variable_name() {
95                ast::ArithmeticTarget::Variable(name.to_owned())
96            }
97
98        rule variable_name() -> &'input str =
99            $(['a'..='z' | 'A'..='Z' | '_'](['a'..='z' | 'A'..='Z' | '_' | '0'..='9']*))
100
101        rule _() -> () = quiet!{[' ' | '\t' | '\n' | '\r']*} {}
102
103        rule literal_number() -> i64 =
104            // Literal with explicit radix (format: <base>#<literal>)
105            radix:decimal_literal() "#" s:$(['0'..='9' | 'a'..='z' | 'A'..='Z']+) {?
106                // TODO: Support bases larger than 36. from_str_radix can't handle that.
107                if !(2..=36).contains(&radix) {
108                    return Err("invalid base");
109                }
110
111                i64::from_str_radix(s, radix as u32).or(Err("i64"))
112            } /
113            // Hex literal
114            "0" ['x' | 'X'] s:$(['0'..='9' | 'a'..='f' | 'A'..='F']*) {?
115                i64::from_str_radix(s, 16).or(Err("i64"))
116            } /
117            // Octal literal
118            s:$("0" ['0'..='8']*) {?
119                i64::from_str_radix(s, 8).or(Err("i64"))
120            } /
121            // Decimal literal
122            decimal_literal()
123
124        rule decimal_literal() -> i64 =
125            s:$(['1'..='9'] ['0'..='9']*) {?
126                s.parse().or(Err("i64"))
127            }
128    }
129}