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
use crate::ast::{self, Warning};
use crate::errors::*;
use crate::highlighter::{Dumb as DumbHighlighter, Highlighter};
use crate::lexer;
use crate::pos::Range;
use crate::prelude::*;
use rental::rental;
use std::boxed::Box;
use std::io::Write;
use std::sync::Arc;
#[derive(Debug, PartialEq, PartialOrd, Eq, Clone)]
pub struct StmtRentalWrapper {
pub stmt: Arc<rentals::Stmt>,
}
impl StmtRentalWrapper {
pub fn suffix(&self) -> &ast::Stmt {
self.stmt.suffix()
}
}
rental! {
mod rentals {
use crate::ast;
use std::borrow::Cow;
use serde::Serialize;
use std::sync::Arc;
#[rental_mut(covariant,debug)]
pub struct Query {
script: Box<String>,
query: ast::Query<'script>,
}
#[rental(covariant,debug)]
pub struct Stmt {
query: Arc<super::Query>,
stmt: ast::Stmt<'query>,
}
}
}
pub use rentals::Query as QueryRental;
pub use rentals::Stmt as StmtRental;
#[cfg_attr(tarpaulin, skip)]
impl PartialEq for rentals::Stmt {
fn eq(&self, other: &Self) -> bool {
self.suffix() == other.suffix()
}
}
impl Eq for rentals::Stmt {}
#[cfg_attr(tarpaulin, skip)]
impl PartialOrd for rentals::Stmt {
fn partial_cmp(&self, _other: &Self) -> Option<std::cmp::Ordering> {
None
}
}
#[derive(Debug, Clone)]
pub struct Query {
pub query: Arc<rentals::Query>,
pub source: String,
pub warnings: Vec<Warning>,
pub locals: usize,
}
impl<'run, 'event, 'script> Query
where
'script: 'event,
'event: 'run,
{
pub fn suffix(&self) -> &ast::Query {
self.query.suffix()
}
pub fn parse(script: &'script str, reg: &Registry, aggr_reg: &AggrRegistry) -> Result<Self> {
let mut source = script.to_string();
let mut warnings = vec![];
let mut locals = 0;
source.push('\n');
let query = rentals::Query::try_new(Box::new(source.clone()), |src| {
let lexemes: Result<Vec<_>> = lexer::Tokenizer::new(src.as_str()).collect();
let mut filtered_tokens = Vec::new();
for t in lexemes? {
let keep = !t.value.is_ignorable();
if keep {
filtered_tokens.push(Ok(t));
}
}
let (script, local_count, ws) = crate::parser::g::QueryParser::new()
.parse(filtered_tokens)?
.up_script(reg, aggr_reg)?;
warnings = ws;
locals = local_count;
Ok(script)
})
.map_err(|e: rental::RentalError<Error, Box<String>>| e.0)?;
Ok(Self {
query: Arc::new(query),
source,
locals,
warnings,
})
}
#[cfg_attr(tarpaulin, skip)]
pub fn highlight_script_with<H: Highlighter>(script: &str, h: &mut H) -> std::io::Result<()> {
let mut script = script.to_string();
script.push('\n');
let tokens: Vec<_> = lexer::Tokenizer::new(&script).collect();
h.highlight(tokens)
}
pub fn format_error_from_script<H: Highlighter>(
script: &str,
h: &mut H,
e: &Error,
) -> std::io::Result<()> {
let mut script = script.to_string();
script.push('\n');
let tokens: Vec<_> = lexer::Tokenizer::new(&script).collect();
match e.context() {
(Some(Range(start, end)), _) => {
h.highlight_runtime_error(tokens, start, end, Some(e.into()))?;
h.finalize()
}
_other => {
write!(h.get_writer(), "Error: {}", e)?;
h.finalize()
}
}
}
pub fn format_warnings_with<H: Highlighter>(&self, h: &mut H) -> std::io::Result<()> {
let mut warnings = self.warnings.clone();
warnings.sort();
warnings.dedup();
for w in &warnings {
let tokens: Vec<_> = lexer::Tokenizer::new(&self.source).collect();
h.highlight_runtime_error(tokens, w.outer.0, w.outer.1, Some(w.into()))?;
}
h.finalize()
}
pub fn format_error(&self, e: &Error) -> String {
let mut h = DumbHighlighter::default();
if self.format_error_with(&mut h, &e).is_ok() {
h.to_string()
} else {
format!("Failed to extract code for error: {}", e)
}
}
pub fn format_error_with<H: Highlighter>(&self, h: &mut H, e: &Error) -> std::io::Result<()> {
Self::format_error_from_script(&self.source, h, e)
}
}
#[cfg(test)]
mod test {
use super::*;
fn parse(query: &str) {
let reg = crate::registry();
let aggr_reg = crate::aggr_registry();
if let Err(e) = Query::parse(query, ®, &aggr_reg) {
eprintln!("{}", e);
assert!(false)
} else {
assert!(true)
}
}
#[test]
fn for_in_select() {
parse(r#"select for event of case (a, b) => b end from in into out;"#);
}
#[test]
fn script_with_args() {
parse(
r#"
define script test
with
beep = "beep"
script
{ "beep": "{args.beep}" }
end;
create script beep from test;
create script boop from test
with
beep = "boop" # override
end;
# Stream ingested data into script with default params
select event from in into beep;
# Stream ingested data into script with overridden params
select event from in into boop;
# Stream script operator synthetic events into out stream
select event from beep into out;
select event from boop into out;
"#,
)
}
}