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