diff options
Diffstat (limited to 'modelica-parser-lalrpop/src/parser.lalrpop')
-rw-r--r-- | modelica-parser-lalrpop/src/parser.lalrpop | 107 |
1 files changed, 100 insertions, 7 deletions
diff --git a/modelica-parser-lalrpop/src/parser.lalrpop b/modelica-parser-lalrpop/src/parser.lalrpop index 36aa07d..447c51f 100644 --- a/modelica-parser-lalrpop/src/parser.lalrpop +++ b/modelica-parser-lalrpop/src/parser.lalrpop @@ -1,6 +1,7 @@ use std::str::FromStr; -use ast::{ModelicaModel, ComponentDeclaration, ComponentClause, ComponentPrefix, Connection, - SimpleEquation, Expr, BinOperator, MathUnaryFunc}; +use ast::{ModelicaCode, ModelicaPackage, ModelicaBlock, ModelicaConnector, ModelicaType, ModelicaModel, ComponentDeclaration, + ComponentClause, ComponentPrefix, Connection, SimpleEquation, Expr, + BinOperator, MathUnaryFunc}; // This is an incomplete, non-standards-compliant, minimum-viable parser // Based on the Modelica 3.3r1 Spec @@ -34,9 +35,85 @@ pub boolean: bool = { // Grammar +pub file: Vec<ModelicaCode> = { + <chunks:modelica_code+> => chunks, +}; + +pub modelica_code: ModelicaCode = { + model => ModelicaCode::Model(<>), + // TODO: class + // TODO: 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?> + <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, + component_clauses:cpc, }, +}; + +pub 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?> + <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, + component_clauses:cpc, + public_component_clauses:public, + protected_component_clauses:protected, + connections:cc, + equations:se, + extends:vec![] }, +}; + pub model: ModelicaModel = { - "model" <n:identifier> <desc:string_literal?> <cpc:component_clause*> "equation" <cc:connect_clause*> <se:simple_equation*> "end" identifier ";" => - ModelicaModel { name:n, description:desc, component_clauses:cpc, connections:cc, equations:se, extends:vec![] }, + "model" <n:identifier> <desc:string_literal?> + <cpc:component_clause*> + "equation" + <cc:connect_clause*> + <se:simple_equation*> + "end" identifier ";" => + ModelicaModel { + name:n, + description:desc, + component_clauses:cpc, + connections:cc, + equations:se, + extends:vec![] }, }; value_declaration: Expr = { @@ -53,8 +130,25 @@ component_clause: ComponentClause = { }; component_declaration: ComponentDeclaration = { - <name:identifier> <units:units_declaration?> <value:value_declaration?> <desc:string_literal?> (",")? => - ComponentDeclaration { name:name, description:desc, value:value, units:units, quantity:None }, + <name:identifier> + <ad:array_dimensions?> + <units:units_declaration?> + <value:value_declaration?> + <desc:string_literal?> + (",")? => + ComponentDeclaration { + name:name, + dimensions:ad, + description:desc, + value:value, + units:units, + quantity:None }, +}; + +// TODO: this is very partial/cludgy +array_dimensions: Vec<i64> = { + "[" <dimensions:(<integer> ","?)+> "]" => dimensions, + "[" ":" "]" => vec![], }; component_prefix: ComponentPrefix = { @@ -68,7 +162,6 @@ component_prefix: ComponentPrefix = { }; simple_equation: SimpleEquation = { - <lhs:expr> ":=" <rhs:expr> ";" => SimpleEquation {lhs:lhs, rhs:rhs}, <lhs:expr> "=" <rhs:expr> ";" => SimpleEquation {lhs:lhs, rhs:rhs}, }; |