goomsl_lex.l 4.13 KB
Newer Older
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
%{

#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "goomsl.h"
#include "goomsl_private.h"
#include "goomsl_yacc.h"
void yyerror(char *);
void yyparse(void);

GoomSL *currentGoomSL;
static int  string_size;
static char string[1024];
%}

DIGIT    [0-9]
XDIGIT   [0-9a-f]
ID       [a-zA-Z_@&][a-zA-Z0-9_\.]*

%S C_COMMENT
%S LINE_COMMENT
%S STRING

%%

<LINE_COMMENT,C_COMMENT,INITIAL>^[ \t]*\n           { ++currentGoomSL->num_lines; /* Ignore empty lines */ }
<LINE_COMMENT,C_COMMENT,INITIAL>^[ \t]*"//"[^\n]*\n { ++currentGoomSL->num_lines; /* Ignore empty lines */ }

<LINE_COMMENT>\n    { ++currentGoomSL->num_lines; yylval.charValue=*yytext; BEGIN INITIAL; return '\n'; }
<INITIAL>\n         { ++currentGoomSL->num_lines; yylval.charValue=*yytext; return '\n'; }

<C_COMMENT>"*/"                { BEGIN INITIAL; }
<C_COMMENT>\n                  { ++currentGoomSL->num_lines; }
<C_COMMENT,LINE_COMMENT>.      { /* eat up comment */ }

<INITIAL>"#RST_LINE#"             { currentGoomSL->num_lines = 0; }
38
<INITIAL>"#FILE ".*"#"            { currentGoomSL->num_lines = 0; /* printf("%s\n", yytext); */ }
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
<INITIAL>"#"[^\n]*                { /* ignore preprocessor lines */ }

<INITIAL>"/*"                { BEGIN C_COMMENT; }
<INITIAL>"//"                { BEGIN LINE_COMMENT; }
<INITIAL>\"                  { BEGIN STRING; string_size=0; }

<STRING>"\\n"                { string[string_size++] = '\n'; }
<STRING>"\\\""               { string[string_size++] = '\"'; }
<STRING>\"                   { /* fin de la chaine: on cree le pointeur qui va bien */
                               unsigned int tmp;
                               BEGIN INITIAL;
                               string[string_size]=0;
                               tmp = gsl_malloc(currentGoomSL, string_size+1);
                               strcpy((char*)currentGoomSL->ptrArray[tmp],string);
                               sprintf(yylval.strValue, "0x%08x", tmp);
                               return LTYPE_PTR;
                             }
<STRING>.                    { string[string_size++] = *yytext; }

<INITIAL>"float"                 { return FLOAT_TK; }
<INITIAL>"int"                   { return INT_TK;   }
<INITIAL>"boolean"               { return INT_TK;   }
<INITIAL>"ptr"                   { return PTR_TK;   }
<INITIAL>"string"                { return PTR_TK;   }
<INITIAL>"declare"               { return DECLARE;  }
<INITIAL>"external"              { return EXTERNAL; }
<INITIAL>"struct"                { return STRUCT;   }
<INITIAL>"not"                   { return NOT;      }
<INITIAL>"while"                 { return WHILE;    } 
<INITIAL>"do"                    { return DO;       }
<INITIAL>"for"                   { return FOR;      }
<INITIAL>"in"                    { return IN;       }
<INITIAL>"true"                  { strncpy(yylval.strValue, "1", 2047);    return LTYPE_INTEGER; }
<INITIAL>"false"                 { strncpy(yylval.strValue, "0", 2047);    return LTYPE_INTEGER; }
<INITIAL>{ID}                    { strncpy(yylval.strValue, yytext, 2047); return LTYPE_VAR;     }
<INITIAL>{DIGIT}+                { strncpy(yylval.strValue, yytext, 2047); return LTYPE_INTEGER; }
<INITIAL>\'.\'                   { sprintf(yylval.strValue, "%d", (int)yytext[1]); return LTYPE_INTEGER; }
<INITIAL>"0x"{XDIGIT}+           { strncpy(yylval.strValue, yytext, 2047); return LTYPE_INTEGER; }
<INITIAL>{DIGIT}+"."{DIGIT}*     { strncpy(yylval.strValue, yytext, 2047); return LTYPE_FLOAT;   }
<INITIAL>{DIGIT}+"%"             { sprintf(yylval.strValue, "%3.2f", atof(yytext)/100.0f); return LTYPE_FLOAT; }
<INITIAL>"+="                    { return PLUS_EQ; }
<INITIAL>"*="                    { return MUL_EQ; }
<INITIAL>"-="                    { return SUB_EQ; }
<INITIAL>"/="                    { return DIV_EQ; }
<INITIAL>"<="                    { return LOW_EQ;  }
<INITIAL>">="                    { return SUP_EQ;  }
<INITIAL>"!="                    { return NOT_EQ;  }
<INITIAL>"<>"                    { return NOT_EQ;  }
<INITIAL>[ \t]+                  /* eat up whitespace */
<INITIAL>.                       { yylval.charValue = *yytext; return *yytext;    }

%%


int yywrap(void) { return 1; yyunput(0,0); }