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