Skip to content

Commit 0e57555

Browse files
committed
Add default for entire ast.
Signed-off-by: James Goppert <james.goppert@gmail.com>
1 parent 52734dd commit 0e57555

File tree

4 files changed

+82
-48
lines changed

4 files changed

+82
-48
lines changed

src/s1_parser/ast.rs

+68-46
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ pub struct ClassDefinition {
2828
pub specifier: ClassSpecifier,
2929
}
3030

31-
#[derive(CommonTraits!)]
31+
#[derive(CommonTraits!, Default)]
3232
pub enum ClassSpecifier {
33+
#[default]
34+
Empty,
3335
Long {
3436
name: String,
3537
description: Vec<String>,
@@ -45,19 +47,10 @@ pub enum ClassSpecifier {
4547
},
4648
}
4749

48-
impl Default for ClassSpecifier {
49-
fn default() -> Self {
50-
ClassSpecifier::Long {
51-
name: "".to_string(),
52-
description: Vec::new(),
53-
composition: Vec::new(),
54-
name_end: "".to_string(),
55-
}
56-
}
57-
}
58-
59-
#[derive(CommonTraits!)]
50+
#[derive(CommonTraits!, Default)]
6051
pub enum CompositionPart {
52+
#[default]
53+
Empty,
6154
ElementList {
6255
visibility: Visibility,
6356
elements: Vec<Element>,
@@ -73,8 +66,10 @@ pub enum CompositionPart {
7366
},
7467
}
7568

76-
#[derive(CommonTraits!)]
69+
#[derive(CommonTraits!, Default)]
7770
pub enum Element {
71+
#[default]
72+
Empty,
7873
ImportClause {
7974
alias: String,
8075
name: Name,
@@ -93,14 +88,14 @@ pub enum Element {
9388
},
9489
}
9590

96-
#[derive(CommonTraits!)]
91+
#[derive(CommonTraits!, Default)]
9792
pub struct ComponentDeclaration {
9893
pub declaration: Declaration,
9994
pub condition_attribute: Option<Expression>,
10095
pub description: Description,
10196
}
10297

103-
#[derive(CommonTraits!)]
98+
#[derive(CommonTraits!, Default)]
10499
pub struct ComponentDeclaration1 {
105100
pub declaration: Declaration,
106101
pub description: Description,
@@ -112,29 +107,29 @@ pub struct ClassPrefixes {
112107
pub class_type: ClassType,
113108
}
114109

115-
#[derive(CommonTraits!)]
110+
#[derive(CommonTraits!, Default)]
116111
pub struct ComponentClause {
117112
pub type_prefix: TypePrefix,
118113
pub type_specifier: TypeSpecifier,
119114
pub array_subscripts: Option<ArraySubscripts>,
120115
pub components: Vec<ComponentDeclaration>,
121116
}
122117

123-
#[derive(CommonTraits!)]
118+
#[derive(CommonTraits!, Default)]
124119
pub struct ComponentClause1 {
125120
pub type_prefix: TypePrefix,
126121
pub type_specifier: TypeSpecifier,
127122
pub component_declaration1: ComponentDeclaration1,
128123
}
129124

130-
#[derive(CommonTraits!)]
125+
#[derive(CommonTraits!, Default)]
131126
pub struct Declaration {
132127
pub name: String,
133128
pub array_subscripts: Option<ArraySubscripts>,
134129
pub modification: Option<Modification>,
135130
}
136131

137-
#[derive(CommonTraits!)]
132+
#[derive(CommonTraits!, Default)]
138133
pub struct TypeSpecifier {
139134
pub leading_period: bool,
140135
pub name: Name,
@@ -143,8 +138,10 @@ pub struct TypeSpecifier {
143138
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
144139
// Equations
145140

146-
#[derive(CommonTraits!)]
141+
#[derive(CommonTraits!, Default)]
147142
pub enum Equation {
143+
#[default]
144+
Empty,
148145
Simple {
149146
lhs: Expression,
150147
rhs: Expression,
@@ -167,7 +164,7 @@ pub enum Equation {
167164
},
168165
}
169166

170-
#[derive(CommonTraits!)]
167+
#[derive(CommonTraits!, Default)]
171168
pub struct IfEquationBlock {
172169
pub cond: Expression,
173170
pub eqs: Vec<Equation>,
@@ -176,8 +173,10 @@ pub struct IfEquationBlock {
176173
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
177174
// Statements
178175

179-
#[derive(CommonTraits!)]
176+
#[derive(CommonTraits!, Default)]
180177
pub enum Statement {
178+
#[default]
179+
Empty,
181180
Assignment {
182181
comp: ComponentReference,
183182
rhs: Expression,
@@ -206,7 +205,7 @@ pub enum Statement {
206205
},
207206
}
208207

209-
#[derive(CommonTraits!)]
208+
#[derive(CommonTraits!, Default)]
210209
pub struct IfStatementBlock {
211210
pub cond: Expression,
212211
pub stmts: Vec<Statement>,
@@ -215,8 +214,10 @@ pub struct IfStatementBlock {
215214
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
216215
// Expressions
217216

218-
#[derive(CommonTraits!)]
217+
#[derive(CommonTraits!, Default)]
219218
pub enum Expression {
219+
#[default]
220+
Empty,
220221
UnsignedInteger(String),
221222
UnsignedReal(String),
222223
Boolean(bool),
@@ -249,40 +250,44 @@ pub enum Expression {
249250
},
250251
}
251252

252-
#[derive(CommonTraits!)]
253+
#[derive(CommonTraits!, Default)]
253254
pub struct IfExpressionBlock {
254255
pub cond: Expression,
255256
pub expr: Expression,
256257
}
257258

258-
#[derive(CommonTraits!)]
259+
#[derive(CommonTraits!, Default)]
259260
pub struct ComponentReference {
260261
pub local: bool,
261262
pub parts: Vec<RefPart>,
262263
}
263264

264-
#[derive(CommonTraits!)]
265+
#[derive(CommonTraits!, Default)]
265266
pub struct RefPart {
266267
pub name: String,
267268
pub array_subscripts: Option<ArraySubscripts>,
268269
}
269270

270-
#[derive(CommonTraits!)]
271+
#[derive(CommonTraits!, Default)]
271272
pub struct ArraySubscripts {
272273
pub sub: Vec<Subscript>,
273274
}
274275

275-
#[derive(CommonTraits!)]
276+
#[derive(CommonTraits!, Default)]
276277
pub enum Subscript {
278+
#[default]
279+
Empty,
277280
Colon,
278281
Expression(Expression),
279282
}
280283

281284
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
282285
// Modification
283286

284-
#[derive(CommonTraits!)]
287+
#[derive(CommonTraits!, Default)]
285288
pub enum Argument {
289+
#[default]
290+
Empty,
286291
Modification {
287292
name: Name,
288293
each: bool,
@@ -294,8 +299,10 @@ pub enum Argument {
294299
Redeclaration,
295300
}
296301

297-
#[derive(CommonTraits!)]
302+
#[derive(CommonTraits!, Default)]
298303
pub enum Modification {
304+
#[default]
305+
Empty,
299306
Expression {
300307
expr: ModExpr,
301308
},
@@ -305,10 +312,14 @@ pub enum Modification {
305312
},
306313
}
307314

308-
#[derive(CommonTraits!)]
315+
#[derive(CommonTraits!, Default)]
309316
pub enum ModExpr {
317+
#[default]
318+
Empty,
310319
Break,
311-
Expression { expr: Expression },
320+
Expression {
321+
expr: Expression,
322+
},
312323
}
313324

314325
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
@@ -320,14 +331,14 @@ pub struct Description {
320331
pub annotation: Vec<Argument>,
321332
}
322333

323-
#[derive(CommonTraits!)]
334+
#[derive(CommonTraits!, Default)]
324335
pub struct TypePrefix {
325336
pub connection: Connection,
326337
pub variability: Variability,
327338
pub causality: Causality,
328339
}
329340

330-
#[derive(CommonTraits!)]
341+
#[derive(CommonTraits!, Default)]
331342
pub struct ForIndex {
332343
pub ident: String,
333344
pub in_expr: Option<Expression>,
@@ -342,7 +353,7 @@ pub struct Span {
342353
pub right: usize,
343354
}
344355

345-
#[derive(CommonTraits!)]
356+
#[derive(CommonTraits!, Default)]
346357
pub struct ElementFlags {
347358
pub replaceable: bool,
348359
pub redeclare: bool,
@@ -351,36 +362,44 @@ pub struct ElementFlags {
351362
pub outer: bool,
352363
}
353364

354-
#[derive(CommonTraits!)]
365+
#[derive(CommonTraits!, Default)]
355366
pub enum Causality {
356-
None,
367+
#[default]
368+
Empty,
357369
Input,
358370
Output,
359371
}
360372

361-
#[derive(CommonTraits!)]
373+
#[derive(CommonTraits!, Default)]
362374
pub enum Variability {
375+
#[default]
376+
Empty,
363377
Continuous,
364378
Discrete,
365379
Parameter,
366380
Constant,
367381
}
368382

369-
#[derive(CommonTraits!)]
383+
#[derive(CommonTraits!, Default)]
370384
pub enum Visibility {
385+
#[default]
386+
Empty,
371387
Public,
372388
Protected,
373389
}
374390

375-
#[derive(CommonTraits!)]
391+
#[derive(CommonTraits!, Default)]
376392
pub enum Connection {
377-
None,
393+
#[default]
394+
Empty,
378395
Flow,
379396
Stream,
380397
}
381398

382-
#[derive(CommonTraits!)]
399+
#[derive(CommonTraits!, Default)]
383400
pub enum UnaryOp {
401+
#[default]
402+
Empty,
384403
Paren,
385404
Not,
386405
Negative,
@@ -389,8 +408,10 @@ pub enum UnaryOp {
389408
ElemPositive,
390409
}
391410

392-
#[derive(CommonTraits!)]
411+
#[derive(CommonTraits!, Default)]
393412
pub enum BinaryOp {
413+
#[default]
414+
Empty,
394415
Paren,
395416
Not,
396417
Add,
@@ -417,6 +438,7 @@ pub enum BinaryOp {
417438
#[derive(CommonTraits!, Default)]
418439
pub enum ClassType {
419440
#[default]
441+
Empty,
420442
Class,
421443
Model,
422444
Record,
@@ -433,7 +455,7 @@ pub enum ClassType {
433455
Operator,
434456
}
435457

436-
#[derive(CommonTraits!)]
458+
#[derive(CommonTraits!, Default)]
437459
pub struct Name {
438460
pub ident: Vec<String>,
439461
}

src/s1_parser/modelica.lalrpop

+2-2
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,9 @@ pub TypePrefix: ast::TypePrefix = {
480480
<variability: Variability?>
481481
<causality: Causality?> => {
482482
ast::TypePrefix {
483-
connection: connection.unwrap_or(ast::Connection::None),
483+
connection: connection.unwrap_or(ast::Connection::Empty),
484484
variability: variability.unwrap_or(ast::Variability::Continuous),
485-
causality: causality.unwrap_or(ast::Causality::None),
485+
causality: causality.unwrap_or(ast::Causality::Empty),
486486
}
487487
}
488488
}

src/s2_analysis/print_visitor.rs

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl Visitor for PrintVisitor {
7676
ast::Expression::Der { .. } => {
7777
self.print("der");
7878
}
79+
ast::Expression::Empty { .. } => {}
7980
}
8081
}
8182

@@ -91,6 +92,7 @@ impl Visitor for PrintVisitor {
9192
ast::Equation::For { .. } => {
9293
self.print("for");
9394
}
95+
ast::Equation::Empty { .. } => {}
9496
}
9597
}
9698

0 commit comments

Comments
 (0)