tour_parser/
ast.rs

1//! Full syntax definition that will actually be generated.
2use std::rc::Rc;
3use syn::*;
4use tour_core::Delimiter;
5
6use crate::syntax::*;
7
8/// Template statement.
9pub enum StmtTempl {
10    /// Regular statement.
11    Scalar(Scalar),
12    /// Scoped statement.
13    Scope(Scope),
14}
15
16pub enum Scalar {
17    /// Static content.
18    Static {
19        value: Rc<str>,
20        index: u32,
21    },
22    /// Import and alias external template.
23    Use(UseTempl),
24    /// Render block or external template.
25    Render(RenderTempl),
26    /// Render body for layout.
27    Yield(YieldTempl),
28    /// Rust item that will be generated as is.
29    Item(Rc<ItemTempl>),
30    /// Rust expression.
31    Expr {
32        expr: Rc<Expr>,
33        delim: Delimiter,
34    },
35}
36
37/// Scoped rust statement.
38pub enum Scope {
39    /// Block of statements.
40    Root { stmts: Vec<StmtTempl> },
41    /// If statement.
42    If {
43        templ: IfTempl,
44        stmts: Vec<StmtTempl>,
45        else_branch: Option<(Token![else],Box<Scope>)>
46    },
47    /// For statement.
48    For {
49        templ: ForTempl,
50        stmts: Vec<StmtTempl>,
51        else_branch: Option<(Token![else],Box<Scope>)>
52    },
53    /// Block declaration.
54    Block {
55        templ: BlockTempl,
56        stmts: Vec<StmtTempl>,
57    },
58}
59
60impl Scope {
61    pub(crate) fn stack_mut(&mut self) -> &mut Vec<StmtTempl> {
62        match self {
63            Self::Root { stmts } => stmts,
64            Self::Block { stmts, .. } => stmts,
65            Self::For { else_branch: Some(branch), .. } => branch.1.stack_mut(),
66            Self::For { stmts, .. } => stmts,
67            Self::If { else_branch: Some(branch), .. } => branch.1.stack_mut(),
68            Self::If { stmts, .. } => stmts,
69        }
70    }
71}
72