use std::str::FromStr; // XXX: use modelica_ast::{Model, Component, ComponentPrefix, Connection, SimpleEquation, Bin}; use modelica_ast::*; // 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: Model = { "model" "equation" "end" identifier ";" => Model { name:n, components: cd, connections: cc, equations: se, extends: vec![] }, }; component_declaration: Component = { string_literal? ";" => Component { prefix:prefix, specifier:specifier, name:name}, "=" string_literal? ";" => Component { prefix:prefix, specifier:specifier, name:name}, }; 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, };