option.c: fixed warnings
[k8jam.git] / src / parse.c
blob0ff4a59b8de98bccd684292bb3f4a6e3167e260b
1 /*
2 * Copyright 1993, 2000 Christopher Seiwald.
3 * This file is part of Jam - see jam.c for Copyright information.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * parse.c - make and destroy parse trees as driven by the parser
20 #include "jam.h"
21 #include "lists.h"
22 #include "parse.h"
23 #include "scan.h"
24 #include "newstr.h"
27 static PARSE *yypsave;
30 //#include "jamgram.c"
31 #define ParseTOKENTYPE token_t
32 #define ParseARG_PDECL
33 extern void *ParseAlloc(void *(*mallocProc)(size_t));
34 extern void Parse(
35 void *yyp, /* The parser */
36 int yymajor, /* The major token code number */
37 ParseTOKENTYPE yyminor /* The value for the token */
38 ParseARG_PDECL /* Optional %extra_argument parameter */
40 extern void ParseFree(
41 void *p, /* The parser to be deleted */
42 void (*freeProc)(void*) /* Function used to reclaim memory */
45 static int yyparse (void) {
46 int tk;
47 void *pp = ParseAlloc(malloc);
48 #ifndef NDEBUG
49 /*ParseTrace(stderr, "**");*/
50 #endif
51 do {
52 tk = yylex();
53 Parse(pp, tk, yylval);
54 } while (tk != 0);
55 ParseFree(pp, free);
56 return 0;
60 void parse_file (const char *f) {
61 /* suspend scan of current file and push this new file in the stream */
62 yyfparse(f);
63 /* now parse each block of rules and execute it */
64 /* execute it outside of the parser so that recursive */
65 /* calls to yyrun() work (no recursive yyparse's) */
66 for (;;) {
67 LOL l;
68 PARSE *p;
69 int jmp = 0; /* JMP_NONE */
70 /* $(<) and $(>) empty in outer scope */
71 lol_init(&l);
72 /* filled by yyparse() calling parse_save() */
73 yypsave = 0;
74 /* if parse error or empty parse, outta here */
75 if (yyparse() || !(p = yypsave)) break;
76 /* run the parse tree */
77 list_free((*(p->func))(p, &l, &jmp));
78 parse_free(p);
83 void parse_save (PARSE *p) {
84 yypsave = p;
88 PARSE *parse_make (
89 LIST *(*func) (PARSE *p, LOL *args, int *jmp),
90 PARSE *left,
91 PARSE *right,
92 PARSE *third,
93 const char *string,
94 const char *string1,
95 int num)
97 PARSE *p = (PARSE *)malloc(sizeof(PARSE));
98 p->func = func;
99 p->left = left;
100 p->right = right;
101 p->third = third;
102 p->string = string;
103 p->string1 = string1;
104 p->num = num;
105 p->refs = 1;
106 return p;
110 void parse_refer (PARSE *p) {
111 ++p->refs;
115 void parse_free (PARSE *p) {
116 if (--p->refs) return;
117 if (p->string) freestr(p->string);
118 if (p->string1) freestr(p->string1);
119 if (p->left) parse_free(p->left);
120 if (p->right) parse_free(p->right);
121 if (p->third) parse_free(p->third);
122 free((char *)p);
126 JAMFA_CONST const char *multiform_suffix (int cnt) {
127 return (cnt == 1 ? "" : "s");