Source file spec.ml
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
(** Specification structures for LRGrep
This module defines the core data structures that represent the user's
error specification after parsing and resolution: clauses, branches, and
their relationships.
Design:
- A *clause* represents one alternative in the user's grammar definition.
Multiple clauses can be grouped together (e.g., using `%%shortest`).
- A *branch* is a single pattern-action pair. Each clause contains one
or more branches (patterns), each with its own action.
- Key relationships:
- [of_clause] maps each clause to the set of branches in that clause
- [clause] maps each branch to its containing clause
- [priority] tracks the priority chain: branches in the same group
have the same priority, branches in different groups have higher
priority if they appear earlier
- Data structures:
- [branches]: Contains all branch-level information:
- [clause], [pattern], [expr]: The branch's membership, syntax, and
compiled regular expression
- [of_clause]: Mapping from clauses to branches
- [lookaheads]: Optional lookahead constraints for each branch
- [br_captures]: Captured variables for each branch
- [is_total], [is_partial]: Whether branches accept all inputs or
can fail
- [priority]: Priority mapping for branch handling
- The import_rules function transforms the user-facing syntax into these
internal structures:
- Parsing clauses and their actions (total/partial/unreachable)
- Compiling patterns to Expr.t using the [Transl.transl] function
- Computing priority relationships between branches
Tricky implementation details:
- The priority system is complex: branches within a group share priority,
but branches in later groups have lower priority. This enables
specification like:
clause1 : pattern1a | pattern1b (high priority)
clause2 : pattern2a | pattern2b (lower priority)
- The [is_total] vs [is_partial] distinction matters for coverage analysis:
total clauses accept any lookahead, partial clauses have specific
lookahead constraints.
- The [lookaheads] field stores optional lookahead specifications, which
can be terminals or `first(nonterminal)` to match the first symbol
of a nonterminal.
- The [br_captures] vector tracks which variables are captured by each
branch, for register allocation during code generation.
*)
open Utils
open Misc
open Fix.Indexing
open Info
open Regexp
module Clause = Unsafe_cardinal()
type ('g, 'r) clause = ('g * 'r) Clause.t
type clause_def = {
new_group: bool;
shortest: bool;
syntax: Syntax.clause;
}
type ('g, 'r) clauses = {
definitions : (('g, 'r) clause, clause_def) vector;
captures : (('g, 'r) clause, (Capture.n, Syntax.capture_kind * string) indexmap) vector;
}
module Branch = Unsafe_cardinal()
type ('g, 'r) branch = ('g * 'r) Branch.t
type ('g, 'r) branches = {
clause: (('g, 'r) branch, ('g, 'r) clause index) vector;
pattern: (('g, 'r) branch, Syntax.pattern) vector;
expr: (('g, 'r) branch, 'g Expr.t) vector;
of_clause : (('g, 'r) clause, ('g, 'r) branch indexset) vector;
lookaheads : (('g, 'r) branch, 'g terminal indexset option) vector;
br_captures : (('g, 'r) branch, Capture.n indexset) vector;
is_total: ('g, 'r) branch Boolvector.t;
is_partial: ('g, 'r) branch Boolvector.t;
priority: (('g, 'r) branch, ('g, 'r) branch opt index) vector;
}
type 'g _rule = Rule : ('g, 'r) clauses * ('g, 'r) branches -> 'g _rule
let import_rule (type g) (g : g grammar)
(rg : g Redgraph.graph)
(indices : g Transl.Indices.t)
(trie : g Redgraph.target_trie)
(rule : Syntax.rule) : g _rule
=
let open struct type r end in
let module Clauses = Vector.Of_array(struct
type a = clause_def
let array =
let index = function
| [] -> []
| [clause] -> [{new_group = true; shortest = false; syntax=clause}]
| clauses ->
List.mapi begin fun i (clause : Syntax.clause) ->
begin match clause.action with
| Syntax.Total _ -> ()
| Syntax.Partial (pos, _) ->
Syntax.error pos
"%%partial clauses are not supported in a %%shortest group"
| Syntax.Unreachable pos ->
Syntax.error pos
"unreachable \".\" clauses are not supported in a %%shortest group"
end;
List.iter begin fun pat ->
match pat.Syntax.lookaheads with
| [] -> ()
| (_, pos) :: _ ->
Syntax.error pos
"lookahead constraints are not supported in a %%shortest group"
end clause.patterns;
{new_group = (i = 0); shortest = true; syntax=clause}
end clauses
in
Array.of_list (List.concat_map index rule.clauses)
end)
in
let module Branches = Vector.Of_array(struct
type a = Clauses.n index * Syntax.pattern * bool * bool
let array =
Vector.mapi begin fun clause def ->
List.mapi (fun i pattern -> (clause, pattern, i = 0 && def.new_group, def.shortest))
def.syntax.patterns
end Clauses.vector
|> Vector.to_list
|> List.flatten
|> Array.of_list
end)
in
let branch_count = Vector.length Branches.vector in
let clause = Vector.map (fun (c,_,_,_) -> c) Branches.vector in
let pattern = Vector.map (fun (_,p,_,_) -> p) Branches.vector in
let priority =
let last = ref Opt.none in
Vector.mapi begin fun index (_,_,ng,sh) ->
if ng then
last := Opt.some index;
if sh
then Option.get (Index.pred !last)
else !last
end Branches.vector
in
let of_clause =
let index = ref 0 in
let import clause =
let count = List.length clause.syntax.patterns in
let first = Index.of_int branch_count !index in
index := !index + count;
let last = Index.of_int branch_count (!index - 1) in
IndexSet.init_interval first last
in
Vector.map import Clauses.vector
in
let lookaheads =
Vector.map begin fun pattern ->
match pattern.Syntax.lookaheads with
| [] -> None
| symbols ->
let lookahead_msg =
"Lookahead can either be a terminal or `first(nonterminal)'"
in
let sym_pattern (sym, pos) =
match sym with
| Syntax.Apply ("first", [sym]) ->
begin match Symbol.desc g (Transl.Indices.get_symbol g pos sym) with
| T t ->
let t = Terminal.to_string g t in
Syntax.error pos "%s; in first(%s), %s is a terminal"
lookahead_msg t t
| N n -> Nonterminal.first g n
end
| Syntax.Name _ ->
begin match Symbol.desc g (Transl.Indices.get_symbol g pos sym) with
| N n ->
Syntax.error pos "%s; %s is a nonterminal"
lookahead_msg (Nonterminal.to_string g n)
| T t -> IndexSet.singleton t
end
| _ ->
Syntax.error pos "%s" lookahead_msg
in
Some (List.fold_left IndexSet.union IndexSet.empty (List.map sym_pattern symbols))
end pattern
in
let is_partial =
Boolvector.init branch_count begin fun br ->
match Clauses.vector.:(clause.:(br)).syntax.action with
| Syntax.Partial _ -> true
| Syntax.Total _ -> false
| Syntax.Unreachable pos ->
Syntax.warn pos "unreachable clauses \"{.}\" are not yet supported";
false
end
in
let is_total =
Boolvector.init branch_count begin fun br ->
Option.is_none lookaheads.:(br) &&
not (Boolvector.test is_partial br)
end
in
let br_captures = Vector.make branch_count IndexSet.empty in
let expr = Vector.make branch_count Expr.empty in
let clause_count = Vector.length Clauses.vector in
let captures =
let gensym = Capture.gensym () in
Vector.init clause_count @@ fun clause ->
let capture_tbl = Hashtbl.create 7 in
let capture_def = ref IndexMap.empty in
let capture kind name =
let key = (kind, name) in
match Hashtbl.find_opt capture_tbl key with
| Some index -> index
| None ->
let index = gensym () in
Hashtbl.add capture_tbl key index;
capture_def := IndexMap.add index key !capture_def;
index
in
let translate_branch (br : Branches.n index) =
let cap, exp = Transl.transl g rg indices trie ~capture pattern.:(br).expr in
br_captures.:(br) <- cap;
expr.:(br) <- exp;
in
IndexSet.iter translate_branch of_clause.:(clause);
!capture_def
in
let module Clauses_def = Clause.Eq(struct
type t = g * r
type n = Clauses.n
let n = Vector.length Clauses.vector
end) in
let Refl = Clauses_def.eq in
let module Branches_def = Branch.Eq(struct
type t = g * r
type n = Branches.n
let n = Vector.length Branches.vector
end) in
let Refl = Branches_def.eq in
let clauses = {definitions = Clauses.vector; captures} in
let branches = {clause; pattern; expr; of_clause; lookaheads;
br_captures; is_total; is_partial; priority} in
Rule (clauses, branches)
let branch_count branches = Vector.length branches.expr