Skip to content

Commit 4614c89

Browse files
authored
Merge pull request #18 from noRubidium/enable-antiquoting-for-pattern-matching
implement antiquotation for pattern matching
2 parents 709d142 + 2b40f61 commit 4614c89

File tree

8 files changed

+52
-1
lines changed

8 files changed

+52
-1
lines changed

lib/pattern.ml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ let expand_float ~loc s = [%pat? `Float [%p Ast_builder.Default.pfloat ~loc s]]
1616

1717
let expand_var ~loc var = Ast_builder.Default.ppat_var ~loc var
1818

19+
let expand_anti_quotation ~ppat_loc = function
20+
| PPat (ppat, _) -> ppat
21+
| PStr _
22+
| PSig _
23+
| PTyp _ -> Raise.bad_pat_antiquotation_payload ~loc:ppat_loc
24+
1925
let rec expand ~loc ~path pat =
2026
match pat with
2127
| [%pat? _] -> [%pat? _]
@@ -31,6 +37,9 @@ let rec expand ~loc ~path pat =
3137
expand_intlit ~loc s
3238
| {ppat_desc = Ppat_constant (Pconst_float (s, None)); _} -> expand_float ~loc s
3339
| {ppat_desc = Ppat_var v; _} -> expand_var ~loc v
40+
| {ppat_desc = Ppat_extension ({txt = "y"; _}, p); ppat_loc; _}
41+
->
42+
expand_anti_quotation ~ppat_loc p
3443
| [%pat? [%p? left] | [%p? right]]
3544
->
3645
([%pat? [%p expand ~loc ~path left] | [%p expand ~loc ~path right]])
@@ -69,4 +78,3 @@ and expand_record ~loc ~ppat_loc ~path l =
6978
| [] -> assert false
7079
| [single] -> single
7180
| hd::tl -> List.fold_left (fun acc elm -> [%pat? [%p acc] | [%p elm]]) hd tl
72-

lib/raise.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ let bad_expr_antiquotation_payload ~loc =
1414
Location.raise_errorf
1515
~loc
1616
"ppx_yojson: bad antiquotation payload, should be a single expression"
17+
18+
let bad_pat_antiquotation_payload ~loc =
19+
Location.raise_errorf
20+
~loc
21+
"ppx_yojson: bad antiquotation payload, should be a pattern"

lib/raise.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ val too_many_fields_in_record_pattern :
2424
val bad_expr_antiquotation_payload :
2525
loc: Ppxlib.Location.t ->
2626
'a
27+
28+
(** Use this for bad payload in pattern antiquotation [[%y? ...]]. *)
29+
val bad_pat_antiquotation_payload :
30+
loc: Ppxlib.Location.t ->
31+
'a

test/rewriter/errors/dune.inc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,32 @@
155155
(action (diff expr_unsupported_record_field.expected expr_unsupported_record_field.actual))
156156
)
157157

158+
(library
159+
(name pat_anti_quotation_payload)
160+
(modules pat_anti_quotation_payload)
161+
(preprocess (pps ppx_yojson))
162+
)
163+
164+
(rule
165+
(targets pat_anti_quotation_payload.actual)
166+
(deps (:pp pp.exe) (:input pat_anti_quotation_payload.ml))
167+
(action
168+
(setenv "OCAML_ERROR_STYLE" "short"
169+
(setenv "OCAML_COLOR" "never"
170+
(with-stderr-to
171+
%{targets}
172+
(bash "./%{pp} -no-color --impl %{input} || true")
173+
)
174+
)
175+
)
176+
)
177+
)
178+
179+
(alias
180+
(name runtest)
181+
(action (diff pat_anti_quotation_payload.expected pat_anti_quotation_payload.actual))
182+
)
183+
158184
(library
159185
(name pat_integer_literal_binary)
160186
(modules pat_integer_literal_binary)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
File "pat_anti_quotation_payload.ml", line 2, characters 14-26:
2+
Error: ppx_yojson: bad antiquotation payload, should be a pattern
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let invalid_anti_quotation_pattern = function
2+
| [%yojson? [%y `Int _a]] -> false
3+
| _ -> true

test/rewriter/pp.expected.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,6 @@ let patterns =
8989
| `Intlit "1" as _int_32 -> ()
9090
| `Intlit "1" as _native_int -> ()
9191
| _s as _var -> ()
92+
| `Assoc (("a", `Int _i)::[]) as _var -> ()
9293
| _ as _any -> ())
9394
[@warning "-11"])

test/rewriter/test.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ let patterns = function [@warning "-11"]
5252
| [%yojson? 1l] as _int_32 -> ()
5353
| [%yojson? 1n] as _native_int -> ()
5454
| [%yojson? _s] as _var -> ()
55+
| [%yojson? { a = [%y? `Int _i]}] as _var -> ()
5556
| [%yojson? _] as _any -> ()

0 commit comments

Comments
 (0)