Skip to content

Commit 1f644b6

Browse files
committed
All sets.txt tests now pass; un-ditto'd bound identifiers
Signed-off-by: Andrew Helwer <[email protected]>
1 parent f3f4a5d commit 1f644b6

File tree

1 file changed

+49
-35
lines changed

1 file changed

+49
-35
lines changed

test/parser/translate_syntax_tree.ml

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,27 @@ and ts_node = {
1414
children : field_or_node list;
1515
}
1616

17-
let leaf (name : string) : field_or_node = Node {name; children = []}
17+
let leaf_node (name : string) : ts_node = {
18+
name;
19+
children = []
20+
}
21+
22+
let leaf (name : string) : field_or_node = Node (leaf_node name)
1823

1924
let field_leaf (field_name : string) (name : string) : field_or_node =
20-
Field (field_name, {name; children = []})
25+
Field (field_name, leaf_node name)
2126

27+
let node_list (ls : ts_node list) : field_or_node list =
28+
List.map (fun e -> Node e) ls
29+
2230
let node_list_map (f : 'a -> ts_node) (ls : 'a list) : field_or_node list =
23-
List.map (fun e -> Node e) (List.map f ls)
31+
node_list (List.map f ls)
32+
33+
let field_list (field : string) (ls : 'a list) : field_or_node list =
34+
List.map (fun e -> Field (field, e)) ls
2435

2536
let field_list_map (field : string) (f : 'a -> ts_node) (ls : 'a list) : field_or_node list =
26-
List.map (fun e -> Field (field, e)) (List.map f ls)
37+
field_list field (List.map f ls)
2738

2839
let rec ts_node_to_sexpr (node : ts_node) : Sexp.t =
2940
let flatten_child (child : field_or_node) : Sexp.t list =
@@ -55,16 +66,13 @@ type decl_or_ref =
5566

5667
let as_specific_name (if_id : decl_or_ref) (id : string) : ts_node =
5768
match id with
58-
| "Nat" -> {name = "nat_number_set"; children = []}
59-
| "Int" -> {name = "int_number_set"; children = []}
60-
| "Real" -> {name = "real_number_set"; children = []}
61-
| "FALSE" -> {name = "boolean"; children = []}
62-
| "TRUE" -> {name = "boolean"; children = []}
63-
| "STRING" -> {name = "string_set"; children = []}
64-
| _ -> {
65-
name = (match if_id with | Declaration -> "identifier" | Reference -> "identifier_ref");
66-
children = []
67-
}
69+
| "Nat" -> leaf_node "nat_number_set"
70+
| "Int" -> leaf_node "int_number_set"
71+
| "Real" -> leaf_node "real_number_set"
72+
| "FALSE" -> leaf_node "boolean"
73+
| "TRUE" -> leaf_node "boolean"
74+
| "STRING" -> leaf_node "string_set"
75+
| _ -> leaf_node (match if_id with | Declaration -> "identifier" | Reference -> "identifier_ref")
6876

6977
(** The standardized test corpus requires counting escaped strings (for syntax
7078
highlighting reasons) so we do a foldl over the characters of the string
@@ -198,8 +206,8 @@ let translate_operator_parameter ((name, shape) : Util.hint * Expr.T.shape) : fi
198206

199207
let translate_number (_number : string) (decimal : string) : ts_node =
200208
if String.empty = decimal
201-
then {name = "nat_number"; children = []}
202-
else {name = "real_number"; children = []}
209+
then leaf_node "nat_number"
210+
else leaf_node "real_number"
203211

204212
(** Translates the substitution component of INSTANCE statements like s <- expr *)
205213
let rec translate_substitution ((hint, expr) : (Util.hint * Expr.T.expr)) : field_or_node =
@@ -279,18 +287,25 @@ and translate_jlist (bullet : Expr.T.bullet) (juncts : Expr.T.expr list) : ts_no
279287
children = [leaf ("bullet_" ^ jtype); Node (translate_expr expr)]
280288
}) juncts
281289
}
282-
and translate_quantifier_bound ((_hint, _, bound_domain) : Expr.T.bound) : ts_node =
283-
match bound_domain with
284-
| Domain expr -> {
285-
name = "quantifier_bound";
286-
children = [
287-
field_leaf "intro" "identifier";
288-
leaf "set_in";
289-
Field ("set", translate_expr expr)
290-
]
291-
}
292-
| Ditto -> failwith "ditto quantifier"
293-
| _ -> failwith "unknown quantifier bound type"
290+
and translate_quantifier_bound ((names, domain) : (ts_node list * Expr.T.expr)) : ts_node = {
291+
name = "quantifier_bound";
292+
children = List.flatten [
293+
field_list "intro" names;
294+
[leaf "set_in"];
295+
[Field ("set", translate_expr domain)]
296+
]
297+
}
298+
299+
and translate_quantifier_bounds (bounds : Expr.T.bound list) : ts_node list =
300+
let group_bounds (bounds : Expr.T.bound list) : (ts_node list * Expr.T.expr) list =
301+
let (final_groups, _) = List.fold_right (fun ((_, _, domain) : Expr.T.bound) ((groups, partial_group) : ((ts_node list * Expr.T.expr) list) * ts_node list) ->
302+
match domain with
303+
| Ditto -> (groups, leaf_node "identifier" :: partial_group)
304+
| Domain expr -> ((leaf_node "identifier" :: partial_group, expr) :: groups, [])
305+
| _ -> failwith "unknown quantifier bound type"
306+
) bounds ([], [])
307+
in final_groups
308+
in bounds |> group_bounds |> List.map translate_quantifier_bound
294309

295310
and translate_tuple_quantifier_bound ((names, bound_domain) : Expr.T.tuply_name * Expr.T.bound_domain) : ts_node =
296311
match names, bound_domain with
@@ -445,12 +460,11 @@ and translate_tuple_set_filter (names : Util.hint list) (set : Expr.T.expr) (fil
445460
]
446461
}
447462

448-
and translate_set_map (map : Expr.T.expr) (bounds : Expr.T.bound list) : ts_node =
449-
let bounds = Expr.T.unditto bounds in {
463+
and translate_set_map (map : Expr.T.expr) (bounds : Expr.T.bound list) : ts_node = {
450464
name = "set_map";
451465
children = List.flatten [
452466
[Field ("map", translate_expr map)];
453-
field_list_map "generator" translate_quantifier_bound bounds;
467+
field_list "generator" (translate_quantifier_bounds bounds);
454468
]
455469
}
456470

@@ -468,7 +482,7 @@ and translate_bounded_quantification (quantifier : Expr.T.quantifier) (bounds :
468482
name = "bounded_quantification";
469483
children = List.flatten [
470484
[field_leaf "quantifier" (match quantifier with | Forall -> "forall" | Exists -> "exists")];
471-
node_list_map translate_quantifier_bound bounds;
485+
field_list "generator" (translate_quantifier_bounds bounds);
472486
[Field ("expression", translate_expr expr)];
473487
]
474488
}
@@ -505,11 +519,11 @@ and translate_expr (expr : Expr.T.expr) : ts_node =
505519
}
506520
| Case (cases, other) -> translate_case cases other
507521
| Bang (expr, selectors) -> translate_subexpression expr selectors
508-
| _ -> {name = "expr_ph"; children = []}
522+
| _ -> leaf_node "expr_ph"
509523

510524
let translate_operator_definition (defn : Expr.T.defn) : ts_node =
511525
match defn.core with
512-
| Recursive (_name, _shape) -> {name = "recursive_ph"; children = []}
526+
| Recursive (_name, _shape) -> leaf_node "recursive_ph"
513527
| Operator (name, expr) -> (
514528
match expr.core with
515529
(* Operators with parameters are represented by a LAMBDA expression. *)
@@ -526,7 +540,7 @@ let translate_operator_definition (defn : Expr.T.defn) : ts_node =
526540
name = "function_definition";
527541
children = List.flatten [
528542
[field_leaf "name" "identifier"];
529-
node_list_map translate_quantifier_bound bounds;
543+
bounds |> translate_quantifier_bounds |> node_list;
530544
[leaf "def_eq"];
531545
[Field ("definition", translate_expr expr)]
532546
]

0 commit comments

Comments
 (0)