diff options
author | bnewbold <bnewbold@robocracy.org> | 2016-09-18 14:24:44 -0700 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2016-09-18 14:25:07 -0700 |
commit | f264a289fe0d504a99c01d5787edb92af8f6ca91 (patch) | |
tree | d46f54df1d2db703583a0ae7d612428982c8fb2a /src/modelica_parser.lalrpop | |
parent | 77183773db84f5cd287bed62d3c844346a36bd74 (diff) | |
download | modelthing-f264a289fe0d504a99c01d5787edb92af8f6ca91.tar.gz modelthing-f264a289fe0d504a99c01d5787edb92af8f6ca91.zip |
work on a minimal modelica parser+ast
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, +}; + |