-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtctok.cpp
9479 lines (8178 loc) · 289 KB
/
tctok.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: d:/cvsroot/tads/tads3/tctok.cpp,v 1.5 1999/07/11 00:46:58 MJRoberts Exp $";
#endif
/*
* Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
tctok.cpp - TADS3 compiler tokenizer
Function
Notes
The tokenizer features an integrated C-style preprocessor. The
preprocessor is integrated into the tokenizer for efficiency; since
the preprocessor uses the same lexical structure as the the TADS
language, we need only tokenize the input stream once, and the result
can be used both for preprocessing and for parsing.
Modified
04/12/99 MJRoberts - Creation
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include "os.h"
#include "t3std.h"
#include "vmerr.h"
#include "vmhash.h"
#include "tcerr.h"
#include "tcerrnum.h"
#include "tctok.h"
#include "tcsrc.h"
#include "tcmain.h"
#include "tchost.h"
#include "tcprs.h"
#include "tctarg.h"
#include "charmap.h"
#include "vmdatasrc.h"
#include "vmfile.h"
/* ------------------------------------------------------------------------ */
/*
* Standard macro table. This implements the interface using a standard
* hash table object.
*/
class CTcBasicMacroTable: public CTcMacroTable
{
public:
CTcBasicMacroTable(int hash_table_size, CVmHashFunc *hash_function,
int own_hash_func)
: tab(hash_table_size, hash_function, own_hash_func)
{
}
virtual void add(CVmHashEntry *entry) { tab.add(entry); }
virtual void remove(CVmHashEntry *entry) { tab.remove(entry); }
virtual CVmHashEntry *find(const char *str, size_t len)
{ return tab.find(str, len); }
virtual void enum_entries(void (*func)(void *, CVmHashEntry *), void *ctx)
{ tab.enum_entries(func, ctx); }
virtual void debug_dump() { tab.debug_dump(); }
private:
/* our hash table */
CVmHashTable tab;
};
/* ------------------------------------------------------------------------ */
/*
* string embedded expression context
*/
void tok_embed_ctx::start_expr(wchar_t qu, int triple, int report)
{
if (level < countof(stk))
{
s = stk + level;
s->enter(qu, triple);
}
else if (report)
{
G_tok->log_error(TCERR_EMBEDDING_TOO_DEEP);
}
++level;
}
/* ------------------------------------------------------------------------ */
/*
* Initialize the tokenizer
*/
CTcTokenizer::CTcTokenizer(CResLoader *res_loader,
const char *default_charset)
{
int i;
os_time_t timer;
struct tm *tblk;
const char *tstr;
char timebuf[50];
struct kwdef
{
const char *kw_text;
tc_toktyp_t kw_tok_id;
};
static const kwdef kwlist[] =
{
{ "self", TOKT_SELF },
{ "targetprop", TOKT_TARGETPROP },
{ "targetobj", TOKT_TARGETOBJ },
{ "definingobj", TOKT_DEFININGOBJ },
{ "inherited", TOKT_INHERITED },
{ "delegated", TOKT_DELEGATED },
{ "argcount", TOKT_ARGCOUNT },
{ "if", TOKT_IF },
{ "else", TOKT_ELSE },
{ "for", TOKT_FOR },
{ "while", TOKT_WHILE },
{ "do", TOKT_DO },
{ "switch", TOKT_SWITCH },
{ "case", TOKT_CASE },
{ "default", TOKT_DEFAULT },
{ "goto", TOKT_GOTO },
{ "break", TOKT_BREAK },
{ "continue", TOKT_CONTINUE },
// { "and", TOKT_AND },
// { "or", TOKT_OR },
// { "not", TOKT_NOT },
{ "function", TOKT_FUNCTION },
{ "return", TOKT_RETURN },
{ "local", TOKT_LOCAL },
{ "object", TOKT_OBJECT },
{ "nil", TOKT_NIL },
{ "true", TOKT_TRUE },
{ "pass", TOKT_PASS },
{ "external", TOKT_EXTERNAL },
{ "extern", TOKT_EXTERN },
{ "formatstring", TOKT_FORMATSTRING },
{ "class", TOKT_CLASS },
{ "replace", TOKT_REPLACE },
{ "modify", TOKT_MODIFY },
{ "new", TOKT_NEW },
// { "delete", TOKT_DELETE },
{ "throw", TOKT_THROW },
{ "try", TOKT_TRY },
{ "catch", TOKT_CATCH },
{ "finally", TOKT_FINALLY },
{ "intrinsic", TOKT_INTRINSIC },
{ "dictionary", TOKT_DICTIONARY },
{ "grammar", TOKT_GRAMMAR },
{ "enum", TOKT_ENUM },
{ "template", TOKT_TEMPLATE },
{ "static", TOKT_STATIC },
{ "foreach", TOKT_FOREACH },
{ "export", TOKT_EXPORT },
{ "propertyset", TOKT_PROPERTYSET },
{ "transient", TOKT_TRANSIENT },
{ "replaced", TOKT_REPLACED },
{ "property", TOKT_PROPERTY },
{ "operator", TOKT_OPERATOR },
{ "method", TOKT_METHOD },
{ "invokee", TOKT_INVOKEE },
// { "void", TOKT_VOID },
// { "int", TOKT_INT },
// { "string", TOKT_STRING },
// { "list", TOKT_LIST },
// { "boolean", TOKT_BOOLEAN },
// { "any", TOKT_ANY },
/* end-of-table marker */
{ 0, TOKT_INVALID }
};
const kwdef *kwp;
/* remember my resource loader */
res_loader_ = res_loader;
/* there's no stream yet */
str_ = 0;
/* no external source yet */
ext_src_ = 0;
/* start numbering the file descriptors at zero */
next_filedesc_id_ = 0;
/* there are no file descriptors yet */
desc_head_ = 0;
desc_tail_ = 0;
desc_list_ = 0;
desc_list_cnt_ = desc_list_alo_ = 0;
/* empty out the input line buffer */
clear_linebuf();
/* start out with a minimal line buffer size */
linebuf_.ensure_space(4096);
expbuf_.ensure_space(4096);
/* set up at the beginning of the input line buffer */
start_new_line(&linebuf_, 0);
/* remember the default character set */
default_charset_ = lib_copy_str(default_charset);
/* we don't have a default character mapper yet */
default_mapper_ = 0;
/* create an input mapper for the default character set, if specified */
if (default_charset != 0)
default_mapper_ = CCharmapToUni::load(res_loader, default_charset);
/*
* if the default character set wasn't specified, or we failed to
* load a mapper for the specified character set, use a plain ASCII
* mapper
*/
if (default_mapper_ == 0)
default_mapper_ = new CCharmapToUniASCII();
/* presume we're not in preprocessor-only mode */
pp_only_mode_ = FALSE;
/* presume we're not in list-includes mode */
list_includes_mode_ = FALSE;
/* presume we're not in test report mode */
test_report_mode_ = FALSE;
/* allow preprocessing directives */
allow_pp_ = TRUE;
/* there are no previously-included files yet */
prev_includes_ = 0;
/* by default, use the "collapse" mode for newlines in strings */
string_newline_spacing_ = NEWLINE_SPACING_COLLAPSE;
/* start out with ALL_ONCE mode off */
all_once_ = FALSE;
/* by default, ignore redundant includes without warning */
warn_on_ignore_incl_ = FALSE;
/* there are no include path entries yet */
incpath_head_ = incpath_tail_ = 0;
/* not in a quoted string yet */
in_quote_ = '\0';
in_triple_ = FALSE;
/* not in a #if block yet */
if_sp_ = 0;
if_false_level_ = 0;
/* not processing a preprocessor constant expression */
in_pp_expr_ = FALSE;
/* we don't have a current or appended line yet */
last_desc_ = 0;
last_linenum_ = 0;
appended_desc_ = 0;
appended_linenum_ = 0;
/* allocate the first token-list block */
init_src_block_list();
/* create the #define and #undef symbol tables */
defines_ = new CTcBasicMacroTable(512, new CVmHashFuncCS(), TRUE);
undefs_ = new CVmHashTable(64, new CVmHashFuncCS(), TRUE);
/* create the special __LINE__ and __FILE__ macros */
defines_->add(new CTcHashEntryPpLINE(this));
defines_->add(new CTcHashEntryPpFILE(this));
/* get the current time and date */
timer = os_time(0);
tblk = os_localtime(&timer);
tstr = asctime(tblk);
/*
* add the __DATE__ macro - the format is "Mmm dd yyyy", where "Mmm"
* is the three-letter month name generated by asctime(), "dd" is
* the day of the month, with a leading space for numbers less than
* ten, and "yyyy" is the year.
*/
sprintf(timebuf, "'%.3s %2d %4d'",
tstr + 4, tblk->tm_mday, tblk->tm_year + 1900);
add_define("__DATE__", timebuf);
/* add the __TIME__ macro - 24-hour "hh:mm:ss" format */
sprintf(timebuf, "'%.8s'", tstr + 11);
add_define("__TIME__", timebuf);
/*
* Allocate a pool of macro resources. The number we start with is
* arbitrary, since we'll add more as needed, but we want to try to
* allocate enough up front that we avoid time-consuming memory
* allocations later. On the other hand, we don't want to
* pre-allocate a huge number of objects that we'll never use.
*/
for (macro_res_avail_ = 0, macro_res_head_ = 0, i = 0 ; i < 7 ; ++i)
{
CTcMacroRsc *rsc;
/* allocate a new object */
rsc = new CTcMacroRsc();
/* add it onto the master list */
rsc->next_ = macro_res_head_;
macro_res_head_ = rsc;
/* add it onto the available list */
rsc->next_avail_ = macro_res_avail_;
macro_res_avail_ = rsc;
}
/* create the keyword hash table */
kw_ = new CVmHashTable(64, new CVmHashFuncCS(), TRUE);
/* populate the keyword table */
for (kwp = kwlist ; kwp->kw_text != 0 ; ++kwp)
kw_->add(new CTcHashEntryKw(kwp->kw_text, kwp->kw_tok_id));
/* no ungot tokens yet */
unget_head_ = unget_cur_ = 0;
/* no string capture file */
string_fp_ = 0;
string_fp_map_ = 0;
/* there's no current token yet */
curtok_.settyp(TOKT_NULLTOK);
curtok_.set_text("<Start of Input>", 16);
}
/*
* Initialize the source save block list
*/
void CTcTokenizer::init_src_block_list()
{
/* allocate the first source block */
src_cur_ = src_head_ = new CTcTokSrcBlock();
/* set up to write into the first block */
src_ptr_ = src_head_->get_buf();
src_rem_ = TCTOK_SRC_BLOCK_SIZE;
}
/* ------------------------------------------------------------------------ */
/*
* Delete the tokenizer
*/
CTcTokenizer::~CTcTokenizer()
{
/* delete all streams */
delete_source();
/* delete the string capture file */
if (string_fp_ != 0)
delete string_fp_;
/* delete all file descriptors */
while (desc_head_ != 0)
{
/* remember the next descriptor */
CTcTokFileDesc *nxt = desc_head_->get_next();
/* delete this one */
delete desc_head_;
/* move on to the next one */
desc_head_ = nxt;
}
/* delete the unget list */
unget_cur_ = 0;
while (unget_head_ != 0)
{
/* remember the next element */
CTcTokenEle *nxt = unget_head_->getnxt();
/* delete this element */
delete unget_head_;
/* advance to the next element */
unget_head_ = nxt;
}
/* delete the file descriptor index array */
if (desc_list_ != 0)
t3free(desc_list_);
/* delete our default character set string copy */
lib_free_str(default_charset_);
/* release our reference on our default character mapper */
default_mapper_->release_ref();
/* forget about all of our previous include files */
while (prev_includes_ != 0)
{
tctok_incfile_t *nxt;
/* remember the next file */
nxt = prev_includes_->nxt;
/* delete this one */
t3free(prev_includes_);
/* move on to the next one */
prev_includes_ = nxt;
}
/* delete the include path list */
while (incpath_head_ != 0)
{
tctok_incpath_t *nxt;
/* remember the next entry in the path */
nxt = incpath_head_->nxt;
/* delete this entry */
t3free(incpath_head_);
/* move on to the next one */
incpath_head_ = nxt;
}
/* delete the macro resources */
while (macro_res_head_ != 0)
{
CTcMacroRsc *nxt;
/* remember the next one */
nxt = macro_res_head_->next_;
/* delete this one */
delete macro_res_head_;
/* move on to the next one */
macro_res_head_ = nxt;
}
/* delete the token list */
delete src_head_;
/* delete the #define and #undef symbol tables */
delete defines_;
delete undefs_;
/* delete the keyword hash table */
delete kw_;
/* if we created a mapping for the string capture file, release it */
if (string_fp_map_ != 0)
string_fp_map_->release_ref();
}
/* ------------------------------------------------------------------------ */
/*
* Clear the line buffer
*/
void CTcTokenizer::clear_linebuf()
{
/* clear the buffer */
linebuf_.clear_text();
/* reset our read point to the start of the line buffer */
p_.set(linebuf_.get_buf());
}
/* ------------------------------------------------------------------------ */
/*
* Get a textual representation of an operator token
*/
const char *CTcTokenizer::get_op_text(tc_toktyp_t op)
{
struct tokname_t
{
tc_toktyp_t typ;
const char *nm;
};
static const tokname_t toknames[] =
{
{ TOKT_EOF, "<end of file>" },
{ TOKT_SYM, "<symbol>" },
{ TOKT_INT, "<integer>" },
{ TOKT_SSTR, "<single-quoted string>" },
{ TOKT_DSTR, "<double-quoted string>" },
{ TOKT_DSTR_START, "<double-quoted string>" },
{ TOKT_DSTR_MID, "<double-quoted string>" },
{ TOKT_DSTR_END, "<double-quoted string>" },
{ TOKT_RESTR, "<regex string>" },
{ TOKT_LPAR, "(" },
{ TOKT_RPAR, ")" },
{ TOKT_COMMA, "," },
{ TOKT_DOT, "." },
{ TOKT_LBRACE, "{" },
{ TOKT_RBRACE, "}", },
{ TOKT_LBRACK, "[", },
{ TOKT_RBRACK, "]", },
{ TOKT_EQ, "=", },
{ TOKT_EQEQ, "==", },
{ TOKT_ASI, ":=" },
{ TOKT_PLUS, "+" },
{ TOKT_MINUS, "-" },
{ TOKT_TIMES, "*" },
{ TOKT_DIV, "/", },
{ TOKT_MOD, "%" },
{ TOKT_GT, ">" },
{ TOKT_LT, "<" },
{ TOKT_GE, ">=" },
{ TOKT_LE, "<=" },
{ TOKT_NE, "!=" },
{ TOKT_ARROW, "->" },
{ TOKT_COLON, ":" },
{ TOKT_SEM, ";" },
{ TOKT_AND, "&" },
{ TOKT_ANDAND, "&&" },
{ TOKT_OR, "|" },
{ TOKT_OROR, "||" },
{ TOKT_XOR, "^" },
{ TOKT_SHL, "<<" },
{ TOKT_ASHR, ">>" },
{ TOKT_LSHR, ">>>" },
{ TOKT_INC, "++" },
{ TOKT_DEC, "--" },
{ TOKT_PLUSEQ, "+=" },
{ TOKT_MINEQ, "-=" },
{ TOKT_TIMESEQ, "*=" },
{ TOKT_DIVEQ, "/=" },
{ TOKT_MODEQ, "%=" },
{ TOKT_ANDEQ, "&=" },
{ TOKT_OREQ, "|=" },
{ TOKT_XOREQ, "^=" },
{ TOKT_SHLEQ, "<<=" },
{ TOKT_ASHREQ, ">>=" },
{ TOKT_LSHREQ, ">>>=" },
{ TOKT_NOT, "! (not)" },
{ TOKT_BNOT, "~" },
{ TOKT_POUND, "#" },
{ TOKT_POUNDPOUND, "##" },
{ TOKT_POUNDAT, "#@" },
{ TOKT_ELLIPSIS, "..." },
{ TOKT_QUESTION, "?" },
{ TOKT_QQ, "??" },
{ TOKT_COLONCOLON, "::" },
{ TOKT_FLOAT, "<float>" },
{ TOKT_BIGINT, "<bigint>" },
{ TOKT_AT, "@" },
{ TOKT_DOTDOT, ".." },
{ TOKT_SELF, "self" },
{ TOKT_TARGETPROP, "targetprop" },
{ TOKT_TARGETOBJ, "targetobj" },
{ TOKT_DEFININGOBJ, "definingobj" },
{ TOKT_INHERITED, "inherited" },
{ TOKT_DELEGATED, "delegated" },
{ TOKT_IF, "if" },
{ TOKT_ELSE, "else" },
{ TOKT_FOR, "for" },
{ TOKT_WHILE, "while" },
{ TOKT_DO, "do" },
{ TOKT_SWITCH, "switch" },
{ TOKT_CASE, "case" },
{ TOKT_DEFAULT, "default" },
{ TOKT_GOTO, "goto" },
{ TOKT_BREAK, "break" },
{ TOKT_CONTINUE, "continue" },
{ TOKT_FUNCTION, "function" },
{ TOKT_RETURN, "return" },
{ TOKT_LOCAL, "local" },
{ TOKT_OBJECT, "object" },
{ TOKT_NIL, "nil" },
{ TOKT_TRUE, "true" },
{ TOKT_PASS, "pass" },
{ TOKT_EXTERNAL, "external" },
{ TOKT_EXTERN, "extern" },
{ TOKT_FORMATSTRING, "formatstring" },
{ TOKT_CLASS, "class" },
{ TOKT_REPLACE, "replace" },
{ TOKT_MODIFY, "modify" },
{ TOKT_NEW, "new" },
// { TOKT_DELETE, "delete" },
{ TOKT_THROW, "throw" },
{ TOKT_TRY, "try" },
{ TOKT_CATCH, "catch" },
{ TOKT_FINALLY, "finally" },
{ TOKT_INTRINSIC, "intrinsic" },
{ TOKT_DICTIONARY, "dictionary" },
{ TOKT_GRAMMAR, "grammar" },
{ TOKT_ENUM, "enum" },
{ TOKT_TEMPLATE, "template" },
{ TOKT_STATIC, "static" },
{ TOKT_FOREACH, "foreach" },
{ TOKT_EXPORT, "export" },
{ TOKT_PROPERTYSET, "propertyset" },
{ TOKT_TRANSIENT, "transient" },
{ TOKT_REPLACED, "replaced" },
{ TOKT_PROPERTY, "property" },
{ TOKT_OPERATOR, "operator" },
{ TOKT_METHOD, "method" },
{ TOKT_INVOKEE, "invokee" },
// { TOKT_VOID, "void" },
// { TOKT_INTKW, "int" },
// { TOKT_STRING, "string" },
// { TOKT_LIST, "list" },
// { TOKT_BOOLEAN, "boolean" },
// { TOKT_ANY, "any"},
{ TOKT_INVALID, 0 }
};
const tokname_t *p;
/* search for the token */
for (p = toknames ; p->nm != 0 ; ++p)
{
/* if this is our token, return the associated name string */
if (p->typ == op)
return p->nm;
}
/* we didn't find it */
return "<unknown>";
}
/* ------------------------------------------------------------------------ */
/*
* Reset the tokenizer. Delete the current source object and all of the
* saved source text. This can be used after compilation of a unit
* (such as a debugger expression) is completed and the intermediate
* parser state is no longer needed.
*/
void CTcTokenizer::reset()
{
/* delete the source object */
delete_source();
/* delete saved token text */
if (src_head_ != 0)
{
/* delete the list */
delete src_head_;
/* re-initialize the source block list */
init_src_block_list();
}
}
/* ------------------------------------------------------------------------ */
/*
* Delete the source file, if any, including any parent include files.
*/
void CTcTokenizer::delete_source()
{
/* delete the current stream and all enclosing parents */
while (str_ != 0)
{
CTcTokStream *nxt;
/* remember the next stream in the list */
nxt = str_->get_parent();
/* delete this stream */
delete str_;
/* move up to the next one */
str_ = nxt;
}
/* there are no more streams */
str_ = 0;
}
/* ------------------------------------------------------------------------ */
/*
* Set up to read a source file. Returns zero on success, or a non-zero
* error code on failure.
*/
int CTcTokenizer::set_source(const char *src_filename, const char *orig_name)
{
CTcTokFileDesc *desc;
CTcSrcFile *src;
int charset_error;
int default_charset_error;
/* empty out the input line buffer */
clear_linebuf();
/* set up at the beginning of the input line buffer */
start_new_line(&linebuf_, 0);
/* create a reader for the source file */
src = CTcSrcFile::open_source(src_filename, res_loader_,
default_charset_, &charset_error,
&default_charset_error);
if (src == 0)
{
/* if we had a problem loading the default character set, log it */
if (default_charset_error)
log_error(TCERR_CANT_LOAD_DEFAULT_CHARSET, default_charset_);
/* return failure */
return TCERR_CANT_OPEN_SRC;
}
/* find or create a file descriptor for this filename */
desc = get_file_desc(src_filename, strlen(src_filename), FALSE,
orig_name, strlen(orig_name));
/*
* Create a stream to read the source file. The new stream has no
* parent, because this is the top-level source file, and was not
* included from any other file.
*/
str_ = new CTcTokStream(desc, src, 0, charset_error, if_sp_);
/* success */
return 0;
}
/*
* Set up to read source code from a memory buffer
*/
void CTcTokenizer::set_source_buf(const char *buf, size_t len)
{
CTcSrcMemory *src;
/* empty out the input line buffer */
clear_linebuf();
/* reset the scanning state to the start of a brand new stream */
in_pp_expr_ = FALSE;
last_linenum_ = 0;
unsplicebuf_.clear_text();
in_quote_ = '\0';
in_triple_ = FALSE;
comment_in_embedding_.reset();
macro_in_embedding_.reset();
main_in_embedding_.reset();
if_sp_ = 0;
if_false_level_ = 0;
unget_cur_ = 0;
/* set up at the beginning of the input line buffer */
start_new_line(&linebuf_, 0);
/* create a reader for the memory buffer */
src = new CTcSrcMemory(buf, len, default_mapper_);
/*
* Create a stream to read the source file. The new stream has no
* parent, because this is the top-level source file, and was not
* included from any other file.
*/
str_ = new CTcTokStream(0, src, 0, 0, if_sp_);
}
/* ------------------------------------------------------------------------ */
/*
* Stuff text into the source stream.
*/
void CTcTokenizer::stuff_text(const char *txt, size_t len, int expand)
{
CTcTokString expbuf;
int p_ofs;
/* if desired, expand macros */
if (expand)
{
/* expand macros in the text, storing the result in 'expbuf' */
expand_macros(&expbuf, txt, len);
/* use the expanded version as the stuffed text now */
txt = expbuf.get_text();
len = expbuf.get_text_len();
}
/* get the current p_ offset */
p_ofs = p_.getptr() - curbuf_->get_text();
/* insert the text into the buffer */
curbuf_->insert(p_ofs, txt, len);
/* reset p_ in case the curbuf_ buffer was reallocated for expansion */
start_new_line(curbuf_, p_ofs);
}
/* ------------------------------------------------------------------------ */
/*
* Find or create a file descriptor for a given filename
*/
CTcTokFileDesc *CTcTokenizer::get_file_desc(const char *fname,
size_t fname_len,
int always_create,
const char *orig_fname,
size_t orig_fname_len)
{
CTcTokFileDesc *orig_desc;
CTcTokFileDesc *desc;
/* presume we won't find an original descriptor in the list */
orig_desc = 0;
/*
* Search the list of existing descriptors to find one that matches.
* Do this regardless of whether we're allowed to re-use an existing
* one or not - even if we're creating a new one unconditionaly, we
* need to know if there's an earlier copy that already exists so we
* can associate the new one with the original.
*/
for (desc = desc_head_ ; desc != 0 ; desc = desc->get_next())
{
/* check for a name match */
if (strlen(desc->get_fname()) == fname_len
&& memcmp(desc->get_fname(), fname, fname_len) == 0)
{
/*
* if we're allowed to return an existing descriptor, return
* this one, since it's for the same filename
*/
if (!always_create)
return desc;
/*
* we have to create a new descriptor even though we have an
* existing one - remember the original so we can point the
* new one back to the original
*/
orig_desc = desc;
/*
* no need to look any further - we've found the first
* instance of this filename in our list
*/
break;
}
}
/* we didn't find a match - create a new descriptor */
desc = new CTcTokFileDesc(fname, fname_len, next_filedesc_id_++,
orig_desc, orig_fname, orig_fname_len);
/* link it in at the end of the master list */
desc->set_next(0);
if (desc_tail_ == 0)
desc_head_ = desc;
else
desc_tail_->set_next(desc);
desc_tail_ = desc;
/* expand our array index if necessary */
if (desc_list_cnt_ >= desc_list_alo_)
{
size_t siz;
/* allocate or expand the array */
desc_list_alo_ += 10;
siz = desc_list_alo_ * sizeof(desc_list_[0]);
if (desc_list_ == 0)
desc_list_ = (CTcTokFileDesc **)t3malloc(siz);
else
desc_list_ = (CTcTokFileDesc **)t3realloc(desc_list_, siz);
}
/* add the new array entry */
desc_list_[desc_list_cnt_++] = desc;
/* return it */
return desc;
}
/* ------------------------------------------------------------------------ */
/*
* Add an include path entry. Each new entry goes at the end of the
* list, after all previous entries.
*/
void CTcTokenizer::add_inc_path(const char *path)
{
tctok_incpath_t *entry;
/* create a new path list entry */
entry = (tctok_incpath_t *)t3malloc(sizeof(tctok_incpath_t)
+ strlen(path));
/* store the path in the entry */
strcpy(entry->path, path);
/* link this entry at the end of our list */
if (incpath_tail_ != 0)
incpath_tail_->nxt = entry;
else
incpath_head_ = entry;
incpath_tail_ = entry;
entry->nxt = 0;
}
/* ------------------------------------------------------------------------ */
/*
* Set the string capture file.
*/
void CTcTokenizer::set_string_capture(osfildef *fp)
{
/* delete any old capture file */
if (string_fp_ != 0)
delete string_fp_;
/*
* Remember the new capture file. Use a duplicate handle, since we
* pass ownership of the handle to the CVmFileSource object (i.e.,
* it'll close the handle when done).
*/
string_fp_ = new CVmFileSource(osfdup(fp, "w"));
/*
* if we don't already have a character mapping to translate from
* our internal unicode characters back into the source file
* character set, create one now
*/
if (string_fp_map_ == 0)
{
/* try creating a mapping for the default character set */
if (default_charset_ != 0)
string_fp_map_ =
CCharmapToLocal::load(res_loader_, default_charset_);
/* if we couldn't create the mapping, use a default ASCII mapping */
if (string_fp_map_ == 0)
string_fp_map_ = CCharmapToLocal::load(res_loader_, "us-ascii");
}
}
/* ------------------------------------------------------------------------ */
/*
* Get the next token in the input stream, reading additional lines from
* the source file as needed.
*/
tc_toktyp_t CTcTokenizer::next()
{
/* the current token is about to become the previous token */
prvtok_ = curtok_;
/* if there's an un-got token, return it */
if (unget_cur_ != 0)
{
/* get the current unget token */
curtok_ = *unget_cur_;
/* we've now consumed this ungotten token */
unget_cur_ = unget_cur_->getprv();
/* return the new token's type */
return curtok_.gettyp();
}
/* if there's an external source, get its next token */
if (ext_src_ != 0)
{
const CTcToken *ext_tok;
/* get the next token from the external source */
ext_tok = ext_src_->get_next_token();
/* check to see if we got a token */
if (ext_tok == 0)
{
/*
* restore the current token in effect before this source was
* active
*/
curtok_ = *ext_src_->get_enclosing_curtok();
/*
* this source has no more tokens - restore the enclosing
* source, and keep going so we try getting a token from it
*/
ext_src_ = ext_src_->get_enclosing_source();
/* return the token type */
return curtok_.gettyp();
}
else
{
/* we got a token - copy it to our internal token buffer */
curtok_ = *ext_tok;
/* return its type */