diff options
author | bnewbold <bnewbold@robocracy.org> | 2016-12-26 00:03:07 -0800 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2016-12-26 00:03:09 -0800 |
commit | 957c2f75ceccfab3d8a8e546961db5f053876689 (patch) | |
tree | 5eae47044a720fa29b1f9396f54b7db9a482de4d /modelica-parser-lalrpop | |
parent | 1e04c8274e473039d2b183c120ab7f3b1b981c3e (diff) | |
download | modelthing-957c2f75ceccfab3d8a8e546961db5f053876689.tar.gz modelthing-957c2f75ceccfab3d8a8e546961db5f053876689.zip |
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.
Diffstat (limited to 'modelica-parser-lalrpop')
-rw-r--r-- | modelica-parser-lalrpop/examples/parse_file.rs | 2 | ||||
-rw-r--r-- | modelica-parser-lalrpop/src/ast.rs | 55 | ||||
-rw-r--r-- | modelica-parser-lalrpop/src/lib.rs | 9 | ||||
-rw-r--r-- | modelica-parser-lalrpop/src/parser.lalrpop | 86 |
4 files changed, 6 insertions, 146 deletions
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<String>, - pub children: Vec<ModelicaCode>, -} - -#[derive(Clone, PartialEq)] -pub struct ModelicaType { - pub name: String, - pub description: Option<String>, - pub component: ComponentDeclaration, -} - -#[derive(Clone, PartialEq)] -pub struct ModelicaBlock { - pub name: String, - pub description: Option<String>, - pub components: Vec<Component>, - pub public_components: Vec<Component>, - pub protected_components: Vec<Component>, - pub equations: Vec<SimpleEquation>, - pub connections: Vec<Connection>, -} - -#[derive(Clone, PartialEq)] -pub struct ModelicaConnector { - pub name: String, - pub description: Option<String>, - pub components: Vec<Component>, -} - -#[derive(Clone, PartialEq)] -pub struct ModelicaRecord { - pub name: String, - pub description: Option<String>, - pub components: Vec<Component>, -} #[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<ModelicaCode> = { - within_clause? <chunks:modelica_code+> => 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" <n:identifier> <desc:string_literal?> - extends_clause* - <children:modelica_code*> - "end" identifier ";" => - ModelicaPackage { - name:n, - description:desc, - children:children, - }, -}; - -pub connector: ModelicaConnector = { - "connector" <n:identifier> <desc:string_literal?> - <cpc:component_clause*> - "end" identifier ";" => - ModelicaConnector { - name:n, - description:desc, - components: { collapse_components(&cpc) } }, -}; - -pub record: ModelicaRecord = { - "record" <n:identifier> <desc:string_literal?> - <cpc:component_clause*> - "end" identifier ";" => - ModelicaRecord { - name:n, - description:desc, - components: { collapse_components(&cpc) } }, -}; - -type_declaration: ModelicaType = { - "type" <n:identifier> <desc:string_literal?> - "=" <cpd:component_declaration> ";" => - ModelicaType { - name:n, - description:desc, - component:cpd, }, -}; - -pub block: ModelicaBlock = { - "block" <n:identifier> <desc:string_literal?> - extends_clause* - <cpc:component_clause*> - <public:("public" <component_clause*>)?> - <protected:("protected" <component_clause*>)?> - "equation" - <cc:connect_clause*> - <se:simple_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" <n:identifier> <desc:string_literal?> extends_clause* <cpc:component_clause*> - connector* "equation" <cc:connect_clause*> <se:simple_equation*> @@ -268,11 +190,5 @@ term: Expr = { "log" "(" <e:expr> ")" => Expr::MathUnaryExpr(MathUnaryFunc::Log, Box::new(e)), "log10" "(" <e:expr> ")" => Expr::MathUnaryExpr(MathUnaryFunc::Log10, Box::new(e)), "(" <e:expr> ")" => e, - // Obviously a hack here, only supporting up to 4 elements in an array - "[" <e:expr> "]" => Expr::Array(vec![e]), - "[" <e1:expr> ";" <e2:expr> "]" => Expr::Array(vec![e1, e2]), - "[" <e1:expr> "," <e2:expr> "]" => Expr::Array(vec![e1, e2]), - "[" <e1:expr> "," <e2:expr> "," <e3:expr> "]" => Expr::Array(vec![e1, e2, e3]), - "[" <e1:expr> "," <e2:expr> "," <e3:expr> "," <e4:expr> "]" => Expr::Array(vec![e1, e2, e3, e4]), }; |