forked from cel-rust/cel-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
176 lines (146 loc) · 4.36 KB
/
mod.rs
File metadata and controls
176 lines (146 loc) · 4.36 KB
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
use crate::common::value::CelVal;
use std::collections::BTreeMap;
pub mod operators;
pub struct Ast {
pub expr: IdedExpr,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub enum Expr {
#[default]
/// UnspecifiedExprKind represents an unset expression with no specified properties.
Unspecified,
/// CallKind represents a function call.
Call(CallExpr),
/// ComprehensionKind represents a comprehension expression generated by a macro.
Comprehension(Box<ComprehensionExpr>),
/// IdentKind represents a simple variable, constant, or type identifier.
Ident(String),
/// ListKind represents a list literal expression.
List(ListExpr),
/// LiteralKind represents a primitive scalar literal.
Literal(CelVal),
/// MapKind represents a map literal expression.
Map(MapExpr),
/// SelectKind represents a field selection expression.
Select(SelectExpr),
/// StructKind represents a struct literal expression.
Struct(StructExpr),
}
#[derive(Clone, Debug, PartialEq)]
pub enum EntryExpr {
StructField(StructFieldExpr),
MapEntry(MapEntryExpr),
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct IdedExpr {
pub id: u64,
pub expr: Expr,
}
#[derive(Clone, Debug, PartialEq)]
pub struct IdedEntryExpr {
pub id: u64,
pub expr: EntryExpr,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct CallExpr {
pub func_name: String,
pub target: Option<Box<IdedExpr>>,
pub args: Vec<IdedExpr>,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct SelectExpr {
pub operand: Box<IdedExpr>,
pub field: String,
pub test: bool,
/// is_extension indicates whether the field access uses protobuf extension syntax.
/// Extension fields are accessed using msg.(ext.field) syntax where the parentheses
/// indicate an extension field lookup.
pub is_extension: bool,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct StructExpr {
pub type_name: String,
pub entries: Vec<IdedEntryExpr>,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct MapExpr {
pub entries: Vec<IdedEntryExpr>,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ListExpr {
pub elements: Vec<IdedExpr>,
pub optional_indices: Vec<usize>,
}
impl ListExpr {
pub fn new(elements: Vec<IdedExpr>) -> Self {
Self::new_with_optionals(elements, Vec::default())
}
pub fn new_with_optionals(elements: Vec<IdedExpr>, optional_indices: Vec<usize>) -> Self {
Self {
elements,
optional_indices,
}
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct StructFieldExpr {
pub field: String,
pub value: IdedExpr,
pub optional: bool,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct MapEntryExpr {
pub key: IdedExpr,
pub value: IdedExpr,
pub optional: bool,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ComprehensionExpr {
pub iter_range: IdedExpr,
pub iter_var: String,
pub iter_var2: Option<String>,
pub accu_var: String,
pub accu_init: IdedExpr,
pub loop_cond: IdedExpr,
pub loop_step: IdedExpr,
pub result: IdedExpr,
}
#[derive(Debug, Default)]
pub struct SourceInfo {
offsets: BTreeMap<u64, OffsetRange>,
pub source: String,
}
impl SourceInfo {
pub fn add_offset(&mut self, id: u64, start: u32, stop: u32) {
self.offsets.insert(id, OffsetRange { start, stop });
}
pub fn offset_for(&self, id: u64) -> Option<(u32, u32)> {
self.offsets.get(&id).map(|range| (range.start, range.stop))
}
pub(crate) fn pos_for(&self, id: u64) -> Option<(isize, isize)> {
match self.offset_for(id) {
Some((start, _)) => {
let start = start as isize;
let mut offset = 0;
let mut line = 0;
for l in self.source.split_inclusive('\n') {
line += 1;
offset += l.len() as isize;
if start < offset {
return Some((line, start + (l.len() as isize) - offset + 1));
}
}
None
}
None => None,
}
}
pub fn snippet(&self, line: isize) -> Option<&str> {
self.source.lines().nth(line as usize)
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct OffsetRange {
pub start: u32,
pub stop: u32,
}