diff options
Diffstat (limited to 'src/modelica_parser.lalrpop')
-rw-r--r-- | src/modelica_parser.lalrpop | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/modelica_parser.lalrpop b/src/modelica_parser.lalrpop new file mode 100644 index 0000000..09036bd --- /dev/null +++ b/src/modelica_parser.lalrpop @@ -0,0 +1,95 @@ +// XXX: use std::str::FromStr; + +// This is an incomplete, non-standards-compliant, minimum-viable parser + +grammar; + +// Lexical Tokens + +identifier: () = { + r"[a-zA-Z_][a-zA-Z_0-9]*", +}; + +string_literal: () = { + r#""[^"\\]*""#, + //<s:r#""[^"\\]*""#> => &s[1..s.len()-1], +}; + +pub integer: () = { + r"[+-]?\d+", +}; + +float: () = { + r"[+-]?\d+\.\d*([eE][-+]?\d+)?", +}; + + +// Grammar + +pub model: () = { + "model" identifier component_declaration* "equation" equation_entry* "end" identifier ";", +}; + +equation_entry: () = { + simple_equation, + connect_clause, +}; + +component_declaration: () = { + component_prefix? identifier identifier string_literal? ";", +}; + +component_prefix: () = { + "flow", + "stream", + "input", + "output", + "discrete", + "parameter", + "constant", +}; + +simple_equation: () = { + expr "=" expr ";", +}; + +connect_clause: () = { + "connect" "(" identifier "," identifier ")" ";", +}; + +// This weird expr/factor/term hierarchy is for binary operator precedence +expr: () = { + expr "+" factor, + expr "-" factor, + factor, +}; + +factor: () = { + factor "*" term, + factor "/" term, + "-" term, + term, +}; + +term: () = { + integer, + float, + identifier, + "der" "(" expr ")", + "abs" "(" expr ")", + "(" expr ")", +}; + +binary_op: () = { + "+", + "-", + "*", + "/", +}; + +//// Meta/High-Level + +pub file: () = { + model, +}; + |