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 --- src/modelica_parser.lalrpop | 97 --------------------------------------------- 1 file changed, 97 deletions(-) delete mode 100644 src/modelica_parser.lalrpop (limited to 'src/modelica_parser.lalrpop') diff --git a/src/modelica_parser.lalrpop b/src/modelica_parser.lalrpop deleted file mode 100644 index f5d7958..0000000 --- a/src/modelica_parser.lalrpop +++ /dev/null @@ -1,97 +0,0 @@ -use std::str::FromStr; -use modelica_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