morgana_core/ast/
mod.rs

1use regex::Regex;
2
3#[derive(Debug, Clone)]
4pub struct Morg {
5    pub name: Option<Identifier>,
6    pub start: Option<StartStatement>,
7    pub definitions: Vec<Definition>,
8    pub skips: Vec<Skip>,
9    pub specials: Vec<Special>,
10}
11
12impl std::fmt::Display for Morg {
13    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14        writeln!(f, "Morg {{ ")?;
15        if let Some(name) = &self.name {
16            writeln!(f, "name: {name}")?;
17        }
18
19        if let Some(start) = &self.start {
20            writeln!(f, "start: {start}")?;
21        }
22
23        for def in &self.definitions {
24            writeln!(f, "{def}")?;
25        }
26
27        for special in &self.specials {
28            writeln!(f, "{special}")?;
29        }
30
31        for skip in &self.skips {
32            writeln!(f, "{skip}")?;
33        }
34
35        write!(f, "}}")
36    }
37}
38
39pub type Identifier = String;
40pub type StartStatement = Identifier;
41pub type Skip = Builtin;
42
43#[derive(Debug, Clone, derive_more::Display)]
44#[display(fmt = "{} : {}", name, constructor)]
45pub struct SumConstructor {
46    pub name: Identifier,
47    pub constructor: Expression,
48}
49
50pub type SumDefinition = Vec<SumConstructor>;
51
52#[derive(Debug, Clone, derive_more::Display)]
53#[display(fmt = "{} : {}", field, r#type)]
54pub struct ProdConstructor {
55    pub field: Identifier,
56    pub r#type: Expression,
57}
58
59pub type ProdDefinition = Vec<ProdConstructor>;
60
61#[derive(Debug, Clone, derive_more::Display)]
62#[display(fmt = "special {} := {}", key, constructor)]
63pub struct Special {
64    pub key: SpecialKeyword,
65    pub constructor: SpecialConstructor,
66}
67
68#[derive(Debug, Clone, derive_more::Display)]
69pub enum SpecialConstructor {
70    #[display(fmt = "|{:?}|", "_0")]
71    Sum(SumDefinition),
72    #[display(fmt = "({:?})", "_0")]
73    Prod(ProdDefinition),
74    #[display(fmt = "{}", "_0")]
75    Alias(Expression),
76}
77
78#[derive(Debug, Clone, derive_more::Display)]
79pub enum SpecialKeyword {
80    Comment,
81    Whitespace,
82    Keyword,
83    Operator,
84    Delimiter,
85    Builtin,
86    Identifier,
87}
88
89#[derive(Debug, Clone, derive_more::Display)]
90pub enum Definition {
91    #[display(fmt = "{}", "_0")]
92    Node(NodeDefinition),
93    #[display(fmt = "{}", "_0")]
94    Squash(SquashDefinition),
95}
96
97#[derive(Debug, Clone, derive_more::Display)]
98#[display(fmt = "node {} := {}", name, constructor)]
99pub struct NodeDefinition {
100    pub name: Identifier,
101    pub constructor: NodeConstructor,
102}
103
104#[derive(Debug, Clone, derive_more::Display)]
105pub enum NodeConstructor {
106    #[display(fmt = "|{:?}|", "_0")]
107    Sum(SumDefinition),
108    #[display(fmt = "({:?})", "_0")]
109    Prod(ProdDefinition),
110    #[display(fmt = "{}", "_0")]
111    Alias(Expression),
112}
113
114#[derive(Debug, Clone, derive_more::Display)]
115#[display(fmt = "squash {} := {}", name, constructor)]
116pub struct SquashDefinition {
117    pub name: Identifier,
118    pub constructor: SquashConstructor,
119}
120
121#[derive(Debug, Clone, derive_more::Display)]
122pub enum SquashConstructor {
123    #[display(fmt = "|{:?}|", "_0")]
124    Sum(SumDefinition),
125    #[display(fmt = "{}", "_0")]
126    Alias(Expression),
127}
128
129#[derive(Debug, Clone)]
130pub struct Expression {
131    pub factors: Vec<Factor>,
132}
133
134impl std::fmt::Display for Expression {
135    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
136        for factor in &self.factors {
137            write!(f, "{factor}")?;
138        }
139        write!(f, "")
140    }
141}
142
143#[derive(Debug, Clone, derive_more::Display)]
144pub enum Factor {
145    #[display(fmt = "{:?}", "_0")]
146    NonTerminal(NonTerminal),
147    #[display(fmt = "{}", "_0")]
148    Builtin(Builtin),
149    #[display(fmt = "{}", "_0")]
150    Option(OptionW),
151    #[display(fmt = "{}", "_0")]
152    Repetition(Repetition),
153    #[display(fmt = "{:?}", "_0")]
154    RepetitionSep(RepetitionSep),
155    #[display(fmt = "{}", "_0")]
156    Once(Once),
157    #[display(fmt = "{:?}", "_0")]
158    OnceSep(OnceSep),
159}
160
161pub type OptionW = Box<Expression>;
162pub type Repetition = Box<Expression>;
163pub type RepetitionSep = (Box<Expression>, Box<Expression>);
164pub type Once = Box<Expression>;
165pub type OnceSep = (Box<Expression>, Box<Expression>);
166
167pub type NonTerminal = Vec<Identifier>;
168
169#[derive(Debug, Clone, derive_more::Display)]
170pub enum Builtin {
171    #[display(fmt = "'{}'", "_0")]
172    Char(String),
173    #[display(fmt = "\"{}\"", "_0")]
174    String(String),
175    #[display(fmt = "/{}/", "_0")]
176    Regex(Regex),
177}