Skip to content

Commit f7059a8

Browse files
committed
Add lifetimes to visitor to enable tree annotation.
Signed-off-by: James Goppert <james.goppert@gmail.com>
1 parent dcc8f4c commit f7059a8

File tree

7 files changed

+44
-33
lines changed

7 files changed

+44
-33
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.vscode
33
*.py
44
*.orig
5+
*.swp

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 translator with focus on Casadi, Sympy, JAX, and Collimator generation"
5-
version = "0.6.1"
5+
version = "0.6.2"
66
edition = "2021"
77
license = "Apache-2.0"
88

src/s1_parser/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use serde::{Deserialize, Serialize};
22

33
derive_alias! {
4-
#[derive(CommonTraits!)] = #[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Hash)];
4+
#[derive(CommonTraits!)] = #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)];
55
}
66

77
#[derive(CommonTraits!, Default)]

src/s1_parser/print_visitor.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Default for PrintVisitor {
2121
}
2222
}
2323

24-
impl Visitor for PrintVisitor {
24+
impl<'a> Visitor<'a> for PrintVisitor {
2525
fn enter_any(&mut self) {
2626
self.level += 1;
2727
}
@@ -30,21 +30,21 @@ impl Visitor for PrintVisitor {
3030
self.level -= 1;
3131
}
3232

33-
fn enter_stored_definition(&mut self, _def: &ast::StoredDefinition) {
33+
fn enter_stored_definition(&mut self, _def: &'a ast::StoredDefinition) {
3434
self.print("Stored Definition");
3535
}
3636

37-
fn enter_class_definition(&mut self, class: &ast::ClassDefinition) {
37+
fn enter_class_definition(&mut self, class: &'a ast::ClassDefinition) {
3838
if let ast::ClassSpecifier::Long { name, .. } = &class.specifier {
3939
self.print(&format!("class {}", name));
4040
}
4141
}
4242

43-
fn exit_class_definition(&mut self, _class: &ast::ClassDefinition) {
43+
fn exit_class_definition(&mut self, _class: &'a ast::ClassDefinition) {
4444
println!("\n");
4545
}
4646

47-
fn enter_expression(&mut self, expr: &ast::Expression) {
47+
fn enter_expression(&mut self, expr: &'a ast::Expression) {
4848
match expr {
4949
ast::Expression::Binary { op, .. } => {
5050
self.print(&format!("{:?}", op));
@@ -79,7 +79,7 @@ impl Visitor for PrintVisitor {
7979
}
8080
}
8181

82-
fn enter_equation(&mut self, eq: &ast::Equation) {
82+
fn enter_equation(&mut self, eq: &'a ast::Equation) {
8383
match eq {
8484
ast::Equation::Connect { .. } => {
8585
self.print("connect");
@@ -94,7 +94,7 @@ impl Visitor for PrintVisitor {
9494
}
9595
}
9696

97-
fn exit_element(&mut self, elem: &ast::Element) {
97+
fn exit_element(&mut self, elem: &'a ast::Element) {
9898
match elem {
9999
ast::Element::ComponentClause { clause, .. } => {
100100
for comp in clause.components.iter() {
@@ -109,7 +109,7 @@ impl Visitor for PrintVisitor {
109109
}
110110
}
111111

112-
fn exit_component_reference(&mut self, comp: &ast::ComponentReference) {
112+
fn exit_component_reference(&mut self, comp: &'a ast::ComponentReference) {
113113
let mut s: String = "".to_string();
114114
for (index, part) in comp.parts.iter().enumerate() {
115115
if index != 0 || comp.local {

src/s1_parser/visitor.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
use crate::s1_parser::ast;
22

3-
pub trait Visitor {
3+
pub trait Visitor<'a> {
44
fn enter_any(&mut self) {}
55
fn exit_any(&mut self) {}
66

7-
fn enter_expression(&mut self, _expr: &ast::Expression) {}
8-
fn exit_expression(&mut self, _expr: &ast::Expression) {}
7+
#[allow(unused_variables)]
8+
fn enter_expression(&mut self, _expr: &'a ast::Expression) {}
9+
fn exit_expression(&mut self, _expr: &'a ast::Expression) {}
910

10-
fn enter_element(&mut self, _expr: &ast::Element) {}
11-
fn exit_element(&mut self, _expr: &ast::Element) {}
11+
fn enter_element(&mut self, _elem: &'a ast::Element) {}
12+
fn exit_element(&mut self, _elem: &'a ast::Element) {}
1213

13-
fn enter_class_definition(&mut self, _class: &ast::ClassDefinition) {}
14-
fn exit_class_definition(&mut self, _class: &ast::ClassDefinition) {}
14+
fn enter_class_definition(&mut self, _class: &'a ast::ClassDefinition) {}
15+
fn exit_class_definition(&mut self, _class: &'a ast::ClassDefinition) {}
1516

16-
fn enter_equation(&mut self, _eq: &ast::Equation) {}
17-
fn exit_equation(&mut self, _eq: &ast::Equation) {}
17+
fn enter_equation(&mut self, _eq: &'a ast::Equation) {}
18+
fn exit_equation(&mut self, _eq: &'a ast::Equation) {}
1819

19-
fn enter_statement(&mut self, _stmt: &ast::Statement) {}
20-
fn exit_statement(&mut self, _stmt: &ast::Statement) {}
20+
fn enter_statement(&mut self, _stmt: &'a ast::Statement) {}
21+
fn exit_statement(&mut self, _stmt: &'a ast::Statement) {}
2122

22-
fn enter_stored_definition(&mut self, _def: &ast::StoredDefinition) {}
23-
fn exit_stored_definition(&mut self, _def: &ast::StoredDefinition) {}
23+
fn enter_stored_definition(&mut self, _def: &'a ast::StoredDefinition) {}
24+
fn exit_stored_definition(&mut self, _def: &'a ast::StoredDefinition) {}
2425

25-
fn enter_component_reference(&mut self, _def: &ast::ComponentReference) {}
26-
fn exit_component_reference(&mut self, _def: &ast::ComponentReference) {}
26+
fn enter_component_reference(&mut self, _comp: &'a ast::ComponentReference) {}
27+
fn exit_component_reference(&mut self, _comp: &'a ast::ComponentReference) {}
2728
}

src/s1_parser/walker.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use crate::s1_parser::ast;
33
use crate::s1_parser::visitor::Visitor;
44

55
impl Walker {
6-
pub fn walk_stored_definition<V: Visitor>(visitor: &mut V, def: &ast::StoredDefinition) {
6+
pub fn walk_stored_definition<'a, V: Visitor<'a>>(
7+
visitor: &mut V,
8+
def: &'a ast::StoredDefinition,
9+
) {
710
visitor.enter_any();
811
visitor.enter_stored_definition(def);
912
for class in def.classes.iter() {
@@ -13,7 +16,10 @@ impl Walker {
1316
visitor.exit_any();
1417
}
1518

16-
pub fn walk_class_definition<V: Visitor>(visitor: &mut V, class: &ast::ClassDefinition) {
19+
pub fn walk_class_definition<'a, V: Visitor<'a>>(
20+
visitor: &mut V,
21+
class: &'a ast::ClassDefinition,
22+
) {
1723
visitor.enter_any();
1824
visitor.enter_class_definition(class);
1925

@@ -45,14 +51,14 @@ impl Walker {
4551
visitor.exit_any();
4652
}
4753

48-
pub fn walk_element<V: Visitor>(visitor: &mut V, eq: &ast::Element) {
54+
pub fn walk_element<'a, V: Visitor<'a>>(visitor: &mut V, eq: &'a ast::Element) {
4955
visitor.enter_any();
5056
visitor.enter_element(eq);
5157
visitor.exit_element(eq);
5258
visitor.exit_any();
5359
}
5460

55-
pub fn walk_equation<V: Visitor>(visitor: &mut V, eq: &ast::Equation) {
61+
pub fn walk_equation<'a, V: Visitor<'a>>(visitor: &mut V, eq: &'a ast::Equation) {
5662
visitor.enter_any();
5763
visitor.enter_equation(eq);
5864
match eq {
@@ -89,7 +95,7 @@ impl Walker {
8995
visitor.exit_any();
9096
}
9197

92-
pub fn walk_statement<V: Visitor>(visitor: &mut V, stmt: &ast::Statement) {
98+
pub fn walk_statement<'a, V: Visitor<'a>>(visitor: &mut V, stmt: &'a ast::Statement) {
9399
visitor.enter_any();
94100
visitor.enter_statement(stmt);
95101
match stmt {
@@ -130,7 +136,7 @@ impl Walker {
130136
visitor.exit_any();
131137
}
132138

133-
pub fn walk_expression<V: Visitor>(visitor: &mut V, expr: &ast::Expression) {
139+
pub fn walk_expression<'a, V: Visitor<'a>>(visitor: &mut V, expr: &'a ast::Expression) {
134140
visitor.enter_any();
135141
visitor.enter_expression(expr);
136142
match expr {
@@ -180,7 +186,10 @@ impl Walker {
180186
visitor.exit_any();
181187
}
182188

183-
pub fn walk_component_reference<V: Visitor>(visitor: &mut V, comp: &ast::ComponentReference) {
189+
pub fn walk_component_reference<'a, V: Visitor<'a>>(
190+
visitor: &mut V,
191+
comp: &'a ast::ComponentReference,
192+
) {
184193
visitor.enter_any();
185194
visitor.enter_component_reference(comp);
186195
visitor.exit_component_reference(comp);

0 commit comments

Comments
 (0)