1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//! Parser for invariant DSL expressions.
use crate::grammar::{Grammar, Rule};
use pest::Parser;
use sentri_core::model::{BinaryOp, Expression, Invariant};
use sentri_core::Result;
/// Parser for invariant DSL.
pub struct InvariantParser;
impl InvariantParser {
/// Parse a single invariant definition.
pub fn parse_invariant(input: &str) -> Result<Invariant> {
let parsed = Grammar::parse(Rule::invariant_def, input)
.map_err(|e| sentri_core::InvarError::ConfigError(e.to_string()))?;
let invariant_rule = parsed.into_iter().next().ok_or_else(|| {
sentri_core::InvarError::ConfigError("No invariant found".to_string())
})?;
let inner = invariant_rule.into_inner();
let inner_items: Vec<_> = inner.collect();
if inner_items.is_empty() {
return Err(sentri_core::InvarError::ConfigError(
"Expected invariant name and expression".to_string(),
));
}
let name = inner_items[0].as_str().to_string();
// Parse optional layer specifications and expression
let (layers, expr_idx) = if inner_items.len() > 2 {
// Check if second item is layer specification
let second_item = &inner_items[1];
if second_item.as_rule() == Rule::layer_name
|| (second_item.as_str().starts_with('(') && second_item.as_str().contains(','))
{
// This is a layer specification, parse all layer names
let mut layer_specs = Vec::new();
for item in inner_items.iter().take(inner_items.len() - 1).skip(1) {
let item_str = item
.as_str()
.trim_matches(|c| c == '(' || c == ')' || c == ',')
.trim();
if !item_str.is_empty() {
layer_specs.push(item_str.to_string());
}
}
(layer_specs, inner_items.len() - 1)
} else {
(vec![], 1)
}
} else {
(vec![], 1)
};
let expression = Self::parse_expr(inner_items[expr_idx].clone())?;
Ok(Invariant {
name,
description: None,
expression,
severity: "medium".to_string(),
category: "general".to_string(),
is_always_true: true,
layers,
phases: vec![],
})
}
fn parse_expr(rule: pest::iterators::Pair<Rule>) -> Result<Expression> {
use pest::iterators::Pair;
fn parse_pair(pair: Pair<Rule>) -> Result<Expression> {
match pair.as_rule() {
Rule::expr
| Rule::logical_or
| Rule::logical_and
| Rule::comparison
| Rule::unary => {
let items: Vec<_> = pair.into_inner().collect();
if items.is_empty() {
return Err(sentri_core::InvarError::ConfigError(
"Expected expression".to_string(),
));
}
let mut left = parse_pair(items[0].clone())?;
let mut i = 1;
while i < items.len() {
let operator = &items[i];
i += 1;
if i >= items.len() {
return Err(sentri_core::InvarError::ConfigError(
"Expected operand after operator".to_string(),
));
}
let right = parse_pair(items[i].clone())?;
i += 1;
match operator.as_rule() {
Rule::and => {
left = Expression::Logical {
left: Box::new(left),
op: sentri_core::model::LogicalOp::And,
right: Box::new(right),
};
}
Rule::or => {
left = Expression::Logical {
left: Box::new(left),
op: sentri_core::model::LogicalOp::Or,
right: Box::new(right),
};
}
Rule::eq => {
left = Expression::BinaryOp {
left: Box::new(left),
op: BinaryOp::Eq,
right: Box::new(right),
};
}
Rule::neq => {
left = Expression::BinaryOp {
left: Box::new(left),
op: BinaryOp::Neq,
right: Box::new(right),
};
}
Rule::lt => {
left = Expression::BinaryOp {
left: Box::new(left),
op: BinaryOp::Lt,
right: Box::new(right),
};
}
Rule::gt => {
left = Expression::BinaryOp {
left: Box::new(left),
op: BinaryOp::Gt,
right: Box::new(right),
};
}
Rule::lte => {
left = Expression::BinaryOp {
left: Box::new(left),
op: BinaryOp::Lte,
right: Box::new(right),
};
}
Rule::gte => {
left = Expression::BinaryOp {
left: Box::new(left),
op: BinaryOp::Gte,
right: Box::new(right),
};
}
_ => {}
}
}
Ok(left)
}
Rule::primary => {
let mut inner = pair.into_inner();
let next = inner.next();
if let Some(inner_pair) = next {
parse_pair(inner_pair)
} else {
// Empty primary - should not happen in well-formed grammar
Err(sentri_core::InvarError::ConfigError(
"Unexpected empty primary expression".to_string(),
))
}
}
Rule::function_call => {
let items: Vec<_> = pair.into_inner().collect();
if items.is_empty() {
return Err(sentri_core::InvarError::ConfigError(
"Expected function name".to_string(),
));
}
let name = items[0].as_str().to_string();
let args: Result<Vec<_>> = items[1..]
.iter()
.map(|arg| parse_pair(arg.clone()))
.collect();
Ok(Expression::FunctionCall { name, args: args? })
}
Rule::boolean => {
let val = pair.as_str() == "true";
Ok(Expression::Boolean(val))
}
Rule::integer => {
let val = pair.as_str().parse::<i128>().map_err(|_| {
sentri_core::InvarError::ConfigError("Invalid integer".to_string())
})?;
Ok(Expression::Int(val))
}
Rule::identifier => Ok(Expression::Var(pair.as_str().to_string())),
Rule::qualified_id => {
let items: Vec<_> = pair.into_inner().collect();
if items.len() != 2 {
return Err(sentri_core::InvarError::ConfigError(
"Expected layer::identifier".to_string(),
));
}
let layer = items[0].as_str().to_string();
let var = items[1].as_str().to_string();
Ok(Expression::LayerVar { layer, var })
}
Rule::var_id => {
let mut inner = pair.into_inner();
if let Some(first) = inner.next() {
if first.as_rule() == Rule::qualified_id {
let items: Vec<_> = first.into_inner().collect();
if items.len() == 2 {
let layer = items[0].as_str().to_string();
let var = items[1].as_str().to_string();
return Ok(Expression::LayerVar { layer, var });
}
} else if first.as_rule() == Rule::simple_id {
return Ok(Expression::Var(first.as_str().to_string()));
} else {
return parse_pair(first);
}
}
Err(sentri_core::InvarError::ConfigError(
"Expected identifier or layer::identifier".to_string(),
))
}
_ => Err(sentri_core::InvarError::ConfigError(format!(
"Unexpected rule: {:?}",
pair.as_rule()
))),
}
}
parse_pair(rule)
}
}
/// Parse a complete invariant definition string.
pub fn parse_invariant(input: &str) -> Result<Invariant> {
InvariantParser::parse_invariant(input)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_simple_invariant() {
let input = r#"invariant BalancePositive { balance >= 0 }"#;
let result = parse_invariant(input);
if let Err(ref e) = result {
eprintln!("Parse error: {}", e);
}
assert!(result.is_ok());
let inv = result.unwrap();
assert_eq!(inv.name, "BalancePositive");
}
#[test]
fn test_parse_invariant_with_and() {
let input = r#"invariant MultiCondition { balance >= 0 && total_supply > 0 }"#;
let result = parse_invariant(input);
if let Err(ref e) = result {
eprintln!("Parse error: {}", e);
}
assert!(result.is_ok());
}
#[test]
fn test_invalid_invariant_no_expression() {
let input = r#"invariant Empty { }"#;
let result = parse_invariant(input);
assert!(result.is_err());
}
}