Skip to content

Commit

Permalink
Add metaquotations for signatures.
Browse files Browse the repository at this point in the history
  • Loading branch information
paurkedal committed May 9, 2016
1 parent ef5e021 commit 6839b22
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion ppx_metaquot.ml
Expand Up @@ -11,6 +11,8 @@
[%pat? ...] maps to code which creates the pattern represented by ...
[%str ...] maps to code which creates the structure represented by ...
[%stri ...] maps to code which creates the structure item represented by ...
[%sig: ...] maps to code which creates the signature represented by ...
[%sigi: ...] maps to code which creates the signature item represented by ...
[%type: ...] maps to code which creates the core type represented by ...
Quoted code can refer to expressions representing AST fragments,
Expand All @@ -20,6 +22,7 @@
[%t ...] where ... is an expression of type Parsetree.core_type
[%p ...] where ... is an expression of type Parsetree.pattern
[%%s ...] where ... is an expression of type Parsetree.structure
or Parsetree.signature depending on the context.
All locations generated by the meta quotation are by default set
Expand Down Expand Up @@ -149,6 +152,15 @@ module Main : sig end = struct
cons (super # lift_Parsetree_structure_item x))
str (nil ())

method! lift_Parsetree_signature sign =
List.fold_right
(function
| {psig_desc=Psig_extension(({txt="s";loc}, e), _); _} ->
append (get_exp loc e)
| x ->
cons (super # lift_Parsetree_signature_item x))
sign (nil ())

method! lift_Parsetree_core_type = function
| {ptyp_desc=Ptyp_extension({txt="t";loc}, e); _} -> map (get_exp loc e)
| x -> super # lift_Parsetree_core_type x
Expand Down Expand Up @@ -206,6 +218,10 @@ module Main : sig end = struct
(exp_lifter !loc this) # lift_Parsetree_structure e
| Pexp_extension({txt="stri";_}, PStr [e]) ->
(exp_lifter !loc this) # lift_Parsetree_structure_item e
| Pexp_extension({txt="sig";_}, PSig e) ->
(exp_lifter !loc this) # lift_Parsetree_signature e
| Pexp_extension({txt="sigi";_}, PSig [e]) ->
(exp_lifter !loc this) # lift_Parsetree_signature_item e
| Pexp_extension({txt="type";loc=l}, e) ->
(exp_lifter !loc this) # lift_Parsetree_core_type (get_typ l e)
| _ ->
Expand All @@ -223,6 +239,10 @@ module Main : sig end = struct
(pat_lifter this) # lift_Parsetree_structure e
| Ppat_extension({txt="stri";_}, PStr [e]) ->
(pat_lifter this) # lift_Parsetree_structure_item e
| Ppat_extension({txt="sig";_}, PSig e) ->
(pat_lifter this) # lift_Parsetree_signature e
| Ppat_extension({txt="sigi";_}, PSig [e]) ->
(pat_lifter this) # lift_Parsetree_signature_item e
| Ppat_extension({txt="type";loc=l}, e) ->
(pat_lifter this) # lift_Parsetree_core_type (get_typ l e)
| _ ->
Expand All @@ -239,8 +259,19 @@ module Main : sig end = struct
end;
super.structure_item this x

and signature this l =
with_loc
(fun () -> super.signature this l)

and signature_item this x =
begin match x.psig_desc with
| Psig_attribute x -> handle_attr x
| _ -> ()
end;
super.signature_item this x

in
{super with expr; pat; structure; structure_item}
{super with expr; pat; structure; structure_item; signature; signature_item}

let () = Ast_mapper.run_main expander
end

0 comments on commit 6839b22

Please sign in to comment.