#[derive(Debug, Clone, PartialEq)]
pub struct Span {
pub start: usize,
pub end: usize,
}
impl Span {
pub fn new(start: usize, end: usize) -> Self {
Self { start, end }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Ident(pub String);
#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
String(String),
Number(f64),
Dimension(f64, crate::token::Unit),
Bool(bool),
Null,
Identifier(Ident),
Array(Vec<Expression>),
Table(Vec<TableFieldValue>),
Index {
base: Box<Expression>,
index: Box<Expression>,
},
Call(CallExpression),
Component(ComponentExpression),
BinaryOp {
op: BinOpKind,
lhs: Box<Expression>,
rhs: Box<Expression>,
},
UnaryOp {
op: UnaryOpKind,
operand: Box<Expression>,
},
Function {
params: Vec<Ident>,
body: BlockStatement,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct TableFieldValue {
pub name: Ident,
pub value: Expression,
}
#[derive(Debug, Clone, PartialEq)]
pub enum BinOpKind {
Add,
Sub,
Mul,
Div,
Rem,
Eq,
NotEq,
Lt,
Gt,
LtEq,
GtEq,
And,
Or,
Pipe, Query, Dot, }
#[derive(Debug, Clone, PartialEq)]
pub enum UnaryOpKind {
Neg,
Not,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CallExpression {
pub callee: Box<Expression>,
pub args: Vec<Expression>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ConstructorCall {
pub method: Ident,
pub args: Vec<Expression>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ComponentExpression {
pub component_type: Ident,
pub constructors: Vec<ConstructorCall>,
pub body: BlockStatement,
}
#[derive(Debug, Clone, PartialEq)]
pub struct BlockStatement {
pub statements: Vec<Statement>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Statement {
Assignment(AssignmentStatement),
Reassign {
target: Expression,
value: Expression,
},
Return(ReturnStatement),
If(IfStatement),
Block(BlockStatement),
Expression(Expression),
ForIn {
binding: Ident,
iterable: Expression,
body: BlockStatement,
},
While {
condition: Expression,
body: BlockStatement,
},
Break,
Continue,
Import {
items: Vec<ImportItem>,
path: String,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct AssignmentStatement {
pub name: Ident,
pub value: Expression,
pub exported: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ImportItem {
Named(Ident),
NamedAlias { name: Ident, alias: Ident },
PositionalAlias { index: usize, alias: Ident },
}
#[derive(Debug, Clone, PartialEq)]
pub struct ReturnStatement {
pub value: Option<Expression>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct IfStatement {
pub condition: Expression,
pub then_branch: BlockStatement,
pub else_branch: Option<ElseBranch>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ElseBranch {
Block(BlockStatement),
If(Box<IfStatement>),
}