Import libtu instead of using submodules
[notion.git] / libtu / parser.c
blob3a29dd3ad949b48146c97a8f8cb5fa0010128eef
1 /*
2 * libtu/parser.c
4 * Copyright (c) Tuomo Valkonen 1999-2002.
6 * You may distribute and modify this library under the terms of either
7 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
8 */
10 #include <string.h>
11 #include <errno.h>
13 #include "parser.h"
14 #include "misc.h"
15 #include "output.h"
17 #define MAX_TOKENS 256
18 #define MAX_NEST 256
21 enum{
22 P_NONE=1,
23 P_EOF,
24 P_STMT,
25 P_STMT_NS,
26 P_STMT_SECT,
27 P_BEG_SECT,
28 P_END_SECT
32 /* */
35 static bool opt_include(Tokenizer *tokz, int n, Token *toks);
38 static ConfOpt common_opts[]={
39 {"include", "s", opt_include, NULL},
40 {NULL, NULL, NULL, NULL}
44 /* */
47 static int read_statement(Tokenizer *tokz, Token *tokens, int *ntok_ret)
49 int ntokens=0;
50 Token *tok=NULL;
51 int had_comma=0; /* 0 - no, 1 - yes, 2 - not had, not expected */
52 int retval=0;
53 int e=0;
55 while(1){
56 tok=&tokens[ntokens];
58 if(!tokz_get_token(tokz, tok)){
59 e=1;
60 continue;
63 if(ntokens==MAX_TOKENS-1){
64 e=E_TOKZ_TOKEN_LIMIT;
65 tokz_warn_error(tokz, tok->line, e);
66 if(!(tokz->flags&TOKZ_ERROR_TOLERANT))
67 break;
68 }else{
69 ntokens++;
72 if(!TOK_IS_OP(tok)){
73 if(ntokens==1 && !had_comma){
74 /* first token */
75 had_comma=2;
76 }else{
77 if(had_comma==0)
78 goto syntax;
80 had_comma=0;
82 continue;
85 /* It is an operator */
86 ntokens--;
88 switch(TOK_OP_VAL(tok)){
89 case OP_SCOLON:
90 retval=(ntokens==0 ? P_NONE : P_STMT_NS);
91 break;
93 case OP_NEXTLINE:
94 retval=(ntokens==0 ? P_NONE : P_STMT);
95 break;
97 case OP_L_BRC:
98 retval=(ntokens==0 ? P_BEG_SECT : P_STMT_SECT);
99 break;
101 case OP_R_BRC:
102 if(ntokens==0){
103 retval=P_END_SECT;
104 }else{
105 tokz_unget_token(tokz, tok);
106 retval=P_STMT_NS;
108 break;
110 case OP_EOF:
111 retval=(ntokens==0 ? P_EOF : P_STMT_NS);
113 if(had_comma==1){
114 e=E_TOKZ_UNEXPECTED_EOF;
115 goto handle_error;
118 goto end;
120 case OP_COMMA:
121 if(had_comma!=0)
122 goto syntax;
124 had_comma=1;
125 continue;
127 default:
128 goto syntax;
131 if(had_comma!=1)
132 break;
134 syntax:
135 e=E_TOKZ_SYNTAX;
136 handle_error:
137 tokz_warn_error(tokz, tok->line, e);
139 if(!(tokz->flags&TOKZ_ERROR_TOLERANT) || retval!=0)
140 break;
143 end:
144 if(e!=0)
145 retval=-retval;
147 *ntok_ret=ntokens;
149 return retval;
153 static bool find_beg_sect(Tokenizer *tokz)
155 Token tok=TOK_INIT;
157 while(tokz_get_token(tokz, &tok)){
158 if(TOK_IS_OP(&tok)){
159 if(TOK_OP_VAL(&tok)==OP_NEXTLINE)
160 continue;
162 if(TOK_OP_VAL(&tok)==OP_SCOLON)
163 return FALSE;
165 if(TOK_OP_VAL(&tok)==OP_L_BRC)
166 return TRUE;
169 tokz_unget_token(tokz, &tok);
170 break;
172 return FALSE;
176 /* */
179 static const ConfOpt* lookup_option(const ConfOpt *opts, const char *name)
181 while(opts->optname!=NULL){
182 if(strcmp(opts->optname, name)==0)
183 return opts;
184 opts++;
186 return NULL;
190 static bool call_end_sect(Tokenizer *tokz, const ConfOpt *opts)
192 opts=lookup_option(opts, "#end");
193 if(opts!=NULL)
194 return opts->fn(tokz, 0, NULL);
196 return TRUE;
200 static bool call_cancel_sect(Tokenizer *tokz, const ConfOpt *opts)
202 opts=lookup_option(opts, "#cancel");
203 if(opts!=NULL)
204 return opts->fn(tokz, 0, NULL);
206 return TRUE;
210 /* */
213 bool parse_config_tokz(Tokenizer *tokz, const ConfOpt *options)
215 Token tokens[MAX_TOKENS];
216 bool alloced_optstack=FALSE;
217 int i, t, ntokens=0;
218 int init_nest_lvl;
219 bool had_error;
220 int errornest=0;
221 bool is_default=FALSE;
223 /* Allocate tokz->optstack if it does not yet exist (if it does,
224 * we have been called from an option handler)
226 if(!tokz->optstack){
227 tokz->optstack=ALLOC_N(const ConfOpt*, MAX_NEST);
228 if(!tokz->optstack){
229 warn_err();
230 return FALSE;
233 memset(tokz->optstack, 0, sizeof(ConfOpt*)*MAX_NEST);
234 init_nest_lvl=tokz->nest_lvl=0;
235 alloced_optstack=TRUE;
236 }else{
237 init_nest_lvl=tokz->nest_lvl;
240 tokz->optstack[init_nest_lvl]=options;
242 for(i=0; i<MAX_TOKENS; i++)
243 tok_init(&tokens[i]);
246 while(1){
247 had_error=FALSE;
249 /* free the tokens */
250 while(ntokens--)
251 tok_free(&tokens[ntokens]);
253 /* read the tokens */
254 t=read_statement(tokz, tokens, &ntokens);
256 if((had_error=t<0))
257 t=-t;
259 switch(t){
260 case P_STMT:
261 case P_STMT_NS:
262 case P_STMT_SECT:
264 if(errornest)
265 had_error=TRUE;
266 else if(tokz->flags&TOKZ_PARSER_INDENT_MODE)
267 verbose_indent(tokz->nest_lvl);
269 if(!TOK_IS_IDENT(tokens+0)){
270 had_error=TRUE;
271 tokz_warn_error(tokz, tokens->line,
272 E_TOKZ_IDENTIFIER_EXPECTED);
275 if(t==P_STMT){
276 if(find_beg_sect(tokz))
277 t=P_STMT_SECT;
280 if(had_error)
281 break;
283 /* Got the statement and its type */
285 options=lookup_option(tokz->optstack[tokz->nest_lvl],
286 TOK_IDENT_VAL(tokens+0));
287 if(options==NULL)
288 options=lookup_option(common_opts, TOK_IDENT_VAL(tokens+0));
289 if(options==NULL && (tokz->flags&TOKZ_DEFAULT_OPTION)){
290 options=lookup_option(tokz->optstack[tokz->nest_lvl], "#default");
291 is_default=(options!=NULL);
294 if(options==NULL){
295 had_error=TRUE;
296 tokz_warn_error(tokz, tokens->line, E_TOKZ_UNKNOWN_OPTION);
297 }else if(!is_default) {
298 had_error=!check_args(tokz, tokens, ntokens, options->argfmt);
301 if(had_error)
302 break;
304 /* Found the option and arguments are ok */
306 if(options->opts!=NULL){
307 if(t!=P_STMT_SECT){
308 had_error=TRUE;
309 tokz_warn_error(tokz, tokz->line, E_TOKZ_LBRACE_EXPECTED);
310 }else if(tokz->nest_lvl==MAX_NEST-1){
311 tokz_warn_error(tokz, tokz->line, E_TOKZ_MAX_NEST);
312 had_error=TRUE;
313 }else{
314 tokz->nest_lvl++;
315 tokz->optstack[tokz->nest_lvl]=options->opts;
317 }else if(t==P_STMT_SECT){
318 had_error=TRUE;
319 tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX);
322 if(!had_error && options->fn!=NULL){
323 had_error=!options->fn(tokz, ntokens, tokens);
324 if(t==P_STMT_SECT && had_error)
325 tokz->nest_lvl--;
327 break;
329 case P_EOF:
330 if(tokz_popf(tokz)){
331 break;
332 }else if(tokz->nest_lvl>0 || errornest>0){
333 tokz_warn_error(tokz, 0, E_TOKZ_UNEXPECTED_EOF);
334 had_error=TRUE;
336 goto eof;
338 case P_BEG_SECT:
339 had_error=TRUE;
340 errornest++;
341 tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX);
342 break;
344 case P_END_SECT:
345 if(tokz->nest_lvl+errornest==0){
346 tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX);
347 had_error=TRUE;
350 if(had_error)
351 break;
353 if(errornest!=0){
354 errornest--;
355 }else{
356 had_error=!call_end_sect(tokz, tokz->optstack[tokz->nest_lvl]);
357 tokz->nest_lvl--;
360 if(tokz->nest_lvl<init_nest_lvl)
361 goto eof;
364 if(!had_error)
365 continue;
367 if(t==P_STMT_SECT)
368 errornest++;
370 if(!(tokz->flags&TOKZ_ERROR_TOLERANT))
371 break;
374 eof:
375 /* free the tokens */
376 while(ntokens--)
377 tok_free(&tokens[ntokens]);
379 while(tokz->nest_lvl>=init_nest_lvl){
380 if(tokz->flags&TOKZ_ERROR_TOLERANT || !had_error)
381 call_end_sect(tokz, tokz->optstack[tokz->nest_lvl]);
382 else
383 call_cancel_sect(tokz, tokz->optstack[tokz->nest_lvl]);
384 tokz->nest_lvl--;
387 /* Free optstack if it was alloced by this call */
388 if(alloced_optstack){
389 free(tokz->optstack);
390 tokz->optstack=NULL;
391 tokz->nest_lvl=0;
394 if(tokz->flags&TOKZ_PARSER_INDENT_MODE)
395 verbose_indent(init_nest_lvl);
397 return !had_error;
401 /* */
404 bool parse_config(const char *fname, const ConfOpt *options, int flags)
406 Tokenizer *tokz;
407 bool ret;
409 tokz=tokz_open(fname);
411 if(tokz==NULL)
412 return FALSE;
414 tokz->flags|=flags&~TOKZ_READ_COMMENTS;
416 ret=parse_config_tokz(tokz, options);
418 tokz_close(tokz);
420 return ret;
424 bool parse_config_file(FILE *file, const ConfOpt *options, int flags)
426 Tokenizer *tokz;
427 bool ret;
429 tokz=tokz_open_file(file, NULL);
431 if(tokz==NULL)
432 return FALSE;
434 tokz->flags|=flags&~TOKZ_READ_COMMENTS;
436 ret=parse_config_tokz(tokz, options);
438 tokz_close(tokz);
440 return ret;
445 * Argument validity checking stuff
449 static int arg_match(Token *tok, char c, bool si)
451 char c2=tok->type;
453 if(c=='.' || c=='*')
454 return 0;
456 if(c2==c)
457 return 0;
459 if(si){
460 if(c2=='i' && c=='s'){
461 TOK_SET_IDENT(tok, TOK_STRING_VAL(tok));
462 return 0;
465 if(c2=='s' && c=='i'){
466 TOK_SET_STRING(tok, TOK_IDENT_VAL(tok));
467 return 0;
471 if(c2=='c' && c=='l'){
472 TOK_SET_LONG(tok, TOK_CHAR_VAL(tok));
473 return 0;
476 if(c2=='l' && c=='c'){
477 TOK_SET_CHAR(tok, TOK_LONG_VAL(tok));
478 return 0;
481 if(c2=='l' && c=='d'){
482 TOK_SET_DOUBLE(tok, TOK_LONG_VAL(tok));
483 return 0;
486 if(c=='b'){
487 if(c2=='l'){
488 TOK_SET_BOOL(tok, TOK_LONG_VAL(tok));
489 return 0;
490 }else if(c2=='i'){
491 if(strcmp(TOK_IDENT_VAL(tok), "TRUE")==0){
492 tok_free(tok);
493 TOK_SET_BOOL(tok, TRUE);
494 return 0;
495 }else if(strcmp(TOK_IDENT_VAL(tok), "FALSE")==0){
496 tok_free(tok);
497 TOK_SET_BOOL(tok, FALSE);
498 return 0;
503 return E_TOKZ_INVALID_ARGUMENT;
507 static int check_argument(const char **pret, Token *tok, const char *p,
508 bool si)
510 int mode;
511 int e=E_TOKZ_TOO_MANY_ARGS;
513 again:
514 mode=0;
516 if(*p=='*'){
517 *pret=p;
518 return 0;
519 }else if(*p=='?'){
520 mode=1;
521 p++;
522 }else if(*p==':'){
523 mode=2;
524 p++;
525 }else if(*p=='+'){
526 *pret=p;
527 return arg_match(tok, *(p-1), si);
530 while(*p!='\0'){
531 e=arg_match(tok, *p, si);
532 if(e==0){
533 p++;
534 while(mode==2 && *p==':'){
535 if(*++p=='\0')
536 break; /* Invalid argument format string, though... */
537 p++;
539 *pret=p;
540 return 0;
543 if(mode==0)
544 break;
546 p++;
548 if(mode==1)
549 goto again;
551 /* mode==2 */
553 if(*p!=':')
554 break;
555 p++;
556 e=E_TOKZ_TOO_MANY_ARGS;
559 *pret=p;
560 return e;
564 static bool args_at_end(const char *p)
566 if(p==NULL)
567 return TRUE;
569 while(*p!='\0'){
570 if(*p=='*' || *p=='+')
571 p++;
572 else if(*p=='?')
573 p+=2;
574 else
575 return FALSE;
578 return TRUE;
582 bool do_check_args(const Tokenizer *tokz, Token *tokens, int ntokens,
583 const char *fmt, bool si)
585 int i;
586 int e;
588 if(fmt==NULL){
589 if(ntokens!=1)
590 tokz_warn_error(tokz, tokens[0].line, E_TOKZ_TOO_MANY_ARGS);
591 return ntokens==1;
594 for(i=1; i<ntokens; i++){
595 e=check_argument(&fmt, &tokens[i], fmt, si);
596 if(e!=0){
597 tokz_warn_error(tokz, tokens[i].line, e);
598 return FALSE;
602 if(!args_at_end(fmt)){
603 tokz_warn_error(tokz, tokens[i].line, E_TOKZ_TOO_FEW_ARGS);
604 return FALSE;
607 return TRUE;
611 bool check_args(const Tokenizer *tokz, Token *tokens, int ntokens,
612 const char *fmt)
614 return do_check_args(tokz, tokens, ntokens, fmt, FALSE);
618 bool check_args_loose(const Tokenizer *tokz, Token *tokens, int ntokens,
619 const char *fmt)
621 return do_check_args(tokz, tokens, ntokens, fmt, TRUE);
625 /* */
628 static bool try_include(Tokenizer *tokz, const char *fname)
630 FILE *f;
632 f=fopen(fname, "r");
634 if(f==NULL)
635 return FALSE;
637 if(!tokz_pushf_file(tokz, f, fname)){
638 fclose(f);
639 return FALSE;
642 return TRUE;
646 static bool try_include_dir(Tokenizer *tokz, const char *dir, int dlen,
647 const char *file)
649 char *tmpname;
650 bool retval;
652 tmpname=scatn(dir, dlen, file, -1);
654 if(tmpname==NULL){
655 warn_err();
656 return FALSE;
659 retval=try_include(tokz, tmpname);
661 free(tmpname);
663 return retval;
667 static bool opt_include(Tokenizer *tokz, int n, Token *toks)
669 const char *fname=TOK_STRING_VAL(toks+1);
670 const char *lastndx=NULL;
671 bool retval, e;
672 int i=0;
674 if(fname[0]!='/' && tokz->name!=NULL)
675 lastndx=strrchr(tokz->name, '/');
677 if(lastndx==NULL)
678 retval=try_include(tokz, fname);
679 else
680 retval=try_include_dir(tokz, tokz->name, lastndx-tokz->name+1, fname);
682 if(retval==TRUE)
683 return TRUE;
685 e=errno;
687 if(tokz->includepaths!=NULL){
688 while(tokz->includepaths[i]!=NULL){
689 if(try_include_dir(tokz, tokz->includepaths[i], -1, fname))
690 return TRUE;
691 i++;
695 warn_obj(fname, "%s", strerror(e));
697 return FALSE;
701 extern void tokz_set_includepaths(Tokenizer *tokz, char **paths)
703 tokz->includepaths=paths;
708 ConfOpt libtu_dummy_confopts[]={
709 END_CONFOPTS
714 bool parse_config_tokz_skip_section(Tokenizer *tokz)
716 return parse_config_tokz(tokz, libtu_dummy_confopts);