tremor-script 0.8.0

Tremor Script Interpreter
Documentation
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Copyright 2018-2020, Wayfair GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// We allow dead code and unused code in the main line because
// it is just a utility
// This isn't a external crate so we don't worry about docs
// #![deny(missing_docs)]
#![allow(dead_code, unused)]
#![recursion_limit = "1024"]
#![deny(
    clippy::all,
    clippy::result_unwrap_used,
    clippy::option_unwrap_used,
    clippy::unnecessary_unwrap,
    clippy::pedantic
)]
#![allow(clippy::must_use_candidate, clippy::missing_errors_doc)]

mod ast;
mod ctx;
mod datetime;
mod errors;
mod grok;
mod highlighter;
mod interpreter;
mod lexer;
mod parser;
mod path;
mod pos;
mod registry;
mod script;
mod std_lib;
mod tilde;
mod utils;
#[macro_use]
extern crate rental;

use crate::errors::{Error, ErrorKind, Result};
use crate::highlighter::{Highlighter, Term as TermHighlighter};
use crate::path::load as load_module_path;
use crate::pos::{Span, Spanned};
use crate::script::{AggrType, Return, Script};
use chrono::{Timelike, Utc};
use clap::{App, Arg};
use ctx::{EventContext, EventOriginUri};
use halfbrown::hashmap;
use simd_json::borrowed::{Object, Value};
use simd_json::prelude::*;
use std::fs::File;
use std::io::prelude::*;
use std::io::{BufReader, Read};
use std::iter::FromIterator;

#[macro_use]
extern crate serde_derive;

use crate::registry::Registry;

/// Default recursion limit
pub const RECURSION_LIMIT: u32 = 1024;
/// recursion limit
///
#[inline]
pub fn recursion_limit() -> u32 {
    RECURSION_LIMIT
}

/// Get a nanosecond timestamp
#[allow(clippy::cast_sign_loss)]
fn nanotime() -> u64 {
    let now = Utc::now();
    let seconds: u64 = now.timestamp() as u64;
    let nanoseconds: u64 = u64::from(now.nanosecond());

    (seconds * 1_000_000_000) + nanoseconds
}

#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
fn main() -> Result<()> {
    let module_path = load_module_path();

    let matches = App::new("tremor-script")
        .version(option_env!("CARGO_PKG_VERSION").unwrap_or(""))
        .about("Tremor interpreter")
        .arg(
            Arg::with_name("SCRIPT")
                .help("The script to execute")
                .required(true)
                .index(1),
        )
        .arg(
            Arg::with_name("process")
                .long("process")
                .help("Processes each line on stdin through the script"),
        )
        .arg(
            Arg::with_name("lex")
                .short("l")
                .long("lex")
                .help("Prints the lexemes"),
        )
        .arg(
            Arg::with_name("ENCODING")
                .long("encoding")
                .takes_value(true)
                .help("The codec to decode events from stdin from"),
        )
        .arg(
            Arg::with_name("event")
                .short("e")
                .takes_value(true)
                .multiple(true)
                .help("The event to load."),
        )
        .arg(
            Arg::with_name("string")
                .long("string")
                .takes_value(true)
                .help("A string to load."),
        )
        .arg(
            Arg::with_name("highlight-source")
                .short("s")
                .takes_value(false)
                .help("Prints the highlighted script."),
        )
        .arg(
            Arg::with_name("highlight-preprocess-source")
                .long("pre-process")
                .takes_value(false)
                .help("Prints the highlighted preprocessed script."),
        )
        .arg(
            Arg::with_name("print-ast")
                .short("a")
                .takes_value(false)
                .help("Prints the ast highlighted."),
        )
        .arg(
            Arg::with_name("print-ast-raw")
                .short("r")
                .takes_value(false)
                .help("Prints the ast with no highlighting."),
        )
        .arg(
            Arg::with_name("print-result-raw")
                .short("x")
                .takes_value(false)
                .help("Prints the result with no highlighting."),
        )
        .arg(
            Arg::with_name("quiet")
                .short("q")
                .takes_value(false)
                .help("Do not print the result."),
        )
        .arg(
            Arg::with_name("replay-influx")
                .takes_value(false)
                .help("Replays a file containing influx line protocol."),
        )
        .arg(
            Arg::with_name("docs")
                .short("d")
                .long("docs")
                .takes_value(true)
                .help("Prints docs for a script."),
        )
        .get_matches();

    let script_file = matches
        .value_of("SCRIPT")
        .ok_or_else(|| Error::from("No script file provided"))?;

    let mut raw = String::new();
    let mut input = File::open(&script_file)?;
    input.read_to_string(&mut raw)?;

    #[allow(unused_mut)]
    let mut reg: Registry = registry::registry();

    let mp = load_module_path();

    if matches.is_present("lex") {
        println!();
        raw.push('\n');
        let mut include_stack = lexer::IncludeStack::default();
        let cu = include_stack.push(script_file)?;
        let lexemes = if matches.is_present("highlight-preprocess-source") {
            lexer::Preprocessor::preprocess(
                &crate::path::load(),
                &script_file,
                &mut raw,
                cu,
                &mut include_stack,
            )?
        } else {
            lexer::Tokenizer::new(&raw).collect()
        };
        for l in lexemes {
            match l {
                Ok(Spanned {
                    span: Span { start, end },
                    value,
                }) => {
                    if start.line == end.line {
                        println!(
                            "{:>3}:{:3}-{:3}> {}",
                            start.line,
                            start.column,
                            end.column,
                            value.prettify()
                        )
                    } else {
                        println!(
                            "{:>3}:{:3}-{:3}:{}> {}",
                            start.line,
                            start.column,
                            end.line,
                            end.column,
                            value.prettify()
                        )
                    }
                }
                Err(e) => println!("ERR> {}", e),
            }
        }
        return Ok(());
    }

    match Script::parse(&mp, script_file, raw.clone(), &reg) {
        Ok(runnable) => {
            let mut h = TermHighlighter::new();
            runnable.format_warnings_with(&mut h)?;

            if matches.is_present("process") {
                let mut state = Value::null();
                let codec = matches.value_of("ENCODING").unwrap_or("json");

                loop {
                    let mut input = String::new();
                    match std::io::stdin().read_line(&mut input) {
                        Ok(0) => {
                            // ALLOW: main.rs
                            std::process::exit(0);
                        }
                        Ok(n) => {
                            let now = nanotime();
                            let mut event = match codec {
                                "json" => {
                                    match simd_json::to_borrowed_value(unsafe {
                                        input.as_bytes_mut()
                                    }) {
                                        Ok(v) => v,
                                        Err(e) => {
                                            eprintln!("invalid event: {}", e);
                                            continue;
                                        }
                                    }
                                }
                                "influx" => match tremor_influx::decode(input.as_str(), now) {
                                    Ok(Some(v)) => v,
                                    Ok(None) => continue,
                                    Err(e) => {
                                        eprintln!("invalid event: {}", e);
                                        continue;
                                    }
                                },

                                "string" => Value::from(input),
                                _ => {
                                    // ALLOW: main.rs
                                    std::process::exit(1);
                                }
                            };
                            let mut global_map = Value::object();
                            let r = runnable.run(
                                &EventContext::new(nanotime(), Some(EventOriginUri::default())),
                                AggrType::Tick,
                                &mut event,
                                &mut state,
                                &mut global_map,
                            );
                            match r {
                                Ok(Return::Drop) => (),
                                Ok(Return::Emit { value, port }) => {
                                    match port.unwrap_or_else(|| String::from("out")).as_str() {
                                        "error" | "stderr" => eprintln!("{}", value.encode()),
                                        _ => println!("{}", value.encode()),
                                    }
                                }
                                Ok(Return::EmitEvent { port }) => {
                                    match port.unwrap_or_else(|| String::from("out")).as_str() {
                                        "error" | "stderr" => eprintln!("{}", event.encode()),
                                        _ => println!("{}", event.encode()),
                                    }
                                }
                                Err(e) => eprintln!("error processing event: {}", e),
                            }
                        }
                        Err(error) => {
                            // ALLOW: main.rs
                            std::process::exit(1);
                        }
                    }
                }
            } else if let Some(name) = matches.value_of("docs") {
                let docs = runnable.docs();
                let consts = &docs.consts;
                let fns = &docs.fns;

                if let Some(m) = &docs.module {
                    println!("{}", m.print_with_name(name));
                }
                if !consts.is_empty() {
                    println!("## Constants");
                    for c in consts {
                        println!("{}", c.to_string())
                    }
                }

                if !fns.is_empty() {
                    println!("## Functions");
                    for f in fns {
                        println!("{}", f.to_string())
                    }
                }

                // ALLOW: main.rs
                std::process::exit(0);
            }
            if matches.is_present("highlight-source") {
                println!();
                let mut h = TermHighlighter::new();
                Script::highlight_script_with(&raw, &mut h)?;
            }
            if matches.is_present("highlight-preprocess-source") {
                println!();
                if matches.is_present("print-results-raw") {
                } else {
                    let mut h = TermHighlighter::new();
                    Script::highlight_preprocess_script_with(script_file, &raw, &mut h)?;
                }
            }

            if matches.is_present("print-ast") {
                let ast = simd_json::to_string_pretty(&runnable.script.suffix())?;
                println!();
                let mut h = TermHighlighter::new();
                Script::highlight_script_with(&ast, &mut h)?;
            }
            if matches.is_present("print-ast-raw") {
                let ast = simd_json::to_string_pretty(&runnable.script.suffix())?;
                println!();
                println!("{}", ast);
            }

            if matches.is_present("highlight-source")
                || matches.is_present("print-ast")
                || matches.is_present("print-ast-raw")
                || matches.is_present("highlight-preprocess-source")
            {
                // ALLOW: main.rs
                std::process::exit(0);
            }

            let mut inputs = Vec::new();
            let mut events = if let Some(influx_file) = matches.value_of("replay-influx") {
                let mut r = Vec::new();
                let input = File::open(&influx_file)?;
                let buff_input = BufReader::new(input);
                let lines: std::io::Result<Vec<Vec<u8>>> = buff_input
                    .lines()
                    .map(|s| s.map(String::into_bytes))
                    .collect();
                inputs = lines?;
                for i in &inputs {
                    let s = std::str::from_utf8(i)?;
                    if let Some(i) = tremor_influx::decode(s, 0)
                        .map_err(|e| ErrorKind::InvalidInfluxData(s.to_string(), e))?
                    {
                        r.push(i);
                    }
                }
                r
            } else if let Some(event_files) = matches.values_of("event") {
                let mut r = Vec::new();
                for event_file in event_files {
                    let mut bytes = Vec::new();
                    let mut input = File::open(&event_file)?;
                    input.read_to_end(&mut bytes)?;
                    inputs.push(bytes);
                }
                for i in &mut inputs {
                    r.push(simd_json::to_borrowed_value(i)?)
                }
                r
            } else if let Some(string_file) = matches.value_of("string") {
                let mut input = File::open(&string_file)?;
                let mut raw = String::new();
                input.read_to_string(&mut raw)?;
                let raw = raw.trim_end().to_string();

                vec![simd_json::borrowed::Value::from(raw)]
            } else {
                vec![simd_json::borrowed::Value::object()]
            };

            let mut global_map = Value::object();
            let mut state = Value::null();
            let mut event = events
                .pop()
                .ok_or_else(|| Error::from("At least one event needs to be specified"))?;
            for event in &mut events {
                runnable.run(
                    &EventContext::new(0, Some(EventOriginUri::default())),
                    AggrType::Tick,
                    event,
                    &mut state,
                    &mut global_map,
                )?;
            }
            let expr = runnable.run(
                &EventContext::new(0, Some(EventOriginUri::default())),
                AggrType::Emit,
                &mut event,
                &mut state,
                &mut global_map,
            );
            match expr {
                // Separate out the special case of emitting the inbound event,
                // this way we don't have to clone it on the way out and can
                // use the reference that was passed in instead.
                Ok(Return::EmitEvent { port }) => {
                    println!("Interpreter ran ok");
                    if matches.is_present("quiet") {
                    } else if matches.is_present("print-result-raw") {
                        println!(
                            "{}",
                            simd_json::to_string_pretty(&Return::Emit { value: event, port })?
                        );
                    } else {
                        let result = format!(
                            "{} ",
                            simd_json::to_string_pretty(&Return::Emit { value: event, port })?
                        );
                        let lexed_tokens: Vec<_> = lexer::Tokenizer::new(&result)
                            .filter_map(Result::ok)
                            .collect();
                        let mut h = TermHighlighter::new();
                        h.highlight(Some(script_file), &lexed_tokens)?;
                    }
                }
                // Handle the other success returns
                Ok(result) => {
                    println!("Interpreter ran ok");
                    if matches.is_present("quiet") {
                    } else if matches.is_present("print-result-raw") {
                        println!("{}", simd_json::to_string_pretty(&result)?);
                    } else {
                        let result = format!("{} ", simd_json::to_string_pretty(&result)?);
                        let lexed_tokens: Vec<_> = lexer::Tokenizer::new(&result)
                            .filter_map(Result::ok)
                            .collect();
                        let mut h = TermHighlighter::new();
                        h.highlight(Some(script_file), &lexed_tokens)?;
                    }
                }
                // Hande and print runtime errors.
                Err(e) => {
                    let mut h = TermHighlighter::new();
                    runnable.format_error_with(&mut h, &e)?;
                    // ALLOW: main.rs
                    std::process::exit(1);
                }
            }
        }
        // Handle and print compile time errors.
        Err(e) => {
            let mut h = TermHighlighter::new();
            if let Err(e) = Script::format_error_from_script(&raw, &mut h, &e) {
                eprintln!("Error: {}", e);
            };
            // ALLOW: main.rs
            std::process::exit(1);
        }
    };
    Ok(())
}