|
1 /* |
|
2 * libtu/tokenizer.h |
|
3 * |
|
4 * Copyright (c) Tuomo Valkonen 1999-2000. |
|
5 * |
|
6 * This file is distributed under the terms of the "Artistic License". |
|
7 * See the included file LICENSE for details. |
|
8 */ |
|
9 |
|
10 #ifndef __LIBTU_TOKENIZER_H |
|
11 #define __LIBTU_TOKENIZER_H |
|
12 |
|
13 #include <stdio.h> |
|
14 #include "types.h" |
|
15 |
|
16 |
|
17 #define TOK_SET_LONG(TOK, VAL) {(TOK)->type=TOK_LONG; (TOK)->u.lval=VAL;} |
|
18 #define TOK_SET_DOUBLE(TOK, VAL) {(TOK)->type=TOK_DOUBLE; (TOK)->u.dval=VAL;} |
|
19 #define TOK_SET_CHAR(TOK, VAL) {(TOK)->type=TOK_CHAR; (TOK)->u.cval=VAL;} |
|
20 #define TOK_SET_STRING(TOK, VAL) {(TOK)->type=TOK_STRING; (TOK)->u.sval=VAL;} |
|
21 #define TOK_SET_IDENT(TOK, VAL) {(TOK)->type=TOK_IDENT; (TOK)->u.sval=VAL;} |
|
22 #define TOK_SET_COMMENT(TOK, VAL) {(TOK)->type=TOK_COMMENT; (TOK)->u.sval=VAL;} |
|
23 #define TOK_SET_OP(TOK, VAL) {(TOK)->type=TOK_OP; (TOK)->u.opval=VAL;} |
|
24 |
|
25 #define TOK_TYPE(TOK) ((TOK)->type) |
|
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_LONG(TOK) ((TOK)->type==TOK_LONG) |
|
35 #define TOK_IS_DOUBLE(TOK) ((TOK)->type==TOK_DOUBLE) |
|
36 #define TOK_IS_CHAR(TOK) ((TOK)->type==TOK_CHAR) |
|
37 #define TOK_IS_STRING(TOK) ((TOK)->type==TOK_STRING) |
|
38 #define TOK_IS_IDENT(TOK) ((TOK)->type==TOK_IDENT) |
|
39 #define TOK_IS_COMMENT(TOK) ((TOK)->type==TOK_COMMENT) |
|
40 #define TOK_IS_OP(TOK) ((TOK)->type==TOK_OP) |
|
41 |
|
42 #define TOK_OP_IS(TOK, OP) ((TOK)->type==TOK_OP && (TOK)->u.opval==(OP)) |
|
43 |
|
44 #define TOK_TAKE_STRING_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) |
|
45 #define TOK_TAKE_IDENT_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) |
|
46 #define TOK_TAKE_COMMENT_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) |
|
47 |
|
48 |
|
49 enum{ |
|
50 TOK_INVALID=0, |
|
51 TOK_LONG, |
|
52 TOK_DOUBLE, |
|
53 TOK_CHAR, |
|
54 TOK_STRING, |
|
55 TOK_IDENT, |
|
56 TOK_COMMENT, |
|
57 TOK_OP |
|
58 }; |
|
59 |
|
60 |
|
61 enum{ |
|
62 #define OP2(X,Y) ((X)|((Y)<<8)) |
|
63 #define OP3(X,Y,Z) ((X)|((Y)<<8)|((Z)<<16)) |
|
64 |
|
65 OP_L_PAR= '(', OP_R_PAR= ')', OP_L_BRK= '[', OP_R_BRK= ']', |
|
66 OP_L_BRC= '{', OP_R_BRC= '}', OP_COMMA= ',', OP_SCOLON= ';', |
|
67 |
|
68 OP_PLUS= '+', OP_MINUS= '-', OP_MUL= '*', OP_DIV= '/', |
|
69 OP_MOD= '%', OP_POW= '^', OP_OR= '|', OP_AND= '&', |
|
70 /*OP_NOT= '~',*/ OP_NOT= '!', OP_ASGN= '=', OP_LT= '<', |
|
71 OP_GT= '>', OP_DOT= '.', OP_COLON= ':', OP_QMARK= '?', |
|
72 OP_AT= '@', |
|
73 OP_NEXTLINE='\n',OP_EOF= -1, |
|
74 |
|
75 OP_INC= OP2('+','+'), OP_DEC= OP2('-','-'), |
|
76 OP_LSHIFT= OP2('<','<'), OP_RSHIFT= OP2('>','>'), |
|
77 OP_AS_INC= OP2('+','='), OP_AS_DEC= OP2('-','='), |
|
78 OP_AS_MUL= OP2('*','='), OP_AS_DIV= OP2('/','='), |
|
79 OP_AS_MOD= OP2('%','='), OP_AS_POW= OP2('^','='), |
|
80 |
|
81 /* AS_OR= OP2('|','='), AS_AND= OP2('&','='), */ |
|
82 OP_EQ= OP2('=','='), OP_NE= OP2('!','='), |
|
83 OP_LE= OP2('<','='), OP_GE= OP2('>','=') |
|
84 |
|
85 /* L_AND= OP2('&','&'), L_OR= OP2('|','|'), |
|
86 L_XOR= OP2('^','^'), */ |
|
87 |
|
88 /* AsLShift= OP3('<','<','='), |
|
89 AsRShift= OP3('>','>','='), */ |
|
90 |
|
91 #undef OP2 |
|
92 #undef OP3 |
|
93 }; |
|
94 |
|
95 |
|
96 typedef struct{ |
|
97 int type; |
|
98 int line; |
|
99 union{ |
|
100 long lval; |
|
101 double dval; |
|
102 char cval; |
|
103 char *sval; |
|
104 int opval; |
|
105 } u; |
|
106 } Token; |
|
107 |
|
108 #define TOK_INIT {0, 0, {0}} |
|
109 |
|
110 |
|
111 extern void tok_free(Token*tok); |
|
112 extern void tok_init(Token*tok); |
|
113 |
|
114 |
|
115 /* */ |
|
116 |
|
117 |
|
118 enum{ |
|
119 TOKZ_IGNORE_NEXTLINE=0x1, |
|
120 TOKZ_READ_COMMENTS=0x2, |
|
121 TOKZ_PARSER_INDENT_MODE=0x4 |
|
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_UNEXPECTED_EOS, |
|
146 E_TOKZ_IDENTIFIER_EXPECTED |
|
147 }; |
|
148 |
|
149 |
|
150 struct _ConfOpt; |
|
151 |
|
152 typedef struct{ |
|
153 FILE *file; |
|
154 const char *name; |
|
155 int flags; |
|
156 int line; |
|
157 int ungetc; |
|
158 const struct _ConfOpt **optstack; |
|
159 int nest_lvl; |
|
160 void *user_data; |
|
161 } Tokenizer; |
|
162 |
|
163 |
|
164 extern Tokenizer *tokz_open(const char *fname); |
|
165 extern Tokenizer *tokz_open_file(FILE *file); |
|
166 extern void tokz_close(Tokenizer *tokz); |
|
167 extern bool tokz_get_token(Tokenizer *tokz, Token *tok); |
|
168 extern void tokz_warn_error(const Tokenizer *tokz, int line, int e); |
|
169 |
|
170 #endif /* __LIBTU_TOKENIZER_H */ |