Skip to content

Commit

Permalink
provide a common type for regex, so third party libraries can use it …
Browse files Browse the repository at this point in the history
…and a special syntax for re
  • Loading branch information
Hongbo Zhang committed Jul 10, 2016
1 parent f6debf3 commit dd10eb3
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 1 deletion.
13 changes: 12 additions & 1 deletion jscomp/syntax/ast_attributes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ let process_attributes_rev (attrs : t) =
st, attr::acc
) ( `Nothing, []) attrs

let process_class_type_decl_rev (attrs) =
let process_class_type_decl_rev attrs =
List.fold_left (fun (st, acc) (({txt; loc}, _) as attr : attr) ->
match txt, st with
| "bs", `Nothing
Expand All @@ -109,6 +109,17 @@ let process_class_type_decl_rev (attrs) =
st, attr::acc
) ( `Nothing, []) attrs

let process_const_string_rev attrs =
List.fold_left (fun (st, acc) (({txt; loc}, _) as attr : attr) ->
match txt, st with
| "bs.re", `Nothing
->
`Has_re, acc
| _ , _ ->
st, attr::acc
) ( `Nothing, []) attrs


let bs_obj : attr
= {txt = "bs.obj" ; loc = Location.none}, Ast_payload.empty

Expand Down
3 changes: 3 additions & 0 deletions jscomp/syntax/ast_attributes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ val process_attributes_rev :
val process_class_type_decl_rev :
t -> [ `Nothing | `Has] * t

val process_const_string_rev :
t ->
[> `Has_re | `Nothing ] * t
val bs_obj : attr
val bs : attr
val bs_this : attr
3 changes: 3 additions & 0 deletions jscomp/syntax/ast_literal.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ module Lid = struct
let js_null = Longident.Ldot (Lident "Js", "null")
let js_undefined = Longident.Ldot (Lident "Js", "undefined")
let js_null_undefined = Longident.Ldot (Lident "Js", "null_undefined")

let pervasives_re_id = Longident.Ldot (Lident "Pervasives", "js_re")
let js_re_id = Longident.Ldot (Lident "Js", "re")
end

module No_loc = struct
Expand Down
3 changes: 3 additions & 0 deletions jscomp/syntax/ast_literal.mli
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ module Lid : sig
val js_null : t
val js_undefined : t
val js_null_undefined : t

val pervasives_re_id : t
val js_re_id : t
end

type expression_lit = Parsetree.expression lit
Expand Down
27 changes: 27 additions & 0 deletions jscomp/syntax/ast_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ let method_call_back_id () =
else
Ast_literal.Lid.js_meth_callback

let re_id () =
if Js_config.get_env () = Browser then
Ast_literal.Lid.pervasives_re_id
else
Ast_literal.Lid.js_re_id
let arity_lit = "Arity_"

let mk_args loc n tys =
Expand Down Expand Up @@ -306,6 +311,28 @@ let handle_raw loc payload =
["",exp]}
end

let handle_regexp loc payload =
match Ast_payload.as_string_exp payload with
| None ->
Location.raise_errorf ~loc "bs.raw can only be applied to a string"
| Some exp ->
let pval_prim = ["js_pure_expr"] in
{exp with pexp_desc =
Ast_comb.local_extern_cont loc
~pval_prim
~pval_type:(arrow ""
(Ast_literal.type_string ~loc ())
(Ast_literal.type_any ~loc ()) )
(fun f ->
Exp.constraint_ ~loc
(Exp.apply ~loc f ["", exp])
(Typ.constr ~loc {txt = re_id (); loc} [])
)
}




let handle_raw_structure loc payload =
begin match Ast_payload.as_string_exp payload with
| Some exp
Expand Down
4 changes: 4 additions & 0 deletions jscomp/syntax/ast_util.mli
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,9 @@ val handle_debugger :

val handle_raw :
loc -> Ast_payload.t -> Parsetree.expression

val handle_regexp :
loc -> Ast_payload.t -> Parsetree.expression

val handle_raw_structure :
loc -> Ast_payload.t -> Parsetree.structure_item
5 changes: 5 additions & 0 deletions jscomp/syntax/ppx_entry.ml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ let rec unsafe_mapper : Ast_mapper.mapper =
{txt = "bs.raw"; loc} , payload)
->
Ast_util.handle_raw loc payload
| Pexp_extension (
{txt = "bs.re"; loc} , payload)
->
Ast_util.handle_regexp loc payload

(** [bs.debugger], its output should not be rewritten any more*)
| Pexp_extension ({txt = "bs.debugger"; loc} , payload)
-> {e with pexp_desc = Ast_util.handle_debugger loc payload}
Expand Down
4 changes: 4 additions & 0 deletions jscomp/test/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,8 @@ test_react.cmj : ../runtime/js.cmj
test_react.cmx : ../runtime/js.cmx
test_react_case.cmj :
test_react_case.cmx :
test_regex.cmj :
test_regex.cmx :
test_runtime_encoding.cmj : ../stdlib/array.cmi
test_runtime_encoding.cmx : ../stdlib/array.cmx
test_scope.cmj :
Expand Down Expand Up @@ -1345,6 +1347,8 @@ test_react.cmo : ../runtime/js.cmo
test_react.cmj : ../runtime/js.cmj
test_react_case.cmo :
test_react_case.cmj :
test_regex.cmo :
test_regex.cmj :
test_runtime_encoding.cmo : ../stdlib/array.cmi
test_runtime_encoding.cmj : ../stdlib/array.cmj
test_scope.cmo :
Expand Down
2 changes: 2 additions & 0 deletions jscomp/test/test.mllib
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,5 @@ class_setter_getter
gpr496_test

local_class_type

test_regex
6 changes: 6 additions & 0 deletions jscomp/test/test_regex.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@





let v = [%bs.re "/b/g"]
8 changes: 8 additions & 0 deletions lib/js/test/test_regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.7.1 , PLEASE EDIT WITH CARE
'use strict';


var v = (/b/g);

exports.v = v;
/* v Not a pure module */

0 comments on commit dd10eb3

Please sign in to comment.