Skip to content

Commit 46b137e

Browse files
committed
Add extends and import elements.
Signed-off-by: James Goppert <james.goppert@gmail.com>
1 parent a905e54 commit 46b137e

File tree

2 files changed

+71
-64
lines changed

2 files changed

+71
-64
lines changed

src/s1_parser/ast.rs

+11-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use indexmap::IndexMap;
22
use serde::{Deserialize, Serialize};
33
use std::fmt;
4-
use std::hash::{Hash, Hasher};
54

65
derive_alias! {
76
#[derive(CommonTraits!)] = #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)];
@@ -108,23 +107,19 @@ pub struct ClassDefinition {
108107
pub node_data: NodeData,
109108
pub name: String,
110109
pub class_type: ClassType,
110+
pub extends: Vec<TypeSpecifier>,
111+
pub imports: Vec<ImportClause>,
111112
pub flags: ClassFlags,
112113
pub modification: Vec<Argument>,
113114
pub description: DescriptionString,
114-
//pub composition: Vec<CompositionPart>,
115115
pub components: IndexMap<String, ComponentDeclaration>,
116+
pub classes: IndexMap<String, ClassDefinition>,
116117
pub equations: Vec<Equation>,
117118
pub algorithms: Vec<Vec<Statement>>,
118119
pub initial_equations: Vec<Equation>,
119120
pub initial_algorithms: Vec<Vec<Statement>>,
120121
}
121122

122-
impl Hash for ClassDefinition {
123-
fn hash<H: Hasher>(&self, state: &mut H) {
124-
self.node_data.id.hash(state);
125-
}
126-
}
127-
128123
#[derive(CommonTraits!, Default)]
129124
pub enum ClassSpecifier {
130125
#[default]
@@ -189,11 +184,12 @@ pub struct AlgorithmSection {
189184
}
190185

191186
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
187+
#[allow(clippy::large_enum_variant)]
192188
pub enum Element {
193189
#[default]
194190
Empty,
195191
ClassDefinition(ClassDefinition),
196-
ComponentClause(ComponentClause),
192+
ComponentClause(Vec<ComponentDeclaration>),
197193
ExtendsClause(ExtendsClause),
198194
ImportClause(ImportClause),
199195
}
@@ -223,6 +219,12 @@ pub struct ExtendsClause {
223219
pub struct ComponentDeclaration {
224220
pub node_data: NodeData,
225221
pub name: String,
222+
pub type_specifier: TypeSpecifier,
223+
pub flags: ElementFlags,
224+
pub connection: Connection,
225+
pub variability: Variability,
226+
pub causality: Causality,
227+
pub visibility: Visibility,
226228
pub array_subscripts: Vec<Subscript>,
227229
pub modification: Option<Modification>,
228230
pub condition_attribute: Option<Expression>,
@@ -235,18 +237,6 @@ pub struct ClassPrefixes {
235237
pub class_type: ClassType,
236238
}
237239

238-
#[derive(CommonTraits!, Default)]
239-
pub struct ComponentClause {
240-
pub node_data: NodeData,
241-
pub type_specifier: TypeSpecifier,
242-
pub flags: ElementFlags,
243-
pub connection: Connection,
244-
pub variability: Variability,
245-
pub causality: Causality,
246-
pub array_subscripts: Vec<Subscript>,
247-
pub components: Vec<ComponentDeclaration>,
248-
}
249-
250240
#[derive(CommonTraits!, Default)]
251241
pub struct Declaration {
252242
pub node_data: NodeData,

src/s1_parser/modelica.lalrpop

+60-43
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,24 @@ pub ClassDefinition: ast::ClassDefinition = {
204204
match part {
205205
ast::CompositionPart::ElementList(list) => {
206206
for element in list.elements {
207+
match element {
208+
ast::Element::ImportClause(import) => {
209+
def.imports.push(import);
210+
}
211+
ast::Element::ExtendsClause(extends) => {
212+
def.extends.push(extends.type_specifier);
213+
}
214+
ast::Element::ClassDefinition(class) => {
215+
def.classes.insert(class.name.clone(), class);
216+
}
217+
ast::Element::ComponentClause(components) => {
218+
for mut comp in components {
219+
comp.visibility = list.visibility.clone();
220+
def.components.insert(comp.name.clone(), comp);
221+
}
222+
}
223+
ast::Element::Empty => {}
224+
}
207225
}
208226
},
209227
ast::CompositionPart::AlgorithmSection(sec) => {
@@ -423,30 +441,33 @@ pub ElementFlags : ast::ElementFlags = {
423441
}
424442

425443
pub Element : ast::Element = {
426-
<elem: ComponentClause> => ast::Element::ComponentClause(elem),
444+
<elem: ElementComponentClause> => ast::Element::ComponentClause(elem),
427445
<elem: ClassDefinition> => ast::Element::ClassDefinition(elem),
428446
<elem: ExtendsClause> => ast::Element::ExtendsClause(elem),
429447
}
430448

431-
pub ElementComponentClause : ast::ComponentClause = {
432-
<left: @L> <flags: ElementFlags> <clause: ComponentClause> <right: @R> => {
433-
let mut res = clause;
434-
res.flags = flags;
435-
res.node_data.span = (left, right);
436-
res
449+
pub ElementComponentClause : Vec<ast::ComponentDeclaration> = {
450+
<left: @L> <flags: ElementFlags> <mut components: ComponentClause> <right: @R> => {
451+
for comp in &mut components {
452+
comp.flags.replaceable |= flags.replaceable;
453+
comp.flags.redeclare |= flags.redeclare;
454+
comp.flags.is_final |= flags.is_final;
455+
comp.flags.inner |= flags.inner;
456+
comp.flags.outer |= flags.outer;
457+
}
458+
components
437459
},
438460
}
439461

440462
pub ElementClassDefinition : ast::ClassDefinition = {
441-
<left: @L> <flags: ElementFlags> <def: ClassDefinition> <right: @R> => {
442-
let mut res = def;
443-
res.flags.redeclare = flags.redeclare;
444-
res.flags.is_final = flags.is_final;
445-
res.flags.inner = flags.inner;
446-
res.flags.outer = flags.outer;
447-
res.flags.replaceable = flags.replaceable;
448-
res.node_data.span = (left, right);
449-
res
463+
<left: @L> <flags: ElementFlags> <mut def: ClassDefinition> <right: @R> => {
464+
def.flags.redeclare |= flags.redeclare;
465+
def.flags.is_final |= flags.is_final;
466+
def.flags.inner |= flags.inner;
467+
def.flags.outer |= flags.outer;
468+
def.flags.replaceable |= flags.replaceable;
469+
def.node_data.span = (left, right);
470+
def
450471
},
451472
}
452473

@@ -532,22 +553,21 @@ pub ExtendsClause: ast::ExtendsClause = {
532553

533554
//✅ component-clause :
534555
//✅ type-prefix type-specifier [ array-subscripts ] component-list
535-
pub ComponentClause: ast::ComponentClause = {
536-
<left: @L> <type_prefix: TypePrefix>
556+
pub ComponentClause: Vec<ast::ComponentDeclaration> = {
557+
<type_prefix: TypePrefix>
537558
<type_specifier: TypeSpecifier>
538559
<array_subscripts: ArraySubscripts?>
539-
<components: ComponentList> <right: @R> => {
540-
let id = context.new_id();
541-
ast::ComponentClause {
542-
node_data: NodeData::new(id, left, right),
543-
type_specifier,
544-
flags: ast::ElementFlags::default(),
545-
connection: type_prefix.connection,
546-
variability: type_prefix.variability,
547-
causality: type_prefix.causality,
548-
array_subscripts: array_subscripts.unwrap_or(Vec::new()),
549-
components,
560+
<mut components: ComponentList> => {
561+
for comp in &mut components {
562+
comp.type_specifier = type_specifier.clone();
563+
comp.causality = type_prefix.causality.clone();
564+
comp.variability = type_prefix.variability.clone();
565+
comp.connection = type_prefix.connection.clone();
566+
if let Some(ref subs) = array_subscripts {
567+
comp.array_subscripts = subs.clone();
568+
}
550569
}
570+
components
551571
}
552572
}
553573

@@ -602,6 +622,7 @@ pub ComponentDeclaration: ast::ComponentDeclaration = {
602622
modification: declaration.modification,
603623
condition_attribute,
604624
description,
625+
..Default::default()
605626
}
606627
}
607628
}
@@ -707,21 +728,16 @@ pub Argument: ast::Argument = {
707728

708729
//✅ component-clause1 :
709730
//✅ type-prefix type-specifier component-declaration1
710-
pub ComponentClause1: ast::ComponentClause = {
711-
<left: @L> <type_prefix: TypePrefix>
731+
pub ComponentClause1: ast::ComponentDeclaration = {
732+
<type_prefix: TypePrefix>
712733
<type_specifier: TypeSpecifier>
713-
<component_declaration1: ComponentDeclaration1> <right: @R> => {
714-
let id = context.new_id();
715-
ast::ComponentClause {
716-
node_data: NodeData::new(id, left, right),
717-
type_specifier,
718-
flags: ast::ElementFlags::default(),
719-
connection: type_prefix.connection,
720-
variability: type_prefix.variability,
721-
causality: type_prefix.causality,
722-
array_subscripts: Vec::new(),
723-
components: vec![component_declaration1],
724-
}
734+
<component_declaration1: ComponentDeclaration1> => {
735+
let mut comp = component_declaration1;
736+
comp.type_specifier = type_specifier;
737+
comp.causality = type_prefix.causality;
738+
comp.variability = type_prefix.variability;
739+
comp.connection = type_prefix.connection;
740+
comp
725741
}
726742
}
727743

@@ -738,6 +754,7 @@ pub ComponentDeclaration1: ast::ComponentDeclaration = {
738754
modification: declaration.modification,
739755
condition_attribute: None,
740756
description,
757+
..Default::default()
741758
}
742759
}
743760
}

0 commit comments

Comments
 (0)