Skip to content

Commit

Permalink
Add ArraySubscript.
Browse files Browse the repository at this point in the history
Signed-off-by: James Goppert <james.goppert@gmail.com>
  • Loading branch information
jgoppert committed Feb 4, 2025
1 parent e263c35 commit 6c21442
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "rumoca_parser"
authors = ["James Goppert", "Benjamin Perseghetti"]
description = "A Modelica parser leveraging LALRPOP"
version = "0.10.3"
version = "0.10.4"
edition = "2021"
license = "Apache-2.0"

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ pub mod s2_analysis;
extern crate macro_rules_attribute;

pub use s1_parser::ast;
pub use s2_analysis::parser_helper::parse_file;
pub use s2_analysis::parser_helper::{parse, parse_file};
pub use s2_analysis::print_visitor::PrintVisitor;
pub use s2_analysis::{Node, NodeMutRef, NodeRef, Visitable, VisitableMut, Visitor, VisitorMut};
2 changes: 1 addition & 1 deletion src/s0_lexer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod tests {

#[test]
fn test_lexer() {
let filename = String::from("test/models/integrator.mo");
let filename = String::from("tests/models/integrator.mo");
let source_code = std::fs::read_to_string(filename).unwrap();
let mut lexer = Lexer::new(&source_code);
for result in &mut lexer {
Expand Down
11 changes: 8 additions & 3 deletions src/s1_parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub struct ClassPrefixes {
pub struct ComponentClause {
pub type_prefix: TypePrefix,
pub type_specifier: TypeSpecifier,
pub array_subscripts: Vec<Subscript>,
pub array_subscripts: ArraySubscripts,
pub components: Vec<ComponentDeclaration>,
}

Expand All @@ -123,7 +123,7 @@ pub struct ComponentClause1 {
#[derive(CommonTraits!, Default)]
pub struct Declaration {
pub name: String,
pub array_subscripts: Vec<Subscript>,
pub array_subscripts: ArraySubscripts,
pub modification: Option<Modification>,
}

Expand Down Expand Up @@ -252,7 +252,12 @@ pub struct ComponentReference {
#[derive(CommonTraits!, Default)]
pub struct RefPart {
pub name: String,
pub array_subscripts: Vec<Subscript>,
pub array_subscripts: ArraySubscripts,
}

#[derive(CommonTraits!, Default)]
pub struct ArraySubscripts {
pub subscripts: Vec<Subscript>,
}

#[derive(CommonTraits!, Default)]
Expand Down
14 changes: 8 additions & 6 deletions src/s1_parser/modelica.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ pub ComponentClause: ast::ComponentClause = {
ast::ComponentClause {
type_prefix,
type_specifier,
array_subscripts: array_subscripts.unwrap_or(Vec::new()),
array_subscripts: array_subscripts.unwrap_or(ast::ArraySubscripts::default()),
components,
}
}
Expand Down Expand Up @@ -531,7 +531,7 @@ pub Declaration: ast::Declaration = {
<modification:Modification?> => {
ast::Declaration {
name,
array_subscripts: array_subscripts.unwrap_or(Vec::new()),
array_subscripts: array_subscripts.unwrap_or(ast::ArraySubscripts::default()),
modification,
}
}
Expand Down Expand Up @@ -1138,7 +1138,7 @@ SimpleExpression: ast::Expression = {
local: false,
parts: vec![ast::RefPart{
name: func,
array_subscripts: Vec::new()
array_subscripts: ast::ArraySubscripts::default(),
}]
},
args,
Expand Down Expand Up @@ -1185,7 +1185,7 @@ pub RefPart: ast::RefPart = {
<name:IDENT> <array_subscripts:ArraySubscripts?> => {
ast::RefPart {
name,
array_subscripts: array_subscripts.unwrap_or(Vec::new()),
array_subscripts: array_subscripts.unwrap_or(ast::ArraySubscripts::default()),
}
}
}
Expand Down Expand Up @@ -1233,8 +1233,10 @@ pub FunctionCallArguments: Vec<ast::Expression> = {

//✅ array-subscripts :
//✅ "[" subscript { "," subscript } "]"
pub ArraySubscripts: Vec<ast::Subscript> = {
"[" <sub: SeparatedList<Subscript, ",">> "]" => sub,
pub ArraySubscripts: ast::ArraySubscripts = {
"[" <subscripts: SeparatedList<Subscript, ",">> "]" => {
ast::ArraySubscripts{ subscripts }
}
}
//✅ subscript :
//✅ ":" | expression
Expand Down
2 changes: 2 additions & 0 deletions src/s2_analysis/ast_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ from_node!(
IfExpressionBlock,
ComponentReference,
RefPart,
ArraySubscripts,
Subscript,
Argument,
Modification,
Expand Down Expand Up @@ -170,6 +171,7 @@ define_node_enums!(
IfExpressionBlock,
ComponentReference,
RefPart,
ArraySubscripts,
Subscript,
Argument,
Modification,
Expand Down
10 changes: 6 additions & 4 deletions src/s2_analysis/parser_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ use md5;
use std::process;

pub fn parse_file(filename: &str) -> ast::StoredDefinition {
let file_txt = std::fs::read_to_string(filename).expect("failed to read file");
parse(filename, &file_txt)
}

pub fn parse(filename: &str, file_txt: &str) -> ast::StoredDefinition {
let mut files = SimpleFiles::new();
let file_id = files.add(
filename,
std::fs::read_to_string(filename).expect("failed to read file"),
);
let file_id = files.add(filename, file_txt);
let file = files.get(file_id).expect("failed to get file id");
let file_txt = file.source();
let lexer = Lexer::new(file_txt);
Expand Down
25 changes: 16 additions & 9 deletions src/s2_analysis/visitable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl<'a> Visitable<'a> for NodeRef<'a> {
IfExpressionBlock,
ComponentReference,
RefPart,
ArraySubscripts,
Subscript,
Argument,
Modification,
Expand Down Expand Up @@ -284,9 +285,7 @@ impl<'a> Visitable<'a> for ast::ComponentClause {
visitor.enter_component_clause(self);
self.type_prefix.accept(visitor);
self.type_specifier.accept(visitor);
for sub in self.array_subscripts.iter() {
sub.accept(visitor);
}
self.array_subscripts.accept(visitor);
for comp in self.components.iter() {
comp.accept(visitor);
}
Expand All @@ -311,9 +310,7 @@ impl<'a> Visitable<'a> for ast::Declaration {
fn accept<V: Visitor<'a> + ?Sized>(&'a self, visitor: &mut V) {
visitor.enter_any(NodeRef::Declaration(self));
visitor.enter_declaration(self);
for sub in self.array_subscripts.iter() {
sub.accept(visitor);
}
self.array_subscripts.accept(visitor);
if let Some(modif) = self.modification.as_ref() {
modif.accept(visitor);
}
Expand Down Expand Up @@ -545,14 +542,24 @@ impl<'a> Visitable<'a> for ast::RefPart {
fn accept<V: Visitor<'a> + ?Sized>(&'a self, visitor: &mut V) {
visitor.enter_any(NodeRef::RefPart(self));
visitor.enter_ref_part(self);
for sub in self.array_subscripts.iter() {
sub.accept(visitor);
}
self.array_subscripts.accept(visitor);
visitor.exit_ref_part(self);
visitor.exit_any(NodeRef::RefPart(self));
}
}

impl<'a> Visitable<'a> for ast::ArraySubscripts {
fn accept<V: Visitor<'a> + ?Sized>(&'a self, visitor: &mut V) {
visitor.enter_any(NodeRef::ArraySubscripts(self));
visitor.enter_array_subscripts(self);
for sub in self.subscripts.iter() {
sub.accept(visitor);
}
visitor.exit_array_subscripts(self);
visitor.exit_any(NodeRef::ArraySubscripts(self));
}
}

impl<'a> Visitable<'a> for ast::Subscript {
fn accept<V: Visitor<'a> + ?Sized>(&'a self, visitor: &mut V) {
visitor.enter_any(NodeRef::Subscript(self));
Expand Down
25 changes: 16 additions & 9 deletions src/s2_analysis/visitable_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl VisitableMut for Node {
IfExpressionBlock,
ComponentReference,
RefPart,
ArraySubscripts,
Subscript,
Argument,
Modification,
Expand Down Expand Up @@ -283,9 +284,7 @@ impl VisitableMut for ast::ComponentClause {
visitor.enter_component_clause_mut(self);
self.type_prefix.accept_mut(visitor);
self.type_specifier.accept_mut(visitor);
for sub in self.array_subscripts.iter_mut() {
sub.accept_mut(visitor);
}
self.array_subscripts.accept_mut(visitor);
for comp in self.components.iter_mut() {
comp.accept_mut(visitor);
}
Expand All @@ -310,9 +309,7 @@ impl VisitableMut for ast::Declaration {
fn accept_mut<V: VisitorMut + ?Sized>(&mut self, visitor: &mut V) {
visitor.enter_any(NodeRef::Declaration(self));
visitor.enter_declaration_mut(self);
for sub in self.array_subscripts.iter_mut() {
sub.accept_mut(visitor);
}
self.array_subscripts.accept_mut(visitor);
if let Some(modif) = self.modification.as_mut() {
modif.accept_mut(visitor);
}
Expand Down Expand Up @@ -544,14 +541,24 @@ impl VisitableMut for ast::RefPart {
fn accept_mut<V: VisitorMut + ?Sized>(&mut self, visitor: &mut V) {
visitor.enter_any(NodeRef::RefPart(self));
visitor.enter_ref_part_mut(self);
for sub in self.array_subscripts.iter_mut() {
sub.accept_mut(visitor);
}
self.array_subscripts.accept_mut(visitor);
visitor.exit_ref_part_mut(self);
visitor.exit_any(NodeRef::RefPart(self));
}
}

impl VisitableMut for ast::ArraySubscripts {
fn accept_mut<V: VisitorMut + ?Sized>(&mut self, visitor: &mut V) {
visitor.enter_any(NodeRef::ArraySubscripts(self));
visitor.enter_array_subscripts_mut(self);
for sub in self.subscripts.iter_mut() {
sub.accept_mut(visitor);
}
visitor.exit_array_subscripts_mut(self);
visitor.exit_any(NodeRef::ArraySubscripts(self));
}
}

impl VisitableMut for ast::Subscript {
fn accept_mut<V: VisitorMut + ?Sized>(&mut self, visitor: &mut V) {
visitor.enter_any(NodeRef::Subscript(self));
Expand Down
1 change: 1 addition & 0 deletions src/s2_analysis/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub trait Visitor<'a> {
ComponentReference,
RefPart,
Subscript,
ArraySubscripts,
Argument,
Modification,
ModExpr,
Expand Down
1 change: 1 addition & 0 deletions src/s2_analysis/visitor_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub trait VisitorMut {
ComponentReference,
RefPart,
Subscript,
ArraySubscripts,
Argument,
Modification,
ModExpr,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 6c21442

Please sign in to comment.