-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoboChart_Parser.thy
188 lines (152 loc) · 6.28 KB
/
RoboChart_Parser.thy
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
section \<open> RoboChart Parser Library \<close>
theory RoboChart_Parser
imports RoboChart_AST "HOL.Real"
keywords
"var" "const" "clock" "opdecl" "terminates"
"broadcast" "event" "precondition" "postcondition"
"uses" "provides" "requires"
"state" "entry" "during" "exit" "probabilistic" "initial" "final" "junction"
"transition" "frm" "to" "trigger" "probability" "condition" "action" "!" "?" "]>" "<["
"opref" "sref" "rref" "cref" "connection" "on" "async" "mult"
begin
text \<open> We define a set of parser combinators for the RoboChart commands. These simply produce
elements of the AST, but do no semantic processing. The only exception is that types are parsed
and interpreted as these cannot be defined within most structures. Terms are not processed,
as this depends on the particular action semantics being employed. \<close>
ML \<open>
structure RC_Parser =
struct
open HOLogic;
open Parse;
open Scan;
open Syntax;
open RC_AST;
fun quad1 (((a, b), c), d) = (a, b, c, d);
fun quint1 ((((a, b), c), d), e) = (a, b, c, d, e);
val nameParser = name -- ($$$ "::" |-- typ);
val variableParser =
(nameParser -- (option (@{keyword "="} |-- term)))
>> variable;
val parameterParser =
@{keyword "("} |-- repeat (nameParser --| @{keyword ","}) -- nameParser --| @{keyword ")"}
>> (fn (xs, x) => xs @ [x])
val varDeclParser =
(@{keyword "var"} >> (fn _ => VAR) || @{keyword "const"} >> (fn _ => CNST))
-- repeat1 variableParser >> VarDecl;
val clockDeclParser =
(@{keyword "clock"} |-- name) >> ClockDecl;
val terminatesParser =
optional (@{keyword "["} |-- (@{keyword "terminates"} >> (fn _ => true)) --| @{keyword "]"}) false;
val operationSigParser =
@{keyword "opdecl"} |-- (name -- parameterParser -- terminatesParser) >> triple1 >> OpDecl
val eventDecl =
(Scan.optional (@{keyword "broadcast"} >> (fn _ => true)) false
-- (@{keyword "event"} |-- repeat1 (name -- optional ($$$ "::" |-- typ) @{type_name unit}))) >> EventDecl
val intfKeyParser =
varDeclParser || clockDeclParser || operationSigParser || eventDecl
val interfaceParser =
(name --
(@{keyword "="} |--
repeat1 intfKeyParser
)) >> mk_Interface;
val usesParser = @{keyword uses} |-- name >> UsesDecl;
val provParser = @{keyword provides} |-- name >> ProvDecl;
val reqParser = @{keyword requires} |-- name >> ReqDecl;
val containerDeclParser =
(intfKeyParser >> IntfDecl) || usesParser || provParser || reqParser;
val actionParser =
(@{keyword "entry"} |-- name >> Entry) ||
(@{keyword "during"} |-- name >> During) ||
(@{keyword "exit"} |-- name >> Exit);
(*
val eventParser =
((name --| (@{keyword "?"} -- @{keyword "["})) -- name --| @{keyword "]"}) >> Input ||
((name --| (@{keyword "!"} -- @{keyword "["})) -- term --| @{keyword "]"}) >> Output
val triggerParser =
(@{keyword "trigger"}) |--
option ($$$ "[" |-- term --| $$$ "]>") --
eventParser --
option ($$$ "<[" |-- term --| $$$ "]")
>> (fn ((bg, ev), ed) => Trigger_ext (bg, ev, NONE, [], ed, ()))
*)
val transitionParser =
(@{keyword "transition"} |-- name) --
($$$ "[" |--
(@{keyword "frm"} |-- name) --
(@{keyword "to"} |-- name) --
option (@{keyword "trigger"} |-- term) --
option (@{keyword "probability"} |-- term) --
option (@{keyword "condition"} |-- typ) --
option (@{keyword "action"} |-- term)
--| $$$ "]") >> (fn (n, (((((s, t), tr), p), c), a)) => Named_ext (n, Transition_ext (s, t, tr, p, c, a, ())));
(* We construct the node parser so that it is recursive, with descent in a maximum depth value.
This value can be unlimited but must be fixed we initialising the parser. We fix it to 4
for now, as it seems unlikely we'd ever want nested nodes of greater depth than this. *)
fun nodeParser 0 =
(@{keyword "initial"} |-- name >> Initial) ||
(@{keyword "junction"} |-- name >> Junction) ||
(@{keyword "final"} |-- name >> Final) ||
(@{keyword "probabilistic"} |-- name >> ProbabilisticJunction) |
nodeParser n =
nodeParser 0 ||
(@{keyword "state"} |-- name --
optional ($$$ "[" |--
repeat (actionParser >> ActionDecl
|| nodeParser (n - 1) >> InnerNodeDecl
|| transitionParser >> InnerTransitionDecl)
--| $$$ "]") [] >> (fn (n, ds) => mk_State n ds))
val stateMachineBodyParser =
(containerDeclParser >> StmContainerDecl) || (nodeParser 4 >> NodeDecl) || (transitionParser >> TransitionDecl) ;
val stateMachineDefParser =
(name --
(@{keyword "="} |--
repeat1 stateMachineBodyParser
));
val operationBodyParser =
(stateMachineBodyParser >> OpStmDecl) ||
((@{keyword "precondition"} |-- term) >> PreDecl) ||
((@{keyword "postcondition"} |-- term) >> PostDecl) ||
(@{keyword "terminates"} >> K TerminatesDecl)
val operationParser =
((name -- parameterParser) -- (@{keyword "="} |-- repeat1 operationBodyParser))
>> mk_Operation
val connectionParser =
((@{keyword connection} |-- name)
-- (@{keyword on} |-- name)
-- (@{keyword to} |-- name)
-- (@{keyword on} |-- name)
-- Scan.optional (@{keyword async} >> K true) false
-- Scan.optional (@{keyword mult} >> K true) false
) >> (fn (((((f,o1),t),o2),a),m) => Connection ((f, o1), (t, o2), a, m))
val controllerDeclParser =
(containerDeclParser >> CtrlContainerDecl) ||
(connectionParser >> ConnectionDecl) ||
(((@{keyword "sref"} |-- name) -- (@{keyword "="} |-- name)) >> StmRefDecl) ||
(((@{keyword "opref"} |-- name) -- (@{keyword "="} |-- name)) >> OpRefDecl)
val controllerParser =
(name --
(@{keyword "="} |--
repeat1 controllerDeclParser
)) >> mk_Controller;
val roboticPlatformParser =
(name --
(@{keyword "="} |--
repeat1 containerDeclParser
)) >> mk_Container;
val moduleDeclParser =
(containerDeclParser >> RCModuleContainerDecl) ||
(connectionParser >> ModConnectionDecl) ||
(((@{keyword "cref"} |-- name) -- (@{keyword "="} |-- name)) >> CRefDecl) ||
(((@{keyword "rref"} |-- name) -- (@{keyword "="} |-- name)) >> RRefDecl)
val moduleParser =
(name --
(@{keyword "="} |--
repeat1 moduleDeclParser
)) >> mk_RCModule;
val functionParser =
(name -- parameterParser -- ($$$ "::" |-- typ) --
repeat (@{keyword "precondition"} |-- term) --
repeat (@{keyword "postcondition"} |-- term)) >> quint1 >> FuncDecl
end
\<close>
end