-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathscanf.js
2352 lines (2234 loc) · 59.1 KB
/
scanf.js
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Generated CODE, PLEASE EDIT WITH CARE
'use strict';
var Caml_builtin_exceptions = require("../runtime/caml_builtin_exceptions");
var Bytes = require("./bytes");
var Pervasives = require("./pervasives");
var Caml_format = require("../runtime/caml_format");
var Printf = require("./printf");
var Caml_primitive = require("../runtime/caml_primitive");
var CamlinternalFormatBasics = require("./camlinternalFormatBasics");
var Buffer = require("./buffer");
var Caml_curry = require("../runtime/caml_curry");
var $$String = require("./string");
var Caml_string = require("../runtime/caml_string");
var List = require("./list");
var CamlinternalFormat = require("./camlinternalFormat");
function next_char(ib) {
try {
var c = Caml_curry.app1(ib[/* get_next_char */6], /* () */0);
ib[/* current_char */1] = c;
ib[/* current_char_is_valid */2] = /* true */1;
++ ib[3];
if (c === /* "\n" */10) {
++ ib[4];
}
return c;
}
catch (exn){
if (exn === Caml_builtin_exceptions.end_of_file) {
ib[/* current_char */1] = /* "\000" */0;
ib[/* current_char_is_valid */2] = /* false */0;
ib[/* eof */0] = /* true */1;
return /* "\000" */0;
}
else {
throw exn;
}
}
}
function peek_char(ib) {
if (ib[/* current_char_is_valid */2]) {
return ib[/* current_char */1];
}
else {
return next_char(ib);
}
}
function checked_peek_char(ib) {
var c = peek_char(ib);
if (ib[/* eof */0]) {
throw Caml_builtin_exceptions.end_of_file;
}
return c;
}
function end_of_input(ib) {
peek_char(ib);
return ib[/* eof */0];
}
function beginning_of_input(ib) {
return +(ib[/* char_count */3] === 0);
}
function name_of_input(ib) {
var match = ib[/* input_name */8];
if (typeof match === "number") {
if (match) {
return "unnamed function";
}
else {
return "unnamed character string";
}
}
else if (match.tag) {
return "unnamed pervasives input channel";
}
else {
return match[0];
}
}
function char_count(ib) {
if (ib[/* current_char_is_valid */2]) {
return ib[/* char_count */3] - 1;
}
else {
return ib[/* char_count */3];
}
}
function token(ib) {
var tokbuf = ib[/* tokbuf */7];
var tok = Buffer.contents(tokbuf);
tokbuf[/* position */1] = 0;
++ ib[5];
return tok;
}
function ignore_char(width, ib) {
var width$1 = width - 1;
ib[/* current_char_is_valid */2] = /* false */0;
return width$1;
}
function store_char(width, ib, c) {
Buffer.add_char(ib[/* tokbuf */7], c);
return ignore_char(width, ib);
}
function create(iname, next) {
return /* record */[
/* false */0,
/* "\000" */0,
/* false */0,
0,
0,
0,
next,
Buffer.create(1024),
iname
];
}
function from_string(s) {
var i = [0];
var len = s.length;
var next = function () {
if (i[0] >= len) {
throw Caml_builtin_exceptions.end_of_file;
}
else {
var c = s.charCodeAt(i[0]);
++ i[0];
return c;
}
};
return create(/* From_string */0, next);
}
function from_function(param) {
return create(/* From_function */1, param);
}
var file_buffer_size = [1024];
function scan_close_at_end(ic) {
Caml_primitive.caml_ml_close_channel(ic);
throw Caml_builtin_exceptions.end_of_file;
}
function scan_raise_at_end() {
throw Caml_builtin_exceptions.end_of_file;
}
function from_ic(scan_close_ic, iname, ic) {
var len = file_buffer_size[0];
var buf = Caml_string.caml_create_string(len);
var i = [0];
var lim = [0];
var eof = [/* false */0];
var next = function () {
if (i[0] < lim[0]) {
var c = buf[i[0]];
++ i[0];
return c;
}
else if (eof[0]) {
throw Caml_builtin_exceptions.end_of_file;
}
else {
lim[0] = Pervasives.input(ic, buf, 0, len);
if (lim[0]) {
i[0] = 1;
return buf[0];
}
else {
eof[0] = /* true */1;
return Caml_curry.app1(scan_close_ic, ic);
}
}
};
return create(iname, next);
}
var stdin = from_ic(scan_raise_at_end, /* From_file */{
0: "-",
1: Pervasives.stdin,
length: 2,
tag: 0
}, Pervasives.stdin);
function open_in(fname) {
if (fname === "-") {
return stdin;
}
else {
var ic = Pervasives.open_in(fname);
return from_ic(scan_close_at_end, /* From_file */{
0: fname,
1: ic,
length: 2,
tag: 0
}, ic);
}
}
function open_in_bin(fname) {
if (fname === "-") {
return stdin;
}
else {
var ic = Pervasives.open_in_bin(fname);
return from_ic(scan_close_at_end, /* From_file */{
0: fname,
1: ic,
length: 2,
tag: 0
}, ic);
}
}
var memo = [/* [] */0];
function memo_from_ic(scan_close_ic, ic) {
try {
return List.assq(ic, memo[0]);
}
catch (exn){
if (exn === Caml_builtin_exceptions.not_found) {
var ib = from_ic(scan_close_ic, /* From_channel */{
0: ic,
length: 1,
tag: 1
}, ic);
memo[0] = /* :: */[
/* tuple */[
ic,
ib
],
memo[0]
];
return ib;
}
else {
throw exn;
}
}
}
function from_channel(param) {
return memo_from_ic(scan_raise_at_end, param);
}
function close_in(ib) {
var match = ib[/* input_name */8];
if (typeof match === "number") {
return /* () */0;
}
else if (match.tag) {
return Caml_primitive.caml_ml_close_channel(match[0]);
}
else {
return Caml_primitive.caml_ml_close_channel(match[1]);
}
}
var Scan_failure = {
0: "Scanf.Scan_failure",
1: Caml_builtin_exceptions.get_id(),
length: 2,
tag: 248
};
function bad_input_escape(c) {
var s = Caml_curry.app1(Printf.sprintf(/* Format */{
0: /* String_literal */{
0: "illegal escape character ",
1: /* Caml_char */{
0: /* End_of_format */0,
length: 1,
tag: 1
},
length: 2,
tag: 11
},
1: "illegal escape character %C",
length: 2,
tag: 0
}), c);
throw [
Scan_failure,
s
];
}
function bad_token_length(message) {
var s = Caml_curry.app1(Printf.sprintf(/* Format */{
0: /* String_literal */{
0: "scanning of ",
1: /* String */{
0: /* No_padding */0,
1: /* String_literal */{
0: " failed: the specified length was too short for token",
1: /* End_of_format */0,
length: 2,
tag: 11
},
length: 2,
tag: 2
},
length: 2,
tag: 11
},
1: "scanning of %s failed: the specified length was too short for token",
length: 2,
tag: 0
}), message);
throw [
Scan_failure,
s
];
}
function character_mismatch_err(c, ci) {
return Caml_curry.app2(Printf.sprintf(/* Format */{
0: /* String_literal */{
0: "looking for ",
1: /* Caml_char */{
0: /* String_literal */{
0: ", found ",
1: /* Caml_char */{
0: /* End_of_format */0,
length: 1,
tag: 1
},
length: 2,
tag: 11
},
length: 1,
tag: 1
},
length: 2,
tag: 11
},
1: "looking for %C, found %C",
length: 2,
tag: 0
}), c, ci);
}
function check_char(ib, _c) {
while(true) {
var c = _c;
if (c === /* " " */32) {
var ib$1 = ib;
while(true) {
var c$1 = peek_char(ib$1);
if (ib$1[/* eof */0]) {
return 0;
}
else {
var switcher = c$1 - 9;
if (switcher > 4 || switcher < 0) {
if (switcher !== 23) {
return /* () */0;
}
else {
ib$1[/* current_char_is_valid */2] = /* false */0;
continue ;
}
}
else if (switcher === 3 || switcher === 2) {
return /* () */0;
}
else {
ib$1[/* current_char_is_valid */2] = /* false */0;
continue ;
}
}
};
}
else {
var ci = checked_peek_char(ib);
if (ci === c) {
ib[/* current_char_is_valid */2] = /* false */0;
return /* () */0;
}
else if (ci !== 13) {
var s = character_mismatch_err(c, ci);
throw [
Scan_failure,
s
];
}
else if (c === /* "\n" */10) {
ib[/* current_char_is_valid */2] = /* false */0;
_c = /* "\n" */10;
continue ;
}
else {
var s$1 = character_mismatch_err(c, ci);
throw [
Scan_failure,
s$1
];
}
}
};
}
function token_char(ib) {
return token(ib).charCodeAt(0);
}
function token_bool(ib) {
var s = token(ib);
switch (s) {
case "false" :
return /* false */0;
case "true" :
return /* true */1;
default:
var s$1 = Caml_curry.app1(Printf.sprintf(/* Format */{
0: /* String_literal */{
0: "invalid boolean ",
1: /* Caml_string */{
0: /* No_padding */0,
1: /* End_of_format */0,
length: 2,
tag: 3
},
length: 2,
tag: 11
},
1: "invalid boolean %S",
length: 2,
tag: 0
}), s);
throw [
Scan_failure,
s$1
];
}
}
function token_int_literal(conv, ib) {
var tok;
var exit = 0;
var switcher = conv - 88;
if (switcher > 32 || switcher < 0) {
exit = 1;
}
else {
switch (switcher) {
case 10 :
tok = "0b" + token(ib);
break;
case 23 :
tok = "0o" + token(ib);
break;
case 12 :
case 17 :
case 29 :
tok = token(ib);
break;
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
case 8 :
case 9 :
case 11 :
case 13 :
case 14 :
case 15 :
case 16 :
case 18 :
case 19 :
case 20 :
case 21 :
case 22 :
case 24 :
case 25 :
case 26 :
case 27 :
case 28 :
case 30 :
case 31 :
exit = 1;
break;
case 0 :
case 32 :
tok = "0x" + token(ib);
break;
}
}
if (exit === 1) {
throw [
Caml_builtin_exceptions.assert_failure,
[
"scanf.ml",
507,
11
]
];
}
var l = tok.length;
if (l === 0 || tok.charCodeAt(0) !== /* "+" */43) {
return tok;
}
else {
return $$String.sub(tok, 1, l - 1);
}
}
function token_float(ib) {
return Caml_format.caml_float_of_string(token(ib));
}
function scan_decimal_digits(_width, ib) {
while(true) {
var width = _width;
if (width) {
var c = peek_char(ib);
if (ib[/* eof */0]) {
return width;
}
else if (c >= 58) {
if (c !== 95) {
return width;
}
else {
var width$1 = ignore_char(width, ib);
_width = width$1;
continue ;
}
}
else if (c >= 48) {
var width$2 = store_char(width, ib, c);
_width = width$2;
continue ;
}
else {
return width;
}
}
else {
return width;
}
};
}
function scan_decimal_digits_plus(width, ib) {
if (width) {
var c = checked_peek_char(ib);
if (c > 57 || c < 48) {
var s = Caml_curry.app1(Printf.sprintf(/* Format */{
0: /* String_literal */{
0: "character ",
1: /* Caml_char */{
0: /* String_literal */{
0: " is not a decimal digit",
1: /* End_of_format */0,
length: 2,
tag: 11
},
length: 1,
tag: 1
},
length: 2,
tag: 11
},
1: "character %C is not a decimal digit",
length: 2,
tag: 0
}), c);
throw [
Scan_failure,
s
];
}
else {
var width$1 = store_char(width, ib, c);
return scan_decimal_digits(width$1, ib);
}
}
else {
return bad_token_length("decimal digits");
}
}
function scan_digits_plus(basis, digitp, width, ib) {
if (width) {
var c = checked_peek_char(ib);
if (Caml_curry.app1(digitp, c)) {
var _width = store_char(width, ib, c);
while(true) {
var width$1 = _width;
if (width$1) {
var c$1 = peek_char(ib);
if (ib[/* eof */0]) {
return width$1;
}
else if (Caml_curry.app1(digitp, c$1)) {
_width = store_char(width$1, ib, c$1);
continue ;
}
else if (c$1 !== 95) {
return width$1;
}
else {
_width = ignore_char(width$1, ib);
continue ;
}
}
else {
return width$1;
}
};
}
else {
var s = Caml_curry.app2(Printf.sprintf(/* Format */{
0: /* String_literal */{
0: "character ",
1: /* Caml_char */{
0: /* String_literal */{
0: " is not a valid ",
1: /* String */{
0: /* No_padding */0,
1: /* String_literal */{
0: " digit",
1: /* End_of_format */0,
length: 2,
tag: 11
},
length: 2,
tag: 2
},
length: 2,
tag: 11
},
length: 1,
tag: 1
},
length: 2,
tag: 11
},
1: "character %C is not a valid %s digit",
length: 2,
tag: 0
}), c, basis);
throw [
Scan_failure,
s
];
}
}
else {
return bad_token_length("digits");
}
}
function is_binary_digit(param) {
if (param === 49 || param === 48) {
return /* true */1;
}
else {
return /* false */0;
}
}
function scan_binary_int(param, param$1) {
return scan_digits_plus("binary", is_binary_digit, param, param$1);
}
function is_octal_digit(param) {
if (param > 55 || param < 48) {
return /* false */0;
}
else {
return /* true */1;
}
}
function scan_octal_int(param, param$1) {
return scan_digits_plus("octal", is_octal_digit, param, param$1);
}
function is_hexa_digit(param) {
var switcher = param - 48;
if (switcher > 22 || switcher < 0) {
if (switcher > 54 || switcher < 49) {
return /* false */0;
}
else {
return /* true */1;
}
}
else if (switcher > 16 || switcher < 10) {
return /* true */1;
}
else {
return /* false */0;
}
}
function scan_hexadecimal_int(param, param$1) {
return scan_digits_plus("hexadecimal", is_hexa_digit, param, param$1);
}
function scan_sign(width, ib) {
var c = checked_peek_char(ib);
var switcher = c - 43;
if (switcher > 2 || switcher < 0) {
return width;
}
else {
switch (switcher) {
case 1 :
return width;
case 0 :
case 2 :
return store_char(width, ib, c);
}
}
}
function scan_optionally_signed_decimal_int(width, ib) {
var width$1 = scan_sign(width, ib);
return scan_decimal_digits_plus(width$1, ib);
}
function scan_int_conv(conv, width, ib) {
var exit = 0;
var switcher = conv - 88;
if (switcher > 32 || switcher < 0) {
exit = 1;
}
else {
switch (switcher) {
case 10 :
return scan_binary_int(width, ib);
case 12 :
return scan_optionally_signed_decimal_int(width, ib);
case 17 :
var width$1 = width;
var ib$1 = ib;
var width$2 = scan_sign(width$1, ib$1);
var width$3 = width$2;
var ib$2 = ib$1;
var c = checked_peek_char(ib$2);
if (c !== 48) {
return scan_decimal_digits_plus(width$3, ib$2);
}
else {
var width$4 = store_char(width$3, ib$2, c);
if (width$4) {
var c$1 = peek_char(ib$2);
if (ib$2[/* eof */0]) {
return width$4;
}
else if (c$1 >= 99) {
if (c$1 !== 111) {
if (c$1 !== 120) {
return scan_decimal_digits(width$4, ib$2);
}
else {
return scan_hexadecimal_int(store_char(width$4, ib$2, c$1), ib$2);
}
}
else {
return scan_octal_int(store_char(width$4, ib$2, c$1), ib$2);
}
}
else if (c$1 !== 88) {
if (c$1 >= 98) {
return scan_binary_int(store_char(width$4, ib$2, c$1), ib$2);
}
else {
return scan_decimal_digits(width$4, ib$2);
}
}
else {
return scan_hexadecimal_int(store_char(width$4, ib$2, c$1), ib$2);
}
}
else {
return width$4;
}
}
case 23 :
return scan_octal_int(width, ib);
case 29 :
return scan_decimal_digits_plus(width, ib);
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
case 8 :
case 9 :
case 11 :
case 13 :
case 14 :
case 15 :
case 16 :
case 18 :
case 19 :
case 20 :
case 21 :
case 22 :
case 24 :
case 25 :
case 26 :
case 27 :
case 28 :
case 30 :
case 31 :
exit = 1;
break;
case 0 :
case 32 :
return scan_hexadecimal_int(width, ib);
}
}
if (exit === 1) {
throw [
Caml_builtin_exceptions.assert_failure,
[
"scanf.ml",
674,
9
]
];
}
}
function scan_frac_part(width, ib) {
if (width) {
var c = peek_char(ib);
if (ib[/* eof */0] || c > 57 || c < 48) {
return width;
}
else {
return scan_decimal_digits(store_char(width, ib, c), ib);
}
}
else {
return width;
}
}
function scan_exp_part(width, ib) {
if (width) {
var c = peek_char(ib);
if (ib[/* eof */0] || c !== 69 && c !== 101) {
return width;
}
else {
return scan_optionally_signed_decimal_int(store_char(width, ib, c), ib);
}
}
else {
return width;
}
}
function scan_int_part(width, ib) {
var width$1 = scan_sign(width, ib);
return scan_decimal_digits(width$1, ib);
}
function scan_float(width, precision, ib) {
var width$1 = scan_int_part(width, ib);
if (width$1) {
var c = peek_char(ib);
if (ib[/* eof */0]) {
return /* tuple */[
width$1,
precision
];
}
else if (c !== 46) {
return /* tuple */[
scan_exp_part(width$1, ib),
precision
];
}
else {
var width$2 = store_char(width$1, ib, c);
var precision$1 = Pervasives.min(width$2, precision);
var width$3 = width$2 - (precision$1 - scan_frac_part(precision$1, ib));
return /* tuple */[
scan_exp_part(width$3, ib),
precision$1
];
}
}
else {
return /* tuple */[
width$1,
precision
];
}
}
function scan_caml_float(width, precision, ib) {
var width$1 = scan_optionally_signed_decimal_int(width, ib);
if (width$1) {
var c = peek_char(ib);
if (ib[/* eof */0]) {
throw [
Scan_failure,
"no dot or exponent part found in float token"
];
}
else {
var switcher = c - 69;
if (switcher > 32 || switcher < 0) {
if (switcher !== -23) {
throw [
Scan_failure,
"no dot or exponent part found in float token"
];
}
else {
var width$2 = store_char(width$1, ib, c);
var precision$1 = Pervasives.min(width$2, precision);
var width$3 = width$2 - (precision$1 - scan_frac_part(precision$1, ib));
return scan_exp_part(width$3, ib);
}
}
else if (switcher > 31 || switcher < 1) {
return scan_exp_part(width$1, ib);
}
else {
throw [
Scan_failure,
"no dot or exponent part found in float token"
];
}
}
}
else {
throw [
Scan_failure,
"no dot or exponent part found in float token"
];
}
}
function scan_string(stp, width, ib) {
var _width = width;
while(true) {
var width$1 = _width;
if (width$1) {
var c = peek_char(ib);
if (ib[/* eof */0]) {
return width$1;
}
else if (stp) {
if (c === stp[0]) {
ib[/* current_char_is_valid */2] = /* false */0;
return width$1;
}
else {
_width = store_char(width$1, ib, c);
continue ;
}
}
else {
var switcher = c - 9;
if (switcher > 4 || switcher < 0) {
if (switcher !== 23) {
_width = store_char(width$1, ib, c);
continue ;
}
else {