diff options
Diffstat (limited to 'modelica-parser-lalrpop/src/parser.lalrpop')
-rw-r--r-- | modelica-parser-lalrpop/src/parser.lalrpop | 48 |
1 files changed, 35 insertions, 13 deletions
diff --git a/modelica-parser-lalrpop/src/parser.lalrpop b/modelica-parser-lalrpop/src/parser.lalrpop index b05ad76..442baca 100644 --- a/modelica-parser-lalrpop/src/parser.lalrpop +++ b/modelica-parser-lalrpop/src/parser.lalrpop @@ -1,8 +1,9 @@ use std::str::FromStr; use std::collections::HashMap; -use ast::{ModelicaCode, ModelicaPackage, ModelicaBlock, ModelicaConnector, ModelicaType, ModelicaModel, ComponentDeclaration, +use ast::{ModelicaCode, ModelicaPackage, ModelicaBlock, ModelicaConnector, + ModelicaType, ModelicaModel, ComponentDeclaration, ModelicaRecord, ComponentClause, ComponentPrefix, Connection, SimpleEquation, Expr, - BinOperator, MathUnaryFunc}; + BinOperator, MathUnaryFunc, collapse_components}; // This is an incomplete, non-standards-compliant, minimum-viable parser // Based on the Modelica 3.3r1 Spec @@ -16,7 +17,8 @@ grammar; pub identifier: String = { r"[a-zA-Z_][a-zA-Z_0-9]*" => <>.to_string(), - r"[a-zA-Z_][a-zA-Z_0-9]*\.[a-zA-Z_0-9]*" => <>.to_string(), + r"[a-zA-Z_][a-zA-Z_0-9]*\.[a-zA-Z_0-9]+" => <>.to_string(), + r"[a-zA-Z_][a-zA-Z_0-9]*\.[a-zA-Z_0-9]+\.[a-zA-Z_0-9]" => <>.to_string(), }; string_literal: String = { @@ -39,13 +41,13 @@ pub boolean: bool = { // Grammar pub file: Vec<ModelicaCode> = { - <chunks:modelica_code+> => chunks, + within_clause? <chunks:modelica_code+> => chunks, }; pub modelica_code: ModelicaCode = { model => ModelicaCode::Model(<>), // TODO: class - // TODO: record + record => ModelicaCode::Record(<>), block => ModelicaCode::Block(<>), connector => ModelicaCode::Connector(<>), type_declaration => ModelicaCode::Type(<>), @@ -55,6 +57,7 @@ pub modelica_code: ModelicaCode = { pub package: ModelicaPackage = { "package" <n:identifier> <desc:string_literal?> + extends_clause* <children:modelica_code*> "end" identifier ";" => ModelicaPackage { @@ -71,7 +74,17 @@ pub connector: ModelicaConnector = { ModelicaConnector { name:n, description:desc, - component_clauses:cpc, }, + 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 = { @@ -85,6 +98,7 @@ type_declaration: ModelicaType = { pub block: ModelicaBlock = { "block" <n:identifier> <desc:string_literal?> + extends_clause* <cpc:component_clause*> <public:("public" <component_clause*>)?> <protected:("protected" <component_clause*>)?> @@ -95,16 +109,16 @@ pub block: ModelicaBlock = { ModelicaBlock { name:n, description:desc, - component_clauses:cpc, - public_component_clauses: { public.unwrap_or(vec![]) }, - protected_component_clauses: { protected.unwrap_or(vec![]) }, + components: { collapse_components(&cpc) }, + public_components: { collapse_components(&public.unwrap_or(vec![])) }, + protected_components: { collapse_components(&protected.unwrap_or(vec![])) }, connections:cc, - equations:se, - extends:vec![] }, + equations:se }, }; pub model: ModelicaModel = { "partial"? "model" <n:identifier> <desc:string_literal?> + extends_clause* <cpc:component_clause*> connector* "equation" @@ -114,10 +128,18 @@ pub model: ModelicaModel = { ModelicaModel { name:n, description:desc, - component_clauses:cpc, + components: { collapse_components(&cpc) }, connections:cc, equations:se, - extends:vec![] }, + }, +}; + +extends_clause: String = { + "extends" <identifier> ";" => <>, +}; + +within_clause: String = { + "within" <identifier> ";" => <>, }; component_clause: ComponentClause = { |