Skip to main content

clash_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())
19        .map_err(|e| error::WordParseError::ArithmeticExpression(e.into()))
20}
21
22peg::parser! {
23    grammar arithmetic() for str {
24        pub(crate) rule full_expression() -> ast::ArithmeticExpr =
25            ![_] { ast::ArithmeticExpr::Literal(0) } /
26            _ e:expression() _ { e }
27
28        pub(crate) rule expression() -> ast::ArithmeticExpr = precedence!{
29            x:(@) _ "," _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Comma, Box::new(x), Box::new(y)) }
30            --
31            x:lvalue() _ "*=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::Multiply, x, Box::new(y)) }
32            x:lvalue() _ "/=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::Divide, x, Box::new(y)) }
33            x:lvalue() _ "%=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::Modulo, x, Box::new(y)) }
34            x:lvalue() _ "+=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::Add, x, Box::new(y)) }
35            x:lvalue() _ "-=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::Subtract, x, Box::new(y)) }
36            x:lvalue() _ "<<=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::ShiftLeft, x, Box::new(y)) }
37            x:lvalue() _ ">>=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::ShiftRight, x, Box::new(y)) }
38            x:lvalue() _ "&=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::BitwiseAnd, x, Box::new(y)) }
39            x:lvalue() _ "|=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::BitwiseOr, x, Box::new(y)) }
40            x:lvalue() _ "^=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::BitwiseXor, x, Box::new(y)) }
41            x:lvalue() _ "=" _ y:(@) { ast::ArithmeticExpr::Assignment(x, Box::new(y)) }
42            --
43            x:@ _ "?" _ y:expression() _ ":" _ z:(@) { ast::ArithmeticExpr::Conditional(Box::new(x), Box::new(y), Box::new(z)) }
44            --
45            x:(@) _ "||" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::LogicalOr, Box::new(x), Box::new(y)) }
46            --
47            x:(@) _ "&&" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::LogicalAnd, Box::new(x), Box::new(y)) }
48            --
49            x:(@) _ "|" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::BitwiseOr, Box::new(x), Box::new(y)) }
50            --
51            x:(@) _ "^" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::BitwiseXor, Box::new(x), Box::new(y)) }
52            --
53            x:(@) _ "&" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::BitwiseAnd, Box::new(x), Box::new(y)) }
54            --
55            x:(@) _ "==" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Equals, Box::new(x), Box::new(y)) }
56            x:(@) _ "!=" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::NotEquals, Box::new(x), Box::new(y)) }
57            --
58            x:(@) _ "<" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::LessThan, Box::new(x), Box::new(y)) }
59            x:(@) _ ">" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::GreaterThan, Box::new(x), Box::new(y)) }
60            x:(@) _ "<=" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::LessThanOrEqualTo, Box::new(x), Box::new(y)) }
61            x:(@) _ ">=" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::GreaterThanOrEqualTo, Box::new(x), Box::new(y)) }
62            --
63            x:(@) _ "<<" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::ShiftLeft, Box::new(x), Box::new(y)) }
64            x:(@) _ ">>" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::ShiftRight, Box::new(x), Box::new(y)) }
65            --
66            x:(@) _ "+" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Add, Box::new(x), Box::new(y)) }
67            x:(@) _ "-" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Subtract, Box::new(x), Box::new(y)) }
68            --
69            x:(@) _ "*" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Multiply, Box::new(x), Box::new(y)) }
70            x:(@) _ "%" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Modulo, Box::new(x), Box::new(y)) }
71            x:(@) _ "/" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Divide, Box::new(x), Box::new(y)) }
72            --
73            x:@ _ "**" _ y:(@) { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Power, Box::new(x), Box::new(y)) }
74            --
75            "!" _ x:(@) { ast::ArithmeticExpr::UnaryOp(ast::UnaryOperator::LogicalNot, Box::new(x)) }
76            "~" _ x:(@) { ast::ArithmeticExpr::UnaryOp(ast::UnaryOperator::BitwiseNot, Box::new(x)) }
77            --
78            // NOTE: We add negative lookahead to avoid ambiguity with the pre-increment/pre-decrement operators.
79            "+" !['+'] _ x:(@) { ast::ArithmeticExpr::UnaryOp(ast::UnaryOperator::UnaryPlus, Box::new(x)) }
80            "-" !['-'] _ x:(@) { ast::ArithmeticExpr::UnaryOp(ast::UnaryOperator::UnaryMinus, Box::new(x)) }
81            --
82            "++" _ x:lvalue() { ast::ArithmeticExpr::UnaryAssignment(ast::UnaryAssignmentOperator::PrefixIncrement, x) }
83            "--" _ x:lvalue() { ast::ArithmeticExpr::UnaryAssignment(ast::UnaryAssignmentOperator::PrefixDecrement, x) }
84            --
85            x:lvalue() _ "++" { ast::ArithmeticExpr::UnaryAssignment(ast::UnaryAssignmentOperator::PostfixIncrement, x) }
86            x:lvalue() _ "--" { ast::ArithmeticExpr::UnaryAssignment(ast::UnaryAssignmentOperator::PostfixDecrement, x) }
87            --
88            n:literal_number() { ast::ArithmeticExpr::Literal(n) }
89            l:lvalue() { ast::ArithmeticExpr::Reference(l) }
90            "(" _ expr:expression() _ ")" { expr }
91        }
92
93        rule lvalue() -> ast::ArithmeticTarget =
94            name:variable_name() "[" index:expression() "]" {
95                ast::ArithmeticTarget::ArrayElement(name.to_owned(), Box::new(index))
96            } /
97            name:variable_name() {
98                ast::ArithmeticTarget::Variable(name.to_owned())
99            }
100
101        rule variable_name() -> &'input str =
102            $(['a'..='z' | 'A'..='Z' | '_'](['a'..='z' | 'A'..='Z' | '_' | '0'..='9']*))
103
104        rule _() -> () = quiet!{[' ' | '\t' | '\n' | '\r']*} {}
105
106        rule literal_number() -> i64 =
107            // Literal with explicit radix (format: <base>#<literal>)
108            radix:decimal_literal() "#" s:$(['0'..='9' | 'a'..='z' | 'A'..='Z' | '@' | '_']+) {?
109                parse_shell_literal_number(s, radix.cast_unsigned())
110            } /
111            // Hex literal
112            "0" ['x' | 'X'] s:$(['0'..='9' | 'a'..='f' | 'A'..='F']*) {?
113                i64::from_str_radix(s, 16).or(Err("i64"))
114            } /
115            // Octal literal
116            s:$("0" ['0'..='8']*) {?
117                i64::from_str_radix(s, 8).or(Err("i64"))
118            } /
119            // Decimal literal
120            decimal_literal()
121
122        rule decimal_literal() -> i64 =
123            s:$(['1'..='9'] ['0'..='9']*) {?
124                // Parse as u64 first, then cast to i64. This handles values like
125                // 9223372036854775808 (i64::MAX + 1) which is needed for INT64_MIN
126                // when preceded by unary minus: -(9223372036854775808) wraps to i64::MIN.
127                s.parse::<u64>().map(|v| v.cast_signed()).or(Err("i64"))
128            }
129    }
130}
131
132fn parse_shell_literal_number(s: &str, radix: u64) -> Result<i64, &'static str> {
133    if !(2..=64).contains(&radix) {
134        return Err("invalid base");
135    }
136
137    // For bases <= 36: case-insensitive (a-z and A-Z both map to 10-35)
138    // For bases > 36 (bash extension):
139    //   0-9 = 0-9, a-z = 10-35, A-Z = 36-61, @ = 62, _ = 63
140    let mut result: i64 = 0;
141
142    for ch in s.chars() {
143        let digit_val = if radix <= 36 {
144            match ch {
145                '0'..='9' => (ch as u64) - ('0' as u64),
146                'a'..='z' => (ch as u64) - ('a' as u64) + 10,
147                'A'..='Z' => (ch as u64) - ('A' as u64) + 10,
148                _ => return Err("invalid digit"),
149            }
150        } else {
151            match ch {
152                '0'..='9' => (ch as u64) - ('0' as u64),
153                'a'..='z' => (ch as u64) - ('a' as u64) + 10,
154                'A'..='Z' => (ch as u64) - ('A' as u64) + 36,
155                '@' => 62,
156                '_' => 63,
157                _ => return Err("invalid digit"),
158            }
159        };
160
161        if digit_val >= radix {
162            return Err("value too great for base");
163        }
164
165        result = result
166            .wrapping_mul(radix.cast_signed())
167            .wrapping_add(digit_val.cast_signed());
168    }
169
170    Ok(result)
171}