File: assembler.flex

package info (click to toggle)
flasm 1.62-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 524 kB
  • ctags: 824
  • sloc: ansic: 6,123; yacc: 1,208; makefile: 70
file content (351 lines) | stat: -rw-r--r-- 13,063 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
flasm, command line assembler & disassembler of flash actionscript bytecode
Copyright (c) 2001 Opaque Industries, (c) 2002-2007 Igor Kogan
All rights reserved. See LICENSE.TXT for terms of use.
*/

%option nounput
%option warn
%{

#include <math.h>
#include <string.h>
#include <unistd.h>

#include "util.h"
#include "flasm.h"
#include "assembler.tab.h"
#ifdef MEMWATCH
#include "memwatch.h"
#endif

int yyEOF = 0;

char *lexBuffer = NULL;
int lexBufferLen = 0;

#define MAX_INCLUDE_DEPTH 10
#define MAX_LINE_LENGTH 65535

extern char *inputName;

YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
static int include_stack_ptr = 0;
static char *includenames[MAX_INCLUDE_DEPTH];
static int  sLineNumber[MAX_INCLUDE_DEPTH];
static int  column[MAX_INCLUDE_DEPTH];
static char szLine[MAX_LINE_LENGTH];


extern int yylex(void);
int  yywrap(void);
void yyerror(char *msg);
void warning(char *msg);
char *FileName(void);
int LineNumber(void);
int ColumnNumber(void);

inline void count(void);
inline void newLine(void);

static char sbuf[MAX_LINE_LENGTH];
static char *s;

int numActions = 0;

%}

DIGIT    [0-9]
LABELD   [a-zA-Z_][a-zA-Z0-9_]*:
ID       [a-zA-Z_][a-zA-Z0-9_]*
%x MOVIEDECL
%x STRINGSINGLEQ
%x STRINGDOUBLEQ
%x INCL
%x BLOCKCOMMENT

%%

0x[0-9a-fA-F]+                  {
                                    count();
                                    yylval.str = mstrdup(yytext+2);
                                    return HEX;
                                }

-{DIGIT}+                       {
                                    count();
                                    yylval.num = atol(yytext);
                                    return INTEGER;
                                }

{DIGIT}+                        {
                                    count();
                                    yylval.num = atol(yytext);
                                    return INTEGER;
                                }

-?{DIGIT}+"."{DIGIT}*(e("+"|"-"){DIGIT}{1,3})?f {
                                    count();
                                    yylval.str = mstrdup(yytext);
                                    return FLOAT;
                                }

-?{DIGIT}+"."{DIGIT}*(e("+"|"-"){DIGIT}{1,3})? {
                                    count();
                                    yylval.str = mstrdup(yytext);
                                    return DOUBLE;
                                }

c\:{DIGIT}+                     {
                                    count();
                                    yylval.num = atoi(yytext+2);
                                    return CONSTANT; 
                                }

"r:"                            {
                                    count();
                                    return REGISTER;
                                }

#INCLUDE[ \"\'\t]*              {
                                    count();
                                    BEGIN(INCL);
                                }
<INCL>[^\"\'\t\r\n]+            {
                                    /* got the include file name */
                                    count();
                                    if (include_stack_ptr >= MAX_INCLUDE_DEPTH) 
                                        yyerror("Includes nested too deeply");
                                    if (access(yytext, R_OK)!=0)
                                        yyerror("Cannot open include file");
                                    includenames[include_stack_ptr+1] = mstrdup(yytext);
                                }
<INCL>[ \"\'\t\r]*\n            {
                                    /* eat EOL and possible end quote */
                                    newLine();
                                    include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
                                    sLineNumber[include_stack_ptr] = 0;
                                    yyin = fopen(includenames[include_stack_ptr], "r");     
                                    if (!yyin) {
                                        include_stack_ptr--;
                                        sLineNumber[include_stack_ptr]--;
                                        yyerror("Problem opening include file");
                                    }
                                    yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
                                    BEGIN(INITIAL);
                                }

{LABELD}                        {
                                    count();
                                    yylval.str = mstrdup(yytext);
                                    yylval.str[yyleng-1]=0;
                                    return LABEL;
                                }

{ID}                            {
                                    char buf[MAX_KEYWORD_LEN];
                                    struct keyword *k;

                                    lowercase(yytext, buf);
                                    k = in_word_set(buf, yyleng);
                                    count(); 
                                    
                                    if (k != NULL) {
                                        /* count numActions for ifFrameLoaded/IfFrameLoadedExpr */
                                        if (k->numActions > 0)
                                            numActions += k->numActions;
                                        else if (k->numActions < 0)
                                            numActions = 0;
                                        return k->token;
                                    }
                                    else {
                                        yylval.str = mstrdup(yytext);
                                        return STRING;
                                    }
                                }

\'                              {
                                    count();
                                    BEGIN STRINGSINGLEQ;
                                    s = sbuf;
                                }
<STRINGSINGLEQ>\n               {
                                    column[include_stack_ptr]++;
                                    yyerror("Unterminated string");
                                }
<STRINGSINGLEQ>\\b              {   count(); *s++ = '\b';  }
<STRINGSINGLEQ>\\t              {   count(); *s++ = '\t';  }
<STRINGSINGLEQ>\\n              {   count(); *s++ = '\n';  }
<STRINGSINGLEQ>\\f              {   count(); *s++ = '\f';  }
<STRINGSINGLEQ>\\r              {   count(); *s++ = '\r';  }
<STRINGSINGLEQ>\\\\             {   count(); *s++ = '\\';  }
<STRINGSINGLEQ>\\\'             {   count(); *s++ = '\'';  }
<STRINGSINGLEQ>\'               {
                                    count();
                                    *s = 0;
                                    yylval.str = mstrdup(sbuf);
                                    BEGIN(INITIAL);
                                    return STRING;
                                }
<STRINGSINGLEQ>.                {   
                                    count();
                                    *s++ = *yytext;
                                }

\"                              {
                                    count();
                                    BEGIN STRINGDOUBLEQ;
                                    s = sbuf;
                                }
<STRINGDOUBLEQ>\n               {
                                    column[include_stack_ptr]++;
                                    yyerror("Unterminated string");
                                }
<STRINGDOUBLEQ>\\b              {   count(); *s++ = '\b';  }
<STRINGDOUBLEQ>\\t              {   count(); *s++ = '\t';  }
<STRINGDOUBLEQ>\\n              {   count(); *s++ = '\n';  }
<STRINGDOUBLEQ>\\f              {   count(); *s++ = '\f';  }
<STRINGDOUBLEQ>\\r              {   count(); *s++ = '\r';  }
<STRINGDOUBLEQ>\\\\             {   count(); *s++ = '\\';  }
<STRINGDOUBLEQ>\\\"             {   count(); *s++ = '\"';  }
<STRINGDOUBLEQ>\"               {
                                    count(); 
                                    *s = 0;
                                    yylval.str = mstrdup(sbuf);
                                    BEGIN(INITIAL);
                                    return STRING;
                                }
<STRINGDOUBLEQ>.                {   
                                    count();
                                    *s++ = *yytext;
                                }

"movie"[ \t\v\f]+[\'\"]         {
                                    count();
                                    BEGIN MOVIEDECL;
                                    s = sbuf;
                                    return MOVIE;
                                }
<MOVIEDECL>\n                   {
                                    count();
                                    yyerror("Unterminated movie name");
                                }
<MOVIEDECL>[\'\"]               {
                                    count();
                                    *s = 0;
                                    yylval.str = mstrdup(sbuf);
                                    BEGIN(INITIAL);
                                    return MOVIENAME;
                                }
<MOVIEDECL>.                    {
                                    count();
                                    *s++ = *yytext;
                                }
"movie"[ \t\v\f]+[^\n\\'\\"]    {
                                    count();
                                    yyerror("Movie name must be included in quotes");
                                }

"/*"                            {   count(); BEGIN(BLOCKCOMMENT);  }
<BLOCKCOMMENT>[^*\n]*           {   count();                       }
<BLOCKCOMMENT>[^*\n]*\n         {   newLine();                      }
<BLOCKCOMMENT>"*"+[^*/\n]*      {   count();                       }
<BLOCKCOMMENT>"*"+[^*/\n]*\n    {   newLine();                      }
<BLOCKCOMMENT>"*"+"/"           {   count(); BEGIN(INITIAL);       }

"//"                            {
                                    int c;
                                    do
                                       c = input();
                                    while (c != '\n' && c != 0 && c != EOF);

                                    newLine();
                                }

":"                             {   count(); return ':';       }
"("                             {   count(); return '(';       }
")"                             {   count(); return ')';       }
"["                             {   count(); return '[';       }
"]"                             {   count(); return ']';       }
","                             {   count(); return ',';       }
"."                             {   count(); return '.';       }
"="                             {   count(); return '=';       }

[ \t\v\f]                       {   column[include_stack_ptr]++;   }
\n                              {   newLine();  }
\r                              {               }

.                               {   yyerror("Unrecognized character");  }

<<EOF>>                         {
                                    yy_delete_buffer(YY_CURRENT_BUFFER);
                                    if (--include_stack_ptr < 0) {
                                        yyEOF = 1;
                                        yyterminate();
                                    }
                                    else {
                                        yy_switch_to_buffer(include_stack[include_stack_ptr]);
                                    }
                                }


%%


int yywrap()
{
    return(1);
}

char *FileName(void)
{  
    if (include_stack_ptr==0)
        return inputName;
    else
        return includenames[include_stack_ptr];
}

int LineNumber(void)
{
    return sLineNumber[include_stack_ptr];
}

int ColumnNumber(void)
{
    return column[include_stack_ptr];
}

inline void newLine(void)
{
    column[include_stack_ptr] = 0;
    sLineNumber[include_stack_ptr]++;
}

inline void count(void)
{
    /* Count the characters to maintain the current column position */
    column[include_stack_ptr] += yyleng;
}

void warning(char *msg)
{
    strcpy(szLine, yytext + yyleng - ColumnNumber());
    tellUser(0, "\n%s", szLine);
    if (yytext[0] != '\n') tellUser(0, "\n");
    tellUser(0, "%*s", ColumnNumber(), "^");
    tellUser(0, "\nLine %4.4d of %s:\nWarning: %s \n\n", LineNumber() + 1, FileName(), msg);
}

void yyerror(char *msg)
{
    if (!yyEOF && strlen(yytext)) {
        strcpy(szLine, yytext + yyleng - ColumnNumber());
        sLineNumber[include_stack_ptr]++;
        tellUser(0, "\n%s", szLine);
        if (yytext[0] != '\n') tellUser(0, "\n");
        tellUser(0, "%*s", ColumnNumber(), "^");
        tellUser(1, "\nLine %4.4d of %s:\n%s \n", LineNumber(), FileName(), msg);
    }
    else
        tellUser(1, "Unexpected EOF found in %s while looking for input.\n",inputName);
}