Wed, 02 Apr 2003 22:05:14 +0200
trunk: changeset 52
Makefile changes
5 | 1 | /* |
2 | * libtu/tokenizer.h | |
3 | * | |
36 | 4 | * Copyright (c) Tuomo Valkonen 1999-2002. |
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, | |
35 | 126 | TOKZ_ERROR_TOLERANT=0x8, |
127 | TOKZ_READ_FROM_BUFFER=0x10, | |
128 | TOKZ_DEFAULT_OPTION=0x20 | |
5 | 129 | }; |
130 | ||
131 | ||
132 | enum{ | |
133 | E_TOKZ_UNEXPECTED_EOF=1, | |
134 | E_TOKZ_UNEXPECTED_EOL, | |
135 | E_TOKZ_EOL_EXPECTED, | |
136 | E_TOKZ_INVALID_CHAR, | |
137 | E_TOKZ_TOOBIG, | |
138 | E_TOKZ_NUMFMT, | |
139 | E_TOKZ_NUM_JUNK, | |
140 | E_TOKZ_NOTINT, | |
141 | E_TOKZ_RANGE, | |
142 | E_TOKZ_MULTICHAR, | |
143 | ||
144 | E_TOKZ_TOKEN_LIMIT, | |
145 | E_TOKZ_UNKNOWN_OPTION, | |
146 | E_TOKZ_SYNTAX, | |
147 | E_TOKZ_INVALID_ARGUMENT, | |
148 | E_TOKZ_EOS_EXPECTED, | |
149 | E_TOKZ_TOO_FEW_ARGS, | |
150 | E_TOKZ_TOO_MANY_ARGS, | |
151 | E_TOKZ_MAX_NEST, | |
152 | E_TOKZ_IDENTIFIER_EXPECTED, | |
153 | ||
154 | E_TOKZ_LBRACE_EXPECTED | |
155 | }; | |
156 | ||
157 | ||
158 | struct _ConfOpt; | |
159 | ||
160 | typedef struct _Tokenizer_FInfo{ | |
161 | FILE *file; | |
162 | char *name; | |
163 | int line; | |
164 | int ungetc; | |
165 | Token ungettok; | |
166 | } Tokenizer_FInfo; | |
167 | ||
35 | 168 | typedef struct _Tokenizer_Buffer{ |
169 | char *data; | |
170 | int len; | |
171 | int pos; | |
172 | } Tokenizer_Buffer; | |
173 | ||
5 | 174 | typedef struct _Tokenizer{ |
175 | FILE *file; | |
176 | char *name; | |
177 | int line; | |
178 | int ungetc; | |
179 | Token ungettok; | |
180 | ||
35 | 181 | Tokenizer_Buffer buffer; |
182 | ||
5 | 183 | int flags; |
184 | const struct _ConfOpt **optstack; | |
185 | int nest_lvl; | |
186 | void *user_data; | |
187 | ||
188 | int filestack_n; | |
189 | Tokenizer_FInfo *filestack; | |
14 | 190 | |
191 | char **includepaths; | |
5 | 192 | } Tokenizer; |
193 | ||
194 | ||
195 | extern Tokenizer *tokz_open(const char *fname); | |
14 | 196 | extern Tokenizer *tokz_open_file(FILE *file, const char *fname); |
35 | 197 | extern Tokenizer *tokz_prepare_buffer(char *buffer, int len); |
5 | 198 | extern void tokz_close(Tokenizer *tokz); |
199 | extern bool tokz_get_token(Tokenizer *tokz, Token *tok); | |
200 | extern void tokz_unget_token(Tokenizer *tokz, Token *tok); | |
201 | extern void tokz_warn_error(const Tokenizer *tokz, int line, int e); | |
21 | 202 | extern void tokz_warn(const Tokenizer *tokz, int line, const char *fmt, ...); |
5 | 203 | |
204 | extern bool tokz_pushf(Tokenizer *tokz, const char *fname); | |
14 | 205 | extern bool tokz_pushf_file(Tokenizer *tokz, FILE *file, const char *fname); |
5 | 206 | extern bool tokz_popf(Tokenizer *tokz); |
207 | ||
14 | 208 | extern void tokz_set_includepaths(Tokenizer *tokz, char **paths); |
209 | ||
8 | 210 | #endif /* LIBTU_TOKENIZER_H */ |