sanity checking of ram allocation (if the algorithm is correct it's not necessary...
[xorcyst.git] / RCS / scanner.l,v
blob165e8be24eb3c6556b678c432c81c6970b20ef8e
1 head    1.15;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
8 1.15
9 date    2007.11.11.22.35.46;    author khansen; state Exp;
10 branches;
11 next    1.14;
13 1.14
14 date    2007.08.19.11.19.47;    author khansen; state Exp;
15 branches;
16 next    1.13;
18 1.13
19 date    2007.08.12.18.58.49;    author khansen; state Exp;
20 branches;
21 next    1.12;
23 1.12
24 date    2007.08.11.01.25.39;    author khansen; state Exp;
25 branches;
26 next    1.11;
28 1.11
29 date    2007.08.10.20.21.36;    author khansen; state Exp;
30 branches;
31 next    1.10;
33 1.10
34 date    2007.07.22.13.34.16;    author khansen; state Exp;
35 branches;
36 next    1.9;
38 1.9
39 date    2005.01.09.11.20.31;    author kenth;   state Exp;
40 branches;
41 next    1.8;
43 1.8
44 date    2004.12.19.19.59.08;    author kenth;   state Exp;
45 branches;
46 next    1.7;
48 1.7
49 date    2004.12.16.13.22.08;    author kenth;   state Exp;
50 branches;
51 next    1.6;
53 1.6
54 date    2004.12.14.01.50.33;    author kenth;   state Exp;
55 branches;
56 next    1.5;
58 1.5
59 date    2004.12.11.02.12.32;    author kenth;   state Exp;
60 branches;
61 next    1.4;
63 1.4
64 date    2004.12.10.22.43.53;    author kenth;   state Exp;
65 branches;
66 next    1.3;
68 1.3
69 date    2004.12.09.11.15.28;    author kenth;   state Exp;
70 branches;
71 next    1.2;
73 1.2
74 date    2004.12.06.05.06.29;    author kenth;   state Exp;
75 branches;
76 next    1.1;
78 1.1
79 date    2004.06.30.07.56.59;    author kenth;   state Exp;
80 branches;
81 next    ;
84 desc
88 1.15
89 log
90 @compile on mac
92 text
93 @%{
95  * $Id: scanner.l,v 1.14 2007/08/19 11:19:47 khansen Exp khansen $
96  * $Log: scanner.l,v $
97  * Revision 1.14  2007/08/19 11:19:47  khansen
98  * --case-insensitive option
99  *
100  * Revision 1.13  2007/08/12 18:58:49  khansen
101  * ability to generate pure 6502 binary
103  * Revision 1.12  2007/08/11 01:25:39  khansen
104  * includepaths support (-I option)
106  * Revision 1.11  2007/08/10 20:21:36  khansen
107  * *** empty log message ***
109  * Revision 1.10  2007/07/22 13:34:16  khansen
110  * convert tabs to whitespaces
112  * Revision 1.9  2005/01/09 11:20:31  kenth
113  * xorcyst 1.4.5
114  * BLT, BGE
116  * Revision 1.8  2004/12/19 19:59:08  kenth
117  * xorcyst 1.4.0
119  * Revision 1.7  2004/12/16 13:22:08  kenth
120  * added DEFINE parsing
122  * Revision 1.6  2004/12/14 01:50:33  kenth
123  * xorcyst 1.3.0
125  * Revision 1.5  2004/12/11 02:12:32  kenth
126  * xorcyst 1.2.0
128  * Revision 1.4  2004/12/10 22:43:53  kenth
129  * removed FILE_PATH recognition: ".db <a, >b" is erronously parsed otherwise
131  * Revision 1.3  2004/12/09 11:15:28  kenth
132  * bugfix: close file handle
133  * added: "WARNING", "ERROR" recognition
135  * Revision 1.2  2004/12/06 05:06:29  kenth
136  * xorcyst 1.1.0
138  * Revision 1.1  2004/06/30 07:56:59  kenth
139  * Initial revision
141  */
142 int yyparswap;
143 int yyignorecase;
144 void yyerror(const char *);   /* See below */
145 char *strip_label();    /* See below */
146 const char *maybe_ignorecase(char *);
147 #include "loc.h"
148 #include "astnode.h"
149 #include "parser.h"
150 #include "xasm.h"
151 #include <string.h>
152 #include <ctype.h>
153 /* Each time we match a string, move the end cursor to its end. */
154 #define YY_USER_ACTION  yylloc.last_column += yyleng;
156 whitespace      [ \t]+
157 comment         ";"[^\n]*
158 string_literal      \"[^\n"]*\"
159 binary_literal      [0-1]+
160 octal_literal       0[0-7]*
161 decimal_literal     [1-9][0-9]*
162 hex_literal     [0-9A-F]+
163 hex_literal2        [0-9][0-9A-F]*
164 hex_literal3        0[A-F][0-9A-F]*
165 identifier      [A-Z_][A-Z0-9_]{0,254}
166 local_id        [0-9A-Z_]{1,255}
167 pragma_prefix       "."?
168 label_prefix        ^[ \t]*
171 /* At each yylex invocation, mark the current position as the
172     start of the next token.  */
173 LOCATION_STEP (yylloc);
175 {whitespace}        { LOCATION_STEP (yylloc); }
176 {comment}       { LOCATION_STEP (yylloc); }
177 "%"{binary_literal} { yylval.integer = strtol(&yytext[1], NULL, 2); return(INTEGER_LITERAL); }
178 {binary_literal}"B" { yylval.integer = strtol(yytext, NULL, 2); return(INTEGER_LITERAL); }
179 {octal_literal}     { yylval.integer = strtol(yytext, NULL, 8); return(INTEGER_LITERAL); }
180 {decimal_literal}   { yylval.integer = strtol(yytext, NULL, 10); return(INTEGER_LITERAL); }
181 "0X"{hex_literal}   { yylval.integer = strtol(&yytext[2], NULL, 16); return(INTEGER_LITERAL); }
182 "$"{hex_literal}    { yylval.integer = strtol(&yytext[1], NULL, 16); return(INTEGER_LITERAL); }
183 {hex_literal3}"H"   { yylval.integer = strtol(yytext, NULL, 16); return(INTEGER_LITERAL); }
184 {hex_literal2}"H"   { yylval.integer = strtol(yytext, NULL, 16); return(INTEGER_LITERAL); }
185 "'"."'"         { yylval.integer = yytext[1]; return(INTEGER_LITERAL); }
186 "ADC"           { yylval.mnemonic = ADC_MNEMONIC; return(MNEMONIC); }
187 "AND"           { yylval.mnemonic = AND_MNEMONIC; return(MNEMONIC); }
188 "ASL"           { yylval.mnemonic = ASL_MNEMONIC; return(MNEMONIC); }
189 "BCC"           { yylval.mnemonic = BCC_MNEMONIC; return(MNEMONIC); }
190 "BCS"           { yylval.mnemonic = BCS_MNEMONIC; return(MNEMONIC); }
191 "BLT"           { yylval.mnemonic = BCC_MNEMONIC; return(MNEMONIC); }
192 "BGE"           { yylval.mnemonic = BCS_MNEMONIC; return(MNEMONIC); }
193 "BEQ"           { yylval.mnemonic = BEQ_MNEMONIC; return(MNEMONIC); }
194 "BIT"           { yylval.mnemonic = BIT_MNEMONIC; return(MNEMONIC); }
195 "BMI"           { yylval.mnemonic = BMI_MNEMONIC; return(MNEMONIC); }
196 "BNE"           { yylval.mnemonic = BNE_MNEMONIC; return(MNEMONIC); }
197 "BPL"           { yylval.mnemonic = BPL_MNEMONIC; return(MNEMONIC); }
198 "BRK"           { yylval.mnemonic = BRK_MNEMONIC; return(MNEMONIC); }
199 "BVC"           { yylval.mnemonic = BVC_MNEMONIC; return(MNEMONIC); }
200 "BVS"           { yylval.mnemonic = BVS_MNEMONIC; return(MNEMONIC); }
201 "CLC"           { yylval.mnemonic = CLC_MNEMONIC; return(MNEMONIC); }
202 "CLD"           { yylval.mnemonic = CLD_MNEMONIC; return(MNEMONIC); }
203 "CLI"           { yylval.mnemonic = CLI_MNEMONIC; return(MNEMONIC); }
204 "CLV"           { yylval.mnemonic = CLV_MNEMONIC; return(MNEMONIC); }
205 "CMP"           { yylval.mnemonic = CMP_MNEMONIC; return(MNEMONIC); }
206 "CPX"           { yylval.mnemonic = CPX_MNEMONIC; return(MNEMONIC); }
207 "CPY"           { yylval.mnemonic = CPY_MNEMONIC; return(MNEMONIC); }
208 "DEC"           { yylval.mnemonic = DEC_MNEMONIC; return(MNEMONIC); }
209 "DEX"           { yylval.mnemonic = DEX_MNEMONIC; return(MNEMONIC); }
210 "DEY"           { yylval.mnemonic = DEY_MNEMONIC; return(MNEMONIC); }
211 "EOR"           { yylval.mnemonic = EOR_MNEMONIC; return(MNEMONIC); }
212 "INC"           { yylval.mnemonic = INC_MNEMONIC; return(MNEMONIC); }
213 "INX"           { yylval.mnemonic = INX_MNEMONIC; return(MNEMONIC); }
214 "INY"           { yylval.mnemonic = INY_MNEMONIC; return(MNEMONIC); }
215 "JMP"           { yylval.mnemonic = JMP_MNEMONIC; return(MNEMONIC); }
216 "JSR"           { yylval.mnemonic = JSR_MNEMONIC; return(MNEMONIC); }
217 "LDA"           { yylval.mnemonic = LDA_MNEMONIC; return(MNEMONIC); }
218 "LDX"           { yylval.mnemonic = LDX_MNEMONIC; return(MNEMONIC); }
219 "LDY"           { yylval.mnemonic = LDY_MNEMONIC; return(MNEMONIC); }
220 "LSR"           { yylval.mnemonic = LSR_MNEMONIC; return(MNEMONIC); }
221 "NOP"           { yylval.mnemonic = NOP_MNEMONIC; return(MNEMONIC); }
222 "ORA"           { yylval.mnemonic = ORA_MNEMONIC; return(MNEMONIC); }
223 "PHA"           { yylval.mnemonic = PHA_MNEMONIC; return(MNEMONIC); }
224 "PHP"           { yylval.mnemonic = PHP_MNEMONIC; return(MNEMONIC); }
225 "PLA"           { yylval.mnemonic = PLA_MNEMONIC; return(MNEMONIC); }
226 "PLP"           { yylval.mnemonic = PLP_MNEMONIC; return(MNEMONIC); }
227 "ROL"           { yylval.mnemonic = ROL_MNEMONIC; return(MNEMONIC); }
228 "ROR"           { yylval.mnemonic = ROR_MNEMONIC; return(MNEMONIC); }
229 "RTI"           { yylval.mnemonic = RTI_MNEMONIC; return(MNEMONIC); }
230 "RTS"           { yylval.mnemonic = RTS_MNEMONIC; return(MNEMONIC); }
231 "SBC"           { yylval.mnemonic = SBC_MNEMONIC; return(MNEMONIC); }
232 "SEC"           { yylval.mnemonic = SEC_MNEMONIC; return(MNEMONIC); }
233 "SED"           { yylval.mnemonic = SED_MNEMONIC; return(MNEMONIC); }
234 "SEI"           { yylval.mnemonic = SEI_MNEMONIC; return(MNEMONIC); }
235 "STA"           { yylval.mnemonic = STA_MNEMONIC; return(MNEMONIC); }
236 "STX"           { yylval.mnemonic = STX_MNEMONIC; return(MNEMONIC); }
237 "STY"           { yylval.mnemonic = STY_MNEMONIC; return(MNEMONIC); }
238 "TAX"           { yylval.mnemonic = TAX_MNEMONIC; return(MNEMONIC); }
239 "TAY"           { yylval.mnemonic = TAY_MNEMONIC; return(MNEMONIC); }
240 "TSX"           { yylval.mnemonic = TSX_MNEMONIC; return(MNEMONIC); }
241 "TXA"           { yylval.mnemonic = TXA_MNEMONIC; return(MNEMONIC); }
242 "TXS"           { yylval.mnemonic = TXS_MNEMONIC; return(MNEMONIC); }
243 "TYA"           { yylval.mnemonic = TYA_MNEMONIC; return(MNEMONIC); }
244 "X"         { return('X'); }
245 "Y"         { return('Y'); }
246 "A"         { return('A'); }
247 "MASK"          { return(MASK); }
248 "SIZEOF"        { return(SIZEOF); }
249 {pragma_prefix}"DATA"   { return(DATASEG); }
250 {pragma_prefix}"CODE"   { return(CODESEG); }
251 {pragma_prefix}"DATASEG" { return(DATASEG); }
252 {pragma_prefix}"CODESEG" { return(CODESEG); }
253 {pragma_prefix}"IF" { return(IF); }
254 {pragma_prefix}"IFDEF"  { return(IFDEF); }
255 {pragma_prefix}"IFNDEF" { return(IFNDEF); }
256 {pragma_prefix}"ELSE"   { return(ELSE); }
257 {pragma_prefix}"ELIF"   { return(ELIF); }
258 {pragma_prefix}"ENDIF"  { return(ENDIF); }
259 {pragma_prefix}"MACRO"  { return(MACRO); }
260 {pragma_prefix}"END"    { return(END); }
261 {pragma_prefix}"ENDE"   { return(ENDE); }
262 {pragma_prefix}"ENDM"   { return(ENDM); }
263 {pragma_prefix}"ENDP"   { return(ENDP); }
264 {pragma_prefix}"ENDS"   { return(ENDS); }
265 {pragma_prefix}"EQU"    { return(EQU); }
266 {pragma_prefix}"DEFINE" { return(DEFINE); }
267 {pragma_prefix}"INCLUDE" { return(INCSRC); }
268 {pragma_prefix}"INCSRC" { return(INCSRC); }
269 {pragma_prefix}"INCBIN" { return(INCBIN); }
270 {pragma_prefix}"ALIGN"  { return(ALIGN); }
271 {pragma_prefix}"PUBLIC" { return(PUBLIC); }
272 {pragma_prefix}"EXTRN"  { return(EXTRN); }
273 {pragma_prefix}"ZEROPAGE" { return(ZEROPAGE); }
274 {pragma_prefix}"CHARMAP" { return(CHARMAP); }
275 {pragma_prefix}"STRUC"  { return(STRUC); }
276 {pragma_prefix}"UNION"  { return(UNION); }
277 {pragma_prefix}"RECORD" { return(RECORD); }
278 {pragma_prefix}"ENUM"   { return(ENUM); }
279 {pragma_prefix}"PROC"   { return(PROC); }
280 {pragma_prefix}"REPT"   { return(REPT); }
281 {pragma_prefix}"TAG"    { return(TAG); }
282 {pragma_prefix}"TYPE"   { return(TAG); }
283 {pragma_prefix}"LABEL"  { return(_LABEL_); }
284 {pragma_prefix}"MESSAGE" { return(MESSAGE); }
285 {pragma_prefix}"WARNING" { return(WARNING); }
286 {pragma_prefix}"ERROR"  { return(ERROR); }
287 {pragma_prefix}"WHILE"  { return(WHILE); }
288 {pragma_prefix}"PAD"    { return(DSB); }
289 {pragma_prefix}"DB" { return(BYTE); }
290 {pragma_prefix}"BYTE"   { return(BYTE); }
291 {pragma_prefix}"CHAR"   { return(CHAR); }
292 {pragma_prefix}"ASC"    { return(CHAR); }
293 {pragma_prefix}"DW" { return(WORD); }
294 {pragma_prefix}"WORD"   { return(WORD); }
295 {pragma_prefix}"DD" { return(DWORD); }
296 {pragma_prefix}"DWORD"  { return(DWORD); }
297 {pragma_prefix}"DSB"    { return(DSB); }
298 {pragma_prefix}"DSW"    { return(DSW); }
299 {pragma_prefix}"DSD"    { return(DSD); }
300 {pragma_prefix}"ORG"    { return(ORG); }
301 {label_prefix}{identifier}":" { yylval.label = maybe_ignorecase(strip_label()); return(LABEL); }
302 {label_prefix}"@@@@"{local_id}":" { yylval.label = maybe_ignorecase(strip_label()); return(LOCAL_LABEL); }
303 "@@@@"{local_id}      { yylval.ident = maybe_ignorecase(yytext); return(LOCAL_ID); }
304 {identifier}        { yylval.ident = maybe_ignorecase(yytext); return(IDENTIFIER); }
305 {string_literal}    { yytext[yyleng-1] = '\0'; yylval.string = &yytext[1]; return(STRING_LITERAL); }
306 "+"{2,8}        { yylval.ident = yytext; return(FORWARD_BRANCH); }
307 "-"{2,8}        { yylval.ident = yytext; return(BACKWARD_BRANCH); }
308 "::"            { return(SCOPE_OP); }
309 "."         { return('.'); }
310 \n          { LOCATION_LINES (yylloc, yyleng); LOCATION_STEP (yylloc); return('\n'); }
311 ","         { return(','); }
312 ":"         { return(':'); }
313 "#"         { return('#'); }
314 "="         { return('='); }
315 "("         { return( yyparswap ? '[' : '(' ); }
316 ")"         { return( yyparswap ? ']' : ')' ); }
317 "["         { return( yyparswap ? '(' : '[' ); }
318 "]"         { return( yyparswap ? ')' : ']'); }
319 "{"         { return('{'); }
320 "}"         { return('}'); }
321 "&"         { return('&'); }
322 "!"         { return('!'); }
323 "~"         { return('~'); }
324 "-"         { return('-'); }
325 "+"         { return('+'); }
326 "*"         { return('*'); }
327 "/"         { return('/'); }
328 "%"         { return('%'); }
329 "<"         { return('<'); }
330 ">"         { return('>'); }
331 "^"         { return('^'); }
332 "|"         { return('|'); }
333 ">>"                    { return(SHR_OP); }
334 "<<"                    { return(SHL_OP); }
335 "<="                    { return(LE_OP); }
336 ">="                    { return(GE_OP); }
337 "=="                    { return(EQ_OP); }
338 "!="                    { return(NE_OP); }
339 "$"         { return('$'); }
340 "@@"         { return('@@'); }
341 .           { yyerror("Skipping invalid character(s)"); LOCATION_STEP (yylloc); }
344  * Describes a file stack record.
345  * It holds name, location and buffer state for it.
346  */
347 typedef struct tag_file_stack_rec {
348     const char *name; /* Name of the file */
349     YY_BUFFER_STATE buf;    /* flex input buffer */
350     YYLTYPE loc;    /* Current position in file */
351     FILE *fp;   /* File handle */
352 } file_stack_rec;
354 /* Max. nesting depth */
355 #define FILE_STACK_SIZE 32
357 /* Stack of files */
358 static file_stack_rec file_stack[FILE_STACK_SIZE];
360 /* File stack pointer */
361 static int file_stack_ptr;
363 /* Macro to access top of file stack */
364 #define FTOS() file_stack[file_stack_ptr]
367  * Resets the location described by yylloc.
368  */
369 void yyresetloc(void)
371     LOCATION_RESET(yylloc);
375  * Function called by lexer upon EOF(yyin).
376  */
377 int yywrap()
379     /* Discard of buffer that has been scanned */
380     yy_delete_buffer(YY_CURRENT_BUFFER);
381     /* Close current file */
382     fclose(FTOS().fp);
383     /* Resume scanning of previous buffer, if any */
384     if (file_stack_ptr > 0) {
385         /* Pop stack */
386         file_stack_ptr--;
387         /* Restore location */
388         yylloc = FTOS().loc;
389         /* Resume scanning from this buffer */
390         yy_switch_to_buffer(FTOS().buf);
391         /* Not end of all input. */
392         return(0);
393     }
394     else {
395         /* We have reached end of all input. */
396         return(1);
397     }
401  * Opens a file and resumes scanning from there.
402  * The current file's status is pushed on stack.
403  * @@param filename Name of new file to start scanning
404  * @@return error code (0, 1 or 2)
405  */
406 int yypushandrestart(const char *filename)
408     FILE *fp;
409     /* Check for stack overflow */
410     if (file_stack_ptr == FILE_STACK_SIZE-1) {
411         /* Error, stack overflow */
412         return 2;
413     }
414     /* Attempt to open file */
415     fp = fopen(filename, "rt");
416     if (!fp && (filename[0] != '/')) {
417         /* Try search paths */
418         int i;
419         for (i = 0; i < xasm_args.include_path_count; ++i) {
420             const char *include_path = xasm_args.include_paths[i];
421             char *path = (char *)malloc(
422                 strlen(include_path) + strlen("/") + strlen(filename) + 1);
423             strcpy(path, include_path);
424             strcat(path, "/");
425             strcat(path, filename);
426             fp = fopen(path, "rt");
427             free(path);
428             if (fp)
429                 break;
430         }
431     }
432     if (fp) {
433         /* Save position in current file */
434         FTOS().loc = yylloc;
435         /* Push the new file */
436         file_stack_ptr++;
437         FTOS().name = filename;
438         FTOS().buf = yy_create_buffer(fp, YY_BUF_SIZE);
439         FTOS().fp = fp;
440         /* Reset file location */
441         yyresetloc();
442         /* Resume scanning from the new buffer */
443         yy_switch_to_buffer(FTOS().buf);
444         /* Success */
445         return 0;
446     }
447     else {
448         /* Error, couldn't open file */
449         return 1;
450     }
454  * Initializes the lexer, tells it to start tokenizing from the given file.
455  * @@param filename Name of file where scanning is initiated
456  * @@param swap_parens Use ( ) for indirection if 1, [ ] otherwise
457  * @@return 1 if success, 0 otherwise
458  */
459 int yybegin(const char *filename, int swap_parens, int ignore_case)
461     FILE *fp;
462     yyparswap = swap_parens;
463     yyignorecase = ignore_case;
464     /* Attempt to open the given file */
465     fp = fopen(filename, "rt");
466     if (fp) {
467         /* Init stack */
468         file_stack_ptr = 0;
469         FTOS().name = filename;
470         FTOS().buf = yy_create_buffer(fp, YY_BUF_SIZE);
471         FTOS().fp = fp;
472         /* Reset file location */
473         yyresetloc();
474         /* Use the new buffer */
475         yy_switch_to_buffer(FTOS().buf);
476         /* Success */
477         return 1;
478     }
479     else {
480         /* Couldn't open file */
481         return 0;
482     }
486  * Gets the name of the file that is currently being tokenized.
487  */
488 const char *yy_current_filename()
490     return FTOS().name;
494  * Called by the parser to report an error during parsing.
495  * @@param s Error message
496  */
497 void yyerror(const char *s) {
498     int i;
499     /* Print line of form: file:pos:message */
500     fprintf(stderr, "%s:", yy_current_filename());
501     LOCATION_PRINT(stderr, yylloc);
502     fprintf(stderr, ": %s\n", s);
503     /* Print include-trace */
504     for (i=file_stack_ptr-1; i>=0; i--) {
505         fprintf(stderr, "(%s:", file_stack[i].name);
506         LOCATION_PRINT(stderr, file_stack[i].loc);
507         fprintf(stderr, ")\n");
508     }
512  * Call when yytext is a label to remove whitespace and punctuation.
513  */
514 char *strip_label()
516     int start;
517     /* Kill the ':' at the end */
518     yytext[strlen(yytext)-1] = 0;
519     /* Skip the whitespace, if any */
520     start = 0;
521     while ((yytext[start] == ' ') || (yytext[start] == '\t')) start++;
522     return &yytext[start];
525 static char *__strupr(char *str)
527     char *p = str;
528     while (*p) {
529         *p = toupper(*p);
530         ++p;
531     }
532     return str;
535 const char *maybe_ignorecase(char *str)
537     if (yyignorecase)
538         __strupr(str);
539     return str;
544 1.14
546 @--case-insensitive option
548 text
549 @d3 1
550 a3 1
551  * $Id: scanner.l,v 1.13 2007/08/12 18:58:49 khansen Exp khansen $
552 d5 3
553 d60 1
554 d433 10
555 d445 3
556 a447 1
557     return yyignorecase ? strupr(str) : str;
561 1.13
563 @ability to generate pure 6502 binary
565 text
566 @d3 1
567 a3 1
568  * $Id: scanner.l,v 1.12 2007/08/11 01:25:39 khansen Exp khansen $
569 d5 3
570 d48 2
571 a49 1
572 void yyerror(char *);   /* See below */
573 d51 1
574 d205 4
575 a208 4
576 {label_prefix}{identifier}":" { yylval.label = strip_label(); return(LABEL); }
577 {label_prefix}"@@@@"{local_id}":" { yylval.label = strip_label(); return(LOCAL_LABEL); }
578 "@@@@"{local_id}      { yylval.ident = yytext; return(LOCAL_ID); }
579 {identifier}        { yylval.ident = yytext; return(IDENTIFIER); }
580 d310 1
581 a310 1
582 int yypushandrestart(char *filename)
583 d363 1
584 a363 1
585 int yybegin(const char *filename, int swap_parens)
586 d367 1
587 d401 1
588 a401 1
589 void yyerror(char *s) {
590 d428 5
594 1.12
596 @includepaths support (-I option)
598 text
599 @d3 1
600 a3 1
601  * $Id: scanner.l,v 1.11 2007/08/10 20:21:36 khansen Exp khansen $
602 d5 3
603 d199 1
607 1.11
609 @*** empty log message ***
611 text
612 @d3 1
613 a3 1
614  * $Id: scanner.l,v 1.10 2007/07/22 13:34:16 khansen Exp khansen $
615 d5 3
616 d47 1
617 d311 16
621 1.10
623 @convert tabs to whitespaces
625 text
626 @d3 1
627 a3 1
628  * $Id: scanner.l,v 1.9 2005/01/09 11:20:31 kenth Exp khansen $
629 d5 3
630 d239 1
631 a239 1
632     char *name; /* Name of the file */
633 d334 1
634 a334 1
635 int yybegin(char *filename, int swap_parens)
636 d362 1
637 a362 1
638 char *yy_current_filename()
644 @xorcyst 1.4.5
645 BLT, BGE
647 text
648 @d3 1
649 a3 1
650  * $Id: scanner.l,v 1.8 2004/12/19 19:59:08 kenth Exp kenth $
651 d5 4
652 d36 2
653 a37 2
654 void yyerror(char *);   /* See below */
655 char *strip_label();    /* See below */
656 d45 13
657 a57 13
658 whitespace              [ \t]+
659 comment                 ";"[^\n]*
660 string_literal          \"[^\n"]*\"
661 binary_literal          [0-1]+
662 octal_literal           0[0-7]*
663 decimal_literal         [1-9][0-9]*
664 hex_literal             [0-9A-F]+
665 hex_literal2            [0-9][0-9A-F]*
666 hex_literal3            0[A-F][0-9A-F]*
667 identifier              [A-Z_][A-Z0-9_]{0,254}
668 local_id                [0-9A-Z_]{1,255}
669 pragma_prefix           "."?
670 label_prefix            ^[ \t]*
671 d61 1
672 a61 1
673         start of the next token.  */
674 d64 74
675 a137 74
676 {whitespace}            { LOCATION_STEP (yylloc); }
677 {comment}               { LOCATION_STEP (yylloc); }
678 "%"{binary_literal}     { yylval.integer = strtol(&yytext[1], NULL, 2); return(INTEGER_LITERAL); }
679 {binary_literal}"B"     { yylval.integer = strtol(yytext, NULL, 2); return(INTEGER_LITERAL); }
680 {octal_literal}         { yylval.integer = strtol(yytext, NULL, 8); return(INTEGER_LITERAL); }
681 {decimal_literal}       { yylval.integer = strtol(yytext, NULL, 10); return(INTEGER_LITERAL); }
682 "0X"{hex_literal}       { yylval.integer = strtol(&yytext[2], NULL, 16); return(INTEGER_LITERAL); }
683 "$"{hex_literal}        { yylval.integer = strtol(&yytext[1], NULL, 16); return(INTEGER_LITERAL); }
684 {hex_literal3}"H"       { yylval.integer = strtol(yytext, NULL, 16); return(INTEGER_LITERAL); }
685 {hex_literal2}"H"       { yylval.integer = strtol(yytext, NULL, 16); return(INTEGER_LITERAL); }
686 "'"."'"                 { yylval.integer = yytext[1]; return(INTEGER_LITERAL); }
687 "ADC"                   { yylval.mnemonic = ADC_MNEMONIC; return(MNEMONIC); }
688 "AND"                   { yylval.mnemonic = AND_MNEMONIC; return(MNEMONIC); }
689 "ASL"                   { yylval.mnemonic = ASL_MNEMONIC; return(MNEMONIC); }
690 "BCC"                   { yylval.mnemonic = BCC_MNEMONIC; return(MNEMONIC); }
691 "BCS"                   { yylval.mnemonic = BCS_MNEMONIC; return(MNEMONIC); }
692 "BLT"                   { yylval.mnemonic = BCC_MNEMONIC; return(MNEMONIC); }
693 "BGE"                   { yylval.mnemonic = BCS_MNEMONIC; return(MNEMONIC); }
694 "BEQ"                   { yylval.mnemonic = BEQ_MNEMONIC; return(MNEMONIC); }
695 "BIT"                   { yylval.mnemonic = BIT_MNEMONIC; return(MNEMONIC); }
696 "BMI"                   { yylval.mnemonic = BMI_MNEMONIC; return(MNEMONIC); }
697 "BNE"                   { yylval.mnemonic = BNE_MNEMONIC; return(MNEMONIC); }
698 "BPL"                   { yylval.mnemonic = BPL_MNEMONIC; return(MNEMONIC); }
699 "BRK"                   { yylval.mnemonic = BRK_MNEMONIC; return(MNEMONIC); }
700 "BVC"                   { yylval.mnemonic = BVC_MNEMONIC; return(MNEMONIC); }
701 "BVS"                   { yylval.mnemonic = BVS_MNEMONIC; return(MNEMONIC); }
702 "CLC"                   { yylval.mnemonic = CLC_MNEMONIC; return(MNEMONIC); }
703 "CLD"                   { yylval.mnemonic = CLD_MNEMONIC; return(MNEMONIC); }
704 "CLI"                   { yylval.mnemonic = CLI_MNEMONIC; return(MNEMONIC); }
705 "CLV"                   { yylval.mnemonic = CLV_MNEMONIC; return(MNEMONIC); }
706 "CMP"                   { yylval.mnemonic = CMP_MNEMONIC; return(MNEMONIC); }
707 "CPX"                   { yylval.mnemonic = CPX_MNEMONIC; return(MNEMONIC); }
708 "CPY"                   { yylval.mnemonic = CPY_MNEMONIC; return(MNEMONIC); }
709 "DEC"                   { yylval.mnemonic = DEC_MNEMONIC; return(MNEMONIC); }
710 "DEX"                   { yylval.mnemonic = DEX_MNEMONIC; return(MNEMONIC); }
711 "DEY"                   { yylval.mnemonic = DEY_MNEMONIC; return(MNEMONIC); }
712 "EOR"                   { yylval.mnemonic = EOR_MNEMONIC; return(MNEMONIC); }
713 "INC"                   { yylval.mnemonic = INC_MNEMONIC; return(MNEMONIC); }
714 "INX"                   { yylval.mnemonic = INX_MNEMONIC; return(MNEMONIC); }
715 "INY"                   { yylval.mnemonic = INY_MNEMONIC; return(MNEMONIC); }
716 "JMP"                   { yylval.mnemonic = JMP_MNEMONIC; return(MNEMONIC); }
717 "JSR"                   { yylval.mnemonic = JSR_MNEMONIC; return(MNEMONIC); }
718 "LDA"                   { yylval.mnemonic = LDA_MNEMONIC; return(MNEMONIC); }
719 "LDX"                   { yylval.mnemonic = LDX_MNEMONIC; return(MNEMONIC); }
720 "LDY"                   { yylval.mnemonic = LDY_MNEMONIC; return(MNEMONIC); }
721 "LSR"                   { yylval.mnemonic = LSR_MNEMONIC; return(MNEMONIC); }
722 "NOP"                   { yylval.mnemonic = NOP_MNEMONIC; return(MNEMONIC); }
723 "ORA"                   { yylval.mnemonic = ORA_MNEMONIC; return(MNEMONIC); }
724 "PHA"                   { yylval.mnemonic = PHA_MNEMONIC; return(MNEMONIC); }
725 "PHP"                   { yylval.mnemonic = PHP_MNEMONIC; return(MNEMONIC); }
726 "PLA"                   { yylval.mnemonic = PLA_MNEMONIC; return(MNEMONIC); }
727 "PLP"                   { yylval.mnemonic = PLP_MNEMONIC; return(MNEMONIC); }
728 "ROL"                   { yylval.mnemonic = ROL_MNEMONIC; return(MNEMONIC); }
729 "ROR"                   { yylval.mnemonic = ROR_MNEMONIC; return(MNEMONIC); }
730 "RTI"                   { yylval.mnemonic = RTI_MNEMONIC; return(MNEMONIC); }
731 "RTS"                   { yylval.mnemonic = RTS_MNEMONIC; return(MNEMONIC); }
732 "SBC"                   { yylval.mnemonic = SBC_MNEMONIC; return(MNEMONIC); }
733 "SEC"                   { yylval.mnemonic = SEC_MNEMONIC; return(MNEMONIC); }
734 "SED"                   { yylval.mnemonic = SED_MNEMONIC; return(MNEMONIC); }
735 "SEI"                   { yylval.mnemonic = SEI_MNEMONIC; return(MNEMONIC); }
736 "STA"                   { yylval.mnemonic = STA_MNEMONIC; return(MNEMONIC); }
737 "STX"                   { yylval.mnemonic = STX_MNEMONIC; return(MNEMONIC); }
738 "STY"                   { yylval.mnemonic = STY_MNEMONIC; return(MNEMONIC); }
739 "TAX"                   { yylval.mnemonic = TAX_MNEMONIC; return(MNEMONIC); }
740 "TAY"                   { yylval.mnemonic = TAY_MNEMONIC; return(MNEMONIC); }
741 "TSX"                   { yylval.mnemonic = TSX_MNEMONIC; return(MNEMONIC); }
742 "TXA"                   { yylval.mnemonic = TXA_MNEMONIC; return(MNEMONIC); }
743 "TXS"                   { yylval.mnemonic = TXS_MNEMONIC; return(MNEMONIC); }
744 "TYA"                   { yylval.mnemonic = TYA_MNEMONIC; return(MNEMONIC); }
745 "X"                     { return('X'); }
746 "Y"                     { return('Y'); }
747 "A"                     { return('A'); }
748 "MASK"                  { return(MASK); }
749 "SIZEOF"                { return(SIZEOF); }
750 d142 13
751 a154 13
752 {pragma_prefix}"IF"     { return(IF); }
753 {pragma_prefix}"IFDEF"  { return(IFDEF); }
754 {pragma_prefix}"IFNDEF" { return(IFNDEF); }
755 {pragma_prefix}"ELSE"   { return(ELSE); }
756 {pragma_prefix}"ELIF"   { return(ELIF); }
757 {pragma_prefix}"ENDIF"  { return(ENDIF); }
758 {pragma_prefix}"MACRO"  { return(MACRO); }
759 {pragma_prefix}"END"    { return(END); }
760 {pragma_prefix}"ENDE"   { return(ENDE); }
761 {pragma_prefix}"ENDM"   { return(ENDM); }
762 {pragma_prefix}"ENDP"   { return(ENDP); }
763 {pragma_prefix}"ENDS"   { return(ENDS); }
764 {pragma_prefix}"EQU"    { return(EQU); }
765 d157 5
766 a161 5
767 {pragma_prefix}"INCSRC" { return(INCSRC); }
768 {pragma_prefix}"INCBIN" { return(INCBIN); }
769 {pragma_prefix}"ALIGN"  { return(ALIGN); }
770 {pragma_prefix}"PUBLIC" { return(PUBLIC); }
771 {pragma_prefix}"EXTRN"  { return(EXTRN); }
772 d170 3
773 a172 3
774 {pragma_prefix}"TAG"    { return(TAG); }
775 {pragma_prefix}"TYPE"   { return(TAG); }
776 {pragma_prefix}"LABEL"  { return(_LABEL_); }
777 d177 12
778 a188 12
779 {pragma_prefix}"PAD"    { return(DSB); }
780 {pragma_prefix}"DB"     { return(BYTE); }
781 {pragma_prefix}"BYTE"   { return(BYTE); }
782 {pragma_prefix}"CHAR"   { return(CHAR); }
783 {pragma_prefix}"ASC"    { return(CHAR); }
784 {pragma_prefix}"DW"     { return(WORD); }
785 {pragma_prefix}"WORD"   { return(WORD); }
786 {pragma_prefix}"DD"     { return(DWORD); }
787 {pragma_prefix}"DWORD"  { return(DWORD); }
788 {pragma_prefix}"DSB"    { return(DSB); }
789 {pragma_prefix}"DSW"    { return(DSW); }
790 {pragma_prefix}"DSD"    { return(DSD); }
791 d191 30
792 a220 30
793 "@@@@"{local_id}                { yylval.ident = yytext; return(LOCAL_ID); }
794 {identifier}            { yylval.ident = yytext; return(IDENTIFIER); }
795 {string_literal}        { yytext[yyleng-1] = '\0'; yylval.string = &yytext[1]; return(STRING_LITERAL); }
796 "+"{2,8}                { yylval.ident = yytext; return(FORWARD_BRANCH); }
797 "-"{2,8}                { yylval.ident = yytext; return(BACKWARD_BRANCH); }
798 "::"                    { return(SCOPE_OP); }
799 "."                     { return('.'); }
800 \n                      { LOCATION_LINES (yylloc, yyleng); LOCATION_STEP (yylloc); return('\n'); }
801 ","                     { return(','); }
802 ":"                     { return(':'); }
803 "#"                     { return('#'); }
804 "="                     { return('='); }
805 "("                     { return( yyparswap ? '[' : '(' ); }
806 ")"                     { return( yyparswap ? ']' : ')' ); }
807 "["                     { return( yyparswap ? '(' : '[' ); }
808 "]"                     { return( yyparswap ? ')' : ']'); }
809 "{"                     { return('{'); }
810 "}"                     { return('}'); }
811 "&"                     { return('&'); }
812 "!"                     { return('!'); }
813 "~"                     { return('~'); }
814 "-"                     { return('-'); }
815 "+"                     { return('+'); }
816 "*"                     { return('*'); }
817 "/"                     { return('/'); }
818 "%"                     { return('%'); }
819 "<"                     { return('<'); }
820 ">"                     { return('>'); }
821 "^"                     { return('^'); }
822 "|"                     { return('|'); }
823 d227 3
824 a229 3
825 "$"                     { return('$'); }
826 "@@"                    { return('@@'); }
827 .                       { yyerror("Skipping invalid character(s)"); LOCATION_STEP (yylloc); }
828 d236 4
829 a239 4
830         char *name;     /* Name of the file */
831         YY_BUFFER_STATE buf;    /* flex input buffer */
832         YYLTYPE loc;    /* Current position in file */
833         FILE *fp;       /* File handle */
834 d259 1
835 a259 1
836         LOCATION_RESET(yylloc);
837 d267 19
838 a285 19
839         /* Discard of buffer that has been scanned */
840         yy_delete_buffer(YY_CURRENT_BUFFER);
841         /* Close current file */
842         fclose(FTOS().fp);
843         /* Resume scanning of previous buffer, if any */
844         if (file_stack_ptr > 0) {
845                 /* Pop stack */
846                 file_stack_ptr--;
847                 /* Restore location */
848                 yylloc = FTOS().loc;
849                 /* Resume scanning from this buffer */
850                 yy_switch_to_buffer(FTOS().buf);
851                 /* Not end of all input. */
852                 return(0);
853         }
854         else {
855                 /* We have reached end of all input. */
856                 return(1);
857         }
858 d296 27
859 a322 27
860         FILE *fp;
861         /* Check for stack overflow */
862         if (file_stack_ptr == FILE_STACK_SIZE-1) {
863                 /* Error, stack overflow */
864                 return 2;
865         }
866         /* Attempt to open file */
867         fp = fopen(filename, "rt");
868         if (fp) {
869                 /* Save position in current file */
870                 FTOS().loc = yylloc;
871                 /* Push the new file */
872                 file_stack_ptr++;
873                 FTOS().name = filename;
874                 FTOS().buf = yy_create_buffer(fp, YY_BUF_SIZE);
875                 FTOS().fp = fp;
876                 /* Reset file location */
877                 yyresetloc();
878                 /* Resume scanning from the new buffer */
879                 yy_switch_to_buffer(FTOS().buf);
880                 /* Success */
881                 return 0;
882         }
883         else {
884                 /* Error, couldn't open file */
885                 return 1;
886         }
887 d333 21
888 a353 21
889         FILE *fp;
890         yyparswap = swap_parens;
891         /* Attempt to open the given file */
892         fp = fopen(filename, "rt");
893         if (fp) {
894                 /* Init stack */
895                 file_stack_ptr = 0;
896                 FTOS().name = filename;
897                 FTOS().buf = yy_create_buffer(fp, YY_BUF_SIZE);
898                 FTOS().fp = fp;
899                 /* Reset file location */
900                 yyresetloc();
901                 /* Use the new buffer */
902                 yy_switch_to_buffer(FTOS().buf);
903                 /* Success */
904                 return 1;
905         }
906         else {
907                 /* Couldn't open file */
908                 return 0;
909         }
910 d361 1
911 a361 1
912         return FTOS().name;
913 d369 11
914 a379 11
915         int i;
916         /* Print line of form: file:pos:message */
917         fprintf(stderr, "%s:", yy_current_filename());
918         LOCATION_PRINT(stderr, yylloc);
919         fprintf(stderr, ": %s\n", s);
920         /* Print include-trace */
921         for (i=file_stack_ptr-1; i>=0; i--) {
922                 fprintf(stderr, "(%s:", file_stack[i].name);
923                 LOCATION_PRINT(stderr, file_stack[i].loc);
924                 fprintf(stderr, ")\n");
925         }
926 d387 7
927 a393 7
928         int start;
929         /* Kill the ':' at the end */
930         yytext[strlen(yytext)-1] = 0;
931         /* Skip the whitespace, if any */
932         start = 0;
933         while ((yytext[start] == ' ') || (yytext[start] == '\t')) start++;
934         return &yytext[start];
940 @xorcyst 1.4.0
942 text
943 @d3 1
944 a3 1
945  * $Id: scanner.l,v 1.7 2004/12/16 13:22:08 kenth Exp kenth $
946 d5 3
947 d76 2
953 @added DEFINE parsing
955 text
956 @d3 1
957 a3 1
958  * $Id: scanner.l,v 1.6 2004/12/14 01:50:33 kenth Exp kenth $
959 d5 3
960 d153 1
961 d219 1
967 @xorcyst 1.3.0
969 text
970 @d3 1
971 a3 1
972  * $Id: scanner.l,v 1.5 2004/12/11 02:12:32 kenth Exp kenth $
973 d5 3
974 d143 1
980 @xorcyst 1.2.0
982 text
983 @d3 1
984 a3 1
985  * $Id: scanner.l,v 1.4 2004/12/10 22:43:53 kenth Exp kenth $
986 d5 3
987 d41 1
988 a41 1
989 identifier              [A-Z_][A-Z0-9_]{1,255}
990 d121 1
991 d159 1
997 @removed FILE_PATH recognition: ".db <a, >b" is erronously parsed otherwise
999 text
1000 @d3 1
1001 a3 1
1002  * $Id: scanner.l,v 1.3 2004/12/09 11:15:28 kenth Exp kenth $
1003 d5 3
1004 d172 2
1010 @bugfix: close file handle
1011 added: "WARNING", "ERROR" recognition
1013 text
1014 @d3 1
1015 a3 1
1016  * $Id: scanner.l,v 1.2 2004/12/06 05:06:29 kenth Exp kenth $
1017 d5 4
1018 a27 1
1019 file_path               <[^\n>]*>
1020 a168 1
1021 {file_path}             { yylval.string = yytext; return(FILE_PATH); }
1027 @xorcyst 1.1.0
1029 text
1030 @d3 1
1031 a3 1
1032  * $Id: scanner.l,v 1.1 2004/06/30 07:56:59 kenth Exp kenth $
1033 d5 3
1034 d147 2
1035 d209 1
1036 d239 2
1037 d281 1
1038 d312 1
1044 @Initial revision
1046 text
1047 @d3 5
1048 a7 2
1049  * $Id$
1050  * $Log$
1051 d9 1
1052 d29 2
1053 a30 2
1054 identifier              [A-Z_][A-Z0-9_]{0,255}
1055 local_id                [0-9A-Z_]{1,256}
1056 d109 3
1057 d122 1
1058 d124 2
1059 d134 22
1060 a155 11
1061 {pragma_prefix}"DB"     { yylval.datatype = BYTE_DATATYPE; return(DATATYPE); }
1062 {pragma_prefix}"BYTE"   { yylval.datatype = BYTE_DATATYPE; return(DATATYPE); }
1063 {pragma_prefix}"CHAR"   { yylval.datatype = CHAR_DATATYPE; return(DATATYPE); }
1064 {pragma_prefix}"ASC"    { yylval.datatype = CHAR_DATATYPE; return(DATATYPE); }
1065 {pragma_prefix}"DW"     { yylval.datatype = WORD_DATATYPE; return(DATATYPE); }
1066 {pragma_prefix}"WORD"   { yylval.datatype = WORD_DATATYPE; return(DATATYPE); }
1067 {pragma_prefix}"DD"     { yylval.datatype = DWORD_DATATYPE; return(DATATYPE); }
1068 {pragma_prefix}"DWORD"  { yylval.datatype = DWORD_DATATYPE; return(DATATYPE); }
1069 {pragma_prefix}"DSB"    { yylval.datatype = BYTE_DATATYPE; return(STORAGE); }
1070 {pragma_prefix}"DSW"    { yylval.datatype = WORD_DATATYPE; return(STORAGE); }
1071 {pragma_prefix}"DSD"    { yylval.datatype = DWORD_DATATYPE; return(STORAGE); }
1072 d162 1
1073 d169 6
1074 a174 4
1075 "("                     { return('('); }
1076 ")"                     { return(')'); }
1077 "["                     { return('['); }
1078 "]"                     { return(']'); }
1079 d289 2
1080 d292 1
1081 a292 1
1082 int yybegin(char *filename)
1083 d295 1