Skip to content

Commit 580caf4

Browse files
committed
Simplify AST.
Signed-off-by: James Goppert <james.goppert@gmail.com>
1 parent 60c3d72 commit 580caf4

File tree

7 files changed

+63
-106
lines changed

7 files changed

+63
-106
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.9.1"
5+
version = "0.10.0"
66
edition = "2021"
77
license = "Apache-2.0"
88

src/s1_parser/ast.rs

+11-28
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ pub enum ClassSpecifier {
3434
Empty,
3535
Long {
3636
name: String,
37-
description: Vec<String>,
37+
description: DescriptionString,
3838
composition: Vec<CompositionPart>,
3939
name_end: String,
4040
},
4141
Extends {
4242
name: String,
4343
modification: Vec<Argument>,
44-
description: Vec<String>,
44+
description: DescriptionString,
4545
composition: Vec<CompositionPart>,
4646
name_end: String,
4747
},
@@ -83,9 +83,7 @@ pub enum Element {
8383
flags: ElementFlags,
8484
def: ClassDefinition,
8585
},
86-
ExtendsClause {
87-
type_specifier: TypeSpecifier,
88-
},
86+
ExtendsClause(TypeSpecifier),
8987
}
9088

9189
#[derive(CommonTraits!, Default)]
@@ -131,7 +129,7 @@ pub struct Declaration {
131129

132130
#[derive(CommonTraits!, Default)]
133131
pub struct TypeSpecifier {
134-
pub leading_period: bool,
132+
pub local: bool,
135133
pub name: Name,
136134
}
137135

@@ -197,12 +195,8 @@ pub enum Statement {
197195
stmts: Vec<Statement>,
198196
description: Description,
199197
},
200-
Break {
201-
description: Description,
202-
},
203-
Return {
204-
description: Description,
205-
},
198+
Break(Description),
199+
Return(Description),
206200
}
207201

208202
#[derive(CommonTraits!, Default)]
@@ -236,16 +230,11 @@ pub enum Expression {
236230
if_blocks: Vec<IfExpressionBlock>,
237231
else_expr: Box<Option<Expression>>,
238232
},
239-
ArrayArguments {
240-
args: Vec<Expression>,
241-
},
233+
ArrayArguments(Vec<Expression>),
242234
FunctionCall {
243235
comp: ComponentReference,
244236
args: Vec<Expression>,
245237
},
246-
Der {
247-
args: Vec<Expression>,
248-
},
249238
}
250239

251240
#[derive(CommonTraits!, Default)]
@@ -296,9 +285,7 @@ pub enum Argument {
296285
pub enum Modification {
297286
#[default]
298287
Empty,
299-
Expression {
300-
expr: ModExpr,
301-
},
288+
Expression(ModExpr),
302289
Class {
303290
args: Vec<Argument>,
304291
expr: Option<ModExpr>,
@@ -310,9 +297,7 @@ pub enum ModExpr {
310297
#[default]
311298
Empty,
312299
Break,
313-
Expression {
314-
expr: Expression,
315-
},
300+
Expression(Expression),
316301
}
317302

318303
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
@@ -448,10 +433,8 @@ pub enum ClassType {
448433
Operator,
449434
}
450435

451-
#[derive(CommonTraits!, Default)]
452-
pub struct Name {
453-
pub ident: Vec<String>,
454-
}
436+
pub type Name = Vec<String>;
437+
pub type DescriptionString = Vec<String>;
455438

456439
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
457440
// Unit Testing

src/s1_parser/modelica.lalrpop

+42-55
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,13 @@ pub IDENT: String = {
237237
//✅ | extends IDENT [ class-modification ] description-string composition end IDENT
238238
pub LongClassSpecifier: ast::ClassSpecifier = {
239239
<name: IDENT>
240-
<description: DescriptionString?>
240+
<description: DescriptionString>
241241
<composition: Composition>
242242
"end"
243243
<name_end: IDENT> => {
244244
ast::ClassSpecifier::Long {
245245
name,
246-
description: description.unwrap_or(Vec::new()),
246+
description,
247247
composition,
248248
name_end,
249249
}
@@ -433,9 +433,7 @@ pub ImportClause : ast::Element = {
433433
pub ExtendsClause: ast::Element = {
434434
"extends"
435435
<type_specifier: TypeSpecifier> => {
436-
ast::Element::ExtendsClause {
437-
type_specifier,
438-
}
436+
ast::Element::ExtendsClause(type_specifier)
439437
}
440438
}
441439

@@ -552,18 +550,14 @@ pub Modification: ast::Modification = {
552550
args,
553551
expr,
554552
},
555-
"=" <expr: ModExpr> => ast::Modification::Expression {
556-
expr,
557-
},
553+
"=" <expr: ModExpr> => ast::Modification::Expression(expr),
558554
}
559555

560556
//✅ modification-expression :
561557
//✅ expression
562558
//✅ | break
563559
pub ModExpr: ast::ModExpr = {
564-
<expr: Expression> => ast::ModExpr::Expression {
565-
expr
566-
},
560+
<expr: Expression> => ast::ModExpr::Expression(expr),
567561
"break" => ast::ModExpr::Break {}
568562
}
569563

@@ -713,8 +707,8 @@ pub Statement: ast::Statement = {
713707
<stmt: IfStatement> => stmt,
714708
<stmt: ForStatement> => stmt,
715709
<stmt: WhileStatement> => stmt,
716-
"break" <description: Description> => ast::Statement::Break { description },
717-
"return" <description: Description> => ast::Statement::Return { description },
710+
"break" <description: Description> => ast::Statement::Break(description),
711+
"return" <description: Description> => ast::Statement::Return(description),
718712
}
719713

720714
//✅ if-equation :
@@ -1002,6 +996,12 @@ MulOperator : ast::BinaryOp = {
1002996
"./" => ast::BinaryOp::ElemDiv,
1003997
}
1004998

999+
FuncKeyword: String = {
1000+
<"der"> => String::from("der"),
1001+
// <"initial"> => String::from("initial"),
1002+
// <"pure"> => String::from("pure"),
1003+
};
1004+
10051005
SimpleExpression: ast::Expression = {
10061006
//✅ simple-expression :
10071007
//✅ logical-expression [ ":" logical-expression [ ":" logical-expression ] ]
@@ -1072,30 +1072,9 @@ SimpleExpression: ast::Expression = {
10721072
//✅ term :
10731073
//✅ factor { mul-operator factor }
10741074
#[precedence(level="4")] #[assoc(side="left")]
1075-
<lhs:SimpleExpression> "*" <rhs:SimpleExpression> => {
1076-
ast::Expression::Binary {
1077-
op: ast::BinaryOp::Mul,
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::ElemMul,
1085-
lhs: Box::new(lhs),
1086-
rhs: Box::new(rhs),
1087-
}
1088-
},
1089-
<lhs:SimpleExpression> "/" <rhs:SimpleExpression> => {
1075+
<lhs:SimpleExpression> <op: MulOperator> <rhs:SimpleExpression> => {
10901076
ast::Expression::Binary {
1091-
op: ast::BinaryOp::Div,
1092-
lhs: Box::new(lhs),
1093-
rhs: Box::new(rhs),
1094-
}
1095-
},
1096-
<lhs:SimpleExpression> "./" <rhs:SimpleExpression> => {
1097-
ast::Expression::Binary {
1098-
op: ast::BinaryOp::ElemDiv,
1077+
op,
10991078
lhs: Box::new(lhs),
11001079
rhs: Box::new(rhs),
11011080
}
@@ -1128,13 +1107,11 @@ SimpleExpression: ast::Expression = {
11281107
"(" <rhs:ParenthesisExpression> ")" => {
11291108
ast::Expression::Unary {
11301109
op: ast::UnaryOp::Paren,
1131-
rhs: Box::new(rhs)
1110+
rhs: Box::new(rhs),
11321111
}
11331112
},
11341113
"{" <args:SeparatedList<Expression, ",">> "}" => {
1135-
ast::Expression::ArrayArguments {
1136-
args
1137-
}
1114+
ast::Expression::ArrayArguments (args)
11381115
},
11391116
<val:"UNSIGNED-INTEGER"> => {
11401117
ast::Expression::UnsignedInteger(val)
@@ -1148,15 +1125,23 @@ SimpleExpression: ast::Expression = {
11481125
<comp:ComponentReference> => {
11491126
ast::Expression::Ref(comp)
11501127
},
1128+
#[precedence(level="1")]
11511129
<comp:ComponentReference> <args:FunctionCallArguments> => {
11521130
ast::Expression::FunctionCall {
11531131
comp,
1154-
args
1132+
args,
11551133
}
11561134
},
1157-
"der" <args:FunctionCallArguments> => {
1158-
ast::Expression::Der {
1159-
args
1135+
<func: FuncKeyword> <args:FunctionCallArguments> => {
1136+
ast::Expression::FunctionCall {
1137+
comp: ast::ComponentReference{
1138+
local: false,
1139+
parts: vec![ast::RefPart{
1140+
name: func,
1141+
array_subscripts: Vec::new()
1142+
}]
1143+
},
1144+
args,
11601145
}
11611146
},
11621147
}
@@ -1168,26 +1153,24 @@ SimpleExpression: ast::Expression = {
11681153
//✅ type-specifier :
11691154
//✅ ["."] name
11701155
pub TypeSpecifier: ast::TypeSpecifier = {
1171-
<leading_period: "."?> <name: Name> => {
1156+
<local: "."?> <name: Name> => {
11721157
ast::TypeSpecifier{
1173-
leading_period: leading_period.is_some(),
1158+
local: local.is_some(),
11741159
name,
11751160
}
11761161
},
11771162
}
1163+
11781164
//✅ name :
11791165
//✅ IDENT { "." IDENT }
11801166
pub Name: ast::Name = {
11811167
<first:IDENT> <remaining: ("." <IDENT>)*> => {
11821168
let mut v = vec![first];
11831169
v.extend(remaining);
1184-
ast::Name {
1185-
ident: v
1186-
}
1170+
v
11871171
}
11881172
}
11891173

1190-
11911174
//✅ component-reference :
11921175
//✅ [ "." ] IDENT [ array-subscripts ] { "." IDENT [ array-subscripts ] }
11931176
pub ComponentReference: ast::ComponentReference = {
@@ -1263,22 +1246,26 @@ pub Subscript: ast::Subscript = {
12631246
//✅ description :
12641247
//✅ description-string [ annotation-clause ]
12651248
pub Description: ast::Description = {
1266-
<strings: DescriptionString?>
1249+
<strings: DescriptionString>
12671250
<annotation: AnnotationClause?>
12681251
=> {
12691252
ast::Description {
1270-
strings: strings.unwrap_or(Vec::new()),
1253+
strings,
12711254
annotation: annotation.unwrap_or(Vec::new()),
12721255
}
12731256
},
12741257
}
12751258

12761259
//✅ description-string :
12771260
//✅ [ STRING { "+" STRING } ]
1278-
// Note: LALRPOP doesn't like empty expressions, so effectively
1279-
// removing outer [ ], and at least one string is required.
1280-
// Other expressions will need to treat as option.
12811261
pub DescriptionString: Vec<String> = {
1262+
<s:DescriptionStringPart?> => {
1263+
s.unwrap_or(Vec::new())
1264+
}
1265+
}
1266+
// Note: LALRPOP doesn't like empty expressions here, so
1267+
// adding level of indirection with DescriptionStringPart
1268+
DescriptionStringPart: Vec<String> = {
12821269
<first:"STRING"> <remaining: ("+" <"STRING">)*> => {
12831270
let mut v = vec![first];
12841271
v.extend(remaining);

src/s2_analysis/print_visitor.rs

-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ impl Visitor<'_> for PrintVisitor {
7373
ast::Expression::FunctionCall { .. } => {
7474
self.print("function call");
7575
}
76-
ast::Expression::Der { .. } => {
77-
self.print("der");
78-
}
7976
ast::Expression::Empty { .. } => {}
8077
}
8178
}

src/s2_analysis/visitable.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl<'a> Visitable<'a> for ast::Element {
116116
name.accept(visitor);
117117
description.accept(visitor);
118118
}
119-
ast::Element::ExtendsClause { type_specifier } => {
119+
ast::Element::ExtendsClause(type_specifier) => {
120120
type_specifier.accept(visitor);
121121
}
122122
ast::Element::Empty => {}
@@ -380,15 +380,10 @@ impl<'a> Visitable<'a> for ast::Expression {
380380
expr.accept(visitor);
381381
}
382382
}
383-
ast::Expression::Der { args } => {
384-
for arg in args.iter() {
385-
arg.accept(visitor);
386-
}
387-
}
388383
ast::Expression::UnsignedInteger(_) => {}
389384
ast::Expression::UnsignedReal(_) => {}
390385
ast::Expression::Boolean(_) => {}
391-
ast::Expression::ArrayArguments { args } => {
386+
ast::Expression::ArrayArguments(args) => {
392387
for arg in args.iter() {
393388
arg.accept(visitor);
394389
}
@@ -487,7 +482,7 @@ impl<'a> Visitable<'a> for ast::Modification {
487482
visitor.enter_any();
488483
visitor.enter_modification(self);
489484
match self {
490-
ast::Modification::Expression { expr } => {
485+
ast::Modification::Expression(expr) => {
491486
expr.accept(visitor);
492487
}
493488
ast::Modification::Class { args, expr } => {
@@ -511,7 +506,7 @@ impl<'a> Visitable<'a> for ast::ModExpr {
511506
visitor.enter_mod_expr(self);
512507
match self {
513508
ast::ModExpr::Break => {}
514-
ast::ModExpr::Expression { expr } => {
509+
ast::ModExpr::Expression(expr) => {
515510
expr.accept(visitor);
516511
}
517512
ast::ModExpr::Empty => {}

0 commit comments

Comments
 (0)