forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_sorted.ml
233 lines (209 loc) · 6.63 KB
/
build_sorted.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
(* Copyright (C) 2019-Present Authors of BuckleScript
*
* 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 reserved_words =
[|
(* keywork *)
"break";
"case"; "catch"; "continue";
"debugger";"default";"delete";"do";
"else";
"finally";"for";"function";
"if"; "then"; "in";"instanceof";
"new";
"return";
"switch";
"this"; "throw"; "try"; "typeof";
"var"; "void"; "while"; "with";
(* reserved in ECMAScript 5 *)
"class"; "enum"; "export"; "extends"; "import"; "super";
"implements";"interface";
"let";
"package";"private";"protected";"public";
"static";
"yield";
(* other *)
"null";
"true";
"false";
"NaN";
"undefined";
"this";
(* also reserved in ECMAScript 3 *)
"abstract"; "boolean"; "byte"; "char"; "const"; "double";
"final"; "float"; "goto"; "int"; "long"; "native"; "short";
"synchronized";
(* "throws"; *)
(* seems to be fine, like nodejs [assert.throws] *)
"transient"; "volatile";
(* also reserved in ECMAScript 6 *)
"await";
"event";
"location";
"window";
"document";
"eval";
"navigator";
(* "self"; *)
"Array";
"Date";
"Math";
"JSON";
"Object";
"RegExp";
"String";
"Boolean";
"Number";
"Buffer"; (* Node *)
"Map"; (* es6*)
"Set";
"Promise";
"Infinity";
"isFinite";
"ActiveXObject";
"XMLHttpRequest";
"XDomainRequest";
"DOMException";
"Error";
"SyntaxError";
"arguments";
"decodeURI";
"decodeURIComponent";
"encodeURI";
"encodeURIComponent";
"escape";
"unescape";
"fetch";
"isNaN";
"parseFloat";
"parseInt";
(** reserved for commonjs and NodeJS globals*)
"require";
"exports";
"module";
"clearImmediate";
"clearInterval";
"clearTimeout";
"console";
"global";
"process";
"require";
"setImmediate";
"setInterval";
"setTimeout";
"__dirname";
"__filename";
"__esModule"
|]
module SSet = Set.Make(String)
let get_predefined_words (fn : string) =
let v = ref SSet.empty in
let in_chan = open_in_bin fn in
(try
while true do
let new_word = input_line in_chan in
if String.length new_word <> 0 then
v := SSet.add new_word !v
done
with End_of_file -> ());
!v
let fill_extra (ss : SSet.t) : SSet.t =
let v = ref ss in
for i = 0 to Array.length reserved_words - 1 do
v := SSet.add reserved_words.(i) !v
done;
!v
let license = {|
(* Copyright (C) 2019-Present Authors of BuckleScript
*
* 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 binary_search = {|
type element = string
let rec binarySearchAux (arr : element array) (lo : int) (hi : int) key : bool =
let mid = (lo + hi)/2 in
let midVal = Array.unsafe_get arr mid in
(* let c = cmp key midVal [@bs] in *)
if key = midVal then true
else if key < midVal then (* a[lo] =< key < a[mid] <= a[hi] *)
if hi = mid then
(Array.unsafe_get arr lo) = key
else binarySearchAux arr lo mid key
else (* a[lo] =< a[mid] < key <= a[hi] *)
if lo = mid then
(Array.unsafe_get arr hi) = key
else binarySearchAux arr mid hi key
let binarySearch (sorted : element array) (key : element) : bool =
let len = Array.length sorted in
if len = 0 then false
else
let lo = Array.unsafe_get sorted 0 in
(* let c = cmp key lo [@bs] in *)
if key < lo then false
else
let hi = Array.unsafe_get sorted (len - 1) in
(* let c2 = cmp key hi [@bs]in *)
if key > hi then false
else binarySearchAux sorted 0 (len - 1) key
let is_reserved s = binarySearch sorted_keywords s
|}
let main keyword_file output_file =
let ss = get_predefined_words keyword_file in
let ss = fill_extra ss in
let keywords_array =
(SSet.fold
(fun s acc -> acc ^ "\"" ^ s ^ "\";\n "
) ss "let sorted_keywords = [|\n ") ^ "|]\n"
in
let oc = open_out_bin output_file in
output_string oc license ;
output_string oc keywords_array;
output_string oc binary_search;
close_out oc
(*
;;
for i = 0 to Array.length Sys.argv - 1 do
print_endline ">"; print_string Sys.argv.(i)
done
;; *)
let () = main Sys.argv.(1) "ext/js_reserved_map.ml"