Sun, 14 May 2000 18:58:42 +0200
trunk: changeset 13
Use $(ANSI_SOURCE)
0 | 1 | /* |
2 | * libtu/tokenizer.c | |
3 | * | |
9 | 4 | * Copyright (c) Tuomo Valkonen 1999-2000. |
0 | 5 | * See the included file LICENSE for details. |
6 | */ | |
7 | ||
8 | #include <errno.h> | |
9 | #include <stdio.h> | |
10 | #include <ctype.h> | |
11 | #include <limits.h> | |
12 | #include <assert.h> | |
13 | #include <math.h> | |
14 | #include <string.h> | |
15 | ||
5 | 16 | #include <libtu/tokenizer.h> |
17 | #include <libtu/misc.h> | |
18 | #include <libtu/output.h> | |
0 | 19 | |
20 | ||
21 | static const char *errors[]={ | |
22 | DUMMY_TR("(no error)"), | |
23 | DUMMY_TR("Unexpected end of file"), /* E_TOKZ_UNEXPECTED_EOF */ | |
24 | DUMMY_TR("Unexpected end of line"), /* E_TOKZ_UNEXPECTED_EOL */ | |
25 | DUMMY_TR("End of line expected"), /* E_TOKZ_EOL_EXPECTED */ | |
26 | DUMMY_TR("Invalid character"), /* E_TOKZ_INVALID_CHAR*/ | |
27 | DUMMY_TR("Numeric constant too big"), /* E_TOKZ_TOOBIG */ | |
28 | DUMMY_TR("Invalid numberic format"), /* E_TOKZ_NUMFMT */ | |
29 | DUMMY_TR("Junk after numeric constant"), /* E_TOKZ_NUM_JUNK */ | |
30 | DUMMY_TR("Not an integer"), /* E_TOKZ_NOTINT */ | |
31 | DUMMY_TR("Numeric constant out of range"), /* E_TOKZ_RANGE */ | |
32 | DUMMY_TR("Multi-character character constant"), /* E_TOKZ_MULTICHAR */ | |
33 | DUMMY_TR("Token/statement limit reached"), /* E_TOKZ_TOKEN_LIMIT */ | |
34 | DUMMY_TR("Unknown option"), /* E_TOKZ_UNKONWN_OPTION */ | |
35 | DUMMY_TR("Syntax error"), /* E_TOKZ_SYNTAX */ | |
36 | DUMMY_TR("Invalid argument"), /* E_TOKZ_INVALID_ARGUMENT */ | |
37 | DUMMY_TR("End of statement expected"), /* E_TOKZ_EOS_EXPECTED */ | |
38 | DUMMY_TR("Too few arguments"), /* E_TOKZ_TOO_FEW_ARGS */ | |
39 | DUMMY_TR("Too many arguments"), /* E_TOKZ_TOO_MANY_ARGS */ | |
40 | DUMMY_TR("Maximum section nestin level exceeded"), /* E_TOK_Z_MAX_NEST */ | |
41 | DUMMY_TR("Identifier expected"), /* E_TOKZ_IDENTIFIER_EXPECTED */ | |
2 | 42 | DUMMY_TR("Starting brace ('{') expected"), /* E_TOKZ_LBRACE_EXPECTED */ |
0 | 43 | }; |
44 | ||
45 | ||
46 | /* */ | |
47 | ||
48 | #define STRBLEN 32 | |
49 | ||
2 | 50 | #define STRING_DECL(X) int err=0; char* X=NULL; char X##_tmp[STRBLEN]; int X##_tmpl=0 |
51 | #define STRING_DECL_P(X, P) int err=0; char* X=NULL; char X##_tmp[STRBLEN]=P; int X##_tmpl=sizeof(P)-1 | |
52 | #define STRING_APPEND(X, C) {if(!_string_append(&X, X##_tmp, &X##_tmpl, c)) err=-ENOMEM;} | |
0 | 53 | #define STRING_FREE(X) if(X!=NULL) free(X) |
2 | 54 | #define STRING_FINISH(X) {if(err!=0) return err; if(!_string_finish(&X, X##_tmp, X##_tmpl)) err=-ENOMEM;} |
0 | 55 | |
56 | ||
57 | static bool _string_append(char **p, char *tmp, int *tmplen, char c) | |
58 | { | |
59 | char *tmp2; | |
60 | ||
61 | if(*tmplen==STRBLEN-1){ | |
62 | tmp[STRBLEN-1]='\0'; | |
63 | if(*p!=NULL){ | |
64 | tmp2=scat(*p, tmp); | |
65 | free(*p); | |
66 | *p=tmp2; | |
67 | }else{ | |
68 | *p=scopy(tmp); | |
69 | } | |
70 | *tmplen=1; | |
71 | tmp[0]=c; | |
72 | return *p!=NULL; | |
73 | }else{ | |
74 | tmp[(*tmplen)++]=c; | |
75 | return TRUE; | |
76 | } | |
77 | } | |
78 | ||
79 | ||
80 | static bool _string_finish(char **p, char *tmp, int tmplen) | |
81 | { | |
82 | char *tmp2; | |
83 | ||
84 | if(tmplen==0){ | |
85 | if(*p==NULL) | |
86 | *p=scopy(""); | |
87 | }else{ | |
88 | tmp[tmplen]='\0'; | |
89 | if(*p!=NULL){ | |
90 | tmp2=scat(*p, tmp); | |
91 | free(*p); | |
92 | *p=tmp2; | |
93 | }else{ | |
94 | *p=scopy(tmp); | |
95 | } | |
96 | } | |
97 | return *p!=NULL; | |
98 | } | |
99 | ||
100 | ||
101 | /* */ | |
102 | ||
103 | ||
104 | #define INC_LINE() tokz->line++ | |
105 | #define GETCH() _getch(tokz) | |
106 | #define UNGETCH(C) _ungetch(tokz, C) | |
107 | ||
108 | static int _getch(Tokenizer *tokz) | |
109 | { | |
110 | int c; | |
111 | ||
112 | if(tokz->ungetc!=-1){ | |
113 | c=tokz->ungetc; | |
114 | tokz->ungetc=-1; | |
115 | }else{ | |
116 | c=getc(tokz->file); | |
117 | } | |
118 | /* if(c=='\n') | |
119 | tokz->line++;*/ | |
120 | ||
121 | return c; | |
122 | } | |
123 | ||
124 | ||
125 | static void _ungetch(Tokenizer *tokz, int c) | |
126 | { | |
127 | /* if(c=='\n') | |
128 | tokz->line--;*/ | |
129 | tokz->ungetc=c; | |
130 | /*ungetc(c, tokz->file);*/ | |
131 | } | |
132 | ||
133 | ||
134 | /* */ | |
135 | ||
136 | ||
137 | static int scan_line_comment(Token *tok, Tokenizer *tokz) | |
138 | { | |
139 | STRING_DECL_P(s, "#"); | |
140 | int c; | |
141 | ||
142 | c=GETCH(); | |
143 | ||
144 | while(c!='\n' && c!=EOF){ | |
145 | STRING_APPEND(s, c); | |
146 | c=GETCH(); | |
147 | } | |
148 | ||
149 | UNGETCH(c); | |
150 | ||
151 | STRING_FINISH(s); | |
152 | ||
153 | TOK_SET_COMMENT(tok, s); | |
154 | ||
155 | return 0; | |
156 | } | |
157 | ||
158 | ||
159 | static int skip_line_comment(Tokenizer *tokz) | |
160 | { | |
161 | int c; | |
162 | ||
163 | do{ | |
164 | c=GETCH(); | |
165 | }while(c!='\n' && c!=EOF); | |
166 | ||
167 | UNGETCH(c); | |
168 | ||
169 | return 0; | |
170 | } | |
171 | ||
172 | ||
173 | /* */ | |
174 | ||
175 | ||
176 | static int scan_c_comment(Token *tok, Tokenizer *tokz) | |
177 | { | |
178 | STRING_DECL_P(s, "/*"); | |
179 | int c; | |
180 | int st=0; | |
181 | ||
182 | while(1){ | |
183 | c=GETCH(); | |
184 | ||
185 | if(c==EOF){ | |
186 | STRING_FREE(s); | |
187 | return E_TOKZ_UNEXPECTED_EOF; | |
188 | } | |
189 | ||
190 | STRING_APPEND(s, c); | |
191 | ||
192 | if(c=='\n'){ | |
193 | INC_LINE(); | |
194 | }else if(st==0 && c=='*'){ | |
195 | st=1; | |
196 | }else if(st==1){ | |
197 | if(c=='/') | |
198 | break; | |
199 | st=0; | |
200 | } | |
201 | } | |
202 | ||
203 | STRING_FINISH(s); | |
204 | ||
205 | TOK_SET_COMMENT(tok, s); | |
206 | ||
207 | return 0; | |
208 | } | |
209 | ||
210 | ||
211 | static int skip_c_comment(Tokenizer *tokz) | |
212 | { | |
213 | int c; | |
214 | int st=0; | |
215 | ||
216 | while(1){ | |
217 | c=GETCH(); | |
218 | ||
219 | if(c==EOF) | |
220 | return E_TOKZ_UNEXPECTED_EOF; | |
221 | ||
222 | if(c=='\n') | |
223 | INC_LINE(); | |
224 | else if(st==0 && c=='*') | |
225 | st=1; | |
226 | else if(st==1){ | |
227 | if(c=='/') | |
228 | break; | |
229 | st=0; | |
230 | } | |
231 | } | |
232 | ||
233 | return 0; | |
234 | } | |
235 | ||
236 | ||
237 | /* */ | |
238 | ||
239 | ||
240 | static int scan_char_escape(Tokenizer *tokz) | |
241 | { | |
242 | static char* special_chars="nrtbae"; | |
243 | static char* specials="\n\r\t\b\a\033"; | |
244 | int base, max; | |
245 | int i ,c; | |
246 | ||
247 | c=GETCH(); | |
248 | ||
249 | for(i=0;special_chars[i];i++){ | |
250 | if(special_chars[i]==c) | |
251 | return specials[c]; | |
252 | } | |
253 | ||
254 | if(c=='x' || c=='X'){ | |
255 | base=16;max=2;i=0; | |
256 | }else if(c=='d' || c=='D'){ | |
257 | base=10;max=3;i=0; | |
258 | }else if(c=='8' || c=='9'){ | |
259 | base=10;max=2;i=c-'0'; | |
260 | }else if('0'<=c && c<='7'){ | |
261 | base=8;max=2;i=c-'0'; | |
262 | }else if(c=='\n'){ | |
263 | UNGETCH(c); | |
264 | return -2; | |
265 | }else{ | |
266 | return c; | |
267 | } | |
268 | ||
269 | ||
270 | while(--max>=0){ | |
271 | c=GETCH(); | |
272 | ||
273 | if(c==EOF) | |
274 | return EOF; | |
275 | ||
276 | if(c=='\n'){ | |
277 | UNGETCH(c); | |
278 | return -2; | |
279 | } | |
280 | ||
281 | if(base==16){ | |
282 | if(!isxdigit(c)) | |
283 | break; | |
284 | ||
285 | i<<=4; | |
286 | ||
287 | if(isdigit(c)) | |
288 | i+=c-'0'; | |
289 | else if(i>='a') | |
290 | i+=0xa+c-'a'; | |
291 | else | |
292 | i+=0xa+c-'a'; | |
293 | ||
294 | }else if(base==10){ | |
295 | if(!isdigit(c)) | |
296 | break; | |
297 | i*=10; | |
298 | i+=c-'0'; | |
299 | }else{ | |
300 | if(c<'0' || c>'7') | |
301 | break; | |
302 | i<<=3; | |
303 | i+=c-'0'; | |
304 | } | |
305 | } | |
306 | ||
307 | if(max>=0) | |
308 | UNGETCH(c); | |
309 | ||
310 | return i; | |
311 | } | |
312 | ||
313 | ||
314 | /* */ | |
315 | ||
316 | ||
317 | static int scan_string(Token *tok, Tokenizer *tokz, bool escapes) | |
318 | { | |
319 | STRING_DECL(s); | |
320 | int c; | |
321 | ||
322 | while(1){ | |
323 | c=GETCH(); | |
324 | ||
325 | if(c=='"') | |
326 | break; | |
327 | ||
328 | if(c=='\n'){ | |
329 | UNGETCH(c); | |
330 | STRING_FREE(s); | |
331 | return E_TOKZ_UNEXPECTED_EOL; | |
332 | } | |
333 | ||
334 | if(c=='\\' && escapes){ | |
335 | c=scan_char_escape(tokz); | |
336 | if(c==-2){ | |
337 | STRING_FREE(s); | |
338 | return E_TOKZ_UNEXPECTED_EOL; | |
339 | } | |
340 | } | |
341 | ||
342 | if(c==EOF){ | |
343 | STRING_FREE(s); | |
344 | return E_TOKZ_UNEXPECTED_EOF; | |
345 | } | |
346 | ||
347 | STRING_APPEND(s, c); | |
348 | } | |
349 | ||
350 | STRING_FINISH(s); | |
351 | ||
352 | TOK_SET_STRING(tok, s); | |
353 | ||
354 | return 0; | |
355 | } | |
356 | ||
357 | ||
358 | /* */ | |
359 | ||
360 | ||
361 | static int scan_char(Token *tok, Tokenizer *tokz) | |
362 | { | |
363 | int c, c2; | |
364 | ||
365 | c=GETCH(); | |
366 | ||
367 | if(c==EOF) | |
368 | return E_TOKZ_UNEXPECTED_EOF; | |
369 | ||
370 | if(c=='\n') | |
371 | return E_TOKZ_UNEXPECTED_EOL; | |
372 | ||
373 | if(c=='\\'){ | |
374 | c=scan_char_escape(tokz); | |
375 | ||
376 | if(c==EOF) | |
377 | return E_TOKZ_UNEXPECTED_EOF; | |
378 | ||
379 | if(c==-2) | |
380 | return E_TOKZ_UNEXPECTED_EOL; | |
381 | } | |
382 | ||
383 | c2=GETCH(); | |
384 | ||
385 | if(c2!='\'') | |
386 | return E_TOKZ_MULTICHAR; | |
387 | ||
388 | TOK_SET_CHAR(tok, c); | |
389 | ||
390 | return 0; | |
391 | } | |
392 | ||
393 | ||
394 | /* */ | |
395 | ||
396 | ||
397 | #define START_IDENT(X) (isalpha(X) || X=='_' || X=='$') | |
398 | ||
399 | ||
400 | static int scan_identifier(Token *tok, Tokenizer *tokz, int c) | |
401 | { | |
402 | STRING_DECL(s); | |
403 | ||
404 | do{ | |
405 | STRING_APPEND(s, c); | |
406 | c=GETCH(); | |
407 | }while(isalnum(c) || c=='_' || c=='$'); | |
408 | ||
409 | UNGETCH(c); | |
410 | ||
411 | STRING_FINISH(s); | |
412 | ||
413 | TOK_SET_IDENT(tok, s); | |
414 | ||
415 | return 0; | |
416 | } | |
417 | ||
3 | 418 | #define NP_SIMPLE_IMPL |
0 | 419 | #include "numparser2.h" |
420 | #include "np-conv.h" | |
421 | ||
422 | ||
423 | static int scan_number(Token *tok, Tokenizer *tokz, int c) | |
424 | { | |
425 | NPNum num=NUM_INIT; | |
426 | int e; | |
427 | ||
428 | if((e=parse_number(&num, tokz, c))) | |
429 | return e; | |
430 | ||
431 | if(num.type==NPNUM_INT){ | |
432 | long l; | |
433 | if((e=num_to_long(&l, &num, TRUE))) | |
434 | return e; | |
435 | ||
436 | TOK_SET_LONG(tok, l); | |
437 | }else if(num.type==NPNUM_FLOAT){ | |
438 | double d; | |
439 | if((e=num_to_double(&d, &num))) | |
440 | return e; | |
441 | ||
442 | TOK_SET_DOUBLE(tok, d); | |
443 | }else{ | |
444 | return E_TOKZ_NUMFMT; | |
445 | } | |
446 | ||
447 | return 0; | |
448 | } | |
449 | ||
450 | ||
451 | /* */ | |
452 | ||
453 | ||
454 | static uchar op_map[]={ | |
455 | 0x00, /* ________ 0-7 */ | |
456 | 0x00, /* ________ 8-15 */ | |
457 | 0x00, /* ________ 16-23 */ | |
458 | 0x00, /* ________ 24-31 */ | |
459 | 0x62, /* _!___%&_ 32-39 */ | |
460 | 0xff, /* ()*+,-./ 40-47 */ | |
461 | 0x00, /* ________ 48-55 */ | |
462 | 0xfc, /* __:;<=>? 56-63 */ | |
463 | 0x01, /* @_______ 64-71 */ | |
464 | 0x00, /* ________ 72-79 */ | |
465 | 0x00, /* ________ 80-87 */ | |
466 | 0x78, /* ___[_]^_ 88-95 */ | |
467 | 0x00, /* ________ 96-103 */ | |
468 | 0x00, /* ________ 104-111 */ | |
469 | 0x00, /* ________ 112-119 */ | |
470 | 0x38 /* ___{|}__ 120-127 */ | |
471 | }; | |
472 | ||
473 | ||
474 | static bool map_isset(uchar *map, uint ch) | |
475 | { | |
476 | if(ch>127) | |
477 | return FALSE; | |
478 | ||
479 | return map[ch>>3]&(1<<(ch&7)); | |
480 | } | |
481 | ||
482 | ||
483 | static bool is_opch(uint ch) | |
484 | { | |
485 | return map_isset(op_map, ch); | |
486 | } | |
487 | ||
488 | ||
489 | static int scan_op(Token *tok, Tokenizer *tokz, int c) | |
490 | { | |
491 | int c2; | |
492 | int op=-1; | |
493 | ||
494 | /* Quickly check it is an operator character */ | |
495 | if(!is_opch(c)) | |
496 | return E_TOKZ_INVALID_CHAR; | |
497 | ||
498 | switch(c){ | |
499 | case '+': | |
500 | case '-': | |
501 | case '*': | |
502 | /* case '/': Checked elsewhere */ | |
503 | case '%': | |
504 | case '^': | |
505 | case '!': | |
506 | case '=': | |
507 | case '<': | |
508 | case '>': | |
509 | c2=GETCH(); | |
510 | if(c2=='='){ | |
511 | op=c|(c2<<8); | |
512 | }else if(c2==c && (c2!='%' && c2!='!' && c2!='*')){ | |
513 | if(c=='<' || c=='>'){ | |
514 | int c3=GETCH(); | |
515 | if(c3=='='){ | |
516 | op=c|(c2<<8)|(c3<<16); | |
517 | }else{ | |
518 | UNGETCH(c3); | |
519 | op=c|(c2<<8); | |
520 | } | |
521 | }else{ | |
522 | op=c|(c2<<8); | |
523 | } | |
524 | }else{ | |
525 | UNGETCH(c2); | |
526 | op=c; | |
527 | } | |
528 | break; | |
529 | ||
530 | /* It is already known that it is a operator so these are not needed | |
531 | case ':': | |
532 | case '~': | |
533 | case '?': | |
534 | case '.': | |
535 | case ';'; | |
536 | case '{': | |
537 | case '}': | |
538 | case '@': | |
539 | case '|': | |
540 | case '&': | |
541 | */ | |
542 | default: | |
543 | op=c; | |
544 | } | |
545 | ||
546 | TOK_SET_OP(tok, op); | |
547 | ||
548 | return 0; | |
549 | } | |
550 | ||
551 | ||
552 | /* */ | |
553 | ||
554 | ||
555 | void tokz_warn_error(const Tokenizer *tokz, int line, int e) | |
556 | { | |
557 | if(e==E_TOKZ_UNEXPECTED_EOF) | |
558 | line=0; | |
559 | ||
560 | if(e<0) | |
561 | warn_obj_line(tokz->name, line, "%s", strerror(-e)); | |
562 | else | |
563 | warn_obj_line(tokz->name, line, "%s", TR(errors[e])); | |
564 | } | |
565 | ||
566 | ||
567 | bool tokz_get_token(Tokenizer *tokz, Token *tok) | |
568 | { | |
569 | int c, c2, e; | |
570 | ||
1 | 571 | assert(tokz->file!=NULL); |
0 | 572 | |
573 | tok_free(tok); | |
574 | ||
2 | 575 | if(!TOK_IS_INVALID(&(tokz->ungettok))){ |
576 | *tok=tokz->ungettok; | |
577 | tokz->ungettok.type=TOK_INVALID; | |
578 | return TRUE; | |
579 | } | |
580 | ||
0 | 581 | while(1){ |
582 | ||
583 | e=0; | |
584 | ||
585 | do{ | |
586 | c=GETCH(); | |
587 | }while(c!='\n' && c!=EOF && isspace(c)); | |
588 | ||
589 | tok->line=tokz->line; | |
590 | ||
591 | switch(c){ | |
592 | case EOF: | |
593 | TOK_SET_OP(tok, OP_EOF); | |
594 | return TRUE; | |
595 | ||
596 | case '\n': | |
597 | INC_LINE(); | |
598 | ||
599 | if(tokz->flags&TOKZ_IGNORE_NEXTLINE) | |
600 | continue; | |
601 | ||
602 | TOK_SET_OP(tok, OP_NEXTLINE); | |
603 | ||
604 | return TRUE; | |
605 | ||
606 | case '\\': | |
607 | do{ | |
608 | c=GETCH(); | |
609 | if(c==EOF){ | |
610 | TOK_SET_OP(tok, OP_EOF); | |
611 | return FALSE; | |
612 | } | |
2 | 613 | if(!isspace(c) && e==0){ |
614 | e=E_TOKZ_EOL_EXPECTED; | |
615 | tokz_warn_error(tokz, tokz->line, e); | |
616 | if(!(tokz->flags&TOKZ_ERROR_TOLERANT)) | |
617 | return FALSE; | |
0 | 618 | } |
619 | }while(c!='\n'); | |
620 | ||
621 | INC_LINE(); | |
622 | continue; | |
623 | ||
624 | case '#': | |
625 | if(tokz->flags&TOKZ_READ_COMMENTS){ | |
626 | e=scan_line_comment(tok, tokz); | |
627 | break; | |
628 | }else if((e=skip_line_comment(tokz))){ | |
629 | break; | |
630 | } | |
631 | ||
632 | continue; | |
633 | ||
634 | case '/': | |
2 | 635 | c2=GETCH(); |
636 | ||
637 | if(c2=='='){ | |
638 | TOK_SET_OP(tok, OP_AS_DIV); | |
639 | return TRUE; | |
0 | 640 | } |
641 | ||
2 | 642 | if(c2!='*'){ |
643 | UNGETCH(c2); | |
644 | TOK_SET_OP(tok, OP_DIV); | |
645 | return TRUE; | |
646 | } | |
647 | ||
648 | if(tokz->flags&TOKZ_READ_COMMENTS){ | |
649 | e=scan_c_comment(tok, tokz); | |
650 | break; | |
651 | }else if((e=skip_c_comment(tokz))){ | |
652 | break; | |
653 | } | |
654 | ||
655 | continue; | |
656 | ||
0 | 657 | case '\"': |
658 | e=scan_string(tok, tokz, TRUE); | |
659 | break; | |
660 | ||
661 | case '\'': | |
662 | e=scan_char(tok, tokz); | |
663 | break; | |
664 | ||
665 | default: | |
666 | if(('0'<=c && c<='9') || c=='-' || c=='+'){ | |
667 | e=scan_number(tok, tokz, c); | |
668 | break; | |
669 | } | |
670 | ||
671 | if(START_IDENT(c)) | |
672 | e=scan_identifier(tok, tokz, c); | |
673 | else | |
674 | e=scan_op(tok, tokz, c); | |
675 | } | |
676 | ||
677 | if(!e) | |
678 | return TRUE; | |
679 | ||
680 | tokz_warn_error(tokz, tokz->line, e); | |
681 | return FALSE; | |
682 | } | |
683 | } | |
684 | ||
685 | ||
2 | 686 | void tokz_unget_token(Tokenizer *tokz, Token *tok) |
687 | { | |
688 | tok_free(&(tokz->ungettok)); | |
689 | tokz->ungettok=*tok; | |
690 | tok->type=TOK_INVALID; | |
691 | } | |
692 | ||
693 | ||
1 | 694 | /* |
695 | * File open | |
696 | */ | |
697 | ||
698 | static bool do_tokz_pushf(Tokenizer *tokz) | |
699 | { | |
700 | Tokenizer_FInfo *finfo; | |
701 | ||
702 | finfo=REALLOC_N(tokz->filestack, Tokenizer_FInfo, | |
703 | tokz->filestack_n, tokz->filestack_n+1); | |
704 | ||
705 | if(finfo==NULL) | |
706 | return FALSE; | |
707 | ||
708 | tokz->filestack=finfo; | |
709 | finfo=&(finfo[tokz->filestack_n++]); | |
710 | ||
711 | finfo->file=tokz->file; | |
712 | finfo->name=tokz->name; | |
713 | finfo->line=tokz->line; | |
714 | finfo->ungetc=tokz->ungetc; | |
2 | 715 | finfo->ungettok=tokz->ungettok; |
716 | ||
1 | 717 | return TRUE; |
718 | } | |
719 | ||
720 | ||
721 | bool tokz_pushf_file(Tokenizer *tokz, FILE *file) | |
722 | { | |
723 | if(file==NULL) | |
724 | return FALSE; | |
725 | ||
726 | if(tokz->file!=NULL){ | |
727 | if(!do_tokz_pushf(tokz)){ | |
728 | warn_err(); | |
729 | return FALSE; | |
730 | } | |
731 | } | |
732 | ||
733 | tokz->file=file; | |
734 | tokz->name=NULL; | |
735 | tokz->line=1; | |
2 | 736 | tokz->ungetc=-1; |
737 | tokz->ungettok.type=TOK_INVALID; | |
1 | 738 | |
739 | return TRUE; | |
740 | } | |
741 | ||
742 | ||
743 | bool tokz_pushf(Tokenizer *tokz, const char *fname) | |
744 | { | |
745 | FILE *file; | |
746 | char *fname_copy; | |
0 | 747 | |
748 | file=fopen(fname, "r"); | |
749 | ||
750 | if(file==NULL){ | |
751 | warn_err_obj(fname); | |
1 | 752 | return FALSE; |
0 | 753 | } |
754 | ||
1 | 755 | fname_copy=scopy(fname); |
0 | 756 | |
1 | 757 | if(fname_copy==NULL){ |
758 | warn_err(); | |
759 | goto err1; | |
760 | } | |
761 | ||
762 | if(!tokz_pushf_file(tokz, file)) | |
763 | goto err2; | |
764 | ||
765 | tokz->name=fname_copy; | |
766 | ||
767 | return TRUE; | |
0 | 768 | |
1 | 769 | err2: |
770 | free(fname_copy); | |
771 | err1: | |
772 | fclose(file); | |
773 | return FALSE; | |
0 | 774 | } |
775 | ||
776 | ||
1 | 777 | |
778 | static Tokenizer *tokz_create() | |
0 | 779 | { |
780 | Tokenizer*tokz; | |
781 | ||
782 | tokz=ALLOC(Tokenizer); | |
783 | ||
784 | if(tokz==NULL){ | |
785 | warn_err(); | |
786 | return NULL; | |
787 | } | |
788 | ||
1 | 789 | tokz->file=NULL; |
0 | 790 | tokz->name=NULL; |
791 | tokz->line=1; | |
2 | 792 | tokz->ungetc=-1; |
793 | tokz->ungettok.type=TOK_INVALID; | |
0 | 794 | tokz->flags=0; |
795 | tokz->optstack=NULL; | |
796 | tokz->nest_lvl=0; | |
1 | 797 | tokz->filestack_n=0; |
798 | tokz->filestack=NULL; | |
799 | ||
800 | return tokz; | |
801 | } | |
802 | ||
803 | ||
804 | Tokenizer *tokz_open(const char *fname) | |
805 | { | |
806 | Tokenizer *tokz; | |
807 | ||
808 | tokz=tokz_create(); | |
809 | ||
810 | if(!tokz_pushf(tokz, fname)){ | |
811 | free(tokz); | |
812 | return NULL; | |
813 | } | |
814 | ||
815 | return tokz; | |
816 | } | |
817 | ||
818 | ||
819 | Tokenizer *tokz_open_file(FILE *file) | |
820 | { | |
821 | Tokenizer *tokz; | |
822 | ||
823 | tokz=tokz_create(); | |
824 | ||
825 | if(!tokz_pushf_file(tokz, file)){ | |
826 | free(tokz); | |
827 | return NULL; | |
828 | } | |
0 | 829 | |
830 | return tokz; | |
831 | } | |
832 | ||
833 | ||
1 | 834 | /* |
835 | * File close | |
836 | */ | |
837 | ||
838 | static bool do_tokz_popf(Tokenizer *tokz, bool shrink) | |
839 | { | |
840 | Tokenizer_FInfo *finfo; | |
841 | ||
842 | if(tokz->filestack_n<=0) | |
843 | return FALSE; | |
844 | ||
845 | if(tokz->file!=NULL) | |
846 | fclose(tokz->file); | |
847 | if(tokz->name!=NULL) | |
848 | free(tokz->name); | |
849 | ||
850 | finfo=&(tokz->filestack[--tokz->filestack_n]); | |
851 | ||
852 | tokz->file=finfo->file; | |
853 | tokz->name=finfo->name; | |
854 | tokz->line=finfo->line; | |
855 | tokz->ungetc=finfo->ungetc; | |
2 | 856 | tokz->ungettok=finfo->ungettok; |
857 | ||
1 | 858 | if(tokz->filestack_n==0){ |
859 | free(tokz->filestack); | |
860 | tokz->filestack=NULL; | |
861 | }else if(shrink){ | |
862 | finfo=REALLOC_N(tokz->filestack, Tokenizer_FInfo, | |
863 | tokz->filestack_n+1, tokz->filestack_n); | |
864 | if(finfo==NULL) | |
865 | warn_err(); | |
866 | else | |
867 | tokz->filestack=finfo; | |
868 | } | |
869 | ||
870 | return TRUE; | |
871 | } | |
872 | ||
873 | ||
874 | bool tokz_popf(Tokenizer *tokz) | |
875 | { | |
876 | return do_tokz_popf(tokz, TRUE); | |
877 | } | |
878 | ||
879 | ||
0 | 880 | void tokz_close(Tokenizer *tokz) |
881 | { | |
1 | 882 | while(tokz->filestack_n>0) |
883 | do_tokz_popf(tokz, FALSE); | |
884 | ||
0 | 885 | if(tokz->file!=NULL) |
886 | fclose(tokz->file); | |
1 | 887 | if(tokz->name!=NULL) |
888 | free(tokz->name); | |
2 | 889 | tok_free(&(tokz->ungettok)); |
890 | ||
0 | 891 | free(tokz); |
892 | } | |
893 | ||
894 | ||
1 | 895 | |
0 | 896 | /* */ |
897 | ||
898 | ||
899 | void tok_free(Token *tok) | |
900 | { | |
2 | 901 | if(TOK_IS_STRING(tok) || TOK_IS_IDENT(tok) || TOK_IS_COMMENT(tok)){ |
902 | if(TOK_STRING_VAL(tok)!=NULL) | |
903 | free(TOK_STRING_VAL(tok)); | |
904 | } | |
0 | 905 | |
906 | tok->type=TOK_INVALID; | |
907 | } | |
908 | ||
909 | ||
910 | void tok_init(Token *tok) | |
911 | { | |
912 | static Token dummy=TOK_INIT; | |
913 | ||
914 | memcpy(tok, &dummy, sizeof(*tok)); | |
915 | } | |
916 |