1 | /*****************************************************************************/ |
2 | /* */ |
3 | /* token.h */ |
4 | /* */ |
5 | /* Token list for the ca65 macro assembler */ |
6 | /* */ |
7 | /* */ |
8 | /* */ |
9 | /* (C) 2007-2012, 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 TOKEN_H |
37 | #define TOKEN_H |
38 | |
39 | |
40 | |
41 | /* common */ |
42 | #include "filepos.h" |
43 | #include "inline.h" |
44 | #include "strbuf.h" |
45 | |
46 | |
47 | |
48 | /*****************************************************************************/ |
49 | /* Data */ |
50 | /*****************************************************************************/ |
51 | |
52 | |
53 | |
54 | /* Tokens */ |
55 | typedef enum token_t { |
56 | TOK_NONE, /* Start value, invalid */ |
57 | TOK_EOF, /* End of input file */ |
58 | TOK_SEP, /* Separator (usually newline) */ |
59 | TOK_IDENT, /* An identifier */ |
60 | TOK_LOCAL_IDENT, /* A cheap local identifier */ |
61 | |
62 | TOK_INTCON, /* Integer constant */ |
63 | TOK_CHARCON, /* Character constant */ |
64 | TOK_STRCON, /* String constant */ |
65 | |
66 | TOK_A, /* A)ccumulator */ |
67 | TOK_X, /* X register */ |
68 | TOK_Y, /* Y register */ |
69 | TOK_Z, /* Z register */ |
70 | TOK_S, /* S register */ |
71 | TOK_REG, /* Sweet16 R.. register (in sweet16 mode) */ |
72 | |
73 | TOK_ASSIGN, /* := */ |
74 | TOK_ULABEL, /* :++ or :-- */ |
75 | |
76 | TOK_EQ, /* = */ |
77 | TOK_NE, /* <> */ |
78 | TOK_LT, /* < */ |
79 | TOK_GT, /* > */ |
80 | TOK_LE, /* <= */ |
81 | TOK_GE, /* >= */ |
82 | |
83 | TOK_BOOLAND, /* .and */ |
84 | TOK_BOOLOR, /* .or */ |
85 | TOK_BOOLXOR, /* .xor */ |
86 | TOK_BOOLNOT, /* .not */ |
87 | |
88 | TOK_PLUS, /* + */ |
89 | TOK_MINUS, /* - */ |
90 | TOK_MUL, /* * */ |
91 | TOK_STAR = TOK_MUL, /* Alias */ |
92 | TOK_DIV, /* / */ |
93 | TOK_MOD, /* ! */ |
94 | TOK_OR, /* | */ |
95 | TOK_XOR, /* ^ */ |
96 | TOK_AND, /* & */ |
97 | TOK_SHL, /* << */ |
98 | TOK_SHR, /* >> */ |
99 | TOK_NOT, /* ~ */ |
100 | |
101 | TOK_PC, /* $ if enabled */ |
102 | TOK_NAMESPACE, /* :: */ |
103 | TOK_DOT, /* . */ |
104 | TOK_COMMA, /* , */ |
105 | TOK_HASH, /* # */ |
106 | TOK_COLON, /* : */ |
107 | TOK_LPAREN, /* ( */ |
108 | TOK_RPAREN, /* ) */ |
109 | TOK_LBRACK, /* [ */ |
110 | TOK_RBRACK, /* ] */ |
111 | TOK_LCURLY, /* { */ |
112 | TOK_RCURLY, /* } */ |
113 | TOK_AT, /* @ - in Sweet16 mode */ |
114 | |
115 | TOK_OVERRIDE_ZP, /* z: */ |
116 | TOK_OVERRIDE_ABS, /* a: */ |
117 | TOK_OVERRIDE_FAR, /* f: */ |
118 | |
119 | TOK_MACPARAM, /* Macro parameter, not generated by scanner */ |
120 | TOK_REPCOUNTER, /* Repeat counter, not generated by scanner */ |
121 | |
122 | /* The next ones are tokens for the pseudo instructions. Keep together! */ |
123 | TOK_FIRSTPSEUDO, |
124 | TOK_A16 = TOK_FIRSTPSEUDO, |
125 | TOK_A8, |
126 | TOK_ADDR, |
127 | TOK_ADDRSIZE, |
128 | TOK_ALIGN, |
129 | TOK_ASCIIZ, |
130 | TOK_ASIZE, |
131 | TOK_ASSERT, |
132 | TOK_AUTOIMPORT, |
133 | TOK_BANK, |
134 | TOK_BANKBYTE, |
135 | TOK_BANKBYTES, |
136 | TOK_BLANK, |
137 | TOK_BSS, |
138 | TOK_BYTE, |
139 | TOK_CASE, |
140 | TOK_CHARMAP, |
141 | TOK_CODE, |
142 | TOK_CONCAT, |
143 | TOK_CONDES, |
144 | TOK_CONST, |
145 | TOK_CONSTRUCTOR, |
146 | TOK_CPU, |
147 | TOK_DATA, |
148 | TOK_DBG, |
149 | TOK_DBYT, |
150 | TOK_DEBUGINFO, |
151 | TOK_DEFINE, |
152 | TOK_DEFINED, |
153 | TOK_DEFINEDMACRO, |
154 | TOK_DELMAC, |
155 | TOK_DESTRUCTOR, |
156 | TOK_DWORD, |
157 | TOK_ELSE, |
158 | TOK_ELSEIF, |
159 | TOK_END, |
160 | TOK_ENDENUM, |
161 | TOK_ENDIF, |
162 | TOK_ENDMACRO, |
163 | TOK_ENDPROC, |
164 | TOK_ENDREP, |
165 | TOK_ENDSCOPE, |
166 | TOK_ENDSTRUCT, |
167 | TOK_ENDUNION, |
168 | TOK_ENUM, |
169 | TOK_ERROR, |
170 | TOK_EXITMACRO, |
171 | TOK_EXPORT, |
172 | TOK_EXPORTZP, |
173 | TOK_FARADDR, |
174 | TOK_FATAL, |
175 | TOK_FEATURE, |
176 | TOK_FILEOPT, |
177 | TOK_FORCEIMPORT, |
178 | TOK_FORCEWORD, |
179 | TOK_GLOBAL, |
180 | TOK_GLOBALZP, |
181 | TOK_HIBYTE, |
182 | TOK_HIBYTES, |
183 | TOK_HIWORD, |
184 | TOK_I16, |
185 | TOK_I8, |
186 | TOK_MAKEIDENT, |
187 | TOK_IF, |
188 | TOK_IFBLANK, |
189 | TOK_IFCONST, |
190 | TOK_IFDEF, |
191 | TOK_IFNBLANK, |
192 | TOK_IFNCONST, |
193 | TOK_IFNDEF, |
194 | TOK_IFNREF, |
195 | TOK_IFP02, |
196 | TOK_IFP4510, |
197 | TOK_IFP816, |
198 | TOK_IFPC02, |
199 | TOK_IFPSC02, |
200 | TOK_IFREF, |
201 | TOK_IMPORT, |
202 | TOK_IMPORTZP, |
203 | TOK_INCBIN, |
204 | TOK_INCLUDE, |
205 | TOK_INTERRUPTOR, |
206 | TOK_ISIZE, |
207 | TOK_ISMNEMONIC, |
208 | TOK_LEFT, |
209 | TOK_LINECONT, |
210 | TOK_LIST, |
211 | TOK_LISTBYTES, |
212 | TOK_LOBYTE, |
213 | TOK_LOBYTES, |
214 | TOK_LOCAL, |
215 | TOK_LOCALCHAR, |
216 | TOK_LOWORD, |
217 | TOK_MACPACK, |
218 | TOK_MACRO, |
219 | TOK_MATCH, |
220 | TOK_MAX, |
221 | TOK_MID, |
222 | TOK_MIN, |
223 | TOK_NULL, |
224 | TOK_ORG, |
225 | TOK_OUT, |
226 | TOK_P02, |
227 | TOK_P4510, |
228 | TOK_P816, |
229 | TOK_PAGELENGTH, |
230 | TOK_PARAMCOUNT, |
231 | TOK_PC02, |
232 | TOK_POPCPU, |
233 | TOK_POPSEG, |
234 | TOK_PROC, |
235 | TOK_PSC02, |
236 | TOK_PUSHCPU, |
237 | TOK_PUSHSEG, |
238 | TOK_REFERENCED, |
239 | TOK_RELOC, |
240 | TOK_REPEAT, |
241 | TOK_RES, |
242 | TOK_RIGHT, |
243 | TOK_RODATA, |
244 | TOK_SCOPE, |
245 | TOK_SEGMENT, |
246 | TOK_SET, |
247 | TOK_SETCPU, |
248 | TOK_SIZEOF, |
249 | TOK_SMART, |
250 | TOK_SPRINTF, |
251 | TOK_STRAT, |
252 | TOK_STRING, |
253 | TOK_STRLEN, |
254 | TOK_STRUCT, |
255 | TOK_TAG, |
256 | TOK_TCOUNT, |
257 | TOK_TIME, |
258 | TOK_UNDEF, |
259 | TOK_UNION, |
260 | TOK_VERSION, |
261 | TOK_WARNING, |
262 | TOK_WORD, |
263 | TOK_XMATCH, |
264 | TOK_ZEROPAGE, |
265 | TOK_LASTPSEUDO = TOK_ZEROPAGE, |
266 | |
267 | TOK_COUNT /* Count of tokens */ |
268 | } token_t; |
269 | |
270 | |
271 | |
272 | /* Complete token including attributes and flags */ |
273 | typedef struct Token Token; |
274 | struct Token { |
275 | token_t Tok; /* The actual token value */ |
276 | int WS; /* Flag for "whitespace before token" */ |
277 | long IVal; /* Integer attribute value */ |
278 | StrBuf SVal; /* String attribute value */ |
279 | FilePos Pos; /* Position from which token was read */ |
280 | }; |
281 | |
282 | /* Initializer value for a token */ |
283 | #define STATIC_TOKEN_INITIALIZER { \ |
284 | TOK_NONE, \ |
285 | 0, \ |
286 | 0, \ |
287 | STATIC_STRBUF_INITIALIZER, \ |
288 | STATIC_FILEPOS_INITIALIZER \ |
289 | } |
290 | |
291 | |
292 | |
293 | /*****************************************************************************/ |
294 | /* Code */ |
295 | /*****************************************************************************/ |
296 | |
297 | |
298 | |
299 | int TokHasSVal (token_t Tok); |
300 | /* Return true if the given token has an attached SVal */ |
301 | |
302 | int TokHasIVal (token_t Tok); |
303 | /* Return true if the given token has an attached IVal */ |
304 | |
305 | #if defined(HAVE_INLINE) |
306 | INLINE int TokIsSep (enum token_t T) |
307 | /* Return true if this is a separator token */ |
308 | { |
309 | return (T == TOK_SEP || T == TOK_EOF); |
310 | } |
311 | #else |
312 | # define TokIsSep(T) ((T) == TOK_SEP || (T) == TOK_EOF) |
313 | #endif |
314 | |
315 | void CopyToken (Token* Dst, const Token* Src); |
316 | /* Copy a token. The current value of Dst.SVal is free'd, so Dst must be |
317 | ** initialized. |
318 | */ |
319 | |
320 | |
321 | |
322 | /* End of token.h */ |
323 | |
324 | #endif |
325 | |