bulloak_syntax/
ast.rs

1//! The AST for a bulloak tree file.
2
3use crate::span::Span;
4
5/// An Abstract Syntax Tree (AST) that describes the semantic
6/// structure of a bulloak tree.
7#[derive(Debug, PartialEq, Eq)]
8pub enum Ast {
9    /// The root node of the AST.
10    Root(Root),
11    /// A condition node of the AST.
12    ///
13    /// This node corresponds to a junction in the tree.
14    Condition(Condition),
15    /// An action node of the AST.
16    ///
17    /// This node corresponds to a leaf node of the tree.
18    Action(Action),
19    /// Additional action description.
20    ///
21    /// This node can only appear as a child of an action.
22    ActionDescription(Description),
23}
24
25impl Ast {
26    /// Return the span of this abstract syntax tree.
27    #[must_use]
28    pub fn span(&self) -> &Span {
29        match self {
30            Self::Root(x) => &x.span,
31            Self::Condition(x) => &x.span,
32            Self::Action(x) => &x.span,
33            Self::ActionDescription(x) => &x.span,
34        }
35    }
36
37    /// Whether the current node is an `Action` node.
38    #[must_use]
39    pub fn is_action(&self) -> bool {
40        matches!(self, Self::Action(_))
41    }
42}
43
44/// The root node of the AST.
45#[derive(Debug, PartialEq, Eq)]
46pub struct Root {
47    /// The name that is used for the emitted contract.
48    pub contract_name: String,
49    /// The span that encompasses this node. It includes
50    /// all of its children.
51    pub span: Span,
52    /// The children AST nodes of this node.
53    pub children: Vec<Ast>,
54}
55
56/// A condition node of the AST.
57#[derive(Debug, PartialEq, Eq)]
58pub struct Condition {
59    /// The title of this condition.
60    ///
61    /// For example: "when stuff happens".
62    pub title: String,
63    /// The span that encompasses this node. It includes
64    /// all of its children.
65    pub span: Span,
66    /// The children AST nodes of this node.
67    pub children: Vec<Ast>,
68}
69
70/// An action node of the AST.
71#[derive(Debug, PartialEq, Eq)]
72pub struct Action {
73    /// The title of this action.
74    ///
75    /// For example: "It should revert."
76    pub title: String,
77    /// The span that encompasses this node.
78    pub span: Span,
79    /// The children AST nodes of this node.
80    ///
81    /// For now we only support action description
82    /// nodes.
83    pub children: Vec<Ast>,
84}
85
86/// A description node of the AST.
87#[derive(Debug, PartialEq, Eq)]
88pub struct Description {
89    /// The text of this action.
90    ///
91    /// For example: "Describe your actions."
92    pub text: String,
93    /// The span that encompasses this node.
94    pub span: Span,
95}