forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaml_lexer.ml
359 lines (339 loc) · 11.7 KB
/
caml_lexer.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* 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. *)
(** *)
(* caml_lex_array("abcd") *)
(* [25185, 25699] *)
(* @param s *)
(* @returns {any[]} *)
let fail () = raise (Failure "lexing: empty token")
[%%bs.raw{|
/***********************************************************************/
/* */
/* Objective Caml */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. All rights reserved. This file is distributed */
/* under the terms of the GNU Library General Public License, with */
/* the special exception on linking described in file ../LICENSE. */
/* */
/***********************************************************************/
/* $Id: lexing.c 6045 2004-01-01 16:42:43Z doligez $ */
/* The table-driven automaton for lexers generated by camllex. */
function caml_lex_array(s) {
var l = s.length / 2;
var a = new Array(l);
// when s.charCodeAt(2 * i + 1 ) > 128 (0x80)
// a[i] < 0
// for(var i = 0 ; i <= 0xffff; ++i) { if (i << 16 >> 16 !==i){console.log(i<<16>>16, 'vs',i)}}
//
for (var i = 0; i < l; i++)
a[i] = (s.charCodeAt(2 * i) | (s.charCodeAt(2 * i + 1) << 8)) << 16 >> 16;
return a;
}
/**
* external c_engine : lex_tables -> int -> lexbuf -> int
* lexing.ml
* type lex_tables = {
* lex_base : string;
* lex_backtrk : string;
* lex_default : string;
* lex_trans : string;
* lex_check : string;
* lex_base_code : string;
* lex_backtrk_code : string;
* lex_default_code : string;
* lex_trans_code : string;
* lex_check_code : string;
* lex_code : string;
* }
*
* type lexbuf = {
* refill_buff : lexbuf -> unit ;
* mutable lex_buffer : bytes;
* mutable lex_buffer_len : int;
* mutable lex_abs_pos : int;
* mutable lex_start_pos : int;
* mutable lex_curr_pos : int;
* mutable lex_last_pos : int;
* mutable lex_last_action : int;
* mutable lex_eof_reached : bool;
* mutable lex_mem : int array;
* mutable lex_start_p : position;
* mutable lex_curr_p;
* }
* @param tbl
* @param start_state
* @param lexbuf
* @returns {any}
*/
function $$caml_lex_engine(tbl, start_state, lexbuf) {
// Lexing.lexbuf
var lex_buffer = 1;
var lex_buffer_len = 2;
var lex_start_pos = 4;
var lex_curr_pos = 5;
var lex_last_pos = 6;
var lex_last_action = 7;
var lex_eof_reached = 8;
// Lexing.lex_tables
var lex_base = 0;
var lex_backtrk = 1;
var lex_default = 2;
var lex_trans = 3;
var lex_check = 4;
if (!tbl.lex_default) {
tbl.lex_base = caml_lex_array(tbl[lex_base]);
tbl.lex_backtrk = caml_lex_array(tbl[lex_backtrk]);
tbl.lex_check = caml_lex_array(tbl[lex_check]);
tbl.lex_trans = caml_lex_array(tbl[lex_trans]);
tbl.lex_default = caml_lex_array(tbl[lex_default]);
}
var c;
var state = start_state;
//var buffer = bytes_of_string(lexbuf[lex_buffer]);
var buffer = lexbuf[lex_buffer];
if (state >= 0) {
/* First entry */
lexbuf[lex_last_pos] = lexbuf[lex_start_pos] = lexbuf[lex_curr_pos];
lexbuf[lex_last_action] = -1;
}
else {
/* Reentry after refill */
state = -state - 1;
}
for (;;) {
/* Lookup base address or action number for current state */
var base = tbl.lex_base[state];
if (base < 0)
return -base - 1;
/* See if it's a backtrack point */
var backtrk = tbl.lex_backtrk[state];
if (backtrk >= 0) {
lexbuf[lex_last_pos] = lexbuf[lex_curr_pos];
lexbuf[lex_last_action] = backtrk;
}
/* See if we need a refill */
if (lexbuf[lex_curr_pos] >= lexbuf[lex_buffer_len]) {
if (lexbuf[lex_eof_reached] === 0)
return -state - 1;
else
c = 256;
}
else {
/* Read next input char */
c = buffer[lexbuf[lex_curr_pos]];
lexbuf[lex_curr_pos]++;
}
/* Determine next state */
if (tbl.lex_check[base + c] === state) {
state = tbl.lex_trans[base + c];
}
else {
state = tbl.lex_default[state];
}
/* If no transition on this char, return to last backtrack point */
if (state < 0) {
lexbuf[lex_curr_pos] = lexbuf[lex_last_pos];
if (lexbuf[lex_last_action] == -1)
fail();
else
return lexbuf[lex_last_action];
}
else {
/* Erase the EOF condition only if the EOF pseudo-character was
consumed by the automaton (i.e. there was no backtrack above)
*/
if (c == 256)
lexbuf[lex_eof_reached] = 0;
}
}
}
/***********************************************/
/* New lexer engine, with memory of positions */
/***********************************************/
/**
* s -> Lexing.lex_tables.lex_code
* mem -> Lexing.lexbuf.lex_mem (* int array *)
*/
function caml_lex_run_mem(s, i, mem, curr_pos) {
for (;;) {
var dst = s.charCodeAt(i);
i++;
if (dst == 0xff)
return;
var src = s.charCodeAt(i);
i++;
if (src == 0xff)
mem[dst] = curr_pos;
else
mem[dst] = mem[src];
}
}
/**
* s -> Lexing.lex_tables.lex_code
* mem -> Lexing.lexbuf.lex_mem (* int array *)
*/
function caml_lex_run_tag(s, i, mem) {
for (;;) {
var dst = s.charCodeAt(i);
i++;
if (dst == 0xff)
return;
var src = s.charCodeAt(i);
i++;
if (src == 0xff)
mem[dst] = -1;
else
mem[dst] = mem[src];
}
}
/**
* external c_new_engine : lex_tables -> int -> lexbuf -> int = "caml_new_lex_engine"
* @param tbl
* @param start_state
* @param lexbuf
* @returns {any}
*/
function $$caml_new_lex_engine(tbl, start_state, lexbuf) {
// Lexing.lexbuf
var lex_buffer = 1;
var lex_buffer_len = 2;
var lex_start_pos = 4;
var lex_curr_pos = 5;
var lex_last_pos = 6;
var lex_last_action = 7;
var lex_eof_reached = 8;
var lex_mem = 9;
// Lexing.lex_tables
var lex_base = 0;
var lex_backtrk = 1;
var lex_default = 2;
var lex_trans = 3;
var lex_check = 4;
var lex_base_code = 5;
var lex_backtrk_code = 6;
var lex_default_code = 7;
var lex_trans_code = 8;
var lex_check_code = 9;
var lex_code = 10;
if (!tbl.lex_default) {
tbl.lex_base = caml_lex_array(tbl[lex_base]);
tbl.lex_backtrk = caml_lex_array(tbl[lex_backtrk]);
tbl.lex_check = caml_lex_array(tbl[lex_check]);
tbl.lex_trans = caml_lex_array(tbl[lex_trans]);
tbl.lex_default = caml_lex_array(tbl[lex_default]);
}
if (!tbl.lex_default_code) {
tbl.lex_base_code = caml_lex_array(tbl[lex_base_code]);
tbl.lex_backtrk_code = caml_lex_array(tbl[lex_backtrk_code]);
tbl.lex_check_code = caml_lex_array(tbl[lex_check_code]);
tbl.lex_trans_code = caml_lex_array(tbl[lex_trans_code]);
tbl.lex_default_code = caml_lex_array(tbl[lex_default_code]);
}
if (tbl.lex_code == null) {
//tbl.lex_code = caml_bytes_of_string(tbl[lex_code]);
tbl.lex_code = (tbl[lex_code]);
}
var c, state = start_state;
//var buffer = caml_bytes_of_string(lexbuf[lex_buffer]);
var buffer = lexbuf[lex_buffer];
if (state >= 0) {
/* First entry */
lexbuf[lex_last_pos] = lexbuf[lex_start_pos] = lexbuf[lex_curr_pos];
lexbuf[lex_last_action] = -1;
}
else {
/* Reentry after refill */
state = -state - 1;
}
for (;;) {
/* Lookup base address or action number for current state */
var base = tbl.lex_base[state];
if (base < 0) {
var pc_off = tbl.lex_base_code[state];
caml_lex_run_tag(tbl.lex_code, pc_off, lexbuf[lex_mem]);
return -base - 1;
}
/* See if it's a backtrack point */
var backtrk = tbl.lex_backtrk[state];
if (backtrk >= 0) {
var pc_off = tbl.lex_backtrk_code[state];
caml_lex_run_tag(tbl.lex_code, pc_off, lexbuf[lex_mem]);
lexbuf[lex_last_pos] = lexbuf[lex_curr_pos];
lexbuf[lex_last_action] = backtrk;
}
/* See if we need a refill */
if (lexbuf[lex_curr_pos] >= lexbuf[lex_buffer_len]) {
if (lexbuf[lex_eof_reached] == 0)
return -state - 1;
else
c = 256;
}
else {
/* Read next input char */
c = buffer[lexbuf[lex_curr_pos]];
lexbuf[lex_curr_pos]++;
}
/* Determine next state */
var pstate = state;
if (tbl.lex_check[base + c] == state)
state = tbl.lex_trans[base + c];
else
state = tbl.lex_default[state];
/* If no transition on this char, return to last backtrack point */
if (state < 0) {
lexbuf[lex_curr_pos] = lexbuf[lex_last_pos];
if (lexbuf[lex_last_action] == -1)
fail();
else
return lexbuf[lex_last_action];
}
else {
/* If some transition, get and perform memory moves */
var base_code = tbl.lex_base_code[pstate], pc_off;
if (tbl.lex_check_code[base_code + c] == pstate)
pc_off = tbl.lex_trans_code[base_code + c];
else
pc_off = tbl.lex_default_code[pstate];
if (pc_off > 0)
caml_lex_run_mem(tbl.lex_code, pc_off, lexbuf[lex_mem], lexbuf[lex_curr_pos]);
/* Erase the EOF condition only if the EOF pseudo-character was
consumed by the automaton (i.e. there was no backtrack above)
*/
if (c == 256)
lexbuf[lex_eof_reached] = 0;
}
}
}
|}]
external caml_lex_engine : Lexing.lex_tables -> int -> Lexing.lexbuf -> int =
"$$caml_lex_engine"
[@@bs.val ]
(* [@@bs.local] *)
external caml_new_lex_engine : Lexing.lex_tables -> int -> Lexing.lexbuf -> int
= "$$caml_new_lex_engine"
[@@bs.val ]
(* [@@bs.local] *)