-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathexpression.rs
240 lines (204 loc) · 5.55 KB
/
expression.rs
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
use crate::{Context, Span, State, TypeDef, Value};
use diagnostic::{DiagnosticError, Label, Note};
use std::fmt;
mod array;
mod block;
mod container;
mod function_argument;
mod group;
mod if_statement;
mod noop;
mod object;
mod op;
mod variable;
pub(crate) mod assignment;
pub(crate) mod function_call;
pub(crate) mod literal;
pub(crate) mod predicate;
pub(crate) mod query;
pub use array::Array;
pub use assignment::Assignment;
pub use block::Block;
pub use container::Container;
pub use function_argument::FunctionArgument;
pub use function_call::FunctionCall;
pub use group::Group;
pub use if_statement::IfStatement;
pub use literal::Literal;
pub use noop::Noop;
pub use object::Object;
pub use op::Op;
pub use predicate::Predicate;
pub use query::Query;
pub use variable::Variable;
pub type Resolved = Result<Value, ExpressionError>;
pub trait Expression: Send + Sync + fmt::Debug {
/// Resolve an expression to a concrete [`Value`].
///
/// This method is executed at runtime.
///
/// An expression is allowed to fail, which aborts the running program.
fn resolve(&self, ctx: &mut Context) -> Resolved;
/// Resolve an expression to its [`TypeDef`] type definition.
///
/// This method is executed at compile-time.
fn type_def(&self, state: &crate::State) -> TypeDef;
}
#[derive(Debug, PartialEq)]
pub enum Expr {
Literal(Literal),
Container(Container),
IfStatement(IfStatement),
Op(Op),
Assignment(Assignment),
Query(Query),
FunctionCall(FunctionCall),
Variable(Variable),
}
impl Expression for Expr {
fn resolve(&self, ctx: &mut Context) -> Resolved {
use Expr::*;
match self {
Literal(v) => v.resolve(ctx),
Container(v) => v.resolve(ctx),
IfStatement(v) => v.resolve(ctx),
Op(v) => v.resolve(ctx),
Assignment(v) => v.resolve(ctx),
Query(v) => v.resolve(ctx),
FunctionCall(v) => v.resolve(ctx),
Variable(v) => v.resolve(ctx),
}
}
fn type_def(&self, state: &State) -> TypeDef {
use Expr::*;
match self {
Literal(v) => v.type_def(state),
Container(v) => v.type_def(state),
IfStatement(v) => v.type_def(state),
Op(v) => v.type_def(state),
Assignment(v) => v.type_def(state),
Query(v) => v.type_def(state),
FunctionCall(v) => v.type_def(state),
Variable(v) => v.type_def(state),
}
}
}
impl fmt::Display for Expr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Expr::*;
match self {
Literal(v) => v.fmt(f),
Container(v) => v.fmt(f),
IfStatement(v) => v.fmt(f),
Op(v) => v.fmt(f),
Assignment(v) => v.fmt(f),
Query(v) => v.fmt(f),
FunctionCall(v) => v.fmt(f),
Variable(v) => v.fmt(f),
}
}
}
impl From<Literal> for Expr {
fn from(literal: Literal) -> Self {
Expr::Literal(literal)
}
}
impl From<Container> for Expr {
fn from(container: Container) -> Self {
Expr::Container(container)
}
}
impl From<IfStatement> for Expr {
fn from(if_statement: IfStatement) -> Self {
Expr::IfStatement(if_statement)
}
}
impl From<Op> for Expr {
fn from(op: Op) -> Self {
Expr::Op(op)
}
}
impl From<Assignment> for Expr {
fn from(assignment: Assignment) -> Self {
Expr::Assignment(assignment)
}
}
impl From<Query> for Expr {
fn from(query: Query) -> Self {
Expr::Query(query)
}
}
impl From<FunctionCall> for Expr {
fn from(function_call: FunctionCall) -> Self {
Expr::FunctionCall(function_call)
}
}
impl From<Variable> for Expr {
fn from(variable: Variable) -> Self {
Expr::Variable(variable)
}
}
// -----------------------------------------------------------------------------
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("unhandled error")]
Fallible { span: Span },
}
impl DiagnosticError for Error {
fn labels(&self) -> Vec<Label> {
use Error::*;
match self {
Fallible { span } => vec![
Label::primary("expression can result in runtime error", span),
Label::context("handle the error case to ensure runtime success", span),
],
}
}
fn notes(&self) -> Vec<Note> {
use Error::*;
match self {
Fallible { .. } => vec![Note::SeeErrorDocs],
}
}
}
// -----------------------------------------------------------------------------
#[derive(Debug, Default, PartialEq)]
pub struct ExpressionError {
pub message: String,
pub labels: Vec<Label>,
pub notes: Vec<Note>,
}
impl std::fmt::Display for ExpressionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.message.fmt(f)
}
}
impl std::error::Error for ExpressionError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
impl DiagnosticError for ExpressionError {
fn message(&self) -> String {
self.message.clone()
}
fn labels(&self) -> Vec<Label> {
self.labels.clone()
}
fn notes(&self) -> Vec<Note> {
self.notes.clone()
}
}
impl From<String> for ExpressionError {
fn from(message: String) -> Self {
ExpressionError {
message,
..Default::default()
}
}
}
impl From<&str> for ExpressionError {
fn from(message: &str) -> Self {
message.to_owned().into()
}
}