]> git.sur5r.net Git - cc65/blob - src/cc65/scanner.h
6c3db2ed21423da04df1e849fe55e59e960f9c5b
[cc65] / src / cc65 / scanner.h
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 scanner.h                                 */
4 /*                                                                           */
5 /*                      Source file line info structure                      */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2009, 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
81     /* Tokens denoting types */
82     TOK_FIRST_TYPE,
83     TOK_ENUM            = TOK_FIRST_TYPE,
84     TOK_CHAR,
85     TOK_INT,
86     TOK_DOUBLE,
87     TOK_FLOAT,
88     TOK_LONG,
89     TOK_UNSIGNED,
90     TOK_SIGNED,
91     TOK_SHORT,
92     TOK_STRUCT,
93     TOK_UNION,
94     TOK_VOID,
95     TOK_LAST_TYPE       = TOK_VOID,
96
97     /* Control statements */
98     TOK_DO,
99     TOK_FOR,
100     TOK_GOTO,
101     TOK_IF,
102     TOK_RETURN,
103     TOK_SWITCH,
104     TOK_WHILE,
105
106     TOK_ASM,
107     TOK_CASE,
108     TOK_DEFAULT,
109     TOK_BREAK,
110     TOK_CONTINUE,
111     TOK_ELSE,
112     TOK_ELLIPSIS,
113     TOK_SIZEOF,
114
115     TOK_IDENT,
116     TOK_SEMI,
117
118     /* Primary operators */
119     TOK_LBRACK,
120     TOK_LPAREN,
121     TOK_DOT,
122     TOK_PTR_REF,
123
124     TOK_LCURLY,
125     TOK_RBRACK,
126     TOK_COMP,
127     TOK_INC,
128     TOK_PLUS_ASSIGN,
129     TOK_PLUS,
130     TOK_COMMA,
131     TOK_DEC,
132     TOK_MINUS_ASSIGN,
133     TOK_RCURLY,
134     TOK_MINUS,
135     TOK_MUL_ASSIGN,
136     TOK_STAR,
137     TOK_MUL = TOK_STAR,         /* Alias */
138     TOK_DIV_ASSIGN,
139     TOK_DIV,
140     TOK_BOOL_AND,
141     TOK_AND_ASSIGN,
142     TOK_AND,
143     TOK_NE,
144     TOK_BOOL_NOT,
145     TOK_BOOL_OR,
146     TOK_OR_ASSIGN,
147     TOK_OR,
148     TOK_EQ,
149     TOK_ASSIGN,
150
151     /* Inequalities */
152     TOK_LE,
153     TOK_LT,
154     TOK_GE,
155     TOK_GT,
156
157     TOK_SHL_ASSIGN,
158     TOK_SHL,
159     TOK_SHR_ASSIGN,
160     TOK_SHR,
161     TOK_XOR_ASSIGN,
162     TOK_XOR,
163     TOK_MOD_ASSIGN,
164     TOK_MOD,
165     TOK_QUEST,
166     TOK_COLON,
167     TOK_RPAREN,
168     TOK_SCONST,
169     TOK_ICONST,
170     TOK_CCONST,
171     TOK_FCONST,
172     TOK_WCSCONST,
173
174     TOK_ATTRIBUTE,
175     TOK_FAR,
176     TOK_NEAR,
177     TOK_A,
178     TOK_X,
179     TOK_Y,
180     TOK_AX,
181     TOK_EAX,
182
183     TOK_PRAGMA
184 } token_t;
185
186
187
188 /*****************************************************************************/
189 /*                                   data                                    */
190 /*****************************************************************************/
191
192
193
194 /* Token stuff */
195 typedef struct Token Token;
196 struct Token {
197     token_t     Tok;            /* The token itself */
198     long        IVal;           /* The integer attribute */
199     Double      FVal;           /* The float attribute */
200     ident       Ident;          /* Identifier if IDENT */
201     LineInfo*   LI;             /* Source line where the token comes from */
202     Type*       Type;           /* Type if integer or float constant */
203 };
204
205 extern Token CurTok;            /* The current token */
206 extern Token NextTok;           /* The next token */
207
208
209
210 /*****************************************************************************/
211 /*                                   code                                    */
212 /*****************************************************************************/
213
214
215
216 #if defined(HAVE_INLINE)
217 INLINE int TokIsStorageClass (const Token* T)
218 /* Return true if the token is a storage class specifier */
219 {
220     return (T->Tok >= TOK_FIRST_STORAGE_CLASS && T->Tok <= TOK_LAST_STORAGE_CLASS);
221 }
222 #else
223 #  define TokIsStorageClass(T)  \
224         ((T)->Tok >= TOK_FIRST_STORAGE_CLASS && (T)->Tok <= TOK_LAST_STORAGE_CLASS)
225 #endif
226
227 #if defined(HAVE_INLINE)
228 INLINE int TokIsType (const Token* T)
229 /* Return true if the token is a type */
230 {
231     return (T->Tok >= TOK_FIRST_TYPE && T->Tok <= TOK_LAST_TYPE);
232 }
233 #else
234 #  define TokIsType(T)  ((T)->Tok >= TOK_FIRST_TYPE && (T)->Tok <= TOK_LAST_TYPE)
235 #endif
236
237 #if defined(HAVE_INLINE)
238 INLINE int TokIsTypeQual (const Token* T)
239 /* Return true if the token is a type qualifier */
240 {
241     return (T->Tok >= TOK_FIRST_TYPEQUAL && T->Tok <= TOK_LAST_TYPEQUAL);
242 }
243 #else
244 #  define TokIsTypeQual(T)  ((T)->Tok >= TOK_FIRST_TYPEQUAL && (T)->Tok <= TOK_LAST_TYPEQUAL)
245 #endif
246
247 int TokIsFuncSpec (const Token* T);
248 /* Return true if the token is a function specifier */
249
250 void SymName (char* S);
251 /* Read a symbol from the input stream. The first character must have been
252  * checked before calling this function. The buffer is expected to be at
253  * least of size MAX_IDENTLEN+1.
254  */
255
256 int IsSym (char* S);
257 /* If a symbol follows, read it and return 1, otherwise return 0 */
258
259 void NextToken (void);
260 /* Get next token from input stream */
261
262 void SkipTokens (const token_t* TokenList, unsigned TokenCount);
263 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
264  * This routine is used for error recovery.
265  */
266
267 int Consume (token_t Token, const char* ErrorMsg);
268 /* Eat token if it is the next in the input stream, otherwise print an error
269  * message. Returns true if the token was found and false otherwise.
270  */
271
272 int ConsumeColon (void);
273 /* Check for a colon and skip it. */
274
275 int ConsumeSemi (void);
276 /* Check for a semicolon and skip it. */
277
278 int ConsumeComma (void);
279 /* Check for a comma and skip it. */
280
281 int ConsumeLParen (void);
282 /* Check for a left parenthesis and skip it */
283
284 int ConsumeRParen (void);
285 /* Check for a right parenthesis and skip it */
286
287 int ConsumeLBrack (void);
288 /* Check for a left bracket and skip it */
289
290 int ConsumeRBrack (void);
291 /* Check for a right bracket and skip it */
292
293 int ConsumeLCurly (void);
294 /* Check for a left curly brace and skip it */
295
296 int ConsumeRCurly (void);
297 /* Check for a right curly brace and skip it */
298
299
300
301 /* End of scanner.h */
302 #endif
303
304
305
306
307
308