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 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
//! This module contains the [RtLolaAst] data structures for the RTLola Language.
//!
//! Every node in the abstract syntax tree is assigned a unique id and has a span referencing the node's location in the specification.
use std::cell::RefCell;
use std::hash::Hash;
use serde::{Deserialize, Serialize};
mod conversion;
mod print;
use std::rc::Rc;
use num::rational::Rational64 as Rational;
use rtlola_reporting::Span;
/// The root of a RTLola specification, consisting of stream and trigger declarations.
/// Each declaration contains the id of the Ast node, a span, and declaration-specific components.
///
/// # Ast Node Kinds
/// * [Import] represents an import statement for a module.
/// * [Constant] represents a constant stream.
/// * [Input] represents an input stream.
/// * [Output] represents an output stream.
/// * [Mirror] represents mirror streams, a syntactic sugar for an output stream.
/// * [Trigger] represents a trigger declaration.
/// * [TypeDeclaration] captures a user given type declaration.
///
/// # Related Data Structures
/// * A [NodeId] is a unique identifier given to every node of the [RtLolaAst]
/// * A [Span] links an Ast node to its code location.
#[derive(Debug, Default, Clone)]
pub struct RtLolaAst {
/// The imports of additional modules
pub imports: Vec<Import>,
/// The constant stream declarations
pub constants: Vec<Rc<Constant>>,
/// The input stream declarations
pub inputs: Vec<Rc<Input>>,
/// The output stream declarations
pub outputs: Vec<Rc<Output>>,
/// The mirror stream declarations
pub mirrors: Vec<Rc<Mirror>>,
/// The trigger declarations
pub trigger: Vec<Rc<Trigger>>,
/// The user-defined type declarations
pub type_declarations: Vec<TypeDeclaration>,
/// Next highest NodeId
pub next_node_id: RefCell<NodeId>,
}
impl RtLolaAst {
/// Creates a new and empty [RtLolaAst]
pub(crate) fn empty() -> RtLolaAst {
RtLolaAst {
imports: Vec::new(),
constants: Vec::new(),
inputs: Vec::new(),
outputs: Vec::new(),
mirrors: Vec::new(),
trigger: Vec::new(),
type_declarations: Vec::new(),
next_node_id: RefCell::new(NodeId::default()),
}
}
pub(crate) fn next_id(&self) -> NodeId {
let res = *self.next_node_id.borrow();
self.next_node_id.borrow_mut().id += 1;
self.next_node_id.borrow_mut().prime_counter = 0;
res
}
/// Creates a deep clone of the Ast. I.e. clones the underlying data not the RCs.
pub fn clone_deep(&self) -> RtLolaAst {
let RtLolaAst {
imports,
constants,
inputs,
outputs,
mirrors,
trigger,
type_declarations,
next_node_id,
} = self;
RtLolaAst {
imports: imports.clone(),
constants: constants.iter().map(|c| Rc::new(c.as_ref().clone())).collect(),
inputs: inputs.iter().map(|c| Rc::new(c.as_ref().clone())).collect(),
outputs: outputs.iter().map(|c| Rc::new(c.as_ref().clone())).collect(),
mirrors: mirrors.iter().map(|c| Rc::new(c.as_ref().clone())).collect(),
trigger: trigger.iter().map(|c| Rc::new(c.as_ref().clone())).collect(),
type_declarations: type_declarations.clone(),
next_node_id: next_node_id.clone(),
}
}
}
/// An Ast node representing the import of a module, which brings additional implemented functionality to a specification.
/// The 'math' module, for example, adds pre-defined mathematical functions as the sine or cosine function.
#[derive(Debug, Clone)]
pub struct Import {
/// The name of the module
pub name: Ident,
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the module
pub span: Span,
}
/// An Ast node representing the declaration of a constant.
#[derive(Debug, Clone)]
pub struct Constant {
/// The name of the constant stream
pub name: Ident,
/// The value type of the constant stream
pub ty: Option<Type>,
/// The literal defining the constant
pub literal: Literal,
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the constant stream
pub span: Span,
}
/// An Ast node representing the declaration of an input stream.
#[derive(Debug, Clone)]
pub struct Input {
/// The name of the input stream
pub name: Ident,
/// The value type of the input stream
pub ty: Type,
/// The parameters of a parameterized input stream; The vector is empty in non-parametrized streams.
pub params: Vec<Rc<Parameter>>,
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the input stream
pub span: Span,
}
/// An Ast node representing the declaration of an output stream.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Output {
/// The name of the output stream
pub name: Ident,
/// An optional value type annotation of the output stream
pub annotated_type: Option<Type>,
/// The parameters of a parameterized output stream; The vector is empty in non-parametrized streams
pub params: Vec<Rc<Parameter>>,
/// The spawn declaration of a parameterized stream
pub spawn: Option<SpawnSpec>,
/// The eval declaration of a stream,
pub eval: Vec<EvalSpec>,
/// The close declaration of parametrized stream
pub close: Option<CloseSpec>,
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the output stream
pub span: Span,
}
/// Represents an output stream that mirrors another but filters them.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Mirror {
/// The name of the mirror stream.
pub name: Ident,
/// The condition under which values of the target will be propagated.
pub filter: Expression,
/// The stream that is supposed to be mirrored.
pub target: Ident,
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the parameter
pub span: Span,
}
/// An Ast node representing the declaration of a parameter of a parametrized stream.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Parameter {
/// The name of the parameter
pub name: Ident,
/// An optional value type annotation of the parameter
pub ty: Option<Type>,
/// The index of this parameter in the list of parameter of the respective output stream
pub param_idx: usize,
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the parameter
pub span: Span,
}
/// An Ast node representing the declaration of a spawn condition and expression of a stream.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct SpawnSpec {
/// The expression defining the parameter instances. If the stream has more than one parameter, the expression needs to return a tuple, with one element for each parameter
pub expression: Option<Expression>,
/// The pacing type describing when a new instance is created.
pub annotated_pacing: Option<Expression>,
/// An additional condition for the creation of an instance, i.e., an instance is only created if the condition is true.
pub condition: Option<Expression>,
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the invoke declaration
pub span: Span,
}
/// An Ast node representing the evaluation condition and expression of a stream
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct EvalSpec {
/// The pacing type describing when a new value is computed.
pub annotated_pacing: Option<Expression>,
/// The boolean expression defining the condition, if a stream instance is evaluated.
pub condition: Option<Expression>,
/// The evaluated expression, defining the value of the stream.
pub eval_expression: Option<Expression>,
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the extend declaration
pub span: Span,
}
/// An Ast node representing the declaration of a close condition of a stream
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct CloseSpec {
/// The boolean expression defining the condition, if a stream instance is closed.
pub condition: Expression,
/// The pacing type describing when the close condition is evaluated.
pub annotated_pacing: Option<Expression>,
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the extend declaration
pub span: Span,
}
/// An Ast node representing the declaration of a trigger
#[derive(Debug, Clone)]
pub struct Trigger {
/// The boolean expression of a trigger
pub expression: Expression,
/// The pacing type, which defines when a new value of a stream is computed.
pub annotated_pacing_type: Option<Expression>,
/// The optional trigger message, which is printed if the monitor raises the trigger
pub message: Option<String>,
/// A collection of streams which can be used in the message. Their value is printed when the trigger is fired.
pub info_streams: Vec<Ident>,
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the extend declaration
pub span: Span,
}
/// An Ast node representing the declaration of a user-defined type.
#[allow(clippy::vec_box)]
#[derive(Debug, Clone)]
pub struct TypeDeclaration {
/// The name of the new type.
pub name: Option<Ident>,
/// The components of the new type, e.g. a GPS type might consist of a type for the latitude and for the longitude
pub fields: Vec<Box<TypeDeclField>>,
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the type declaration
pub span: Span,
}
/// An Ast node representing the declaration of a field of a user-defined type.
#[derive(Debug, Clone)]
pub struct TypeDeclField {
/// The type of a field of a user-defined type
pub ty: Type,
/// The name of a field of a user-defined type
pub name: String,
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the type declaration
pub span: Span,
}
/// An Ast node representing an opening or closing parenthesis.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Parenthesis {
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the extend declaration
pub span: Span,
}
impl Parenthesis {
/// Creates a new Parenthesis
pub(crate) fn new(id: NodeId, span: Span) -> Parenthesis {
Parenthesis { id, span }
}
}
/// An Ast node representing the declaration of a value type
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
pub struct Type {
/// The kind of the type, e.g., a tuple
pub kind: TypeKind,
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the extend declaration
pub span: Span,
}
impl Type {
/// Creates a new non-recursive type like `Int` or `Bool`
pub(crate) fn new_simple(id: NodeId, name: String, span: Span) -> Type {
Type {
id,
kind: TypeKind::Simple(name),
span,
}
}
/// Creates a new tuple type
pub(crate) fn new_tuple(id: NodeId, tuple: Vec<Type>, span: Span) -> Type {
Type {
id,
kind: TypeKind::Tuple(tuple),
span,
}
}
/// Creates a new optional type
pub(crate) fn new_optional(id: NodeId, name: Type, span: Span) -> Type {
Type {
id,
kind: TypeKind::Optional(name.into()),
span,
}
}
}
/// Ast representation of the value type of a stream
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
pub enum TypeKind {
/// A simple type, e.g., `Int`
Simple(String),
/// A tuple type, e.g., `(Int32, Float32)`
Tuple(Vec<Type>),
/// An optional type, e.g., `Int?`
Optional(Box<Type>),
}
/// The Ast representation of a stream expression
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Expression {
/// The kind of the root expression, e.g., stream access
pub kind: ExpressionKind,
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the extend declaration
pub span: Span,
}
impl Expression {
/// Creates a new expression
pub(crate) fn new(id: NodeId, kind: ExpressionKind, span: Span) -> Expression {
Expression { kind, id, span }
}
}
#[allow(clippy::large_enum_variant, clippy::vec_box)]
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
/// The Ast representation of a single expression
pub enum ExpressionKind {
/// A literal, e.g., `1`, `"foo"`
Lit(Literal),
/// An identifier, e.g., `foo`
Ident(Ident),
/// Accessing a stream
StreamAccess(Box<Expression>, StreamAccessKind),
/// A default expression, e.g., `a.defaults(to: 0) `
Default(Box<Expression>, Box<Expression>),
/// An offset expression, e.g., `a.offset(by: -1)`
Offset(Box<Expression>, Offset),
/// A discrete window with a duration `duration` as an integer constant and aggregation function `aggregation`
DiscreteWindowAggregation {
/// The accesses stream
expr: Box<Expression>,
/// The duration of the window
duration: Box<Expression>,
/// Flag to mark that the window returns only a value if the complete duration has passed
wait: bool,
/// The aggregation function
aggregation: WindowOperation,
},
/// A sliding window with duration `duration` and aggregation function `aggregation`
SlidingWindowAggregation {
/// The accesses stream
expr: Box<Expression>,
/// The duration of the window
duration: Box<Expression>,
/// Flag to mark that the window returns only a value if the complete duration has passed
wait: bool,
/// The aggregation function
aggregation: WindowOperation,
},
/// A binary operation (For example: `a + b`, `a * b`)
Binary(BinOp, Box<Expression>, Box<Expression>),
/// A unary operation (For example: `!x`, `*x`)
Unary(UnOp, Box<Expression>),
/// An if-then-else expression
Ite(Box<Expression>, Box<Expression>, Box<Expression>),
/// An expression enveloped in parentheses
ParenthesizedExpression(Option<Box<Parenthesis>>, Box<Expression>, Option<Box<Parenthesis>>),
/// An expression was expected, e.g., after an operator like `*`
MissingExpression,
/// A tuple expression
Tuple(Vec<Expression>),
/// Access of a named (`obj.foo`) or unnamed (`obj.0`) struct field
Field(Box<Expression>, Ident),
/// A method call, e.g., `foo.bar(-1)`
Method(Box<Expression>, FunctionName, Vec<Type>, Vec<Expression>),
/// A function call
Function(FunctionName, Vec<Type>, Vec<Expression>),
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
/// The Ast representation of the different aggregation functions
pub enum WindowOperation {
/// Aggregation function to count the number of updated values on the accessed stream
Count,
/// Aggregation function to return the minimum
Min,
/// Aggregation function to return the minimum
Max,
/// Aggregation function to return the addition
Sum,
/// Aggregation function to return the product
Product,
/// Aggregation function to return the average
Average,
/// Aggregation function to return the integral
Integral,
/// Aggregation function to return the conjunction, i.e., the sliding window returns true iff ALL values on the accessed stream inside a window are assigned to true
Conjunction,
/// Aggregation function to return the disjunction, i.e., the sliding window returns true iff AT LEAst ONE value on the accessed stream inside a window is assigned to true
Disjunction,
/// Aggregation function to return the last value, a time bounded hold
Last,
/// Aggregation function to return the variance of all values, assumes equal probability.
Variance,
/// Aggregation function to return the covariance of all values in a tuple stream, assumes equal probability.
Covariance,
/// Aggregation function to return the standard deviation of all values, assumes equal probability.
StandardDeviation,
/// Aggregation function to return the Nth-Percentile
NthPercentile(u8),
}
/// Describes the operation used to access a stream
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum StreamAccessKind {
/// Synchronous access
Sync,
/// Hold access for *incompatible* stream types, returns previous known value
Hold,
/// Optional access, returns value if it exists, called by `.get()`
Get,
/// Boolean Typed access, returning true if the target stream received a new value at the current timestamp. Called with `.is_fresh()`.
Fresh,
}
/// Describes the operation used to access a stream with a offset
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Offset {
/// Discrete offset
Discrete(i16),
/// Real-time offset
RealTime(Rational, TimeUnit),
}
/// Supported time unit for real time expressions
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TimeUnit {
Nanosecond,
Microsecond,
Millisecond,
Second,
Minute,
Hour,
Day,
Week,
/// Note: A year is always, *always*, 365 days long.
Year,
}
/// An Ast node representing the declaration of a literal
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Literal {
/// The kind of the literal, e.g., boolean, string, numeric, ...
pub kind: LitKind,
/// The id of the node in the Ast
pub id: NodeId,
/// The span in the specification declaring the extend declaration
pub span: Span,
}
impl Literal {
/// Creates a new bool literal
pub(crate) fn new_bool(id: NodeId, val: bool, span: Span) -> Literal {
Literal {
id,
kind: LitKind::Bool(val),
span,
}
}
/// Creates a new numeric literal
pub(crate) fn new_numeric(id: NodeId, val: &str, unit: Option<String>, span: Span) -> Literal {
Literal {
id,
kind: LitKind::Numeric(val.to_string(), unit),
span,
}
}
/// Creates a new string literal
pub(crate) fn new_str(id: NodeId, val: &str, span: Span) -> Literal {
Literal {
id,
kind: LitKind::Str(val.to_string()),
span,
}
}
/// Creates a new raw string literal
pub(crate) fn new_raw_str(id: NodeId, val: &str, span: Span) -> Literal {
Literal {
id,
kind: LitKind::RawStr(val.to_string()),
span,
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
/// The Ast representation of literals
pub enum LitKind {
/// A string literal (`"foo"`)
Str(String),
/// A raw string literal (`r#" x " a \ff "#`)
RawStr(String),
/// A numeric value with optional postfix part (`42`, `1.3`, `1Hz`, `100sec`)
/// Stores as a string to have lossless representation
Numeric(String, Option<String>),
/// A boolean literal (`true`)
Bool(bool),
}
/// An Ast node representing a binary operator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BinOp {
/// The `+` operator (addition)
Add,
/// The `-` operator (subtraction)
Sub,
/// The `*` operator (multiplication)
Mul,
/// The `/` operator (division)
Div,
/// The `%` operator (modulus)
Rem,
/// The `**` operator (power)
Pow,
/// The `&&` operator (logical and)
And,
/// The `||` operator (logical or)
Or,
/// The `^` operator (bitwise xor)
BitXor,
/// The `&` operator (bitwise and)
BitAnd,
/// The `|` operator (bitwise or)
BitOr,
/// The `<<` operator (shift left)
Shl,
/// The `>>` operator (shift right)
Shr,
/// The `==` operator (equality)
Eq,
/// The `<` operator (less than)
Lt,
/// The `<=` operator (less than or equal to)
Le,
/// The `!=` operator (not equal to)
Ne,
/// The `>=` operator (greater than or equal to)
Ge,
/// The `>` operator (greater than)
Gt,
}
/// An Ast node representing an unary operator.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum UnOp {
/// The `!` operator for logical inversion
Not,
/// The `-` operator for negation
Neg,
/// The `~` operator for one's complement
BitNot,
}
/// An Ast node representing the name of a called function and also the names of the arguments.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct FunctionName {
/// The name of the function
pub name: Ident,
/// A list containing the name of each argument. If the argument is unnamed, it is represented by `None`.
pub arg_names: Vec<Option<Ident>>,
}
#[derive(Debug, Clone, Eq, Serialize, Deserialize)]
/// This struct represents an identifier in the specification.
/// For example the name of an [Output] or [Input].
pub struct Ident {
/// The name of the identifier
pub name: String,
/// The span in the specification declaring the identifier
pub span: Span,
}
impl Ident {
/// Creates a new identifier.
pub(crate) fn new(name: String, span: Span) -> Ident {
Ident { name, span }
}
}
/// In the equality definition of `Ident`, we only compare the string values
/// and ignore the `Span` info
impl PartialEq for Ident {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Hash for Ident {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.name.hash(state);
}
}
/// Every node in the Ast gets a unique id, represented by a 32bit unsigned integer.
/// They are used in the later analysis phases to store information about Ast nodes.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct NodeId {
/// The actual unique id.
pub id: u32,
/// Counter to track transformations on this node. Increased during syntactic sugar removal.
pub prime_counter: u32,
}
impl NodeId {
/// Creates a new NodeId
pub fn new(x: usize) -> NodeId {
assert!(x < (u32::max_value() as usize));
NodeId {
id: x as u32,
prime_counter: 0u32,
}
}
/// Creates a copy NodeId with incremented prime counter, which indicates a applied transformation for desugarization.
pub fn primed(&self) -> Self {
let NodeId { id, prime_counter } = *self;
NodeId {
id,
prime_counter: prime_counter + 1,
}
}
}