aboutsummaryrefslogtreecommitdiffstats
path: root/modelica-parser-lalrpop/src/parser.lalrpop
diff options
context:
space:
mode:
Diffstat (limited to 'modelica-parser-lalrpop/src/parser.lalrpop')
-rw-r--r--modelica-parser-lalrpop/src/parser.lalrpop30
1 files changed, 22 insertions, 8 deletions
diff --git a/modelica-parser-lalrpop/src/parser.lalrpop b/modelica-parser-lalrpop/src/parser.lalrpop
index cdd15b4..8df904e 100644
--- a/modelica-parser-lalrpop/src/parser.lalrpop
+++ b/modelica-parser-lalrpop/src/parser.lalrpop
@@ -1,12 +1,14 @@
use std::str::FromStr;
-use ast::{ModelicaModel,Component, ComponentPrefix, Connection,
+use ast::{ModelicaModel, ComponentDeclaration, ComponentClause, ComponentPrefix, Connection,
SimpleEquation, Expr, BinOperator};
// This is an incomplete, non-standards-compliant, minimum-viable parser
+// Based on the Modelica 3.3r1 Spec
grammar;
-// Lexical Tokens
+// === Lexical Tokens ===
+// Roughly (but possibly not exactly) follows B.1
pub identifier: String = {
r"[a-zA-Z_][a-zA-Z_0-9]*" => <>.to_string(),
@@ -25,12 +27,16 @@ pub float: f64 = {
r"[+-]?\d+\.\d*([eE][-+]?\d+)?" => f64::from_str(<>).unwrap(),
};
+pub boolean: bool = {
+ "true" => true,
+ "false" => false,
+};
// Grammar
pub model: ModelicaModel = {
- "model" <n:identifier> <cd:component_declaration*> "equation" <cc:connect_clause*> <se:simple_equation*> "end" identifier ";" =>
- ModelicaModel { name:n, components: cd, 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 = {
@@ -41,9 +47,14 @@ units_declaration: String = {
"(" "unit" "=" <units:string_literal> ")" => units
};
-component_declaration: Component = {
- <prefix:component_prefix?> <specifier:identifier> <name:identifier> <units:units_declaration?> <value:value_declaration?> <desc:string_literal?> ";" =>
- Component { prefix:prefix, specifier:specifier, name:name, description:desc, value:value, units:units },
+component_clause: ComponentClause = {
+ <prefix:component_prefix?> <specifier:identifier> <declarations:component_declaration+> ";" =>
+ ComponentClause { prefix:prefix, specifier:specifier, declarations:declarations },
+};
+
+component_declaration: ComponentDeclaration = {
+ <name:identifier> <units:units_declaration?> <value:value_declaration?> <desc:string_literal?> (",")? =>
+ ComponentDeclaration { name:name, description:desc, value:value, units:units },
};
component_prefix: ComponentPrefix = {
@@ -57,7 +68,8 @@ component_prefix: ComponentPrefix = {
};
simple_equation: SimpleEquation = {
- <lhs:expr> "=" <rhs:expr> ";" => SimpleEquation {lhs:lhs, rhs:rhs},
+ <lhs:expr> ":=" <rhs:expr> ";" => SimpleEquation {lhs:lhs, rhs:rhs},
+ <lhs:expr> "=" <rhs:expr> ";" => SimpleEquation {lhs:lhs, rhs:rhs},
};
connect_clause: Connection = {
@@ -86,6 +98,8 @@ factor: Expr = {
term,
};
+// TODO: elementwise operators (".+", "./", ".*", ".-")
+
term: Expr = {
integer => Expr::Integer(<>),
float => Expr::Float(<>),