Skip to content

Commit e263c35

Browse files
committed
Add From implementations for AST node.
Signed-off-by: James Goppert <james.goppert@gmail.com>
1 parent 9fe7ccf commit e263c35

11 files changed

+433
-98
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "rumoca_parser"
33
authors = ["James Goppert", "Benjamin Perseghetti"]
44
description = "A Modelica parser leveraging LALRPOP"
5-
version = "0.10.2"
5+
version = "0.10.3"
66
edition = "2021"
77
license = "Apache-2.0"
88

src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,4 @@ extern crate macro_rules_attribute;
88
pub use s1_parser::ast;
99
pub use s2_analysis::parser_helper::parse_file;
1010
pub use s2_analysis::print_visitor::PrintVisitor;
11-
pub use s2_analysis::Visitable;
12-
pub use s2_analysis::VisitableMut;
13-
pub use s2_analysis::Visitor;
14-
pub use s2_analysis::VisitorMut;
11+
pub use s2_analysis::{Node, NodeMutRef, NodeRef, Visitable, VisitableMut, Visitor, VisitorMut};

src/s1_parser/ast.rs

-84
Original file line numberDiff line numberDiff line change
@@ -4,90 +4,6 @@ derive_alias! {
44
#[derive(CommonTraits!)] = #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)];
55
}
66

7-
#[derive(CommonTraits!, Default)]
8-
pub enum Node {
9-
#[default]
10-
Empty,
11-
StoredDefinition(StoredDefinition),
12-
ClassDefinition(ClassDefinition),
13-
ClassSpecifier(ClassSpecifier),
14-
CompositionPart(CompositionPart),
15-
Element(Element),
16-
ComponentDeclaration(ComponentDeclaration),
17-
ClassPrefixes(ClassPrefixes),
18-
ComponentClause(ComponentClause),
19-
ComponentClause1(ComponentClause1),
20-
Declaration(Declaration),
21-
TypeSpecifier(TypeSpecifier),
22-
Equation(Equation),
23-
IfEquationBlock(IfEquationBlock),
24-
Statement(Statement),
25-
IfStatementBlock(IfStatementBlock),
26-
Expression(Expression),
27-
IfExpressionBlock(IfExpressionBlock),
28-
ComponentReference(ComponentReference),
29-
RefPart(RefPart),
30-
Subscript(Subscript),
31-
Argument(Argument),
32-
Modification(Modification),
33-
ModExpr(ModExpr),
34-
Description(Description),
35-
TypePrefix(TypePrefix),
36-
ForIndex(ForIndex),
37-
Span(Span),
38-
ElementFlags(ElementFlags),
39-
Causality(Causality),
40-
Variability(Variability),
41-
Visibility(Visibility),
42-
Connection(Connection),
43-
UnaryOp(UnaryOp),
44-
BinaryOp(BinaryOp),
45-
ClassType(ClassType),
46-
}
47-
48-
#[derive(Default, Clone, Debug, PartialEq, Eq, Hash)]
49-
pub enum NodeRef<'a> {
50-
#[default]
51-
Empty,
52-
StoredDefinition(&'a StoredDefinition),
53-
ClassDefinition(&'a ClassDefinition),
54-
ClassSpecifier(&'a ClassSpecifier),
55-
CompositionPart(&'a CompositionPart),
56-
Element(&'a Element),
57-
ComponentDeclaration(&'a ComponentDeclaration),
58-
ComponentDeclaration1(&'a ComponentDeclaration1),
59-
ClassPrefixes(&'a ClassPrefixes),
60-
ComponentClause(&'a ComponentClause),
61-
ComponentClause1(&'a ComponentClause1),
62-
Declaration(&'a Declaration),
63-
TypeSpecifier(&'a TypeSpecifier),
64-
Equation(&'a Equation),
65-
IfEquationBlock(&'a IfEquationBlock),
66-
Statement(&'a Statement),
67-
IfStatementBlock(&'a IfStatementBlock),
68-
Expression(&'a Expression),
69-
IfExpressionBlock(&'a IfExpressionBlock),
70-
ComponentReference(&'a ComponentReference),
71-
RefPart(&'a RefPart),
72-
Subscript(&'a Subscript),
73-
Argument(&'a Argument),
74-
Modification(&'a Modification),
75-
ModExpr(&'a ModExpr),
76-
Description(&'a Description),
77-
TypePrefix(&'a TypePrefix),
78-
ForIndex(&'a ForIndex),
79-
Span(&'a Span),
80-
ElementFlags(&'a ElementFlags),
81-
Causality(&'a Causality),
82-
Variability(&'a Variability),
83-
Visibility(&'a Visibility),
84-
Connection(&'a Connection),
85-
UnaryOp(&'a UnaryOp),
86-
BinaryOp(&'a BinaryOp),
87-
ClassType(&'a ClassType),
88-
Name(&'a Name),
89-
}
90-
917
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
928
// File Level Nodes
939

src/s2_analysis/ast_node.rs

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
use super::super::s1_parser::ast::*;
2+
use paste::paste;
3+
4+
macro_rules! define_node_enums {
5+
($($name:ident),*) => {
6+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
7+
pub enum Node {
8+
#[default]
9+
Empty,
10+
$(
11+
$name($name),
12+
)*
13+
}
14+
15+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
16+
pub enum NodeRef<'a> {
17+
#[default]
18+
Empty,
19+
$(
20+
$name(&'a $name),
21+
)*
22+
}
23+
24+
#[derive(Debug, PartialEq, Eq, Hash, Default)]
25+
pub enum NodeMutRef<'a> {
26+
#[default]
27+
Empty,
28+
$(
29+
$name(& 'a mut $name),
30+
)*
31+
}
32+
};
33+
}
34+
35+
macro_rules! from_node {
36+
($($variant:ident),*) => {
37+
paste! {
38+
$(
39+
impl From<Node> for $variant {
40+
fn from(value: Node) -> Self {
41+
if let Node::$variant(inner) = value {
42+
inner
43+
} else {
44+
panic!("Expected Node::{}", stringify!($variant));
45+
}
46+
}
47+
}
48+
49+
impl <'a> From<&'a Node> for &'a $variant {
50+
fn from(value: &'a Node) -> Self {
51+
if let Node::$variant(inner) = value {
52+
inner
53+
} else {
54+
panic!("Expected Node::{}", stringify!($variant));
55+
}
56+
}
57+
}
58+
59+
impl <'a> From<&'a mut Node> for &'a mut $variant {
60+
fn from(value: &'a mut Node) -> Self {
61+
if let Node::$variant(inner) = value {
62+
inner
63+
} else {
64+
panic!("Expected Node::{}", stringify!($variant));
65+
}
66+
}
67+
}
68+
69+
impl From<$variant> for Node {
70+
fn from(value: $variant) -> Self {
71+
Node::$variant(value)
72+
}
73+
}
74+
75+
impl <'a> From<&'a $variant> for NodeRef<'a> {
76+
fn from(value: &'a $variant) -> Self {
77+
NodeRef::$variant(value)
78+
}
79+
}
80+
81+
impl <'a> From<NodeRef<'a>> for &'a $variant {
82+
fn from(value: NodeRef<'a>) -> Self {
83+
if let NodeRef::$variant(inner) = value {
84+
inner
85+
} else {
86+
panic!("Expected NodeRef::{}", stringify!($variant));
87+
}
88+
}
89+
}
90+
91+
impl <'a> From<&'a mut $variant> for NodeMutRef<'a> {
92+
fn from(value: &'a mut $variant) -> Self {
93+
NodeMutRef::$variant(value)
94+
}
95+
}
96+
97+
impl <'a> From<NodeMutRef<'a>> for &'a mut $variant {
98+
fn from(value: NodeMutRef<'a>) -> Self {
99+
if let NodeMutRef::$variant(inner) = value {
100+
inner
101+
} else {
102+
panic!("Expected NodeMutRef::{}", stringify!($variant));
103+
}
104+
}
105+
}
106+
)*
107+
}
108+
};
109+
}
110+
111+
from_node!(
112+
StoredDefinition,
113+
ClassDefinition,
114+
ClassSpecifier,
115+
CompositionPart,
116+
Element,
117+
ComponentDeclaration,
118+
ComponentDeclaration1,
119+
ClassPrefixes,
120+
ComponentClause,
121+
ComponentClause1,
122+
Declaration,
123+
TypeSpecifier,
124+
Equation,
125+
IfEquationBlock,
126+
Statement,
127+
IfStatementBlock,
128+
Expression,
129+
IfExpressionBlock,
130+
ComponentReference,
131+
RefPart,
132+
Subscript,
133+
Argument,
134+
Modification,
135+
ModExpr,
136+
Description,
137+
TypePrefix,
138+
ForIndex,
139+
Span,
140+
ElementFlags,
141+
Causality,
142+
Variability,
143+
Visibility,
144+
Connection,
145+
UnaryOp,
146+
BinaryOp,
147+
ClassType,
148+
Name
149+
);
150+
151+
// Usage:
152+
define_node_enums!(
153+
StoredDefinition,
154+
ClassDefinition,
155+
ClassSpecifier,
156+
CompositionPart,
157+
Element,
158+
ComponentDeclaration,
159+
ComponentDeclaration1,
160+
ClassPrefixes,
161+
ComponentClause,
162+
ComponentClause1,
163+
Declaration,
164+
TypeSpecifier,
165+
Equation,
166+
IfEquationBlock,
167+
Statement,
168+
IfStatementBlock,
169+
Expression,
170+
IfExpressionBlock,
171+
ComponentReference,
172+
RefPart,
173+
Subscript,
174+
Argument,
175+
Modification,
176+
ModExpr,
177+
Description,
178+
TypePrefix,
179+
ForIndex,
180+
Span,
181+
ElementFlags,
182+
Causality,
183+
Variability,
184+
Visibility,
185+
Connection,
186+
UnaryOp,
187+
BinaryOp,
188+
ClassType,
189+
Name
190+
);

src/s2_analysis/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
pub mod ast_node;
12
pub mod parser_helper;
23
pub mod print_visitor;
34
pub mod visitable;
45
pub mod visitable_mut;
56
pub mod visitor;
67
pub mod visitor_mut;
78

9+
pub use ast_node::{Node, NodeMutRef, NodeRef};
810
pub use parser_helper::parse_file;
911
pub use print_visitor::PrintVisitor;
1012
pub use visitable::Visitable;

src/s2_analysis/print_visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use super::ast_node::NodeRef;
12
use crate::s1_parser::ast;
2-
use crate::s1_parser::ast::NodeRef;
33
use crate::s2_analysis::visitor::Visitor;
44

55
pub struct PrintVisitor {

0 commit comments

Comments
 (0)