From 9f82aceb9fbdb42f332d68f4a423123bd0788b2c Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sat, 17 Dec 2016 18:34:47 -0800 Subject: refactor modelica parser into separate crate --- modelica-parser-lalrpop/src/parser.lalrpop | 97 ++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 modelica-parser-lalrpop/src/parser.lalrpop (limited to 'modelica-parser-lalrpop/src/parser.lalrpop') diff --git a/modelica-parser-lalrpop/src/parser.lalrpop b/modelica-parser-lalrpop/src/parser.lalrpop new file mode 100644 index 0000000..cdd15b4 --- /dev/null +++ b/modelica-parser-lalrpop/src/parser.lalrpop @@ -0,0 +1,97 @@ +use std::str::FromStr; +use ast::{ModelicaModel,Component, ComponentPrefix, Connection, + SimpleEquation, Expr, BinOperator}; + +// This is an incomplete, non-standards-compliant, minimum-viable parser + +grammar; + +// Lexical Tokens + +pub identifier: String = { + r"[a-zA-Z_][a-zA-Z_0-9]*" => <>.to_string(), +}; + +string_literal: String = { + r#""[^"\\]*""# => <>.to_string(), + // => &s[1..s.len()-1], +}; + +pub integer: i64 = { + r"[+-]?\d+" => i64::from_str(<>).unwrap(), +}; + +pub float: f64 = { + r"[+-]?\d+\.\d*([eE][-+]?\d+)?" => f64::from_str(<>).unwrap(), +}; + + +// Grammar + +pub model: ModelicaModel = { + "model" "equation" "end" identifier ";" => + ModelicaModel { name:n, components: cd, connections: cc, equations: se, extends: vec![] }, +}; + +value_declaration: Expr = { + "=" => value +}; + +units_declaration: String = { + "(" "unit" "=" ")" => units +}; + +component_declaration: Component = { + ";" => + Component { prefix:prefix, specifier:specifier, name:name, description:desc, value:value, units:units }, +}; + +component_prefix: ComponentPrefix = { + "flow" => ComponentPrefix::Flow, + "stream" => ComponentPrefix::Stream, + "input" => ComponentPrefix::Input, + "output" => ComponentPrefix::Output, + "discrete" => ComponentPrefix::Discrete, + "parameter" => ComponentPrefix::Parameter, + "constant" => ComponentPrefix::Constant, +}; + +simple_equation: SimpleEquation = { + "=" ";" => SimpleEquation {lhs:lhs, rhs:rhs}, +}; + +connect_clause: Connection = { + "connect" "(" "," ")" ";" => + Connection { a: a.to_string(), b: b.to_string()}, +}; + +// This weird expr/factor/term hierarchy is for binary operator precedence +expr: Expr = { + "+" => + Expr::BinExpr(BinOperator::Add, Box::new(lhs), Box::new(rhs)), + "-" => + Expr::BinExpr(BinOperator::Subtract, Box::new(lhs), Box::new(rhs)), + factor, +}; + +factor: Expr = { + "*" => + Expr::BinExpr(BinOperator::Multiply, Box::new(lhs), Box::new(rhs)), + "/" => + Expr::BinExpr(BinOperator::Divide, Box::new(lhs), Box::new(rhs)), + "^" => + Expr::BinExpr(BinOperator::Divide, Box::new(lhs), Box::new(rhs)), + "-" => + Expr::BinExpr(BinOperator::Multiply, Box::new(Expr::Integer(-1)), Box::new(t)), + term, +}; + +term: Expr = { + integer => Expr::Integer(<>), + float => Expr::Float(<>), + identifier => Expr::Ident(<>), + "der" "(" ")" => Expr::Der(Box::new(e)), + "abs" "(" ")" => Expr::Abs(Box::new(e)), + "(" ")" => e, +}; + -- cgit v1.2.3