Skip to content

Commit e2081fb

Browse files
committed
Add visitable trait.
Signed-off-by: James Goppert <james.goppert@gmail.com>
1 parent f7059a8 commit e2081fb

12 files changed

+342
-330
lines changed

src/lalrpop.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
use lalrpop_util::lalrpop_mod;
3+
4+
lalrpop_mod!(
5+
#[rustfmt::skip]
6+
modelica,
7+
"/s1_parser/modelica.rs"
8+
);

src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
pub mod s0_lexer;
22
pub mod s1_parser;
3+
pub mod s2_analysis;
34

45
#[macro_use]
56
extern crate macro_rules_attribute;
67

78
pub use s1_parser::ast;
8-
pub use s1_parser::print_visitor::PrintVisitor;
9-
pub use s1_parser::visitor::Visitor;
10-
pub use s1_parser::walker::Walker;
9+
pub use s2_analysis::parser_helper::parse_file;
10+
pub use s2_analysis::print_visitor::PrintVisitor;
11+
pub use s2_analysis::visitor::Visitable;

src/main.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use clap::Parser;
2-
3-
use rumoca_parser::PrintVisitor;
4-
use rumoca_parser::Walker;
2+
use rumoca_parser::{PrintVisitor, Visitable};
53

64
#[derive(Parser, Debug)]
75
#[command(version, about = "Rumoca Modelica Parser", long_about = None)]
@@ -17,13 +15,13 @@ struct Args {
1715

1816
fn main() -> Result<(), Box<dyn std::error::Error>> {
1917
let args = Args::parse();
20-
let def = rumoca_parser::s1_parser::parse_file(&args.model_file);
18+
let def = rumoca_parser::parse_file(&args.model_file);
2119

2220
if args.verbose {
2321
println!("{:#?}", def);
2422
}
2523

2624
let mut visitor = PrintVisitor::default();
27-
Walker::walk_stored_definition(&mut visitor, &def);
25+
def.accept(&mut visitor);
2826
Ok(())
2927
}

src/s1_parser/ast.rs

+5
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,15 @@ pub enum UnaryOp {
252252
Paren,
253253
Not,
254254
Negative,
255+
Positive,
256+
ElemNegative,
257+
ElemPositive,
255258
}
256259

257260
#[derive(CommonTraits!)]
258261
pub enum BinaryOp {
262+
Paren,
263+
Not,
259264
Add,
260265
Sub,
261266
Mul,

src/s1_parser/mod.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
pub mod ast;
2-
pub mod parser_helper;
3-
pub use parser_helper::parse_file;
4-
pub mod print_visitor;
5-
pub mod visitor;
6-
pub mod walker;
7-
82
use lalrpop_util::lalrpop_mod;
93

104
lalrpop_mod!(
115
#[rustfmt::skip]
12-
modelica,
6+
pub modelica,
137
"/s1_parser/modelica.rs"
148
);

src/s1_parser/modelica.lalrpop

+52-84
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub StoredDefinition: ast::StoredDefinition = {
158158
}
159159
}
160160

161-
pub ClassDefinitionWithFinal: ast::ClassDefinition = {
161+
ClassDefinitionWithFinal: ast::ClassDefinition = {
162162
<is_final: "final"?> <mut class: ClassDefinition> => {
163163
class.is_final = is_final.is_some();
164164
class
@@ -851,7 +851,7 @@ pub ForStatement: ast::Statement = {
851851

852852
//✅ for-indices :
853853
//✅ for-index { "," for-index }
854-
pub ForIndices: Vec<ast::ForIndex> = {
854+
ForIndices: Vec<ast::ForIndex> = {
855855
<indices: SeparatedList<ForIndex, ",">> => indices
856856
}
857857

@@ -928,7 +928,7 @@ pub ConnectEquation: ast::Equation = {
928928
//✅ logical-expression
929929
//🟥 [ ":" logical-expression [ ":" logical-expression ] ]
930930
pub Expression: ast::Expression = {
931-
<simp:SimpleExpression> => <>,
931+
<SimpleExpression> => <>,
932932
"if" <if_cond:SimpleExpression> "then"
933933
<then_expr:SimpleExpression>
934934
<else_if_blocks: ElseIfExpressionBlock*>
@@ -966,7 +966,44 @@ pub ElseExpressionBlock: ast::Expression = {
966966

967967
pub ParenthesisExpression = SimpleExpression;
968968

969-
pub SimpleExpression: ast::Expression = {
969+
//✅ relational-operator :
970+
//✅ "<" | "<=" | ">" | ">=" | "==" | "<>"
971+
RelationalOperator : ast::BinaryOp = {
972+
"<" => ast::BinaryOp::LessThan,
973+
"<=" => ast::BinaryOp::LessThanOrEqual,
974+
">" => ast::BinaryOp::GreaterThan,
975+
">=" => ast::BinaryOp::GreaterThanOrEqual,
976+
"==" => ast::BinaryOp::Equal,
977+
"<>" => ast::BinaryOp::NotEqual,
978+
}
979+
980+
//✅ add-operator :
981+
//✅ "+" | "-" | ".+" | ".-"
982+
AddBinaryOperator : ast::BinaryOp = {
983+
"+" => ast::BinaryOp::Add,
984+
"-" => ast::BinaryOp::Sub,
985+
".+" => ast::BinaryOp::ElemAdd,
986+
".-" => ast::BinaryOp::ElemSub,
987+
}
988+
AddUnaryOperator : ast::UnaryOp = {
989+
"-" => ast::UnaryOp::Negative,
990+
}
991+
992+
FactorOperator : ast::BinaryOp = {
993+
"^" => ast::BinaryOp::Exp,
994+
".^" => ast::BinaryOp::ElemExp,
995+
}
996+
997+
//✅ mul-operator :
998+
//✅ "*" | "/" | ".*" | "./"
999+
MulOperator : ast::BinaryOp = {
1000+
"*" => ast::BinaryOp::Mul,
1001+
"/" => ast::BinaryOp::Div,
1002+
".*" => ast::BinaryOp::ElemMul,
1003+
"./" => ast::BinaryOp::ElemDiv,
1004+
}
1005+
1006+
SimpleExpression: ast::Expression = {
9701007
//✅ simple-expression :
9711008
//✅ logical-expression [ ":" logical-expression [ ":" logical-expression ] ]
9721009
#[precedence(level="11")] #[assoc(side="left")]
@@ -1008,95 +1045,33 @@ pub SimpleExpression: ast::Expression = {
10081045
},
10091046
//✅ relation :
10101047
//✅ arithmetic-expression [ relational-operator arithmetic-expression ]
1011-
//✅ relational-operator :
1012-
//✅ "<" | "<=" | ">" | ">=" | "==" | "<>"
10131048
#[precedence(level="7")] #[assoc(side="left")]
1014-
<lhs:SimpleExpression> "<" <rhs:SimpleExpression> => {
1015-
ast::Expression::Binary {
1016-
op: ast::BinaryOp::LessThan,
1017-
lhs: Box::new(lhs),
1018-
rhs: Box::new(rhs),
1019-
}
1020-
},
1021-
<lhs:SimpleExpression> ">" <rhs:SimpleExpression> => {
1022-
ast::Expression::Binary {
1023-
op: ast::BinaryOp::GreaterThan,
1024-
lhs: Box::new(lhs),
1025-
rhs: Box::new(rhs),
1026-
}
1027-
},
1028-
<lhs:SimpleExpression> "<=" <rhs:SimpleExpression> => {
1049+
<lhs:SimpleExpression> <op:RelationalOperator> <rhs:SimpleExpression> => {
10291050
ast::Expression::Binary {
1030-
op: ast::BinaryOp::LessThanOrEqual,
1051+
op,
10311052
lhs: Box::new(lhs),
10321053
rhs: Box::new(rhs),
10331054
}
10341055
},
1035-
<lhs:SimpleExpression> ">=" <rhs:SimpleExpression> => {
1036-
ast::Expression::Binary {
1037-
op: ast::BinaryOp::GreaterThanOrEqual,
1038-
lhs: Box::new(lhs),
1039-
rhs: Box::new(rhs),
1040-
}
1041-
},
1042-
<lhs:SimpleExpression> "==" <rhs:SimpleExpression> => {
1043-
ast::Expression::Binary {
1044-
op: ast::BinaryOp::Equal,
1045-
lhs: Box::new(lhs),
1046-
rhs: Box::new(rhs),
1047-
}
1048-
},
1049-
<lhs:SimpleExpression> "<>" <rhs:SimpleExpression> => {
1050-
ast::Expression::Binary {
1051-
op: ast::BinaryOp::NotEqual,
1052-
lhs: Box::new(lhs),
1053-
rhs: Box::new(rhs),
1054-
}
1055-
},
1056-
#[precedence(level="12")] #[assoc(side="left")]
1057-
"-" <rhs:SimpleExpression> => {
1058-
ast::Expression::Unary {
1059-
op: ast::UnaryOp::Negative,
1060-
rhs: Box::new(rhs),
1061-
}
1062-
},
10631056
//✅ arithmetic-expression :
10641057
//✅ [ add-operator ] term { add-operator term }
1065-
//✅ add-operator :
1066-
//✅ "+" | "-" | ".+" | ".-"
1067-
#[precedence(level="5")] #[assoc(side="left")]
1068-
<lhs:SimpleExpression> "+" <rhs:SimpleExpression> => {
1069-
ast::Expression::Binary {
1070-
op: ast::BinaryOp::Add,
1071-
lhs: Box::new(lhs),
1072-
rhs: Box::new(rhs),
1073-
}
1074-
},
1075-
<lhs:SimpleExpression> ".+" <rhs:SimpleExpression> => {
1076-
ast::Expression::Binary {
1077-
op: ast::BinaryOp::ElemAdd,
1078-
lhs: Box::new(lhs),
1079-
rhs: Box::new(rhs),
1080-
}
1081-
},
1082-
<lhs:SimpleExpression> "-" <rhs:SimpleExpression> => {
1083-
ast::Expression::Binary {
1084-
op: ast::BinaryOp::Sub,
1085-
lhs: Box::new(lhs),
1058+
#[precedence(level="6")] #[assoc(side="right")]
1059+
<op:AddUnaryOperator> <rhs:SimpleExpression> => {
1060+
ast::Expression::Unary {
1061+
op,
10861062
rhs: Box::new(rhs),
10871063
}
10881064
},
1089-
<lhs:SimpleExpression> ".-" <rhs:SimpleExpression> => {
1065+
#[precedence(level="5")] #[assoc(side="left")]
1066+
<lhs:SimpleExpression> <op:AddBinaryOperator> <rhs:SimpleExpression> => {
10901067
ast::Expression::Binary {
1091-
op: ast::BinaryOp::ElemSub,
1068+
op,
10921069
lhs: Box::new(lhs),
10931070
rhs: Box::new(rhs),
10941071
}
10951072
},
10961073
//✅ term :
10971074
//✅ factor { mul-operator factor }
1098-
//✅ mul-operator :
1099-
//✅ "*" | "/" | ".*" | "./"
11001075
#[precedence(level="4")] #[assoc(side="left")]
11011076
<lhs:SimpleExpression> "*" <rhs:SimpleExpression> => {
11021077
ast::Expression::Binary {
@@ -1129,16 +1104,9 @@ pub SimpleExpression: ast::Expression = {
11291104
//✅ factor :
11301105
//✅ primary [ ( "^" | ".^" ) primary ]
11311106
#[precedence(level="3")] #[assoc(side="left")]
1132-
<lhs:SimpleExpression> "^" <rhs:SimpleExpression> => {
1133-
ast::Expression::Binary {
1134-
op: ast::BinaryOp::Exp,
1135-
lhs: Box::new(lhs),
1136-
rhs: Box::new(rhs),
1137-
}
1138-
},
1139-
<lhs:SimpleExpression> ".^" <rhs:SimpleExpression> => {
1107+
<lhs:SimpleExpression> <op:FactorOperator> <rhs:SimpleExpression> => {
11401108
ast::Expression::Binary {
1141-
op: ast::BinaryOp::ElemExp,
1109+
op,
11421110
lhs: Box::new(lhs),
11431111
rhs: Box::new(rhs),
11441112
}

src/s1_parser/visitor.rs

-28
This file was deleted.

0 commit comments

Comments
 (0)