use smol_str::SmolStr;
use temple_dsl::{ParsedExpr, ParsedTemplate, Span};
pub(crate) const KEYWORDS: &[&str] = &[
"edition", "flow", "entry", "events", "param", "let", "node", "when", "else", "ask", "wake",
"ignore",
];
pub(crate) const DECLARATIONS: &[&str] =
&["edition", "flow", "entry", "events", "param", "let", "node"];
pub(crate) const EDITIONS: &[u32] = &[2026];
pub(crate) const LATEST_EDITION: u32 = EDITIONS[EDITIONS.len() - 1];
pub(crate) const TICK: &str = "tick";
pub(crate) const LITERAL_WORDS: &[&str] = &["true", "false", "null"];
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Identifier {
pub name: SmolStr,
pub span: Span,
}
#[derive(Debug, Clone)]
pub struct Annotation {
pub name: Identifier,
pub value: Option<ParsedExpr>,
pub span: Span,
}
#[derive(Debug, Clone)]
pub struct ParamDeclaration {
pub name: Identifier,
pub default: ParsedExpr,
pub annotations: Vec<Annotation>,
pub span: Span,
}
#[derive(Debug, Clone)]
pub struct LetDeclaration {
pub name: Identifier,
pub params: Option<Vec<Identifier>>,
pub expression: ParsedExpr,
pub span: Span,
}
#[derive(Debug, Clone)]
pub struct NodeDeclaration {
pub name: Identifier,
pub params: Vec<Identifier>,
pub annotations: Vec<Annotation>,
pub lets: Vec<LetDeclaration>,
pub body: Body,
pub span: Span,
}
impl NodeDeclaration {
pub fn branch_count(&self) -> usize {
match &self.body {
Body::When { branches, .. } => branches.len(),
Body::Single(_) => 1,
}
}
}
#[derive(Debug, Clone)]
pub enum Body {
When { branches: Vec<Branch>, span: Span },
Single(Outcome),
}
#[derive(Debug, Clone)]
pub struct Branch {
pub guard: Guard,
pub outcome: Outcome,
pub span: Span,
}
#[derive(Debug, Clone)]
pub enum Guard {
Else(Span),
Expression(ParsedExpr),
}
#[derive(Debug, Clone)]
pub enum Outcome {
Jump {
target: Identifier,
args: Vec<ParsedExpr>,
span: Span,
},
Value {
template: ParsedTemplate,
wake: Option<ParsedExpr>,
span: Span,
},
Ask {
name: Identifier,
payload: Option<ParsedExpr>,
target: Identifier,
args: Vec<ParsedExpr>,
span: Span,
},
Ignore {
span: Span,
},
}
impl Outcome {
pub fn span(&self) -> Span {
match self {
Outcome::Jump { span, .. }
| Outcome::Value { span, .. }
| Outcome::Ask { span, .. }
| Outcome::Ignore { span } => *span,
}
}
}
#[derive(Debug, Clone)]
pub struct NameList {
pub names: Vec<Identifier>,
pub span: Span,
}
#[derive(Debug, Clone, Default)]
pub struct FlowAst {
pub edition: Option<u32>,
pub name: Option<Identifier>,
pub flow_span: Option<Span>,
pub annotations: Vec<Annotation>,
pub entries: Vec<NameList>,
pub events: Vec<NameList>,
pub params: Vec<ParamDeclaration>,
pub lets: Vec<LetDeclaration>,
pub nodes: Vec<NodeDeclaration>,
}
impl FlowAst {
pub fn entry_names(&self) -> impl Iterator<Item = &Identifier> {
self.entries.iter().flat_map(|list| list.names.iter())
}
pub fn event_names(&self) -> impl Iterator<Item = &Identifier> {
self.events.iter().flat_map(|list| list.names.iter())
}
pub fn outcomes(&self) -> impl Iterator<Item = &Outcome> {
self.nodes.iter().flat_map(|node| {
let (single, branches) = match &node.body {
Body::Single(outcome) => (Some(outcome), &[][..]),
Body::When { branches, .. } => (None, branches.as_slice()),
};
single
.into_iter()
.chain(branches.iter().map(|branch| &branch.outcome))
})
}
}