use crate::error::Span;
#[derive(Debug, Clone)]
pub struct File {
pub items: Vec<TopLevelItem>,
pub span: Span,
}
#[derive(Debug, Clone)]
pub enum TopLevelItem {
Import(ImportDef),
ComponentDef(ComponentDef),
FnDef(FnDef),
Node(NodeInvocation),
Stmt(Stmt),
}
#[derive(Debug, Clone)]
pub struct ImportDef {
pub(crate) def: ImportDefType,
pub(crate) span: Span,
}
#[derive(Debug, Clone)]
pub enum ImportDefType {
Module(String, String, Option<ComponentSelection>),
Library(String, String),
}
#[derive(Debug, Clone)]
pub enum ComponentSelection {
Wildcard,
List(Vec<String>),
}
#[derive(Debug, Clone)]
pub struct ComponentDef {
pub name: String,
pub interface: ComponentInterface,
pub body: Vec<NodeItem>,
pub span: Span,
}
#[derive(Debug, Clone)]
pub enum ComponentInterface {
Loose(String), Strict(Vec<Parameter>), None,
}
#[derive(Debug, Clone)]
pub struct Parameter {
pub name: String,
pub is_optional: bool,
}
#[derive(Debug, Clone)]
pub struct FnDef {
pub name: String,
pub params: Vec<String>,
pub body: Vec<FnItem>,
pub span: Span,
}
#[derive(Debug, Clone)]
pub enum FnItem {
Node(NodeInvocation),
Stmt(Stmt),
}
#[derive(Debug, Clone)]
pub struct NodeInvocation {
pub type_name: String,
pub id: Option<Expr>,
pub body: Vec<NodeItem>,
pub span: Span,
}
#[derive(Debug, Clone)]
pub enum NodeItem {
Prop(String, Expr, Span),
Child(NodeInvocation),
Stmt(Stmt),
}
#[derive(Debug, Clone)]
pub struct Block {
pub stmts: Vec<Stmt>,
pub terminator: Option<Expr>,
}
#[derive(Debug, Clone)]
pub enum Stmt {
Assign(Expr, AssignOp, Expr, Span),
Let(String, Expr),
Const(String, Expr),
For(Vec<String>, Expr, Block),
While(Expr, Block),
Return(Option<ReturnType>, Span),
Expr(Expr),
}
#[derive(Debug, PartialEq, Clone)]
pub enum AssignOp {
Equal,
PlusEqual,
MinEqual,
MultEqual,
DivEqual,
}
#[derive(Debug, Clone)]
pub enum ReturnType {
Node(NodeInvocation),
Expr(Expr),
}
#[derive(Debug, Clone)]
pub struct Expr {
pub kind: ExprKind,
pub span: Span,
}
#[derive(Debug, Clone)]
pub enum ExprKind {
Binary(Box<Expr>, String, Box<Expr>),
Unary(String, Box<Expr>),
Literal(Literal),
Variable(String),
Field(Box<Expr>, String, bool),
Index(Box<Expr>, Box<Expr>),
Call(Box<Expr>, Vec<Expr>),
Lambda(FnDef),
If(Box<IfExpr>),
Match(Box<Expr>, Vec<MatchArm>),
Range(Box<Expr>, Box<Expr>, bool), }
#[derive(Debug, Clone)]
pub struct IfExpr {
pub condition: Expr,
pub then_branch: (Vec<Stmt>, Option<Expr>),
pub else_ifs: Vec<(Expr, (Vec<Stmt>, Option<Expr>))>,
pub else_branch: Option<(Vec<Stmt>, Option<Expr>)>,
}
#[derive(Debug, Clone)]
pub struct MatchArm {
pub pattern: String,
pub body: MatchBody,
pub is_var: bool,
}
#[derive(Debug, Clone)]
pub enum MatchBody {
Expr(Expr),
Block(Vec<Stmt>, Option<Expr>),
}
#[derive(Debug, Clone)]
pub enum Literal {
Int(i64),
Float(f64),
Bool(bool),
Str(String),
List(Vec<Expr>),
Map(Vec<(String, Expr)>),
Null,
}