diff options
author | bnewbold <bnewbold@robocracy.org> | 2016-12-18 11:02:38 -0800 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2016-12-18 11:02:38 -0800 |
commit | 37dba2763441721456be50f709c38bee9f6e71f9 (patch) | |
tree | b86c8335c872762bcfb451a2090b3dd3a2684667 /modelica-parser-lalrpop/src/parser.lalrpop | |
parent | c8ddcd6248fd9cfea450b2fc52587b8179c0a0f8 (diff) | |
download | modelthing-37dba2763441721456be50f709c38bee9f6e71f9.tar.gz modelthing-37dba2763441721456be50f709c38bee9f6e71f9.zip |
parser: refactor components into clauses/decl
Diffstat (limited to 'modelica-parser-lalrpop/src/parser.lalrpop')
-rw-r--r-- | modelica-parser-lalrpop/src/parser.lalrpop | 30 |
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(<>), |