-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcprsprg.cpp
5712 lines (4941 loc) · 182 KB
/
tcprsprg.cpp
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
#ifdef RCSID
static char RCSid[] =
"$Header$";
#endif
/*
* Copyright (c) 1999, 2010 Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
tcprsprg.cpp - Program-level parsing
Function
This parser module includes statement node classes that are needed only
for a full compiler that parses at the outer program-level structure -
object definitions, 'extern' statements, etc. This file also contains code
to read and write symbol and object files. This module should not be
needed for tools that need only to compile expressions or code fragments,
such as debuggers or interpreters equipped with "eval()" functionality.
Notes
Modified
01/09/10 MJRoberts - Creation
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "os.h"
#include "t3std.h"
#include "utf8.h"
#include "tcprs.h"
#include "tctarg.h"
#include "tcgen.h"
#include "vmhash.h"
#include "tcmain.h"
#include "vmfile.h"
#include "tcmake.h"
/* ------------------------------------------------------------------------ */
/*
* Add an entry to a global symbol table, and mark the symbol as
* referenced.
*/
void CTcPrsSymtab::add_to_global_symtab(CTcPrsSymtab *tab, CTcSymbol *entry)
{
/* add the entry to the symbol table */
tab->get_hashtab()->add(entry);
/* mark the entry as referenced */
entry->mark_referenced();
}
/* ------------------------------------------------------------------------ */
/*
* Callback context for enumerating the global symbol table entries for
* writing a symbol export file
*/
struct prs_wsf_ctx
{
/* number of symbols so far */
ulong cnt;
/* the file we're writing */
CVmFile *fp;
/* the parser object */
CTcParser *prs;
};
/*
* Signature string for symbol export file. The numerical suffix at the
* end of the initial string is a format version number - we simply
* increment this each time we make a change to the format. On loading a
* symbol file, we check to make sure the format matches; if it doesn't,
* we'll abort with an error, ensuring that we don't try to read a file
* that's not formatted with the current structure. Since symbol files are
* derived directly from source files via compilation, the worst case is
* that the user has to do a full recompile when upgrading to a new
* compiler version, so we don't bother trying to maintain upward
* compatibility among the symbol file formats.
*/
static const char symbol_export_sig[] = "TADS3.SymbolExport.0007\n\r\032";
/*
* Write a symbol export file.
*/
void CTcParser::write_symbol_file(CVmFile *fp, CTcMake *make_obj)
{
prs_wsf_ctx ctx;
long size_ofs;
long cnt_ofs;
long end_ofs;
int i;
int cnt;
/* write the file signature */
fp->write_bytes(symbol_export_sig, sizeof(symbol_export_sig) - 1);
/* write a placeholder for the make-config block size */
size_ofs = fp->get_pos();
fp->write_uint4(0);
/* if there's a make object, tell it to write out its config data */
if (make_obj != 0)
{
long end_ofs;
/* write out the data */
make_obj->write_build_config_to_sym_file(fp);
/* go back and fix up the size */
end_ofs = fp->get_pos();
fp->set_pos(size_ofs);
fp->write_uint4(end_ofs - size_ofs - 4);
/* seek back to the end for continued writing */
fp->set_pos(end_ofs);
}
/*
* write the number of intrinsic function sets, followed by the
* function set names
*/
fp->write_uint4(cnt = G_cg->get_fnset_cnt());
for (i = 0 ; i < cnt ; ++i)
{
const char *nm;
size_t len;
/* get the name */
nm = G_cg->get_fnset_name(i);
/* write the length followed by the name */
fp->write_uint2(len = strlen(nm));
fp->write_bytes(nm, len);
}
/*
* write the number of intrinsic classes, followed by the intrinsic
* class names
*/
fp->write_uint4(cnt = G_cg->get_meta_cnt());
for (i = 0 ; i < cnt ; ++i)
{
const char *nm;
size_t len;
/* get the name */
nm = G_cg->get_meta_name(i);
/* write the length followed by the name */
fp->write_uint2(len = strlen(nm));
fp->write_bytes(nm, len);
}
/*
* write a placeholder for the symbol count, first remembering where
* we put it so that we can come back and fix it up after we know
* how many symbols there actually are
*/
cnt_ofs = fp->get_pos();
fp->write_uint4(0);
/* write each entry from the global symbol table */
ctx.cnt = 0;
ctx.fp = fp;
ctx.prs = this;
global_symtab_->enum_entries(&write_sym_cb, &ctx);
/* go back and fix up the counter */
end_ofs = fp->get_pos();
fp->set_pos(cnt_ofs);
fp->write_uint4(ctx.cnt);
/*
* seek back to the ending offset, in case the caller is embedding
* the symbol data stream in a larger file structure of some kind
* and will be writing additional data after the end of our portion
*/
fp->set_pos(end_ofs);
}
/*
* Callback for symbol table enumeration for writing a symbol export
* file. Writes one symbol to the file.
*/
void CTcParser::write_sym_cb(void *ctx0, CTcSymbol *sym)
{
prs_wsf_ctx *ctx = (prs_wsf_ctx *)ctx0;
/* ask the symbol to write itself to the file */
if (sym->write_to_sym_file(ctx->fp))
{
/* we wrote this symbol - count it */
++(ctx->cnt);
}
}
/* ------------------------------------------------------------------------ */
/*
* Seek to the start of the build configuration information in an object
* file
*/
ulong CTcParser::seek_sym_file_build_config_info(class CVmFile *fp)
{
char buf[128];
ulong siz;
int err = FALSE;
/* read the signature, and make sure it matches */
err_try
{
fp->read_bytes(buf, sizeof(symbol_export_sig) - 1);
if (memcmp(buf, symbol_export_sig,
sizeof(symbol_export_sig) - 1) != 0)
{
/* invalid signature - no config data available */
return 0;
}
/* read the size of the block */
siz = fp->read_uint4();
}
err_catch_disc
{
/* note the error */
err = TRUE;
}
err_end;
/*
* if an error occurred, indicate that no configuration block is
* available
*/
if (err)
return 0;
/*
* tell the caller the size of the block; the block immediately
* follows the size prefix, so the file pointer is set to the right
* position now
*/
return siz;
}
/* ------------------------------------------------------------------------ */
/*
* Read a symbol file
*/
int CTcParser::read_symbol_file(CVmFile *fp)
{
char buf[128];
ulong cnt;
ulong i;
ulong skip_len;
/* read the signature and ensure it's valid */
fp->read_bytes(buf, sizeof(symbol_export_sig) - 1);
if (memcmp(buf, symbol_export_sig, sizeof(symbol_export_sig) - 1) != 0)
{
/* it's invalid - log an error */
G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_SYMEXP_INV_SIG);
/* return failure */
return 1;
}
/*
* read the make-config data size and skip the data block - it's here
* for use by the make utility, and we don't have any use for it here
*/
skip_len = fp->read_uint4();
fp->set_pos(fp->get_pos() + skip_len);
/* read the intrinsic function set count, then read the function sets */
cnt = fp->read_uint4();
for (i = 0 ; i < cnt ; ++i)
{
char buf[256];
/* read the function set name */
if (CTcParser::read_len_prefix_str(fp, buf, sizeof(buf),
TCERR_SYMEXP_SYM_TOO_LONG))
return 101;
/* add it to the function set list */
G_cg->add_fnset(buf);
}
/* read the metaclass count, then read the metaclasses */
cnt = fp->read_uint4();
for (i = 0 ; i < cnt ; ++i)
{
char buf[256];
/* read the metaclass external name */
if (CTcParser::read_len_prefix_str(fp, buf, sizeof(buf),
TCERR_SYMEXP_SYM_TOO_LONG))
return 102;
/* add it to the metaclass list */
G_cg->find_or_add_meta(buf, strlen(buf), 0);
}
/* read the symbol count */
cnt = fp->read_uint4();
/* read the symbols */
for ( ; cnt != 0 ; --cnt)
{
/* read a symbol */
CTcSymbol *sym = CTcSymbol::read_from_sym_file(fp);
/* if that failed, abort */
if (sym == 0)
return 2;
/* check for an existing definition */
CTcSymbol *old_sym = global_symtab_->find(
sym->get_sym(), sym->get_sym_len());
/*
* if we're redefining a weak property symbol, delete the old
* property symbol
*/
if (old_sym != 0
&& old_sym != sym
&& old_sym->get_type() == TC_SYM_PROP
&& ((CTcSymProp *)old_sym)->is_weak())
{
global_symtab_->remove_entry(old_sym);
old_sym = 0;
}
/*
* If the symbol is already in the symbol table, log a warning
* and ignore the symbol. If the new symbol we loaded is the
* same object as the original symbol table entry, it means that
* this is a valid redefinition - the symbol subclass loader
* indicated this by returning the same entry.
*/
if (old_sym == sym)
{
/*
* it's a harmless redefinition - there's no need to warn
* and no need to add the symbol to the table again
*/
}
else if (old_sym != 0)
{
/*
* it's already defined - log it as a pedantic warning (it's
* not a standard-level warning because, if it is actually a
* problem, it'll cause an actual error at link time, so
* there's little value in complaining about it now)
*/
G_tcmain->log_error(0, 0, TC_SEV_PEDANTIC, TCERR_SYMEXP_REDEF,
(int)sym->get_sym_len(), sym->get_sym());
}
else
{
/* add it to the global symbol table */
global_symtab_->add_entry(sym);
}
}
/* success */
return 0;
}
/* ------------------------------------------------------------------------ */
/*
* Write to an object file
*/
/*
* Write to an object file
*/
void CTcParser::write_to_object_file(CVmFile *fp)
{
prs_wsf_ctx ctx;
long cnt_ofs;
long end_ofs;
ulong anon_cnt;
ulong nonsym_cnt;
tcprs_nonsym_obj *nonsym;
CTcSymObj *anon_obj;
CTcGramProdEntry *prod;
ulong prod_cnt;
CTcPrsExport *exp;
ulong exp_cnt;
/*
* reset the object file symbol and dictionary index values - start
* with 1, since 0 is a "null pointer" index value
*/
obj_file_sym_idx_ = 1;
obj_file_dict_idx_ = 1;
/*
* write a placeholder for the symbol index table count, the
* dictionary table count, and the actual symbol count
*/
cnt_ofs = fp->get_pos();
fp->write_uint4(0);
fp->write_uint4(0);
fp->write_uint4(0);
/* write each entry from the global symbol table */
ctx.cnt = 0;
ctx.fp = fp;
ctx.prs = this;
global_symtab_->enum_entries(&write_obj_cb, &ctx);
/* count the anonymous objects */
for (anon_cnt = 0, anon_obj = anon_obj_head_ ; anon_obj != 0 ;
++anon_cnt, anon_obj = (CTcSymObj *)anon_obj->nxt_) ;
/* write the number of anonymous objects */
fp->write_uint4(anon_cnt);
/* write the anonymous objects */
for (anon_obj = anon_obj_head_ ; anon_obj != 0 ;
anon_obj = (CTcSymObj *)anon_obj->nxt_)
{
/* write this anonymous object */
anon_obj->write_to_obj_file(fp);
}
/* count the non-symbol objects */
for (nonsym_cnt = 0, nonsym = nonsym_obj_head_ ; nonsym != 0 ;
++nonsym_cnt, nonsym = nonsym->nxt_) ;
/* write the non-symbol object ID's */
fp->write_uint4(nonsym_cnt);
for (nonsym = nonsym_obj_head_ ; nonsym != 0 ; nonsym = nonsym->nxt_)
fp->write_uint4((ulong)nonsym->id_);
/* remember where we are, and seek back to the counters for fixup */
end_ofs = fp->get_pos();
fp->set_pos(cnt_ofs);
/* write the actual symbol index count */
fp->write_uint4(obj_file_sym_idx_);
/* write the actual dictionary index count */
fp->write_uint4(obj_file_dict_idx_);
/* write the total named symbol count */
fp->write_uint4(ctx.cnt);
/*
* seek back to the ending offset so we can continue on to write the
* remainder of the file
*/
fp->set_pos(end_ofs);
/* write a placeholder for the symbol cross-reference entries */
cnt_ofs = fp->get_pos();
fp->write_uint4(0);
/*
* enumerate entries again, this time writing cross-object
* references (this must be done after we've written out the main
* part of each object, so that we have a full table of all of the
* objects loaded before we write any references)
*/
ctx.cnt = 0;
global_symtab_->enum_entries(&write_obj_ref_cb, &ctx);
/* go back and fix up the cross-reference count */
end_ofs = fp->get_pos();
fp->set_pos(cnt_ofs);
fp->write_uint4(ctx.cnt);
fp->set_pos(end_ofs);
/* write the anonymous object cross-references */
fp->write_uint4(anon_cnt);
for (anon_obj = anon_obj_head_ ; anon_obj != 0 ;
anon_obj = (CTcSymObj *)anon_obj->nxt_)
{
/* write this anonymous object's cross-references */
anon_obj->write_refs_to_obj_file(fp);
}
/* count the grammar productions */
for (prod_cnt = 0, prod = gramprod_head_ ; prod != 0 ;
++prod_cnt, prod = prod->get_next()) ;
/* write the number of productions */
fp->write_uint4(prod_cnt);
/*
* write the main list of grammar rules - this is the list of
* productions that are associated with anonymous match objects and
* hence cannot be replaced or modified
*/
for (prod = gramprod_head_ ; prod != 0 ; prod = prod->get_next())
{
/* write this entry */
prod->write_to_obj_file(fp);
}
/* write a placeholder for the named grammar rule count */
cnt_ofs = fp->get_pos();
fp->write_uint4(0);
/*
* write the private grammar rules - this is the set of rules
* associated with named match objects, so we write these by
* enumerating the symbol table again through the grammar rule writer
* callback
*/
ctx.cnt = 0;
global_symtab_->enum_entries(&write_obj_gram_cb, &ctx);
/* go back and fix up the named grammar rule count */
end_ofs = fp->get_pos();
fp->set_pos(cnt_ofs);
fp->write_uint4(ctx.cnt);
fp->set_pos(end_ofs);
/* count the exports */
for (exp_cnt = 0, exp = exp_head_ ; exp != 0 ;
++exp_cnt, exp = exp->get_next());
/* write the number of exports */
fp->write_uint4(exp_cnt);
/* write the exports */
for (exp = exp_head_ ; exp != 0 ; exp = exp->get_next())
{
/* write this entry */
exp->write_to_obj_file(fp);
}
}
/*
* Callback for symbol table enumeration for writing an object file.
* Writes one symbol to the file.
*/
void CTcParser::write_obj_cb(void *ctx0, CTcSymbol *sym)
{
prs_wsf_ctx *ctx = (prs_wsf_ctx *)ctx0;
/* ask the symbol to write itself to the file */
if (sym->write_to_obj_file(ctx->fp))
{
/* we wrote this symbol - count it */
++(ctx->cnt);
}
}
/*
* Callback for symbol table numeration - write object references to the
* object file.
*/
void CTcParser::write_obj_ref_cb(void *ctx0, CTcSymbol *sym)
{
prs_wsf_ctx *ctx = (prs_wsf_ctx *)ctx0;
/* ask the symbol to write its references to the file */
if (sym->write_refs_to_obj_file(ctx->fp))
++(ctx->cnt);
}
/*
* Callback for symbol table enumeration - write named grammar rules
*/
void CTcParser::write_obj_gram_cb(void *ctx0, CTcSymbol *sym)
{
prs_wsf_ctx *ctx = (prs_wsf_ctx *)ctx0;
CTcSymObj *obj_sym;
/* if it's not an object symbol, we can ignore it */
if (sym->get_type() != TC_SYM_OBJ)
return;
/* cast it to the proper type */
obj_sym = (CTcSymObj *)sym;
/* if it doesn't have a private grammar rule list, ignore it */
if (obj_sym->get_grammar_entry() == 0)
return;
/* count it */
++(ctx->cnt);
/* write the defining symbol's object file index */
ctx->fp->write_uint4(obj_sym->get_obj_file_idx());
/* write the grammar rule */
obj_sym->get_grammar_entry()->write_to_obj_file(ctx->fp);
}
/* ------------------------------------------------------------------------ */
/*
* Turn sourceTextGroup mode on or off
*/
void CTcParser::set_source_text_group_mode(int f)
{
/* set the new mode */
src_group_mode_ = (f != 0);
/*
* If we're turning this mode on for the first time, add the object
* definition for the sourceTextGroup object - this is an anonymous
* object that we automatically create, once per source module; all of
* the sourceTextGroup properties in the module point to this same
* object, which is how we can tell at run-time that they were defined
* in the same module.
*/
if (f && src_group_id_ == TCTARG_INVALID_OBJ)
{
CTcSymObj *obj;
CTPNStmObject *obj_stm;
CTcConstVal cval;
/* create the anonymous object */
src_group_id_ = G_cg->new_obj_id();
/* create an anonymous symbol table entry for the object */
obj = new CTcSymObj(".sourceTextGroup", 16, FALSE,
src_group_id_, FALSE, TC_META_TADSOBJ, 0);
G_prs->add_anon_obj(obj);
/* create an object statement for it */
obj_stm = new CTPNStmObject(obj, FALSE);
obj->set_obj_stm(obj_stm);
/*
* if we know the module name and sequence number, set properties
* for them in the referent object
*/
if (module_name_ != 0)
{
cval.set_sstr(module_name_, strlen(module_name_));
obj_stm->add_prop(src_group_mod_sym_, new CTPNConst(&cval),
FALSE, FALSE);
}
if (module_seqno_ != 0)
{
cval.set_int(module_seqno_);
obj_stm->add_prop(src_group_seq_sym_, new CTPNConst(&cval),
FALSE, FALSE);
}
/* add it to the nested statement list */
add_nested_stm(obj_stm);
}
}
/* ------------------------------------------------------------------------ */
/*
* Parse top-level definitions. Returns the head of a list of statement
* nodes; the statements are the top-level statements in the program.
*/
class CTPNStmProg *CTcParser::parse_top()
{
/* nothing in our list yet */
CTPNStmTop *first_stm = 0;
CTPNStmTop *last_stm = 0;
/* if there are any pending nested statements, add them to the list now */
if (nested_stm_head_ != 0)
{
/* move the nested statement list into the top-level list */
first_stm = nested_stm_head_;
last_stm = nested_stm_tail_;
/* clear out the old list */
nested_stm_head_ = nested_stm_tail_ = 0;
}
/* do not suppress errors */
int suppress_error = FALSE;
/* no end-of-file error yet */
int err = FALSE;
/* keep going until we reach the end of the file */
for (int done = FALSE ; !done && !err ; )
{
/* we don't have a statement yet */
CTPNStmTop *cur_stm = 0;
/*
* presume we'll find a valid statement and hence won't need to
* suppress subsequent errors
*/
int suppress_next_error = FALSE;
/* see what we have */
switch(G_tok->cur())
{
case TOKT_EOF:
/* we're done */
done = TRUE;
break;
case TOKT_FUNCTION:
case TOKT_METHOD:
/* parse the function definition */
cur_stm = parse_function(&err, FALSE, FALSE, FALSE, TRUE);
break;
case TOKT_EXTERN:
/* parse the extern definition */
parse_extern(&err);
break;
case TOKT_INTRINSIC:
/* parse an intrinsic function set definition */
cur_stm = parse_intrinsic(&err);
break;
case TOKT_TRANSIENT:
case TOKT_SYM:
/* it's an object or function definition */
cur_stm = parse_object_or_func(&err, FALSE, suppress_error,
&suppress_next_error);
break;
case TOKT_OBJECT:
/*
* it's an anonymous object with 'object' superclass, or an
* 'object template' definition
*/
cur_stm = parse_object_stm(&err, FALSE);
break;
case TOKT_PLUS:
case TOKT_INC:
cur_stm = parse_plus_object(&err);
break;
case TOKT_CLASS:
/* it's a class definition */
cur_stm = parse_class(&err);
break;
case TOKT_REPLACE:
/* it's a 'replace' statement */
cur_stm = parse_replace(&err);
break;
case TOKT_MODIFY:
/* it's a 'modify' statement */
cur_stm = parse_modify(&err);
break;
case TOKT_PROPERTY:
/* it's a 'property' statement */
parse_property(&err);
break;
case TOKT_EXPORT:
/* it's an 'export' statement */
parse_export(&err);
break;
case TOKT_DICTIONARY:
/* it's a 'dictionary' statement */
cur_stm = parse_dict(&err);
break;
case TOKT_GRAMMAR:
/* it's a 'grammar' statement */
cur_stm = parse_grammar(&err, FALSE, FALSE);
break;
case TOKT_ENUM:
/* it's an 'enum' statement */
parse_enum(&err);
break;
case TOKT_SEM:
/* empty statement - skip it */
G_tok->next();
break;
default:
/*
* note the error, unless we just generated the same error
* for the previous token - if we did, there's no point in
* showing it again, since we're probably skipping lots of
* tokens trying to get resynchronized
*/
if (!suppress_error)
G_tok->log_error_curtok(TCERR_REQ_FUNC_OR_OBJ);
/* suppress the next error of the same kind */
suppress_next_error = TRUE;
/* skip the errant token */
G_tok->next();
break;
}
/*
* set the error suppression status according to whether we just
* found an error - if we did, don't report another of the same
* kind twice in a row, since we're probably just scanning
* through tons of stuff trying to get re-synchronized
*/
suppress_error = suppress_next_error;
/* if we parsed a statement, add it to our list */
if (cur_stm != 0)
{
/* link the statement at the end of our list */
if (last_stm != 0)
last_stm->set_next_stm_top(cur_stm);
else
first_stm = cur_stm;
last_stm = cur_stm;
}
}
/* add the list of nested top-level statements to the statement list */
if (nested_stm_head_ != 0)
{
if (last_stm != 0)
last_stm->set_next_stm_top(nested_stm_head_);
else
first_stm = nested_stm_head_;
}
/*
* we've now moved the nested top-level statement list into the normal
* program statement list, so we can forget this as a separate list
*/
nested_stm_head_ = nested_stm_tail_ = 0;
/* construct a compound statement to hold the top-level statement list */
return new CTPNStmProg(first_stm);
}
/* ------------------------------------------------------------------------ */
/*
* Parse a 'property' top-level statement
*/
void CTcParser::parse_property(int *err)
{
int done;
/* skip the 'property' token */
G_tok->next();
/* parse property names until we run out */
for (done = FALSE ; !done ; )
{
/* we should be looking at a property name */
if (G_tok->cur() == TOKT_SYM)
{
/*
* look up the property; this will add it to the symbol table
* if it isn't already in there, and will generate an error if
* it's already defined as something other than a property
*/
look_up_prop(G_tok->getcur(), TRUE);
/* skip the symbol and check what follows */
if (G_tok->next() == TOKT_COMMA)
{
/* skip the comma and continue */
G_tok->next();
}
else if (G_tok->cur() == TOKT_SEM)
{
/*
* end of the statement - skip the semicolon and we're
* done
*/
G_tok->next();
break;
}
else
{
/*
* if it's a brace, they probably left out the semicolon;
* if it's a symbol, they probably left out the comma;
* otherwise, it's probably a stray token
*/
switch(G_tok->cur())
{
case TOKT_SYM:
/*
* probably left out a comma - flag an error, but
* proceed with parsing this new symbol
*/
G_tok->log_error_curtok(TCERR_PROPDECL_REQ_COMMA);
break;
case TOKT_LBRACE:
case TOKT_RBRACE:
case TOKT_EOF:
/* they probably left out the semicolon */
G_tok->log_error_curtok(TCERR_EXPECTED_SEMI);
return;
default:
/* log an error */
G_tok->log_error_curtok(TCERR_PROPDECL_REQ_COMMA);
/* skip the errant symbol */
G_tok->next();
/*
* if a comma follows, something was probably just
* inserted - skip the comma; if a semicolon follows,
* assume we're at the end of the statement
*/
if (G_tok->cur() == TOKT_COMMA)
{
/*
* we're probably okay again - skip the comma and
* continue
*/
G_tok->next();
}
else if (G_tok->cur() == TOKT_SEM)
{
/* skip the semicolon, and we're done */
G_tok->next();
done = TRUE;
}
/* proceed with what follows */
break;
}
}
}
else
{
/* log an error */
G_tok->log_error_curtok(TCERR_PROPDECL_REQ_SYM);
switch(G_tok->cur())
{
case TOKT_SEM:
case TOKT_LBRACE:
case TOKT_RBRACE:
case TOKT_EOF:
/* they're probably done with the statement */
return;
case TOKT_COMMA:
/*
* they probably just doubled a comma - skip it and
* continue
*/
G_tok->next();
break;
default:
/* skip this token */
G_tok->next();
/*
* if a comma follows, they probably put in a keyword or
* something like that - skip the comma and continue
*/
if (G_tok->cur() == TOKT_COMMA)
G_tok->next();
break;
}
/* if a semicolon follows, we're done */
if (G_tok->cur() == TOKT_SEM)
{
/* skip the semicolon and we're done */
G_tok->next();
break;
}
}
}
}
/* ------------------------------------------------------------------------ */
/*
* Parse an 'export' top-level statement
*/
void CTcParser::parse_export(int *err)
{
CTcPrsExport *exp;
/* skip the 'export' token and see what follows */
switch(G_tok->next())
{
case TOKT_SYM:
/* create a new export record for this symbol token */
exp = add_export(G_tok->getcur()->get_text(),