tokenizer.h

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

mercurial