1 | /*****************************************************************************/ |
2 | /* */ |
3 | /* scanner.h */ |
4 | /* */ |
5 | /* Source file line info structure */ |
6 | /* */ |
7 | /* */ |
8 | /* */ |
9 | /* (C) 1998-2010, Ullrich von Bassewitz */ |
10 | /* Roemerstrasse 52 */ |
11 | /* D-70794 Filderstadt */ |
12 | /* EMail: uz@cc65.org */ |
13 | /* */ |
14 | /* */ |
15 | /* This software is provided 'as-is', without any expressed or implied */ |
16 | /* warranty. In no event will the authors be held liable for any damages */ |
17 | /* arising from the use of this software. */ |
18 | /* */ |
19 | /* Permission is granted to anyone to use this software for any purpose, */ |
20 | /* including commercial applications, and to alter it and redistribute it */ |
21 | /* freely, subject to the following restrictions: */ |
22 | /* */ |
23 | /* 1. The origin of this software must not be misrepresented; you must not */ |
24 | /* claim that you wrote the original software. If you use this software */ |
25 | /* in a product, an acknowledgment in the product documentation would be */ |
26 | /* appreciated but is not required. */ |
27 | /* 2. Altered source versions must be plainly marked as such, and must not */ |
28 | /* be misrepresented as being the original software. */ |
29 | /* 3. This notice may not be removed or altered from any source */ |
30 | /* distribution. */ |
31 | /* */ |
32 | /*****************************************************************************/ |
33 | |
34 | |
35 | |
36 | #ifndef SCANNER_H |
37 | #define SCANNER_H |
38 | |
39 | |
40 | |
41 | /* common */ |
42 | #include "fp.h" |
43 | |
44 | /* cc65 */ |
45 | #include "datatype.h" |
46 | #include "ident.h" |
47 | #include "lineinfo.h" |
48 | |
49 | |
50 | |
51 | /*****************************************************************************/ |
52 | /* Token definitions */ |
53 | /*****************************************************************************/ |
54 | |
55 | |
56 | |
57 | typedef enum token_t { |
58 | TOK_INVALID, |
59 | TOK_CEOF, |
60 | |
61 | /* Storage specifiers */ |
62 | TOK_FIRST_STORAGE_CLASS, |
63 | TOK_AUTO = TOK_FIRST_STORAGE_CLASS, |
64 | TOK_EXTERN, |
65 | TOK_REGISTER, |
66 | TOK_STATIC, |
67 | TOK_TYPEDEF, |
68 | TOK_LAST_STORAGE_CLASS = TOK_TYPEDEF, |
69 | |
70 | /* Tokens denoting type qualifiers */ |
71 | TOK_FIRST_TYPEQUAL, |
72 | TOK_CONST = TOK_FIRST_TYPEQUAL, |
73 | TOK_VOLATILE, |
74 | TOK_RESTRICT, |
75 | TOK_LAST_TYPEQUAL = TOK_RESTRICT, |
76 | |
77 | /* Function specifiers */ |
78 | TOK_INLINE, |
79 | TOK_FASTCALL, |
80 | TOK_CDECL, |
81 | |
82 | /* Tokens denoting types */ |
83 | TOK_FIRST_TYPE, |
84 | TOK_ENUM = TOK_FIRST_TYPE, |
85 | TOK_CHAR, |
86 | TOK_INT, |
87 | TOK_DOUBLE, |
88 | TOK_FLOAT, |
89 | TOK_LONG, |
90 | TOK_UNSIGNED, |
91 | TOK_SIGNED, |
92 | TOK_SHORT, |
93 | TOK_STRUCT, |
94 | TOK_UNION, |
95 | TOK_VOID, |
96 | TOK_LAST_TYPE = TOK_VOID, |
97 | |
98 | /* Control statements */ |
99 | TOK_DO, |
100 | TOK_FOR, |
101 | TOK_GOTO, |
102 | TOK_IF, |
103 | TOK_RETURN, |
104 | TOK_SWITCH, |
105 | TOK_WHILE, |
106 | |
107 | TOK_ASM, |
108 | TOK_CASE, |
109 | TOK_DEFAULT, |
110 | TOK_BREAK, |
111 | TOK_CONTINUE, |
112 | TOK_ELSE, |
113 | TOK_ELLIPSIS, |
114 | TOK_SIZEOF, |
115 | |
116 | TOK_IDENT, |
117 | TOK_SEMI, |
118 | |
119 | /* Primary operators */ |
120 | TOK_LBRACK, |
121 | TOK_LPAREN, |
122 | TOK_DOT, |
123 | TOK_PTR_REF, |
124 | |
125 | TOK_LCURLY, |
126 | TOK_RBRACK, |
127 | TOK_COMP, |
128 | TOK_INC, |
129 | TOK_PLUS_ASSIGN, |
130 | TOK_PLUS, |
131 | TOK_COMMA, |
132 | TOK_DEC, |
133 | TOK_MINUS_ASSIGN, |
134 | TOK_RCURLY, |
135 | TOK_MINUS, |
136 | TOK_MUL_ASSIGN, |
137 | TOK_STAR, |
138 | TOK_MUL = TOK_STAR, /* Alias */ |
139 | TOK_DIV_ASSIGN, |
140 | TOK_DIV, |
141 | TOK_BOOL_AND, |
142 | TOK_AND_ASSIGN, |
143 | TOK_AND, |
144 | TOK_NE, |
145 | TOK_BOOL_NOT, |
146 | TOK_BOOL_OR, |
147 | TOK_OR_ASSIGN, |
148 | TOK_OR, |
149 | TOK_EQ, |
150 | TOK_ASSIGN, |
151 | |
152 | /* Inequalities */ |
153 | TOK_LE, |
154 | TOK_LT, |
155 | TOK_GE, |
156 | TOK_GT, |
157 | |
158 | TOK_SHL_ASSIGN, |
159 | TOK_SHL, |
160 | TOK_SHR_ASSIGN, |
161 | TOK_SHR, |
162 | TOK_XOR_ASSIGN, |
163 | TOK_XOR, |
164 | TOK_MOD_ASSIGN, |
165 | TOK_MOD, |
166 | TOK_QUEST, |
167 | TOK_COLON, |
168 | TOK_RPAREN, |
169 | TOK_SCONST, |
170 | TOK_ICONST, |
171 | TOK_CCONST, |
172 | TOK_FCONST, |
173 | TOK_WCSCONST, |
174 | |
175 | TOK_ATTRIBUTE, |
176 | TOK_FAR, |
177 | TOK_NEAR, |
178 | TOK_A, |
179 | TOK_X, |
180 | TOK_Y, |
181 | TOK_AX, |
182 | TOK_EAX, |
183 | |
184 | TOK_PRAGMA |
185 | } token_t; |
186 | |
187 | |
188 | |
189 | /*****************************************************************************/ |
190 | /* Data */ |
191 | /*****************************************************************************/ |
192 | |
193 | |
194 | |
195 | /* Forward for struct Literal */ |
196 | struct Literal; |
197 | |
198 | /* Token stuff */ |
199 | typedef struct Token Token; |
200 | struct Token { |
201 | token_t Tok; /* The token itself */ |
202 | long IVal; /* The integer attribute */ |
203 | Double FVal; /* The float attribute */ |
204 | struct Literal* SVal; /* String literal is any */ |
205 | ident Ident; /* Identifier if IDENT */ |
206 | LineInfo* LI; /* Source line where the token comes from */ |
207 | Type* Type; /* Type if integer or float constant */ |
208 | }; |
209 | |
210 | extern Token CurTok; /* The current token */ |
211 | extern Token NextTok; /* The next token */ |
212 | |
213 | |
214 | |
215 | /*****************************************************************************/ |
216 | /* Code */ |
217 | /*****************************************************************************/ |
218 | |
219 | |
220 | |
221 | #if defined(HAVE_INLINE) |
222 | INLINE int TokIsStorageClass (const Token* T) |
223 | /* Return true if the token is a storage class specifier */ |
224 | { |
225 | return (T->Tok >= TOK_FIRST_STORAGE_CLASS && T->Tok <= TOK_LAST_STORAGE_CLASS); |
226 | } |
227 | #else |
228 | # define TokIsStorageClass(T) \ |
229 | ((T)->Tok >= TOK_FIRST_STORAGE_CLASS && (T)->Tok <= TOK_LAST_STORAGE_CLASS) |
230 | #endif |
231 | |
232 | #if defined(HAVE_INLINE) |
233 | INLINE int TokIsType (const Token* T) |
234 | /* Return true if the token is a type */ |
235 | { |
236 | return (T->Tok >= TOK_FIRST_TYPE && T->Tok <= TOK_LAST_TYPE); |
237 | } |
238 | #else |
239 | # define TokIsType(T) ((T)->Tok >= TOK_FIRST_TYPE && (T)->Tok <= TOK_LAST_TYPE) |
240 | #endif |
241 | |
242 | #if defined(HAVE_INLINE) |
243 | INLINE int TokIsTypeQual (const Token* T) |
244 | /* Return true if the token is a type qualifier */ |
245 | { |
246 | return (T->Tok >= TOK_FIRST_TYPEQUAL && T->Tok <= TOK_LAST_TYPEQUAL); |
247 | } |
248 | #else |
249 | # define TokIsTypeQual(T) ((T)->Tok >= TOK_FIRST_TYPEQUAL && (T)->Tok <= TOK_LAST_TYPEQUAL) |
250 | #endif |
251 | |
252 | int TokIsFuncSpec (const Token* T); |
253 | /* Return true if the token is a function specifier */ |
254 | |
255 | void SymName (char* S); |
256 | /* Read a symbol from the input stream. The first character must have been |
257 | ** checked before calling this function. The buffer is expected to be at |
258 | ** least of size MAX_IDENTLEN+1. |
259 | */ |
260 | |
261 | int IsSym (char* S); |
262 | /* If a symbol follows, read it and return 1, otherwise return 0 */ |
263 | |
264 | void NextToken (void); |
265 | /* Get next token from input stream */ |
266 | |
267 | void SkipTokens (const token_t* TokenList, unsigned TokenCount); |
268 | /* Skip tokens until we reach TOK_CEOF or a token in the given token list. |
269 | ** This routine is used for error recovery. |
270 | */ |
271 | |
272 | int Consume (token_t Token, const char* ErrorMsg); |
273 | /* Eat token if it is the next in the input stream, otherwise print an error |
274 | ** message. Returns true if the token was found and false otherwise. |
275 | */ |
276 | |
277 | int ConsumeColon (void); |
278 | /* Check for a colon and skip it. */ |
279 | |
280 | int ConsumeSemi (void); |
281 | /* Check for a semicolon and skip it. */ |
282 | |
283 | int ConsumeComma (void); |
284 | /* Check for a comma and skip it. */ |
285 | |
286 | int ConsumeLParen (void); |
287 | /* Check for a left parenthesis and skip it */ |
288 | |
289 | int ConsumeRParen (void); |
290 | /* Check for a right parenthesis and skip it */ |
291 | |
292 | int ConsumeLBrack (void); |
293 | /* Check for a left bracket and skip it */ |
294 | |
295 | int ConsumeRBrack (void); |
296 | /* Check for a right bracket and skip it */ |
297 | |
298 | int ConsumeLCurly (void); |
299 | /* Check for a left curly brace and skip it */ |
300 | |
301 | int ConsumeRCurly (void); |
302 | /* Check for a right curly brace and skip it */ |
303 | |
304 | |
305 | |
306 | /* End of scanner.h */ |
307 | |
308 | #endif |
309 | |