Fri, 27 Oct 2000 15:17:27 +0200
trunk: changeset 28
Added errmsg_* functions alike warn_* -- asprintf and vasprintf
required now.
5 | 1 | /* |
2 | * libtu/tokenizer.h | |
3 | * | |
9 | 4 | * Copyright (c) Tuomo Valkonen 1999-2000. |
5 | 5 | * See the included file LICENSE for details. |
6 | */ | |
7 | ||
8 | 8 | #ifndef LIBTU_TOKENIZER_H |
9 | #define LIBTU_TOKENIZER_H | |
5 | 10 | |
11 | #include <stdio.h> | |
12 | #include "types.h" | |
13 | ||
14 | ||
18 | 15 | #define TOK_SET_BOOL(TOK, VAL) {(TOK)->type=TOK_BOOL; (TOK)->u.bval=VAL;} |
5 | 16 | #define TOK_SET_LONG(TOK, VAL) {(TOK)->type=TOK_LONG; (TOK)->u.lval=VAL;} |
17 | #define TOK_SET_DOUBLE(TOK, VAL) {(TOK)->type=TOK_DOUBLE; (TOK)->u.dval=VAL;} | |
18 | #define TOK_SET_CHAR(TOK, VAL) {(TOK)->type=TOK_CHAR; (TOK)->u.cval=VAL;} | |
19 | #define TOK_SET_STRING(TOK, VAL) {(TOK)->type=TOK_STRING; (TOK)->u.sval=VAL;} | |
20 | #define TOK_SET_IDENT(TOK, VAL) {(TOK)->type=TOK_IDENT; (TOK)->u.sval=VAL;} | |
21 | #define TOK_SET_COMMENT(TOK, VAL) {(TOK)->type=TOK_COMMENT; (TOK)->u.sval=VAL;} | |
22 | #define TOK_SET_OP(TOK, VAL) {(TOK)->type=TOK_OP; (TOK)->u.opval=VAL;} | |
23 | ||
24 | #define TOK_TYPE(TOK) ((TOK)->type) | |
18 | 25 | #define TOK_BOOL_VAL(TOK) ((TOK)->u.bval) |
5 | 26 | #define TOK_LONG_VAL(TOK) ((TOK)->u.lval) |
27 | #define TOK_DOUBLE_VAL(TOK) ((TOK)->u.dval) | |
28 | #define TOK_CHAR_VAL(TOK) ((TOK)->u.cval) | |
29 | #define TOK_STRING_VAL(TOK) ((TOK)->u.sval) | |
30 | #define TOK_IDENT_VAL(TOK) ((TOK)->u.sval) | |
31 | #define TOK_COMMENT_VAL(TOK) ((TOK)->u.sval) | |
32 | #define TOK_OP_VAL(TOK) ((TOK)->u.opval) | |
33 | ||
34 | #define TOK_IS_INVALID(TOK) ((TOK)->type==TOK_INVALID) | |
18 | 35 | #define TOK_IS_BOOL(TOK) ((TOK)->type==TOK_BOOL) |
5 | 36 | #define TOK_IS_LONG(TOK) ((TOK)->type==TOK_LONG) |
37 | #define TOK_IS_DOUBLE(TOK) ((TOK)->type==TOK_DOUBLE) | |
38 | #define TOK_IS_CHAR(TOK) ((TOK)->type==TOK_CHAR) | |
39 | #define TOK_IS_STRING(TOK) ((TOK)->type==TOK_STRING) | |
40 | #define TOK_IS_IDENT(TOK) ((TOK)->type==TOK_IDENT) | |
41 | #define TOK_IS_COMMENT(TOK) ((TOK)->type==TOK_COMMENT) | |
42 | #define TOK_IS_OP(TOK) ((TOK)->type==TOK_OP) | |
43 | ||
44 | #define TOK_OP_IS(TOK, OP) ((TOK)->type==TOK_OP && (TOK)->u.opval==(OP)) | |
45 | ||
46 | #define TOK_TAKE_STRING_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) | |
47 | #define TOK_TAKE_IDENT_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) | |
48 | #define TOK_TAKE_COMMENT_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) | |
49 | ||
50 | ||
51 | enum{ | |
52 | TOK_INVALID=0, | |
20 | 53 | TOK_LONG='l', |
54 | TOK_DOUBLE='d', | |
55 | TOK_CHAR='c', | |
56 | TOK_STRING='s', | |
57 | TOK_IDENT='i', | |
58 | TOK_BOOL='b', | |
59 | TOK_COMMENT='#', | |
60 | TOK_OP='+' | |
5 | 61 | }; |
62 | ||
63 | ||
64 | enum{ | |
65 | #define OP2(X,Y) ((X)|((Y)<<8)) | |
66 | #define OP3(X,Y,Z) ((X)|((Y)<<8)|((Z)<<16)) | |
67 | ||
68 | OP_L_PAR= '(', OP_R_PAR= ')', OP_L_BRK= '[', OP_R_BRK= ']', | |
69 | OP_L_BRC= '{', OP_R_BRC= '}', OP_COMMA= ',', OP_SCOLON= ';', | |
70 | ||
71 | OP_PLUS= '+', OP_MINUS= '-', OP_MUL= '*', OP_DIV= '/', | |
72 | OP_MOD= '%', OP_POW= '^', OP_OR= '|', OP_AND= '&', | |
73 | /*OP_NOT= '~',*/ OP_NOT= '!', OP_ASGN= '=', OP_LT= '<', | |
74 | OP_GT= '>', OP_DOT= '.', OP_COLON= ':', OP_QMARK= '?', | |
75 | OP_AT= '@', | |
76 | OP_NEXTLINE='\n',OP_EOF= -1, | |
77 | ||
78 | OP_INC= OP2('+','+'), OP_DEC= OP2('-','-'), | |
79 | OP_LSHIFT= OP2('<','<'), OP_RSHIFT= OP2('>','>'), | |
80 | OP_AS_INC= OP2('+','='), OP_AS_DEC= OP2('-','='), | |
81 | OP_AS_MUL= OP2('*','='), OP_AS_DIV= OP2('/','='), | |
82 | OP_AS_MOD= OP2('%','='), OP_AS_POW= OP2('^','='), | |
83 | ||
84 | /* AS_OR= OP2('|','='), AS_AND= OP2('&','='), */ | |
85 | OP_EQ= OP2('=','='), OP_NE= OP2('!','='), | |
86 | OP_LE= OP2('<','='), OP_GE= OP2('>','=') | |
87 | ||
88 | /* L_AND= OP2('&','&'), L_OR= OP2('|','|'), | |
89 | L_XOR= OP2('^','^'), */ | |
90 | ||
91 | /* AsLShift= OP3('<','<','='), | |
92 | AsRShift= OP3('>','>','='), */ | |
93 | ||
94 | #undef OP2 | |
95 | #undef OP3 | |
96 | }; | |
97 | ||
98 | ||
99 | typedef struct{ | |
100 | int type; | |
101 | int line; | |
102 | union{ | |
18 | 103 | bool bval; |
5 | 104 | long lval; |
105 | double dval; | |
106 | char cval; | |
107 | char *sval; | |
108 | int opval; | |
109 | } u; | |
110 | } Token; | |
111 | ||
112 | #define TOK_INIT {0, 0, {0}} | |
113 | ||
114 | ||
115 | extern void tok_free(Token*tok); | |
116 | extern void tok_init(Token*tok); | |
117 | ||
118 | ||
119 | /* */ | |
120 | ||
121 | ||
122 | enum{ | |
123 | TOKZ_IGNORE_NEXTLINE=0x1, | |
124 | TOKZ_READ_COMMENTS=0x2, | |
125 | TOKZ_PARSER_INDENT_MODE=0x04, | |
126 | TOKZ_ERROR_TOLERANT=0x8 | |
127 | }; | |
128 | ||
129 | ||
130 | enum{ | |
131 | E_TOKZ_UNEXPECTED_EOF=1, | |
132 | E_TOKZ_UNEXPECTED_EOL, | |
133 | E_TOKZ_EOL_EXPECTED, | |
134 | E_TOKZ_INVALID_CHAR, | |
135 | E_TOKZ_TOOBIG, | |
136 | E_TOKZ_NUMFMT, | |
137 | E_TOKZ_NUM_JUNK, | |
138 | E_TOKZ_NOTINT, | |
139 | E_TOKZ_RANGE, | |
140 | E_TOKZ_MULTICHAR, | |
141 | ||
142 | E_TOKZ_TOKEN_LIMIT, | |
143 | E_TOKZ_UNKNOWN_OPTION, | |
144 | E_TOKZ_SYNTAX, | |
145 | E_TOKZ_INVALID_ARGUMENT, | |
146 | E_TOKZ_EOS_EXPECTED, | |
147 | E_TOKZ_TOO_FEW_ARGS, | |
148 | E_TOKZ_TOO_MANY_ARGS, | |
149 | E_TOKZ_MAX_NEST, | |
150 | E_TOKZ_IDENTIFIER_EXPECTED, | |
151 | ||
152 | E_TOKZ_LBRACE_EXPECTED | |
153 | }; | |
154 | ||
155 | ||
156 | struct _ConfOpt; | |
157 | ||
158 | typedef struct _Tokenizer_FInfo{ | |
159 | FILE *file; | |
160 | char *name; | |
161 | int line; | |
162 | int ungetc; | |
163 | Token ungettok; | |
164 | } Tokenizer_FInfo; | |
165 | ||
166 | typedef struct _Tokenizer{ | |
167 | FILE *file; | |
168 | char *name; | |
169 | int line; | |
170 | int ungetc; | |
171 | Token ungettok; | |
172 | ||
173 | int flags; | |
174 | const struct _ConfOpt **optstack; | |
175 | int nest_lvl; | |
176 | void *user_data; | |
177 | ||
178 | int filestack_n; | |
179 | Tokenizer_FInfo *filestack; | |
14 | 180 | |
181 | char **includepaths; | |
5 | 182 | } Tokenizer; |
183 | ||
184 | ||
185 | extern Tokenizer *tokz_open(const char *fname); | |
14 | 186 | extern Tokenizer *tokz_open_file(FILE *file, const char *fname); |
5 | 187 | extern void tokz_close(Tokenizer *tokz); |
188 | extern bool tokz_get_token(Tokenizer *tokz, Token *tok); | |
189 | extern void tokz_unget_token(Tokenizer *tokz, Token *tok); | |
190 | extern void tokz_warn_error(const Tokenizer *tokz, int line, int e); | |
21 | 191 | extern void tokz_warn(const Tokenizer *tokz, int line, const char *fmt, ...); |
5 | 192 | |
193 | extern bool tokz_pushf(Tokenizer *tokz, const char *fname); | |
14 | 194 | extern bool tokz_pushf_file(Tokenizer *tokz, FILE *file, const char *fname); |
5 | 195 | extern bool tokz_popf(Tokenizer *tokz); |
196 | ||
14 | 197 | extern void tokz_set_includepaths(Tokenizer *tokz, char **paths); |
198 | ||
8 | 199 | #endif /* LIBTU_TOKENIZER_H */ |