use core::mem;
use crate::lexer::{Quote, Span};
use crate::{Date, DateTime, Position};
#[derive(Debug, PartialEq)]
pub(crate) struct Node {
pub(crate) kind: Expression,
pub(crate) position: Position,
pub(crate) span: Span,
}
#[derive(Debug, PartialEq)]
pub(crate) enum Expression {
Literal(Literal),
String { value: String, quote: Quote },
Identifier(String),
Comparison {
operator: Comparison,
left: Box<Node>,
right: Box<Node>,
},
Arithmetic {
operator: Arithmetic,
left: Box<Node>,
right: Box<Node>,
},
Membership {
operator: Membership,
left: Box<Node>,
right: Box<Node>,
},
Presence {
kind: Presence,
negated: bool,
operand: Box<Node>,
},
Unary { operator: Unary, operand: Box<Node> },
And { left: Box<Node>, right: Box<Node> },
Or { left: Box<Node>, right: Box<Node> },
Not { operand: Box<Node> },
List(Vec<Node>),
Object(Vec<Entry>),
Call { name: String, arguments: Vec<Node> },
Index { object: Box<Node>, key: Box<Node> },
Property { object: Box<Node>, name: String },
Cast {
operand: Box<Node>,
type_name: &'static str,
},
Duration(Vec<(i64, &'static str)>),
RelativeDate {
duration: Box<Node>,
direction: Direction,
},
}
#[derive(Debug, PartialEq)]
pub(crate) enum Literal {
Integer(i64),
Float(f64),
Boolean(bool),
Undefined,
Null,
Date(Date),
DateTime(DateTime, u8),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Comparison {
Gt,
Lt,
Gte,
Lte,
EqualEqual,
Ne,
StrictEq,
StrictNe,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Arithmetic {
Add,
Subtract,
Multiply,
Divide,
Modulo,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Membership {
In,
Contains,
Intersects,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Presence {
Blank,
Present,
Defined,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Unary {
Minus,
Bang,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Direction {
Ago,
Future,
Next,
Last,
}
#[derive(Debug, PartialEq)]
pub(crate) struct Entry {
pub(crate) key: String,
pub(crate) style: KeyStyle,
pub(crate) position: Position,
pub(crate) span: Span,
pub(crate) value: Node,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum KeyStyle {
Bare,
Quoted(Quote),
}
#[derive(Debug, PartialEq)]
pub(crate) enum Statement {
Assignment {
target: Node,
value: Node,
position: Position,
span: Span,
},
If {
condition: Node,
then_block: Block,
else_block: Option<Block>,
position: Position,
span: Span,
},
While {
condition: Node,
body: Block,
position: Position,
span: Span,
},
Expression(Node),
}
impl Statement {
pub(crate) fn position(&self) -> Position {
match self {
Statement::Assignment { position, .. }
| Statement::If { position, .. }
| Statement::While { position, .. } => *position,
Statement::Expression(node) => node.position,
}
}
pub(crate) fn span(&self) -> Span {
match self {
Statement::Assignment { span, .. }
| Statement::If { span, .. }
| Statement::While { span, .. } => *span,
Statement::Expression(node) => node.span,
}
}
}
#[derive(Debug, PartialEq)]
pub(crate) struct Block {
pub(crate) statements: Vec<Statement>,
pub(crate) position: Position,
pub(crate) span: Span,
}
#[derive(Debug, PartialEq)]
pub(crate) struct Program {
pub(crate) statements: Vec<Statement>,
pub(crate) position: Position,
pub(crate) span: Span,
}
impl Drop for Node {
fn drop(&mut self) {
let mut pending = Vec::new();
detach(&mut self.kind, &mut pending);
while let Some(mut node) = pending.pop() {
detach(&mut node.kind, &mut pending);
}
}
}
fn detach(kind: &mut Expression, pending: &mut Vec<Node>) {
match mem::replace(kind, Expression::Literal(Literal::Null)) {
Expression::Comparison { left, right, .. }
| Expression::Arithmetic { left, right, .. }
| Expression::Membership { left, right, .. }
| Expression::And { left, right }
| Expression::Or { left, right }
| Expression::Index {
object: left,
key: right,
} => {
pending.push(*left);
pending.push(*right);
}
Expression::Presence { operand, .. }
| Expression::Unary { operand, .. }
| Expression::Not { operand }
| Expression::Property {
object: operand, ..
}
| Expression::Cast { operand, .. }
| Expression::RelativeDate {
duration: operand, ..
} => pending.push(*operand),
Expression::List(nodes)
| Expression::Call {
arguments: nodes, ..
} => pending.extend(nodes),
Expression::Object(entries) => pending.extend(entries.into_iter().map(|entry| entry.value)),
Expression::Literal(_)
| Expression::String { .. }
| Expression::Identifier(_)
| Expression::Duration(_) => {}
}
}
impl Drop for Block {
fn drop(&mut self) {
let mut pending = vec![mem::take(&mut self.statements)];
while let Some(statements) = pending.pop() {
for mut statement in statements {
match &mut statement {
Statement::If {
then_block,
else_block,
..
} => {
pending.push(mem::take(&mut then_block.statements));
if let Some(block) = else_block {
pending.push(mem::take(&mut block.statements));
}
}
Statement::While { body, .. } => {
pending.push(mem::take(&mut body.statements));
}
Statement::Assignment { .. } | Statement::Expression(_) => {}
}
}
}
}
}