Skip to content

Commit b4bc97e

Browse files
committed
Reorganize ast into part/node/fragment.
Signed-off-by: James Goppert <james.goppert@gmail.com>
1 parent ff7758d commit b4bc97e

File tree

9 files changed

+1142
-1104
lines changed

9 files changed

+1142
-1104
lines changed

src/s1_parser/ast.rs

-793
This file was deleted.

src/s1_parser/ast/debug.rs

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
//! This module implements the `fmt::Debug` trait for the AST types.
2+
3+
use super::node::*;
4+
use super::part::*;
5+
use std::fmt;
6+
7+
macro_rules! impl_debug_for_enum {
8+
($enum_name:ident { $($variant:ident),* $(,)? }) => {
9+
impl fmt::Debug for $enum_name {
10+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11+
match self {
12+
$(
13+
Self::$variant(v) => {
14+
if f.alternate() {
15+
write!(f, "{:#?}", v)
16+
} else {
17+
write!(f, "{:?}", v)
18+
}
19+
}
20+
)*
21+
_ => Ok(()), // Default case for ignored variants
22+
}
23+
}
24+
}
25+
};
26+
}
27+
28+
impl fmt::Debug for NodeData {
29+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30+
write!(f, "NodeData {{id: {:?}, span: {:?}}}", self.id, self.span)
31+
}
32+
}
33+
34+
impl fmt::Debug for ClassFlags {
35+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36+
let flags = [
37+
("partial", self.partial),
38+
("encapsulated", self.encapsulated),
39+
("replaceable", self.replaceable),
40+
("redeclare", self.redeclare),
41+
("is_final", self.is_final),
42+
("inner", self.inner),
43+
("outer", self.outer),
44+
]
45+
.iter()
46+
.filter_map(|&(flag, is_set)| if is_set { Some(flag) } else { None })
47+
.collect::<Vec<_>>(); // Collect names of true flags into a vector
48+
write!(f, "[{}]", flags.join(", "))
49+
}
50+
}
51+
52+
impl fmt::Debug for TypeSpecifier {
53+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54+
if self.local {
55+
write!(f, ".{:?}", self.name)
56+
} else {
57+
write!(f, "{:?}", self.name)
58+
}
59+
}
60+
}
61+
62+
impl_debug_for_enum!(Equation {
63+
Connect,
64+
For,
65+
If,
66+
Simple,
67+
});
68+
69+
impl_debug_for_enum!(Statement {
70+
Assignment,
71+
Break,
72+
For,
73+
If,
74+
Return,
75+
While,
76+
});
77+
78+
impl_debug_for_enum!(Expression {
79+
Array,
80+
Binary,
81+
Boolean,
82+
FunctionCall,
83+
If,
84+
Ref,
85+
Unary,
86+
UnsignedInteger,
87+
UnsignedReal
88+
});
89+
90+
impl fmt::Debug for ComponentReference {
91+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92+
let parts = self
93+
.parts
94+
.iter()
95+
.map(|part| format!("{:?}", part))
96+
.collect::<Vec<_>>()
97+
.join(".");
98+
write!(
99+
f,
100+
"ComponentReference {{{}{}, id: {}, span: {:?}}}",
101+
if self.local { "." } else { "" },
102+
parts,
103+
self.node_data.id,
104+
self.node_data.span,
105+
)
106+
}
107+
}
108+
109+
impl fmt::Debug for RefPart {
110+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111+
if self.array_subscripts.is_empty() {
112+
write!(f, "{:?}", self.name)
113+
} else {
114+
write!(f, "{:?}{:?}", self.name, self.array_subscripts)
115+
}
116+
}
117+
}
118+
119+
impl_debug_for_enum!(Subscript { Range, Expression });
120+
121+
impl_debug_for_enum!(Argument {
122+
Modification,
123+
Redeclaration,
124+
Replaceable
125+
});
126+
127+
impl_debug_for_enum!(Modification { Class, Expression });
128+
129+
impl_debug_for_enum!(ModExpr { Break, Expression });
130+
131+
impl fmt::Debug for Description {
132+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133+
write!(f, "{} {:?}", self.strings.join(" "), self.annotation)
134+
}
135+
}
136+
137+
impl fmt::Debug for ElementFlags {
138+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139+
let flags = [
140+
("replaceable", self.replaceable),
141+
("redeclare", self.redeclare),
142+
("is_final", self.is_final),
143+
("inner", self.inner),
144+
("outer", self.outer),
145+
]
146+
.iter()
147+
.filter_map(|&(flag, is_set)| if is_set { Some(flag) } else { None })
148+
.collect::<Vec<_>>(); // Collect names of true flags into a vector
149+
write!(f, "[{}]", flags.join(", "))
150+
}
151+
}
152+
153+
impl fmt::Debug for DescriptionString {
154+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155+
write!(f, "{}", self.parts.join(" "))
156+
}
157+
}
158+
159+
impl fmt::Debug for Name {
160+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161+
write!(f, "{}", self.parts.join("."))
162+
}
163+
}

src/s1_parser/ast/fragment.rs

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//! This module contains AST fragments
2+
//!
3+
//! The fragments do not appear in the AST but are used
4+
//! by the parser to build the AST.
5+
6+
use super::node::*;
7+
use super::part::*;
8+
use serde::{Deserialize, Serialize};
9+
10+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
11+
// Class Level Fragments
12+
13+
#[derive(CommonTraits!, Default)]
14+
pub enum CompositionPart {
15+
#[default]
16+
Empty,
17+
AlgorithmSection(AlgorithmSection),
18+
ElementList(ElementList),
19+
EquationSection(EquationSection),
20+
}
21+
22+
#[derive(CommonTraits!, Default)]
23+
pub struct ElementList {
24+
pub visibility: Visibility,
25+
pub elements: Vec<Element>,
26+
}
27+
28+
#[derive(CommonTraits!, Default)]
29+
pub struct EquationSection {
30+
pub initial: bool,
31+
pub equations: Vec<Equation>,
32+
}
33+
34+
#[derive(CommonTraits!, Default)]
35+
pub struct AlgorithmSection {
36+
pub initial: bool,
37+
pub statements: Vec<Statement>,
38+
}
39+
40+
#[derive(CommonTraits!, Default)]
41+
#[allow(clippy::large_enum_variant)]
42+
pub enum Element {
43+
#[default]
44+
Empty,
45+
ClassDefinition(ClassDefinition),
46+
ComponentClause(Vec<ComponentDeclaration>),
47+
ExtendsClause(ExtendsClause),
48+
ImportClause(ImportClause),
49+
}
50+
51+
#[derive(CommonTraits!, Default)]
52+
pub enum ClassSpecifier {
53+
#[default]
54+
Empty,
55+
Extends(ClassSpecifierExtends),
56+
Long(ClassSpecifierLong),
57+
}
58+
59+
#[derive(CommonTraits!, Default)]
60+
pub struct ClassSpecifierLong {
61+
pub name: String,
62+
pub description: DescriptionString,
63+
pub composition: Vec<CompositionPart>,
64+
pub name_end: String,
65+
}
66+
67+
#[derive(CommonTraits!, Default)]
68+
pub struct ClassSpecifierExtends {
69+
pub name: String,
70+
pub modification: Vec<Argument>,
71+
pub description: DescriptionString,
72+
pub composition: Vec<CompositionPart>,
73+
pub name_end: String,
74+
}
75+
76+
#[derive(CommonTraits!, Default)]
77+
pub struct ClassPrefixes {
78+
pub is_partial: bool,
79+
pub class_type: ClassType,
80+
}
81+
82+
#[derive(CommonTraits!, Default)]
83+
pub struct Declaration {
84+
pub name: String,
85+
pub array_subscripts: Vec<Subscript>,
86+
pub modification: Option<Modification>,
87+
}
88+
89+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
90+
// Equations
91+
92+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
93+
// Statements
94+
95+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
96+
// Expressions
97+
98+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
99+
// Common
100+
101+
#[derive(CommonTraits!, Default)]
102+
pub struct TypePrefix {
103+
pub connection: Connection,
104+
pub variability: Variability,
105+
pub causality: Causality,
106+
}
107+
108+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
109+
// Simple Terminals

src/s1_parser/ast/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub mod debug;
2+
pub mod fragment;
3+
pub mod node;
4+
pub mod part;

0 commit comments

Comments
 (0)