1use crate::{ast, error};
4
5pub fn parse(input: &str) -> Result<ast::ArithmeticExpr, error::WordParseError> {
11 cacheable_parse(input)
12}
13
14#[cached::macros::cached(max_size = 64, key = "String", convert = r#"{ input.to_owned() }"#)]
15fn cacheable_parse(input: &str) -> Result<ast::ArithmeticExpr, error::WordParseError> {
16 tracing::debug!(target: "arithmetic", "parsing arithmetic expression: '{input}'");
17 arithmetic::full_expression(input)
18 .map_err(|e| error::WordParseError::ArithmeticExpression(e.into()))
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 x:lvalue() _ "|=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::BitwiseOr, x, Box::new(y)) }
39 x:lvalue() _ "^=" _ y:(@) { ast::ArithmeticExpr::BinaryAssignment(ast::BinaryOperator::BitwiseXor, x, Box::new(y)) }
40 x:lvalue() _ "=" _ y:(@) { ast::ArithmeticExpr::Assignment(x, Box::new(y)) }
41 --
42 x:@ _ "?" _ y:expression() _ ":" _ z:(@) { ast::ArithmeticExpr::Conditional(Box::new(x), Box::new(y), Box::new(z)) }
43 --
44 x:(@) _ "||" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::LogicalOr, Box::new(x), Box::new(y)) }
45 --
46 x:(@) _ "&&" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::LogicalAnd, Box::new(x), Box::new(y)) }
47 --
48 x:(@) _ "|" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::BitwiseOr, Box::new(x), Box::new(y)) }
49 --
50 x:(@) _ "^" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::BitwiseXor, Box::new(x), Box::new(y)) }
51 --
52 x:(@) _ "&" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::BitwiseAnd, Box::new(x), Box::new(y)) }
53 --
54 x:(@) _ "==" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Equals, Box::new(x), Box::new(y)) }
55 x:(@) _ "!=" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::NotEquals, Box::new(x), Box::new(y)) }
56 --
57 x:(@) _ "<" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::LessThan, Box::new(x), Box::new(y)) }
58 x:(@) _ ">" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::GreaterThan, Box::new(x), Box::new(y)) }
59 x:(@) _ "<=" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::LessThanOrEqualTo, Box::new(x), Box::new(y)) }
60 x:(@) _ ">=" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::GreaterThanOrEqualTo, Box::new(x), Box::new(y)) }
61 --
62 x:(@) _ "<<" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::ShiftLeft, Box::new(x), Box::new(y)) }
63 x:(@) _ ">>" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::ShiftRight, Box::new(x), Box::new(y)) }
64 --
65 x:(@) _ "+" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Add, Box::new(x), Box::new(y)) }
66 x:(@) _ "-" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Subtract, Box::new(x), Box::new(y)) }
67 --
68 x:(@) _ "*" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Multiply, Box::new(x), Box::new(y)) }
69 x:(@) _ "%" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Modulo, Box::new(x), Box::new(y)) }
70 x:(@) _ "/" _ y:@ { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Divide, Box::new(x), Box::new(y)) }
71 --
72 x:@ _ "**" _ y:(@) { ast::ArithmeticExpr::BinaryOp(ast::BinaryOperator::Power, Box::new(x), Box::new(y)) }
73 --
74 "!" _ x:(@) { ast::ArithmeticExpr::UnaryOp(ast::UnaryOperator::LogicalNot, Box::new(x)) }
75 "~" _ x:(@) { ast::ArithmeticExpr::UnaryOp(ast::UnaryOperator::BitwiseNot, Box::new(x)) }
76 --
77 "+" !("+" _ lvalue()) _ x:(@) { ast::ArithmeticExpr::UnaryOp(ast::UnaryOperator::UnaryPlus, Box::new(x)) }
81 "-" !("-" _ lvalue()) _ x:(@) { ast::ArithmeticExpr::UnaryOp(ast::UnaryOperator::UnaryMinus, Box::new(x)) }
82 --
83 "++" _ x:lvalue() { ast::ArithmeticExpr::UnaryAssignment(ast::UnaryAssignmentOperator::PrefixIncrement, x) }
84 "--" _ x:lvalue() { ast::ArithmeticExpr::UnaryAssignment(ast::UnaryAssignmentOperator::PrefixDecrement, x) }
85 --
86 x:lvalue() _ "++" { ast::ArithmeticExpr::UnaryAssignment(ast::UnaryAssignmentOperator::PostfixIncrement, x) }
87 x:lvalue() _ "--" { ast::ArithmeticExpr::UnaryAssignment(ast::UnaryAssignmentOperator::PostfixDecrement, x) }
88 --
89 n:literal_number() { ast::ArithmeticExpr::Literal(n) }
90 l:lvalue() { ast::ArithmeticExpr::Reference(l) }
91 "(" _ expr:expression() _ ")" { expr }
92 }
93
94 rule lvalue() -> ast::ArithmeticTarget =
95 name:variable_name() "[" index:expression() "]" {
96 ast::ArithmeticTarget::ArrayElement(name.to_owned(), Box::new(index))
97 } /
98 name:variable_name() {
99 ast::ArithmeticTarget::Variable(name.to_owned())
100 }
101
102 rule variable_name() -> &'input str =
103 $(['a'..='z' | 'A'..='Z' | '_'](['a'..='z' | 'A'..='Z' | '_' | '0'..='9']*))
104
105 rule _() -> () = quiet!{[' ' | '\t' | '\n' | '\r']*} {}
106
107 rule literal_number() -> i64 =
108 radix:decimal_literal() "#" s:$(['0'..='9' | 'a'..='z' | 'A'..='Z' | '@' | '_']+) {?
110 parse_shell_literal_number(s, radix.cast_unsigned())
111 } /
112 "0" ['x' | 'X'] s:$(['0'..='9' | 'a'..='f' | 'A'..='F']*) {?
114 i64::from_str_radix(s, 16).or(Err("i64"))
115 } /
116 s:$("0" ['0'..='8']*) {?
118 i64::from_str_radix(s, 8).or(Err("i64"))
119 } /
120 decimal_literal()
122
123 rule decimal_literal() -> i64 =
124 s:$(['1'..='9'] ['0'..='9']*) {?
125 s.parse::<u64>().map(|v| v.cast_signed()).or(Err("i64"))
129 }
130 }
131}
132
133fn parse_shell_literal_number(s: &str, radix: u64) -> Result<i64, &'static str> {
134 if !(2..=64).contains(&radix) {
135 return Err("invalid base");
136 }
137
138 let mut result: i64 = 0;
142
143 for ch in s.chars() {
144 let digit_val = if radix <= 36 {
145 match ch {
146 '0'..='9' => (ch as u64) - ('0' as u64),
147 'a'..='z' => (ch as u64) - ('a' as u64) + 10,
148 'A'..='Z' => (ch as u64) - ('A' as u64) + 10,
149 _ => return Err("invalid digit"),
150 }
151 } else {
152 match ch {
153 '0'..='9' => (ch as u64) - ('0' as u64),
154 'a'..='z' => (ch as u64) - ('a' as u64) + 10,
155 'A'..='Z' => (ch as u64) - ('A' as u64) + 36,
156 '@' => 62,
157 '_' => 63,
158 _ => return Err("invalid digit"),
159 }
160 };
161
162 if digit_val >= radix {
163 return Err("value too great for base");
164 }
165
166 result = result
167 .wrapping_mul(radix.cast_signed())
168 .wrapping_add(digit_val.cast_signed());
169 }
170
171 Ok(result)
172}