Skip to content

Commit 6d20b01

Browse files
committed
wip
1 parent c2e33bf commit 6d20b01

24 files changed

+595
-890
lines changed

ocaml-elpi/main_ocaml_elpi_rewriter.ml

+24-9
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ ocaml-elpi.ppx: no program specified. Supported options:
5757
let query =
5858
let open Query in
5959
compile program (Ast.Loc.initial "ppx") @@
60-
Query { predicate = "map.structure"; arguments = D(structure,s,(Q(structure,"Result",N))) } in
60+
CQuery ("map.structure", DC(structure,s,(QC(structure,"Result",NC))),new ctx_for_structure [],RawData.no_constraints) in
6161
if !typecheck then begin
6262
if not @@ Compile.static_check ~checker:Elpi.Builtin.(default_checker ()) query then begin
6363
exit 1
@@ -77,13 +77,16 @@ let erase_loc =
7777
object
7878
inherit [State.t] Ast_traverse.fold_map
7979
method! location _ (st : State.t) = Ocaml_ast_for_elpi.dummy_location, st
80+
method! location_stack l (st : State.t) = [], st
8081
end
8182
;;
8283
8384
let expression_quotation ~depth state _loc s =
8485
let e = Ppxlib.Parse.expression (Lexing.from_string s) in
8586
let e, state = erase_loc#expression e state in
86-
let state, x, gls = (expression).Conversion.embed ~depth state e in
87+
let ctx = new ctx_for_expression [] state in
88+
let csts = RawData.no_constraints in
89+
let state, x, gls = (expression).ContextualConversion.embed ~depth ctx csts state e in
8790
assert(gls = []);
8891
state, x
8992
@@ -93,7 +96,9 @@ let () = Quotation.set_default_quotation expression_quotation
9396
let pattern_quotation ~depth state _loc s =
9497
let e = Ppxlib.Parse.pattern (Lexing.from_string s) in
9598
let e, state = erase_loc#pattern e state in
96-
let state, x, gls = (pattern).Conversion.embed ~depth state e in
99+
let ctx = new ctx_for_pattern [] state in
100+
let csts = RawData.no_constraints in
101+
let state, x, gls = (pattern).ContextualConversion.embed ~depth ctx csts state e in
97102
assert(gls = []);
98103
state, x
99104
@@ -102,7 +107,9 @@ let () = Quotation.register_named_quotation ~name:"pat" pattern_quotation
102107
let type_quotation ~depth state _loc s =
103108
let e = Ppxlib.Parse.core_type (Lexing.from_string s) in
104109
let e, state = erase_loc#core_type e state in
105-
let state, x, gls = (core_type).Conversion.embed ~depth state e in
110+
let ctx = new ctx_for_core_type [] state in
111+
let csts = RawData.no_constraints in
112+
let state, x, gls = (core_type).ContextualConversion.embed ~depth ctx csts state e in
106113
assert(gls = []);
107114
state, x
108115
@@ -113,7 +120,9 @@ let stri_quotation ~depth state _loc s =
113120
match e with
114121
| Ptop_def [e] ->
115122
let e, state = erase_loc#structure_item e state in
116-
let state, x, gls = (structure_item).Conversion.embed ~depth state e in
123+
let ctx = new ctx_for_structure_item [] state in
124+
let csts = RawData.no_constraints in
125+
let state, x, gls = (structure_item).ContextualConversion.embed ~depth ctx csts state e in
117126
assert(gls = []);
118127
state, x
119128
| Ptop_def _ ->
@@ -128,7 +137,9 @@ let sigi_quotation ~depth state _loc s =
128137
match e with
129138
| [e] ->
130139
let e, state = erase_loc#signature_item e state in
131-
let state, x, gls = (signature_item).Conversion.embed ~depth state e in
140+
let ctx = new ctx_for_signature_item [] state in
141+
let csts = RawData.no_constraints in
142+
let state, x, gls = (signature_item).ContextualConversion.embed ~depth ctx csts state e in
132143
assert(gls = []);
133144
state, x
134145
| _ ->
@@ -139,7 +150,9 @@ let () = Quotation.register_named_quotation ~name:"sigi" stri_quotation
139150
let structure_quotation ~depth state _loc s =
140151
let e = Ppxlib.Parse.implementation (Lexing.from_string s) in
141152
let e, state = erase_loc#structure e state in
142-
let state, x, gls = (structure).Conversion.embed ~depth state e in
153+
let ctx = new ctx_for_structure [] state in
154+
let csts = RawData.no_constraints in
155+
let state, x, gls = (structure).ContextualConversion.embed ~depth ctx csts state e in
143156
assert(gls = []);
144157
state, x
145158
@@ -148,7 +161,9 @@ let () = Quotation.register_named_quotation ~name:"str" structure_quotation
148161
let signature_quotation ~depth state _loc s =
149162
let e = Ppxlib.Parse.interface (Lexing.from_string s) in
150163
let e, state = erase_loc#signature e state in
151-
let state, x, gls = (signature).Conversion.embed ~depth state e in
164+
let ctx = new ctx_for_signature [] state in
165+
let csts = RawData.no_constraints in
166+
let state, x, gls = (signature).ContextualConversion.embed ~depth ctx csts state e in
152167
assert(gls = []);
153168
state, x
154169
@@ -166,7 +181,7 @@ let arg_typecheck t =
166181
match Driver.Cookies.get t "typecheck" Ast_pattern.(__) with
167182
| Some _ -> typecheck := true
168183
| _ -> ()
169-
184+
170185
let arg_debug t =
171186
match Driver.Cookies.get t "debug" Ast_pattern.(__) with
172187
| Some _ -> debug := true

ocaml-elpi/ocaml_ast_for_elpi.ml

+8-10
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ let dummy_location =
3131
loc_ghost = false
3232
}
3333

34-
let maybe_override_embed default = fun ~depth st e ->
34+
let maybe_override_embed default = fun ~depth h c st e ->
3535
let open Parsetree in
3636
match e with
3737
| ({ Location.txt = ("e"|"p"|"t"|"m"|"i"); _ }, PStr [{ pstr_desc = Pstr_eval ({ pexp_desc = Parsetree.Pexp_constant (Pconst_string(s,_,_)); pexp_loc = loc; _ },[]) ; _}]) ->
3838
let loc = elpi_loc_of_location loc in
3939
let st, x = Elpi.API.Quotation.lp ~depth st loc s in
4040
st, x, []
41-
| e -> default ~depth st e
41+
| e -> default ~depth h c st e
4242

43-
let maybe_override_embed2 default = fun ~depth st e a ->
43+
let maybe_override_embed2 default = fun ~depth h c st e a ->
4444
let open Parsetree in
4545
match e with
4646
| ({ Location.txt = ("e"|"p"|"t"|"m"|"i"); _ }, PStr [{ pstr_desc = Pstr_eval ({ pexp_desc = Parsetree.Pexp_constant (Pconst_string(s,_,_)); pexp_loc = loc; _ },[]) ; _}]) ->
4747
let loc = elpi_loc_of_location loc in
4848
let st, x = Elpi.API.Quotation.lp ~depth st loc s in
4949
st, x, []
50-
| _ -> default ~depth st e a
50+
| _ -> default ~depth h c st e a
5151

5252
module Warnings = struct
5353
include Warnings
@@ -109,16 +109,16 @@ and location = Location.t = {
109109
loc_start: position;
110110
loc_end: position;
111111
loc_ghost: bool;
112-
} [@@elpi.embed fun default ~depth st start end_ ghost ->
112+
} [@@elpi.embed fun default ~depth h c st start end_ ghost ->
113113
if ghost = false && start = dummy_position && end_ = dummy_position then
114114
let st, v = Elpi.API.FlexibleData.Elpi.make st in
115115
st, Elpi.API.RawData.mkUnifVar v ~args: [] st, []
116116
else
117-
default ~depth st start end_ ghost ]
118-
[@@elpi.default_constructor_readback fun default ~depth st t ->
117+
default ~depth h c st start end_ ghost ]
118+
[@@elpi.default_constructor_readback fun default ~depth h c st t ->
119119
match Elpi.API.RawData.look ~depth t with
120120
| Elpi.API.RawData.UnifVar _ -> st, dummy_location, []
121-
| _ -> default ~depth st t]
121+
| _ -> default ~depth h c st t]
122122

123123
and location_stack = location list
124124

@@ -1111,7 +1111,5 @@ and directive_argument_desc = Parsetree.directive_argument_desc =
11111111
| Pdir_bool of bool
11121112
[@@deriving show, elpi { declaration = parsetree_declaration; mapper = parsetree_mapper }]
11131113

1114-
1115-
11161114
let parsetree_declaration = !parsetree_declaration
11171115
let parsetree_mapper = !parsetree_mapper

ocaml-elpi/tests/dune.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
(rule
33
(targets test_swap.actual.ml)
4-
(deps (:pp pp.exe) (:input test_swap.ml) ../ocaml_ast.elpi)
4+
(deps (:pp pp.exe) (:input test_swap.ml) ../ocaml_ast.elpi test_swap.elpi)
55
(action (run ./%{pp} --impl %{input} --cookie "program=\"test_swap.elpi\"" -o %{targets})))
66

77
(rule

ocaml-elpi/tests/gen_dune.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let output_stanzas filename =
55
Printf.printf {|
66
(rule
77
(targets %s.actual.ml)
8-
(deps (:pp pp.exe) (:input %s.ml) ../ocaml_ast.elpi)
8+
(deps (:pp pp.exe) (:input %s.ml) ../ocaml_ast.elpi %s.elpi)
99
(action (run ./%%{pp} --impl %%{input} --cookie "program=\"%s.elpi\"" -o %%{targets})))
1010

1111
(rule
@@ -18,7 +18,7 @@ let output_stanzas filename =
1818
(preprocess (pps ocaml-elpi.ppx -- --cookie "program=\"ocaml-elpi/tests/%s.elpi\"")))
1919

2020
|}
21-
base base base base base base base base
21+
base base base base base base base base base
2222

2323
let is_test filename =
2424
Filename.check_suffix filename ".ml" &&

ocaml-elpi/tests/test_swap.elpi

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1+
12
map.value-binding (value-binding {{:pat ( [%e "P1"], [%e "P2" ] ) }} E X L)
23
(value-binding {{:pat ( [%e "P2"], [%e "P1" ] ) }} E X L) :- !.
3-

0 commit comments

Comments
 (0)