aboutsummaryrefslogtreecommitdiffstats
path: root/src/modelica_parser.lalrpop
blob: 09036bd369936584bffe066741df64d7fbd28cc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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,
};