forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext_ident.ml
195 lines (160 loc) · 5.45 KB
/
ext_ident.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
* 2017 - Hongbo Zhang, Authors of ReScript
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
let js_flag = 0b1_000 (* check with ocaml compiler *)
(* let js_module_flag = 0b10_000 (\* javascript external modules *\) *)
(* TODO:
check name conflicts with javascript conventions
{[
Ext_ident.convert "^";;
- : string = "$caret"
]}
*)
let js_object_flag = 0b100_000 (* javascript object flags *)
let is_js (i : Ident.t) =
i.flags land js_flag <> 0
let is_js_or_global (i : Ident.t) =
i.flags land (8 lor 1) <> 0
let is_js_object (i : Ident.t) =
i.flags land js_object_flag <> 0
let make_js_object (i : Ident.t) =
i.flags <- i.flags lor js_object_flag
(* It's a js function hard coded by js api, so when printing,
it should preserve the name
*)
let create_js (name : string) : Ident.t =
{ name = name; flags = js_flag ; stamp = 0}
let create = Ident.create
(* FIXME: no need for `$' operator *)
let create_tmp ?(name=Literals.tmp) () = create name
let js_module_table : Ident.t Hash_string.t = Hash_string.create 31
(* This is for a js exeternal module, we can change it when printing
for example
{[
var React$1 = require('react');
React$1.render(..)
]}
Given a name, if duplicated, they should have the same id
*)
(* let create_js_module (name : string) : Ident.t =
let name =
String.concat "" @@ Ext_list.map
(Ext_string.split name '-') Ext_string.capitalize_ascii in
(* TODO: if we do such transformation, we should avoid collision for example:
react-dom
react--dom
check collision later
*)
match Hash_string.find_exn js_module_table name with
| exception Not_found ->
let ans = Ident.create name in
(* let ans = { v with flags = js_module_flag} in *)
Hash_string.add js_module_table name ans;
ans
| v -> (* v *) Ident.rename v
*)
let [@inline] convert ?(op=false) (c : char) : string =
(match c with
| '*' -> "$star"
| '\'' -> "$p"
| '!' -> "$bang"
| '>' -> "$great"
| '<' -> "$less"
| '=' -> "$eq"
| '+' -> "$plus"
| '-' -> if op then "$neg" else "$"
| '@' -> "$at"
| '^' -> "$caret"
| '/' -> "$slash"
| '|' -> "$pipe"
| '.' -> "$dot"
| '%' -> "$percent"
| '~' -> "$tilde"
| '#' -> "$hash"
| ':' -> "$colon"
| '?' -> "$question"
| '&' -> "$amp"
| '(' -> "$lpar"
| ')' -> "$rpar"
| '{' -> "$lbrace"
| '}' -> "$lbrace"
| '[' -> "$lbrack"
| ']' -> "$rbrack"
| _ -> "$unknown")
let [@inline] no_escape (c : char) =
match c with
| 'a' .. 'z' | 'A' .. 'Z'
| '0' .. '9' | '_' | '$' -> true
| _ -> false
exception Not_normal_letter of int
let name_mangle name =
let len = String.length name in
try
for i = 0 to len - 1 do
if not (no_escape (String.unsafe_get name i)) then
raise_notrace (Not_normal_letter i)
done;
name (* Normal letter *)
with
| Not_normal_letter i ->
let buffer = Ext_buffer.create len in
for j = 0 to len - 1 do
let c = String.unsafe_get name j in
if no_escape c then Ext_buffer.add_char buffer c
else
Ext_buffer.add_string buffer (convert ~op:(i=0) c)
done; Ext_buffer.contents buffer
(* TODO:
check name conflicts with javascript conventions
{[
Ext_ident.convert "^";;
- : string = "$caret"
]}
[convert name] if [name] is a js keyword,add "$$"
otherwise do the name mangling to make sure ocaml identifier it is
a valid js identifier
*)
let convert (name : string) =
if Js_reserved_map.is_reserved name then
"$$" ^ name
else name_mangle name
(** keyword could be used in property *)
(* It is currently made a persistent ident to avoid fresh ids
which would result in different signature files
- other solution: use lazy values
*)
let make_unused () = create "_"
let reset () =
Hash_string.clear js_module_table
(* Has to be total order, [x < y]
and [x > y] should be consistent
flags are not relevant here
*)
let compare (x : Ident.t ) ( y : Ident.t) =
let u = x.stamp - y.stamp in
if u = 0 then
Ext_string.compare x.name y.name
else u
let equal ( x : Ident.t) ( y : Ident.t) =
if x.stamp <> 0 then x.stamp = y.stamp
else y.stamp = 0 && x.name = y.name