From 957c2f75ceccfab3d8a8e546961db5f053876689 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Mon, 26 Dec 2016 00:03:07 -0800 Subject: radically simplify scope of parsing Parsing arbitrary files, packages, blocks, connectors, etc caused the compilation time for this parser to explode exponentially, and resulted in huge parser.rs and executable files. This commit rips out all those features. --- modelica-parser-lalrpop/examples/parse_file.rs | 2 +- modelica-parser-lalrpop/src/ast.rs | 55 ---------------- modelica-parser-lalrpop/src/lib.rs | 9 ++- modelica-parser-lalrpop/src/parser.lalrpop | 86 +------------------------- 4 files changed, 6 insertions(+), 146 deletions(-) (limited to 'modelica-parser-lalrpop') diff --git a/modelica-parser-lalrpop/examples/parse_file.rs b/modelica-parser-lalrpop/examples/parse_file.rs index fe87ef0..bac22f1 100644 --- a/modelica-parser-lalrpop/examples/parse_file.rs +++ b/modelica-parser-lalrpop/examples/parse_file.rs @@ -25,7 +25,7 @@ fn main() { } let striped = modelica_parser::strip_comments(&raw); - let result = modelica_parser::parser::parse_file(&striped); + let result = modelica_parser::parse_model(&striped); match result { Ok(_) => println!("=== {}: OK", input), diff --git a/modelica-parser-lalrpop/src/ast.rs b/modelica-parser-lalrpop/src/ast.rs index 52e91b8..7eb9dda 100644 --- a/modelica-parser-lalrpop/src/ast.rs +++ b/modelica-parser-lalrpop/src/ast.rs @@ -3,61 +3,6 @@ use std::clone::Clone; use std::fmt::{Debug, Formatter, Error}; use std::collections::HashMap; -// This represents a block of Modelica code. -// A valid .mo file will be a sequence of these -#[derive(Clone, PartialEq)] -pub enum ModelicaCode { - Class, // unimpl; generic - Model(ModelicaModel), - Record(ModelicaRecord), - Block(ModelicaBlock), - Connector(ModelicaConnector), - Type(ModelicaType), - Package(ModelicaPackage), - Function, // unimpl -} - -// A package is basically a namespace; they can be nested and contain any other -// code chunks (models, etc). -// They are optional; models don't need to live in a package to be valid. -#[derive(Clone, PartialEq)] -pub struct ModelicaPackage { - pub name: String, - pub description: Option, - pub children: Vec, -} - -#[derive(Clone, PartialEq)] -pub struct ModelicaType { - pub name: String, - pub description: Option, - pub component: ComponentDeclaration, -} - -#[derive(Clone, PartialEq)] -pub struct ModelicaBlock { - pub name: String, - pub description: Option, - pub components: Vec, - pub public_components: Vec, - pub protected_components: Vec, - pub equations: Vec, - pub connections: Vec, -} - -#[derive(Clone, PartialEq)] -pub struct ModelicaConnector { - pub name: String, - pub description: Option, - pub components: Vec, -} - -#[derive(Clone, PartialEq)] -pub struct ModelicaRecord { - pub name: String, - pub description: Option, - pub components: Vec, -} #[derive(Clone, PartialEq)] pub struct ModelicaModel { diff --git a/modelica-parser-lalrpop/src/lib.rs b/modelica-parser-lalrpop/src/lib.rs index 26fca79..b515d73 100644 --- a/modelica-parser-lalrpop/src/lib.rs +++ b/modelica-parser-lalrpop/src/lib.rs @@ -12,12 +12,11 @@ use regex::Regex; pub use ast::*; pub use parser::{ - parse_file, parse_model, - parse_package, - parse_block, - parse_connector, - parse_record, + parse_integer, + parse_float, + parse_boolean, + parse_identifier, }; pub fn strip_comments(raw: &str) -> String { diff --git a/modelica-parser-lalrpop/src/parser.lalrpop b/modelica-parser-lalrpop/src/parser.lalrpop index 442baca..94795c6 100644 --- a/modelica-parser-lalrpop/src/parser.lalrpop +++ b/modelica-parser-lalrpop/src/parser.lalrpop @@ -1,7 +1,6 @@ use std::str::FromStr; use std::collections::HashMap; -use ast::{ModelicaCode, ModelicaPackage, ModelicaBlock, ModelicaConnector, - ModelicaType, ModelicaModel, ComponentDeclaration, ModelicaRecord, +use ast::{ModelicaModel, ComponentDeclaration, ComponentClause, ComponentPrefix, Connection, SimpleEquation, Expr, BinOperator, MathUnaryFunc, collapse_components}; @@ -40,87 +39,10 @@ pub boolean: bool = { // Grammar -pub file: Vec = { - within_clause? => chunks, -}; - -pub modelica_code: ModelicaCode = { - model => ModelicaCode::Model(<>), - // TODO: class - record => ModelicaCode::Record(<>), - block => ModelicaCode::Block(<>), - connector => ModelicaCode::Connector(<>), - type_declaration => ModelicaCode::Type(<>), - package => ModelicaCode::Package(<>), - // TODO: function -}; - -pub package: ModelicaPackage = { - "package" - extends_clause* - - "end" identifier ";" => - ModelicaPackage { - name:n, - description:desc, - children:children, - }, -}; - -pub connector: ModelicaConnector = { - "connector" - - "end" identifier ";" => - ModelicaConnector { - name:n, - description:desc, - components: { collapse_components(&cpc) } }, -}; - -pub record: ModelicaRecord = { - "record" - - "end" identifier ";" => - ModelicaRecord { - name:n, - description:desc, - components: { collapse_components(&cpc) } }, -}; - -type_declaration: ModelicaType = { - "type" - "=" ";" => - ModelicaType { - name:n, - description:desc, - component:cpd, }, -}; - -pub block: ModelicaBlock = { - "block" - extends_clause* - - )?> - )?> - "equation" - - - "end" identifier ";" => - ModelicaBlock { - name:n, - description:desc, - components: { collapse_components(&cpc) }, - public_components: { collapse_components(&public.unwrap_or(vec![])) }, - protected_components: { collapse_components(&protected.unwrap_or(vec![])) }, - connections:cc, - equations:se }, -}; - pub model: ModelicaModel = { "partial"? "model" extends_clause* - connector* "equation" @@ -268,11 +190,5 @@ term: Expr = { "log" "(" ")" => Expr::MathUnaryExpr(MathUnaryFunc::Log, Box::new(e)), "log10" "(" ")" => Expr::MathUnaryExpr(MathUnaryFunc::Log10, Box::new(e)), "(" ")" => e, - // Obviously a hack here, only supporting up to 4 elements in an array - "[" "]" => Expr::Array(vec![e]), - "[" ";" "]" => Expr::Array(vec![e1, e2]), - "[" "," "]" => Expr::Array(vec![e1, e2]), - "[" "," "," "]" => Expr::Array(vec![e1, e2, e3]), - "[" "," "," "," "]" => Expr::Array(vec![e1, e2, e3, e4]), }; -- cgit v1.2.3