-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.c
2103 lines (1817 loc) · 52.5 KB
/
parser.c
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
/* Copyright 2009-2024
* Kaz Kylheku <kaz@kylheku.com>
* Vancouver, Canada
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <wchar.h>
#include <signal.h>
#include <wctype.h>
#include <errno.h>
#include "config.h"
#include "alloca.h"
#ifdef __CYGWIN__
#include <sys/utsname.h>
#endif
#if HAVE_SYS_STAT
#include <sys/stat.h>
#endif
#if HAVE_ZLIB
#include <zlib.h>
#endif
#include "lib.h"
#include "signal.h"
#include "unwind.h"
#include "gc.h"
#include "args.h"
#include "utf8.h"
#include "hash.h"
#include "eval.h"
#include "stream.h"
#if HAVE_ZLIB
#include "gzio.h"
#endif
#include "y.tab.h"
#include "sysif.h"
#include "cadr.h"
#include "struct.h"
#include "tree.h"
#include "parser.h"
#include "regex.h"
#include "itypes.h"
#include "arith.h"
#include "buf.h"
#include "vm.h"
#include "ffi.h"
#include "txr.h"
#include "linenoise/linenoise.h"
val parser_s, unique_s, circref_s;
val listener_hist_len_s, listener_multi_line_p_s, listener_sel_inclusive_p_s;
val listener_pprint_s, listener_greedy_eval_s, listener_auto_compound_s;
val rec_source_loc_s, read_unknown_structs_s, read_bad_json_s, read_json_int_s;
val json_s;
val intr_s;
struct cobj_class *parser_cls;
static lino_t *lino_ctx;
static int repl_level = 0;
static val stream_parser_hash, catch_all, catch_error;
static void yy_tok_mark(struct yy_token *tok)
{
gc_conservative_mark(tok->yy_lval.val);
}
static void parser_mark(val obj)
{
int i;
parser_t *p = coerce(parser_t *, obj->co.handle);
assert (p->parser == nil || p->parser == obj);
gc_mark(p->stream);
gc_mark(p->name);
gc_mark(p->prepared_msg);
gc_mark(p->circ_ref_hash);
if (p->syntax_tree != nao)
gc_mark(p->syntax_tree);
yy_tok_mark(&p->recent_tok);
for (i = 0; i < 4; i++)
yy_tok_mark(&p->tok_pushback[i]);
}
static void parser_destroy(val obj)
{
parser_t *p = coerce(parser_t *, obj->co.handle);
parser_cleanup(p);
free(p);
}
static struct cobj_ops parser_ops = cobj_ops_init(eq,
cobj_print_op,
parser_destroy,
parser_mark,
cobj_eq_hash_op,
0);
void parser_common_init(parser_t *p)
{
int i;
yyscan_t yyscan;
val rec_source_loc_var = lookup_var(nil, rec_source_loc_s);
val read_unknown_structs_var = lookup_var(nil, read_unknown_structs_s);
val read_bad_json_var = lookup_var(nil, read_bad_json_s);
val read_json_int = lookup_var(nil, read_json_int_s);
p->parser = nil;
p->lineno = 1;
p->errors = 0;
p->eof = 0;
p->ignore = 0;
p->stream = nil;
p->name = nil;
p->prepared_msg = nil;
p->circ_ref_hash = nil;
p->circ_count = 0;
p->syntax_tree = nil;
p->quasi_level = 0;
yylex_init(&yyscan);
p->scanner = convert(scanner_t *, yyscan);
yyset_extra(p, p->scanner);
p->recent_tok.yy_char = 0;
p->recent_tok.yy_lex_state = 0;
p->recent_tok.yy_lval.val = 0;
for (i = 0; i < 4; i++) {
p->tok_pushback[i].yy_char = 0;
p->tok_pushback[i].yy_lex_state = 0;
p->tok_pushback[i].yy_lval.val = 0;
}
p->tok_idx = 0;
p->rec_source_loc = !nilp(cdr(rec_source_loc_var));
p->read_unknown_structs = !nilp(cdr(read_unknown_structs_var));
p->read_bad_json = !nilp(cdr(read_bad_json_var));
p->read_json_int = !nilp(cdr(read_json_int));
}
void parser_cleanup(parser_t *p)
{
if (p->scanner != 0)
yylex_destroy(p->scanner);
p->scanner = 0;
}
void parser_reset(parser_t *p)
{
yyscan_t yyscan;
parser_cleanup(p);
yylex_init(&yyscan);
p->scanner = convert(scanner_t *, yyscan);
yyset_extra(p, p->scanner);
}
val parser(val stream, val name, val lineno)
{
val self = lit("parser");
parser_t *p = coerce(parser_t *, chk_malloc(sizeof *p));
val parser;
parser_common_init(p);
parser = cobj(coerce(mem_t *, p), parser_cls, &parser_ops);
p->parser = parser;
p->lineno = c_num(default_arg(lineno, one), self);
p->name = name;
p->stream = stream;
return parser;
}
parser_t *parser_get_impl(val self, val parser)
{
return coerce(parser_t *, cobj_handle(self, parser, parser_cls));
}
val ensure_parser(val stream, val name)
{
uses_or2;
loc pcdr = gethash_l(lit("internal error"), stream_parser_hash, stream, nulloc);
val pars = deref(pcdr);
if (pars)
return pars;
return set(pcdr,
parser(stream, or2(name, stream_get_prop(stream, name_k)), one));
}
static void pushback_token(parser_t *p, struct yy_token *tok)
{
assert (p->tok_idx < 4);
p->tok_pushback[p->tok_idx++] = *tok;
}
val parser_set_lineno(val self, val stream, val lineno)
{
val parser = ensure_parser(stream, nil);
parser_t *pi = parser_get_impl(self, parser);
pi->lineno = c_num(lineno, self);
return stream;
}
void prime_parser(parser_t *p, val name, enum prime_parser prim)
{
struct yy_token sec_tok = all_zero_init;
switch (prim) {
case prime_lisp:
sec_tok.yy_char = SECRET_ESCAPE_E;
break;
case prime_interactive:
sec_tok.yy_char = SECRET_ESCAPE_I;
break;
case prime_regex:
sec_tok.yy_char = SECRET_ESCAPE_R;
break;
case prime_json:
sec_tok.yy_char = SECRET_ESCAPE_J;
break;
}
if (p->recent_tok.yy_char && prim != prime_json)
pushback_token(p, &p->recent_tok);
pushback_token(p, &sec_tok);
prime_scanner(p->scanner, prim);
set(mkloc(p->name, p->parser), name);
}
void prime_parser_post(parser_t *p, enum prime_parser prim)
{
p->eof = (p->recent_tok.yy_char == 0);
if (prim == prime_interactive)
p->recent_tok.yy_char = 0;
}
int parser_callgraph_circ_check(struct circ_stack *rs, val obj)
{
for (; rs; rs = rs->up) {
if (rs->obj == obj)
return 0;
}
return 1;
}
static val patch_ref(parser_t *p, val obj)
{
if (consp(obj)) {
val a = pop(&obj);
if (a == circref_s) {
val num = car(obj);
val rep = gethash(p->circ_ref_hash, num);
if (!rep)
yyerrorf(p->scanner, lit("dangling #~s# ref"), num, nao);
if (consp(rep) && car(rep) == circref_s)
yyerrorf(p->scanner, lit("absurd #~s# ref"), num, nao);
if (!p->circ_count--)
yyerrorf(p->scanner, lit("unexpected surplus #~s# ref"), num, nao);
return rep;
}
}
return nil;
}
static void circ_backpatch(parser_t *p, struct circ_stack *up, val obj)
{
val self = lit("parser");
struct circ_stack cs = { up, obj };
if (!parser_callgraph_circ_check(up, obj))
return;
tail:
if (!p->circ_count)
return;
if (!is_ptr(obj))
return;
switch (type(obj)) {
case CONS:
{
us_cons_bind(a, d, obj);
val ra = patch_ref(p, a);
val rd = patch_ref(p, d);
if (ra)
us_rplaca(obj, ra);
else
circ_backpatch(p, &cs, a);
if (rd) {
us_rplacd(obj, rd);
break;
}
obj = d;
goto tail;
}
case VEC:
{
cnum i;
cnum l = c_num(length_vec(obj), self);
for (i = 0; i < l; i++) {
val v = obj->v.vec[i];
val rv = patch_ref(p, v);
if (rv)
set(mkloc(obj->v.vec[i], obj), rv);
else
circ_backpatch(p, &cs, v);
if (!p->circ_count)
break;
}
break;
}
case RNG:
{
val s = from(obj);
val e = to(obj);
val rs = patch_ref(p, s);
val re = patch_ref(p, e);
if (rs)
set_from(obj, rs);
else
circ_backpatch(p, &cs, s);
if (re) {
set_to(obj, re);
break;
}
obj = e;
goto tail;
}
case TNOD:
{
val k = obj->tn.key;
val l = obj->tn.left;
val r = obj->tn.right;
val rk = patch_ref(p, k);
val rl = patch_ref(p, l);
val rr = patch_ref(p, r);
if (rl)
set(mkloc(obj->tn.left, obj), rl);
else
circ_backpatch(p, &cs, l);
if (rr)
set(mkloc(obj->tn.right, obj), rr);
else
circ_backpatch(p, &cs, r);
if (rk)
set(mkloc(obj->tn.key, obj), rk);
obj = k;
goto tail;
}
case COBJ:
if (hashp(obj)) {
val u = get_hash_userdata(obj);
val ru = patch_ref(p, u);
cnum old_circ_count = p->circ_count;
if (ru)
set_hash_userdata(obj, ru);
else
circ_backpatch(p, &cs, u);
if (old_circ_count > 0) {
val cell;
val pairs = nil;
struct hash_iter hi;
us_hash_iter_init(&hi, obj);
while ((cell = hash_iter_next(&hi))) {
circ_backpatch(p, &cs, cell);
push(cell, &pairs);
}
if (old_circ_count != p->circ_count) {
clearhash(obj);
while (pairs) {
val cell = rcyc_pop(&pairs);
sethash(obj, us_car(cell), us_cdr(cell));
}
} else {
while (pairs)
rcyc_pop(&pairs);
}
}
} else if (structp(obj)) {
val stype = struct_type(obj);
val iter;
for (iter = slots(stype); iter; iter = cdr(iter)) {
val sn = car(iter);
val sv = slot(obj, sn);
val rsv = patch_ref(p, sv);
if (rsv)
slotset(obj, sn, rsv);
else
circ_backpatch(p, &cs, sv);
if (p->circ_count <= 0)
break;
}
} else if (treep(obj)) {
val iter = tree_begin(obj, colon_k, colon_k);
val node;
val nodes = nil;
cnum old_circ_count = p->circ_count;
while ((node = tree_next(iter))) {
val k = node->tn.key;
val rk = patch_ref(p, k);
if (rk)
set(mkloc(node->tn.key, node), rk);
else
circ_backpatch(p, &cs, k);
push(node, &nodes);
}
if (nodes && old_circ_count != p->circ_count) {
tree_clear(obj);
while (nodes) {
val node = rcyc_pop(&nodes);
tree_insert_node(obj, node, t);
}
} else {
while (nodes)
rcyc_pop(&nodes);
}
}
break;
case FUN:
if (obj->f.functype == FINTERP) {
val fun = obj->f.f.interp_fun;
circ_backpatch(p, &cs, car(fun));
obj = cadr(fun);
goto tail;
}
default:
break;
}
return;
}
void parser_resolve_circ(parser_t *p)
{
if (p->circ_count == 0)
return;
circ_backpatch(p, 0, p->syntax_tree);
if (p->circ_count > 0)
yyerrorf(p->scanner, lit("not all #<num># refs replaced in object ~s"),
p->syntax_tree, nao);
}
void parser_circ_def(parser_t *p, val num, val expr)
{
if (!p->circ_ref_hash) {
p->circ_ref_hash = make_eq_hash(hash_weak_none);
setcheck(p->parser, p->circ_ref_hash);
}
{
val new_p = nil;
loc pcdr = gethash_l(lit("parser"), p->circ_ref_hash, num, mkcloc(new_p));
if (!new_p && deref(pcdr) != unique_s)
yyerrorf(p->scanner, lit("duplicate #~s= def"), num, nao);
set(pcdr, expr);
}
}
val parser_circ_ref(parser_t *p, val num)
{
val obj = if2(p->circ_ref_hash, gethash(p->circ_ref_hash, num));
if (!obj)
yyerrorf(p->scanner, lit("dangling #~s# ref"), num, nao);
if (obj == unique_s && !p->ignore) {
p->circ_count++;
return cons(circref_s, cons(num, nil));
}
return obj;
}
void open_txr_file(val first_try_path, val *txr_lisp_p,
val *orig_in_resolved_out, val *stream,
val search_dirs, val self)
{
enum { none, tl, tlo, tlz, txr } suffix;
#if HAVE_ZLIB
struct stdio_mode m_r = stdio_mode_init_r;
#endif
if (match_str(first_try_path, lit(".txr"), negone))
suffix = txr;
else if (match_str(first_try_path, lit(".tl"), negone))
suffix = tl;
else if (match_str(first_try_path, lit(".tlo"), negone))
suffix = tlo;
else if (match_str(first_try_path, lit(".tlo.gz"), negone))
suffix = tlz;
else if (match_str(first_try_path, lit(".txr_profile"), negone))
suffix = tl;
else
suffix = none;
#if !HAVE_ZLIB
if (suffix == tlz)
uw_ethrowf(file_error_s, lit("~s: cannot open ~s files: "
"not built with zlib support"),
self, nao);
#endif
errno = 0;
{
val try_path = nil;
FILE *in = 0;
#if HAVE_ZLIB
gzFile zin = 0;
#else
const int zin = 0;
#endif
{
try_path = first_try_path;
errno = 0;
#if HAVE_ZLIB
if (suffix == tlz)
zin = w_gzopen_mode(c_str(try_path, self), L"r", m_r, self);
else
#endif
in = w_fopen(c_str(try_path, self), L"r");
if (in != 0 || zin != 0) {
switch (suffix) {
case tl:
*txr_lisp_p = t;
break;
case tlo: case tlz:
*txr_lisp_p = chr('o');
break;
case txr:
*txr_lisp_p = nil;
break;
default:
break;
}
goto found;
} else {
#ifdef ENOENT
if (errno != ENOENT)
goto except;
#endif
}
}
if (suffix == none && !*txr_lisp_p) {
try_path = scat(lit("."), first_try_path, lit("txr"), nao);
if ((in = w_fopen(c_str(try_path, nil), L"r")) != 0)
goto found;
#ifdef ENOENT
if (errno != ENOENT)
goto except;
#endif
}
if (suffix == none) {
{
try_path = scat(lit("."), first_try_path, lit("tlo"), nao);
errno = 0;
if ((in = w_fopen(c_str(try_path, nil), L"r")) != 0) {
*txr_lisp_p = chr('o');
goto found;
}
#ifdef ENOENT
if (errno != ENOENT)
goto except;
#endif
}
#if HAVE_ZLIB
{
try_path = scat(lit("."), first_try_path, lit("tlo.gz"), nao);
errno = 0;
if ((zin = w_gzopen_mode(c_str(try_path, nil), L"r", m_r, self)) != 0) {
*txr_lisp_p = chr('o');
goto found;
}
#ifdef ENOENT
if (errno != ENOENT)
goto except;
#endif
}
#endif
{
try_path = scat(lit("."), first_try_path, lit("tl"), nao);
errno = 0;
if ((in = w_fopen(c_str(try_path, nil), L"r")) != 0) {
*txr_lisp_p = t;
goto found;
}
#ifdef ENOENT
if (errno != ENOENT)
goto except;
#endif
}
}
if (in == 0 && zin == 0) {
val try_next;
#ifdef ENOENT
except:
#endif
if (abs_path_p(*orig_in_resolved_out))
search_dirs = nil;
else if (search_dirs == t)
search_dirs = load_search_dirs;
#ifdef ENOENT
if (errno != ENOENT || search_dirs == nil)
#else
if (search_dirs == nil)
#endif
uw_ethrowf(errno_to_file_error(errno),
lit("~a: unable to open ~a"), self, *orig_in_resolved_out, nao);
try_next = path_cat(pop(&search_dirs), *orig_in_resolved_out);
open_txr_file(try_next, txr_lisp_p, orig_in_resolved_out, stream,
search_dirs, self);
return;
}
found:
if (in != 0)
*stream = make_stdio_stream(in, try_path);
#if HAVE_ZLIB
else
*stream = make_gzio_stream(zin, -1, try_path, 0);
#endif
*orig_in_resolved_out = try_path;
}
}
val regex_parse(val string, val error_stream)
{
val save_stream = std_error;
val stream = make_string_byte_input_stream(string);
parser_t parser;
error_stream = default_arg_strict(error_stream, std_null);
std_error = if3(error_stream == t, std_output, error_stream);
parser_common_init(&parser);
parser.stream = stream;
{
int gc = gc_state(0);
parse(&parser, if3(std_error != std_null, lit("regex"), null_string),
prime_regex);
gc_state(gc);
}
parser_cleanup(&parser);
std_error = save_stream;
if (parser.errors)
uw_throw(syntax_error_s, lit("regex-parse: syntax errors in regex"));
return parser.syntax_tree;
}
static val lisp_parse_impl(val self, enum prime_parser prime,
val rlcp_p, val source_in,
val error_stream, val error_return_val, val name_in,
val lineno)
{
val source = default_arg_strict(source_in, std_input);
val str = stringp(source);
val input_stream = if3(str, make_string_byte_input_stream(source), source);
val name = default_arg_strict(name_in,
if3(str,
lit("string"),
stream_get_prop(input_stream, name_k)));
val parser = ensure_parser(input_stream, name);
val saved_dyn = dyn_env;
parser_t *pi = parser_get_impl(self, parser);
volatile val parsed = nil;
if (rlcp_p)
pi->rec_source_loc = 1;
uw_simple_catch_begin;
dyn_env = make_env(nil, nil, dyn_env);
error_stream = default_arg_strict(error_stream, std_null);
error_stream = if3(error_stream == t, std_output, error_stream);
class_check (self, error_stream, stream_cls);
if (lineno && !missingp(lineno))
pi->lineno = c_num(lineno, self);
env_vbind(dyn_env, stderr_s, error_stream);
for (;;) {
int gc = gc_state(0);
parse(pi, if3(std_error != std_null, name, null_string), prime);
mut(parser);
gc_state(gc);
if (pi->syntax_tree == nao && pi->errors == 0 && !pi->eof)
continue;
break;
}
if (str) {
int junk = 0;
if (prime == prime_json) {
YYSTYPE yyl;
junk = yylex(&yyl, pi->scanner);
} else {
junk = pi->recent_tok.yy_char;
}
if (junk)
yyerrorf(pi->scanner, lit("trailing material after expression"), nao);
}
parsed = t;
uw_unwind {
dyn_env = saved_dyn;
if (!parsed) {
parser_reset(pi);
}
}
uw_catch_end;
if (pi->errors || pi->syntax_tree == nao) {
if (missingp(error_return_val))
uw_throwf(syntax_error_s, lit("read: ~a: ~a"), name,
if3(pi->syntax_tree == nao,
lit("end of input reached without seeing object"),
lit("errors encountered")), nao);
return error_return_val;
}
gc_hint(parser);
return pi->syntax_tree;
}
val lisp_parse(val source_in, val error_stream, val error_return_val,
val name_in, val lineno)
{
val self = lit("lisp-parse");
return lisp_parse_impl(self, prime_lisp, t, source_in, error_stream,
error_return_val, name_in, lineno);
}
val nread(val source_in, val error_stream, val error_return_val,
val name_in, val lineno)
{
val self = lit("nread");
return lisp_parse_impl(self, prime_lisp, nil, source_in, error_stream,
error_return_val, name_in, lineno);
}
val iread(val source_in, val error_stream, val error_return_val,
val name_in, val lineno)
{
val self = lit("iread");
return lisp_parse_impl(self, prime_interactive, nil, source_in, error_stream,
error_return_val, name_in, lineno);
}
val get_json(val source_in, val error_stream, val error_return_val,
val name_in, val lineno)
{
val self = lit("get-json");
return lisp_parse_impl(self, prime_json, nil, source_in, error_stream,
error_return_val, name_in, lineno);
}
static val read_file_common(val self, val stream, val error_stream, val compiled)
{
val error_val = gensym(nil);
val name = stream_get_prop(stream, name_k);
val first = t;
val big_endian = nil;
val parser = ensure_parser(stream, name);
val not_compiled = null(compiled);
val version_form = nil;
if (compiled) {
parser_t *pi = parser_get_impl(self, parser);
pi->rec_source_loc = 0;
}
for (;;) {
val form = lisp_parse_impl(self, prime_lisp, not_compiled, stream,
error_stream, error_val, name, colon_k);
if (form == error_val) {
if (parser_errors(parser) != zero)
return nil;
break;
}
if (compiled && first) {
val major = car(form);
if (neq(major, num_fast(6)) && neq(major, num_fast(7)))
uw_throwf(error_s,
lit("cannot load ~s: version number mismatch"),
stream, nao);
big_endian = caddr(form);
first = nil;
version_form = form;
} else if (compiled) {
if (consp(car(form))) {
for (; form; form = cdr(form)) {
val item = car(form);
val nlevels = pop(&item);
val nregs = pop(&item);
val bytecode = pop(&item);
val datavec = pop(&item);
val funvec = car(item);
val desc = vm_make_desc(nlevels, nregs, bytecode, datavec, funvec);
if ((big_endian && HAVE_LITTLE_ENDIAN) ||
(!big_endian && !HAVE_LITTLE_ENDIAN))
buf_swap32(bytecode);
(void) vm_execute_toplevel(desc);
gc_hint(desc);
}
} else if (nequal(form, version_form)) {
uw_throwf(error_s, lit("~s: mismatched version ~s in combined .tlo file"),
stream, form, nao);
}
} else {
(void) eval_intrinsic(form, nil, nil);
}
}
return t;
}
val read_eval_stream(val self, val stream, val error_stream)
{
return read_file_common(self, stream, error_stream, nil);
}
val read_compiled_file(val self, val stream, val error_stream)
{
return read_file_common(self, stream, error_stream, t);
}
static val read_objects_common(val stream, val error_stream_in,
val error_return_val, val name,
val lineno, val self)
{
val error_stream = if3(error_stream_in == t,
std_output,
default_arg_strict(error_stream_in, std_null));
val parser = ensure_parser(stream, name);
parser_t *pi = parser_get_impl(self, parser);
list_collect_decl (out, ptail);
if (lineno && !missingp(lineno))
pi->lineno = c_num(lineno, self);
for (;;) {
val form = lisp_parse_impl(self, prime_lisp, t, stream,
error_stream, unique_s, name, colon_k);
if (form == unique_s) {
if (pi->errors) {
if (missingp(error_return_val))
uw_throwf(syntax_error_s, lit("read: ~a: errors encountered"),
name, nao);
return error_return_val;
}
break;
}
ptail = list_collect(ptail, form);
}
return out;
}
val read_objects_from_string(val string, val error_stream,
val error_return_val, val name_in)
{
val self = lit("read-objects-from-string");
val stream = make_string_byte_input_stream(string);
val name = default_arg(name_in, lit("string"));
return read_objects_common(stream, error_stream, error_return_val,
name, one, self);
}
val read_objects(val source_in, val error_stream, val error_return_val,
val name_in, val lineno_in)
{
val self = lit("read-objects");
val source = default_arg_strict(source_in, std_input);
val str = stringp(source);
val input_stream = if3(str, make_string_byte_input_stream(source), source);
val name = default_arg_strict(name_in,
if3(str,
lit("string"),
stream_get_prop(input_stream, name_k)));
return read_objects_common(input_stream, error_stream, error_return_val,
name, lineno_in, self);
}
val txr_parse(val source_in, val error_stream,
val error_return_val, val name_in)
{
val self = lit("txr-parse");
val source = default_arg_strict(source_in, std_input);
val input_stream = if3(stringp(source),
make_string_byte_input_stream(source),
source);
val name = default_arg_strict(name_in,
if3(stringp(source),
lit("string"),
stream_get_prop(input_stream, name_k)));
int gc = gc_state(0);
val saved_dyn = dyn_env;
val parser_obj = ensure_parser(input_stream, name);
parser_t *pi = parser_get_impl(self, parser_obj);
val loading = cdr(lookup_var(nil, load_recursive_s));
uw_simple_catch_begin;
dyn_env = make_env(nil, nil, dyn_env);
error_stream = default_arg_strict(error_stream, std_null);
error_stream = if3(error_stream == t, std_output, error_stream);
class_check (self, error_stream, stream_cls);
parse_once(self, input_stream, name);
uw_unwind {
dyn_env = saved_dyn;
mut(parser_obj);
gc_state(gc);
if (!loading)
uw_release_deferred_warnings();
}
uw_catch_end;
if (pi->errors || pi->syntax_tree == nao) {
if (missingp(error_return_val))
uw_throwf(syntax_error_s, lit("~a: ~a: ~a"), self, name,
if3(pi->syntax_tree == nao,
lit("end of input reached without seeing object"),
lit("errors encountered")), nao);