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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! The CompMD CEL profile (spec ยง8.2): a parser, a type-checker that runs before evaluation, and an evaluator with a
//! work budget. Every error is an F024, with the column it is at.
//!
//! An expression is parsed once into a [`Program`], checked against the types of the variables it sees (a
//! [`Scope`]), and evaluated with their values ([`Values`]). The `md.` functions come from outside, through
//! [`FunctionTypes`] and [`Functions`]: [`crate::md`] has the spec's.
pub(crate) mod ast;
mod check;
mod eval;
mod format;
pub(crate) mod functions;
mod getters;
mod lexer;
pub(crate) mod library;
mod ops;
pub(crate) mod optionals;
mod parser;
mod patterns;
mod pure;
mod selections;
mod strings;
#[cfg(test)]
mod tests;
pub(crate) mod time;
mod types;
mod value;
mod variables;
pub use eval::BUDGET;
pub use parser::MAX_DEPTH;
pub use pure::Pure;
pub use selections::{Indexing, Selection};
pub use types::Type;
pub use value::{Key, Value};
use crate::diagnostic::{Code, Diagnostic, Stage};
use crate::line_index::LineIndex;
use std::collections::BTreeMap;
/// An error in an expression, reported as F024. Each kind has the code point it is at, from the start of the
/// expression, 0-based, and says what is wrong: where it helps, with the nearest known name or the way out.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum Error {
/// Not an expression of the profile, or nested deeper than [`MAX_DEPTH`].
#[error("column {}: {message}", at + 1)]
Syntax {
/// Where it is.
at: usize,
/// What is wrong.
message: String,
},
/// Found by the checker: an unknown name, field or function, or a type that doesn't fit.
#[error("column {}: {message}", at + 1)]
Type {
/// Where it is.
at: usize,
/// What is wrong.
message: String,
},
/// Found while evaluating: division by zero, an index out of range, an overflow, an `md.` function's error.
#[error("column {}: {message}", at + 1)]
Evaluation {
/// Where it is.
at: usize,
/// What is wrong.
message: String,
},
/// The evaluation took more than [`BUDGET`] steps.
#[error("column {}: the expression takes too much work", at + 1)]
Budget {
/// The node being evaluated when the budget ran out.
at: usize,
},
}
impl Error {
pub(crate) fn syntax(at: usize, message: impl Into<String>) -> Self {
Self::Syntax {
at,
message: message.into(),
}
}
pub(crate) fn type_error(at: usize, message: impl Into<String>) -> Self {
Self::Type {
at,
message: message.into(),
}
}
pub(crate) fn evaluation(at: usize, message: impl Into<String>) -> Self {
Self::Evaluation {
at,
message: message.into(),
}
}
/// The code point it is at, from the start of the expression, 0-based.
#[must_use]
pub fn at(&self) -> usize {
match self {
Self::Syntax { at, .. }
| Self::Type { at, .. }
| Self::Evaluation { at, .. }
| Self::Budget { at } => *at,
}
}
/// Where a document reports it, from the start of the expression: a spent budget at the expression's first
/// column (spec ยง8.2), since where a budget runs out differs between processors; any other error where it is.
#[must_use]
pub fn reported_at(&self) -> usize {
match self {
Self::Budget { .. } => 0,
other => other.at(),
}
}
/// The stage that finds it in a document (spec ยง11): validating finds parse and type errors, evaluating the rest.
pub(crate) fn stage(&self) -> Stage {
match self {
Self::Syntax { .. } | Self::Type { .. } => Stage::Validate,
Self::Evaluation { .. } | Self::Budget { .. } => Stage::Evaluate,
}
}
/// What is wrong, without the column.
#[must_use]
pub fn message(&self) -> &str {
match self {
Self::Syntax { message, .. }
| Self::Type { message, .. }
| Self::Evaluation { message, .. } => message,
Self::Budget { .. } => "the expression takes too much work",
}
}
/// The F024 a document reports for this error in `expression`, the text it was parsed from: at the error's line
/// and column in it, found by the stage that finds it in a document (validating for parse and type errors,
/// evaluating for the rest), so an expression outside a document gets the same diagnostic as one in a body.
///
/// ```
/// use compmd::cel::Program;
///
/// let error = Program::parse("1 +").expect_err("not an expression");
/// let diagnostic = error.diagnostic("1 +");
/// assert_eq!((diagnostic.code.to_string(), diagnostic.range.start.column), ("F024".into(), 4));
/// ```
#[must_use]
pub fn diagnostic(&self, expression: &str) -> Diagnostic {
let at = LineIndex::new(expression).location(self.reported_at());
Diagnostic::new(Code::F024, at, self.message()).in_stage(self.stage())
}
}
/// The profile's functions and macros: those called on a value when `method`, as `s.trim()`, and the others
/// otherwise.
pub(crate) fn function_names(method: bool) -> Vec<&'static str> {
let functions = functions::Function::ALL
.iter()
.map(|function| library::Library::definition(*function))
.filter(|definition| {
definition
.overloads
.iter()
.any(|overload| overload.receiver.is_some() == method)
})
.map(|definition| definition.name);
let macros = ast::Macro::NAMES.iter().copied().filter(|_| method);
let has = (!method).then_some("has");
functions.chain(macros).chain(has).collect()
}
/// The types of the `md.` functions, for the checker.
pub trait FunctionTypes {
/// The type `md.<name>` gives for arguments of these types.
///
/// # Errors
///
/// An unknown function, or arguments it doesn't take, reported at the call.
fn result_type(&self, name: &str, args: &[Type]) -> Result<Type, String>;
}
/// The render an expression is evaluated for: the `md.` functions, and time zone offsets for the timestamp getters.
pub trait Functions {
/// The function names, without `md.`, which tell a call of one from a field of `md`.
fn names(&self) -> &[&'static str];
/// Calls `md.<name>`.
///
/// # Errors
///
/// What went wrong, reported at the call.
fn call(&self, name: &str, args: &[Value]) -> Result<Value, String>;
/// The size of the value `md.<name>` would give for `args`, in the evaluator's steps (a string's characters, a
/// list's elements), worked out without building it, so the budget is spent before the call: for a function whose
/// result can be far larger than its arguments. `None`, the default, for one whose result is no larger than they
/// are, which is spent for once built.
fn result_size(&self, name: &str, args: &[Value]) -> Option<usize> {
let _ = (name, args);
None
}
/// The offset from UTC, in seconds east, of `zone` at `at` (seconds since 1970-01-01T00:00:00Z). `zone` is an
/// IANA name or an offset such as `"+02:00"`; `None` is `md.tz`.
///
/// # Errors
///
/// An unknown zone.
fn offset(&self, zone: Option<&str>, at: i64) -> Result<i32, String>;
}
/// The variables an expression sees, by their types: `md`, a definition's params, `each` names. Types share their
/// parts, so extending a scope is cheap.
#[derive(Clone, Debug, Default)]
pub struct Scope(BTreeMap<String, Type>);
impl Scope {
/// This scope with `name` added, or its type replaced.
#[must_use]
pub fn with(mut self, name: impl Into<String>, ty: Type) -> Self {
self.0.insert(name.into(), ty);
self
}
/// The type of `name`.
#[must_use]
pub fn get(&self, name: &str) -> Option<&Type> {
self.0.get(name)
}
/// Every name, with its type.
pub(crate) fn names(&self) -> impl Iterator<Item = &str> {
self.0.keys().map(String::as_str)
}
}
/// The variables an expression sees, by their values. Values share their parts, so extending them is cheap.
#[derive(Clone, Debug, Default)]
pub struct Values(BTreeMap<String, Value>);
impl Values {
/// These values with `name` added, or its value replaced.
#[must_use]
pub fn with(mut self, name: impl Into<String>, value: Value) -> Self {
self.0.insert(name.into(), value);
self
}
/// The value of `name`.
#[must_use]
pub fn get(&self, name: &str) -> Option<&Value> {
self.0.get(name)
}
}
/// A parsed expression: checked against the types of its variables, then evaluated with their values. Parse it
/// once, and check and evaluate it as often as needed.
#[derive(Clone, Debug, PartialEq)]
pub struct Program {
expr: ast::Expr,
}
impl Program {
/// Parses `source`, an expression without its parentheses.
///
/// # Errors
///
/// A syntax error, or nesting deeper than [`MAX_DEPTH`].
pub fn parse(source: &str) -> Result<Self, Error> {
Ok(Self {
expr: parser::parse(source)?,
})
}
/// Type-checks the program against the types of the variables in `scope` and of the `md.` functions, and gives
/// its type. Every branch is checked, also one that is never taken.
///
/// # Errors
///
/// The first type error; an unknown name, field or function comes with the nearest known one, and a `double`
/// where an `int` is needed with the way out.
pub fn check(&self, scope: &Scope, functions: &dyn FunctionTypes) -> Result<Type, Error> {
check::check(&self.expr, scope, functions, check::Profile::Document)
}
/// Evaluates the program within [`BUDGET`] steps.
///
/// # Errors
///
/// An evaluation error: division by zero, an index out of range, overflow, an error of an `md.` function, the
/// budget spent.
pub fn evaluate(&self, values: &Values, functions: &dyn Functions) -> Result<Value, Error> {
eval::evaluate(&self.expr, values, functions, BUDGET)
}
/// Each field the program selects by dot under `path`, `md.doc` say, in the order they are written: its name,
/// and where the name is in the source, from its parse, so a string, raw or triple-quoted, that looks like one
/// isn't one. A field read by index (`md.doc["title"]`) isn't selected by dot, and what a comprehension's own
/// variable of `path`'s first name selects isn't under `path`.
///
/// ```
/// use compmd::cel::Program;
///
/// let program = Program::parse("md.doc.price * (1 + md.doc.tax) + size('md.doc.x')").expect("an expression");
/// let fields: Vec<_> = program.selections("md.doc").into_iter().map(|s| (s.name, s.range)).collect();
/// assert_eq!(fields, [("price".to_string(), 7..12), ("tax".to_string(), 27..30)]);
/// ```
#[must_use]
pub fn selections(&self, path: &str) -> Vec<Selection> {
let path: Vec<&str> = path.split('.').collect();
selections::reads(&self.expr, &path).selections
}
/// Each index the program takes of `path`, `md.doc` say, in the order they are written: the key of a string
/// literal (`md.doc["title"]`), or none for a key computed when it runs (`md.doc[name]`), and where the index is.
/// An index by another literal (`[0]`) names no field and isn't one of them.
///
/// ```
/// use compmd::cel::Program;
///
/// let program = Program::parse("md.doc['due'] + md.doc[key] + md.doc.note").expect("an expression");
/// let keys: Vec<_> = program.indexings("md.doc").into_iter().map(|i| (i.key, i.at)).collect();
/// assert_eq!(keys, [(Some("due".to_string()), 7), (None, 22)]);
/// ```
#[must_use]
pub fn indexings(&self, path: &str) -> Vec<Indexing> {
let path: Vec<&str> = path.split('.').collect();
selections::reads(&self.expr, &path).indexings
}
/// The variables the program reads, each once, in the order it reads them; a comprehension's own variable is
/// left out.
pub(crate) fn variables(&self) -> Vec<String> {
variables::variables(&self.expr)
}
}
/// A parsed program is kept and evaluated on other threads: it holds nothing shared.
const _: () = {
const fn shared_across_threads<T: Send + Sync>() {}
shared_across_threads::<Program>();
};