diff options
Diffstat (limited to 'LuaSL/src/LuaSL_lexer.l')
-rw-r--r-- | LuaSL/src/LuaSL_lexer.l | 158 |
1 files changed, 80 insertions, 78 deletions
diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l index 08a68ef..30e0b6b 100644 --- a/LuaSL/src/LuaSL_lexer.l +++ b/LuaSL/src/LuaSL_lexer.l | |||
@@ -5,8 +5,7 @@ | |||
5 | #include <stdio.h> | 5 | #include <stdio.h> |
6 | 6 | ||
7 | void comment(yyscan_t yyscanner); | 7 | void comment(yyscan_t yyscanner); |
8 | void common(YYSTYPE *lval, char *text, boolean checkIgnorable); | 8 | int common(YYSTYPE *lval, char *text, boolean checkIgnorable, int type); |
9 | void ignorable(char *text); | ||
10 | 9 | ||
11 | %} | 10 | %} |
12 | 11 | ||
@@ -22,90 +21,90 @@ EXPONANT [eE][+-]?{INTEGER} | |||
22 | FLOAT {INTEGER}("."{INTEGER})?{EXPONANT}? | 21 | FLOAT {INTEGER}("."{INTEGER})?{EXPONANT}? |
23 | CHAR '(\\.|[^\\'\n])+' | 22 | CHAR '(\\.|[^\\'\n])+' |
24 | STRING \"(\\.|[^\\"\n])*\" | 23 | STRING \"(\\.|[^\\"\n])*\" |
25 | NAME [[:alpha:]](_|[[:alpha:]]|[[:digit:]])* | 24 | IDENTIFIER [[:alpha:]](_|[[:alpha:]]|[[:digit:]])* |
26 | 25 | ||
27 | %% | 26 | %% |
28 | 27 | ||
29 | /* The order here is important, in mysterious ways. The more specific the lower in case of ambiguities like "floats contain integers". I think, not tested that well yet. */ | 28 | /* The order here is important, in mysterious ways. The more specific the lower in case of ambiguities like "floats contain integers". I think, not tested that well yet. */ |
30 | 29 | ||
31 | /* Ignorables. */ | 30 | /* Ignorables. */ |
32 | [[:space:]]+ %{ common(yylval, yytext, FALSE); ignorable(yytext); %} | 31 | [[:space:]]+ %{ common(yylval, yytext, FALSE, LSL_SPACE); %} |
33 | /* Yes I know this will have problems with huge comments, just being simple to get it to work for now. */ | 32 | /* Yes I know this will have problems with huge comments, just being simple to get it to work for now. */ |
34 | "/*"([^"*/"]*)"*/" %{ common(yylval, yytext, FALSE); ignorable(yytext); %} | 33 | "/*"([^"*/"]*)"*/" %{ common(yylval, yytext, FALSE, LSL_COMMENT); %} |
35 | "//"[^\n]* %{ common(yylval, yytext, FALSE); ignorable(yytext); %} | 34 | "//"[^\n]* %{ common(yylval, yytext, FALSE, LSL_COMMENT_LINE); %} |
36 | 35 | ||
37 | /* Operations. */ | 36 | /* Operations. */ |
38 | "&&" { common(yylval, yytext, TRUE); return LSL_BOOL_AND; } | 37 | "&&" { return common(yylval, yytext, TRUE, LSL_BOOL_AND); } |
39 | "||" { common(yylval, yytext, TRUE); return LSL_BOOL_OR; } | 38 | "||" { return common(yylval, yytext, TRUE, LSL_BOOL_OR); } |
40 | "|" { common(yylval, yytext, TRUE); return LSL_BIT_OR; } | 39 | "|" { return common(yylval, yytext, TRUE, LSL_BIT_OR); } |
41 | "^" { common(yylval, yytext, TRUE); return LSL_BIT_XOR; } | 40 | "^" { return common(yylval, yytext, TRUE, LSL_BIT_XOR); } |
42 | "&" { common(yylval, yytext, TRUE); return LSL_BIT_AND; } | 41 | "&" { return common(yylval, yytext, TRUE, LSL_BIT_AND); } |
43 | "!=" { common(yylval, yytext, TRUE); return LSL_NOT_EQUAL; } | 42 | "!=" { return common(yylval, yytext, TRUE, LSL_NOT_EQUAL); } |
44 | "==" { common(yylval, yytext, TRUE); return LSL_EQUAL; } | 43 | "==" { return common(yylval, yytext, TRUE, LSL_EQUAL); } |
45 | ">=" { common(yylval, yytext, TRUE); return LSL_GREATER_EQUAL; } | 44 | ">=" { return common(yylval, yytext, TRUE, LSL_GREATER_EQUAL); } |
46 | "<=" { common(yylval, yytext, TRUE); return LSL_LESS_EQUAL; } | 45 | "<=" { return common(yylval, yytext, TRUE, LSL_LESS_EQUAL); } |
47 | ">" { common(yylval, yytext, TRUE); return LSL_GREATER_THAN; } | 46 | ">" { return common(yylval, yytext, TRUE, LSL_GREATER_THAN); } |
48 | "<" { common(yylval, yytext, TRUE); return LSL_LESS_THAN; } | 47 | "<" { return common(yylval, yytext, TRUE, LSL_LESS_THAN); } |
49 | ">>" { common(yylval, yytext, TRUE); return LSL_RIGHT_SHIFT; } | 48 | ">>" { return common(yylval, yytext, TRUE, LSL_RIGHT_SHIFT); } |
50 | "<<" { common(yylval, yytext, TRUE); return LSL_LEFT_SHIFT; } | 49 | "<<" { return common(yylval, yytext, TRUE, LSL_LEFT_SHIFT); } |
51 | "+" { common(yylval, yytext, TRUE); return LSL_ADD; } | 50 | "+" { return common(yylval, yytext, TRUE, LSL_ADD); } |
52 | "-" { common(yylval, yytext, TRUE); return LSL_SUBTRACT; } | 51 | "-" { return common(yylval, yytext, TRUE, LSL_SUBTRACT); } |
53 | "*" { common(yylval, yytext, TRUE); return LSL_MULTIPLY; } | 52 | "*" { return common(yylval, yytext, TRUE, LSL_MULTIPLY); } |
54 | "%" { common(yylval, yytext, TRUE); return LSL_MODULO; } | 53 | "%" { return common(yylval, yytext, TRUE, LSL_MODULO); } |
55 | "/" { common(yylval, yytext, TRUE); return LSL_DIVIDE; } | 54 | "/" { return common(yylval, yytext, TRUE, LSL_DIVIDE); } |
56 | "!" { common(yylval, yytext, TRUE); return LSL_BOOL_NOT; } | 55 | "!" { return common(yylval, yytext, TRUE, LSL_BOOL_NOT); } |
57 | "~" { common(yylval, yytext, TRUE); return LSL_BIT_NOT; } | 56 | "~" { return common(yylval, yytext, TRUE, LSL_BIT_NOT); } |
58 | "[" { common(yylval, yytext, TRUE); return LSL_BRACKET_OPEN; } | 57 | "[" { return common(yylval, yytext, TRUE, LSL_BRACKET_OPEN); } |
59 | "]" { common(yylval, yytext, TRUE); return LSL_BRACKET_CLOSE; } | 58 | "]" { return common(yylval, yytext, TRUE, LSL_BRACKET_CLOSE); } |
60 | "(" { common(yylval, yytext, TRUE); return LSL_PARENTHESIS_OPEN; } | 59 | "(" { return common(yylval, yytext, TRUE, LSL_PARENTHESIS_OPEN); } |
61 | ")" { common(yylval, yytext, TRUE); return LSL_PARENTHESIS_CLOSE; } | 60 | ")" { return common(yylval, yytext, TRUE, LSL_PARENTHESIS_CLOSE); } |
62 | "+=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_ADD; } | 61 | "+=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_ADD); } |
63 | "-=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_SUBTRACT; } | 62 | "-=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_SUBTRACT); } |
64 | "*=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_MULTIPLY; } | 63 | "*=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_MULTIPLY); } |
65 | "%=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_MODULO; } | 64 | "%=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_MODULO); } |
66 | "/=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_DIVIDE; } | 65 | "/=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_DIVIDE); } |
67 | "=" { common(yylval, yytext, TRUE); return LSL_ASSIGNMENT_PLAIN; } | 66 | "=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_PLAIN); } |
68 | "." { common(yylval, yytext, TRUE); return LSL_DOT; } | 67 | "." { return common(yylval, yytext, TRUE, LSL_DOT); } |
69 | "--" { common(yylval, yytext, TRUE); return LSL_DECREMENT_PRE; } | 68 | "--" { return common(yylval, yytext, TRUE, LSL_DECREMENT_PRE); } |
70 | "++" { common(yylval, yytext, TRUE); return LSL_INCREMENT_PRE; } | 69 | "++" { return common(yylval, yytext, TRUE, LSL_INCREMENT_PRE); } |
71 | "," { common(yylval, yytext, TRUE); return LSL_COMMA; } | 70 | "," { return common(yylval, yytext, TRUE, LSL_COMMA); } |
72 | 71 | ||
73 | /* Types. */ | 72 | /* Types. */ |
74 | {INTEGER} %{ common(yylval, yytext, TRUE); yylval->value.integerValue = atoi(yytext); return LSL_INTEGER; %} | 73 | {INTEGER} %{ yylval->value.integerValue = atoi(yytext); return common(yylval, yytext, TRUE, LSL_INTEGER); %} |
75 | {FLOAT} %{ common(yylval, yytext, TRUE); yylval->value.floatValue = atof(yytext); return LSL_FLOAT; %} | 74 | {FLOAT} %{ yylval->value.floatValue = atof(yytext); return common(yylval, yytext, TRUE, LSL_FLOAT); %} |
76 | 75 | ||
77 | /* Type keywords. */ | 76 | /* Type keywords. */ |
78 | "float" %{ common(yylval, yytext, TRUE); return LSL_TYPE_FLOAT; %} | 77 | "float" %{ return common(yylval, yytext, TRUE, LSL_TYPE_FLOAT); %} |
79 | "integer" %{ common(yylval, yytext, TRUE); return LSL_TYPE_INTEGER; %} | 78 | "integer" %{ return common(yylval, yytext, TRUE, LSL_TYPE_INTEGER); %} |
80 | "key" %{ common(yylval, yytext, TRUE); return LSL_TYPE_KEY; %} | 79 | "key" %{ return common(yylval, yytext, TRUE, LSL_TYPE_KEY); %} |
81 | "list" %{ common(yylval, yytext, TRUE); return LSL_TYPE_LIST; %} | 80 | "list" %{ return common(yylval, yytext, TRUE, LSL_TYPE_LIST); %} |
82 | "quaternion" %{ common(yylval, yytext, TRUE); return LSL_TYPE_ROTATION; %} | 81 | "quaternion" %{ return common(yylval, yytext, TRUE, LSL_TYPE_ROTATION); %} |
83 | "rotation" %{ common(yylval, yytext, TRUE); return LSL_TYPE_ROTATION; %} | 82 | "rotation" %{ return common(yylval, yytext, TRUE, LSL_TYPE_ROTATION); %} |
84 | "string" %{ common(yylval, yytext, TRUE); return LSL_TYPE_STRING; %} | 83 | "string" %{ return common(yylval, yytext, TRUE, LSL_TYPE_STRING); %} |
85 | "vector" %{ common(yylval, yytext, TRUE); return LSL_TYPE_VECTOR; %} | 84 | "vector" %{ return common(yylval, yytext, TRUE, LSL_TYPE_VECTOR); %} |
86 | 85 | ||
87 | /* Statement keywords. */ | 86 | /* Statement keywords. */ |
88 | "do" %{ common(yylval, yytext, TRUE); return LSL_DO; %} | 87 | "do" %{ return common(yylval, yytext, TRUE, LSL_DO); %} |
89 | "for" %{ common(yylval, yytext, TRUE); return LSL_FOR; %} | 88 | "for" %{ return common(yylval, yytext, TRUE, LSL_FOR); %} |
90 | "else" %{ common(yylval, yytext, TRUE); return LSL_ELSE; %} | 89 | "else" %{ return common(yylval, yytext, TRUE, LSL_ELSE); %} |
91 | "if" %{ common(yylval, yytext, TRUE); return LSL_IF; %} | 90 | "if" %{ return common(yylval, yytext, TRUE, LSL_IF); %} |
92 | "jump" %{ common(yylval, yytext, TRUE); return LSL_JUMP; %} | 91 | "jump" %{ return common(yylval, yytext, TRUE, LSL_JUMP); %} |
93 | "return" %{ common(yylval, yytext, TRUE); return LSL_RETURN; %} | 92 | "return" %{ return common(yylval, yytext, TRUE, LSL_RETURN); %} |
94 | "state" %{ common(yylval, yytext, TRUE); return LSL_STATE_CHANGE; %} | 93 | "state" %{ return common(yylval, yytext, TRUE, LSL_STATE_CHANGE); %} |
95 | "while" %{ common(yylval, yytext, TRUE); return LSL_WHILE; %} | 94 | "while" %{ return common(yylval, yytext, TRUE, LSL_WHILE); %} |
96 | 95 | ||
97 | {NAME} %{ common(yylval, yytext, TRUE); /* yylval->value.nameValue = strdup(yytext); return LSL_NAME; */ %} | 96 | {IDENTIFIER} %{ /* yylval->value.nameValue = strdup(yytext); */ common(yylval, yytext, TRUE, LSL_IDENTIFIER); %} |
98 | 97 | ||
99 | /* Other symbols. */ | 98 | /* Other symbols. */ |
100 | "@" %{ common(yylval, yytext, TRUE); return LSL_LABEL; %} | 99 | "@" %{ return common(yylval, yytext, TRUE, LSL_LABEL); %} |
101 | "{" %{ common(yylval, yytext, TRUE); return LSL_BLOCK_OPEN; %} | 100 | "{" %{ return common(yylval, yytext, TRUE, LSL_BLOCK_OPEN); %} |
102 | "}" %{ common(yylval, yytext, TRUE); return LSL_BLOCK_CLOSE; %} | 101 | "}" %{ return common(yylval, yytext, TRUE, LSL_BLOCK_CLOSE); %} |
103 | ";" %{ common(yylval, yytext, TRUE); return LSL_STATEMENT; %} | 102 | ";" %{ return common(yylval, yytext, TRUE, LSL_STATEMENT); %} |
104 | 103 | ||
105 | <<EOF>> { yyterminate(); } | 104 | <<EOF>> { yyterminate(); } |
106 | 105 | ||
107 | /* Everything else */ | 106 | /* Everything else */ |
108 | . %{ common(yylval, yytext, TRUE); printf(" unexpected character.\n"); %} | 107 | . %{ printf(" unexpected character.\n"); yylval->value.unknownValue = strdup(yytext); common(yylval, yytext, TRUE, LSL_UNKNOWN); %} |
109 | 108 | ||
110 | %% | 109 | %% |
111 | 110 | ||
@@ -126,7 +125,7 @@ static char *ignorableText = NULL; | |||
126 | static int column = 0; | 125 | static int column = 0; |
127 | static int line = 0; | 126 | static int line = 0; |
128 | 127 | ||
129 | void common(YYSTYPE *lval, char *text, boolean checkIgnorable) | 128 | int common(YYSTYPE *lval, char *text, boolean checkIgnorable, int type) |
130 | { | 129 | { |
131 | int i; | 130 | int i; |
132 | 131 | ||
@@ -141,6 +140,8 @@ void common(YYSTYPE *lval, char *text, boolean checkIgnorable) | |||
141 | else | 140 | else |
142 | column++; | 141 | column++; |
143 | 142 | ||
143 | lval->type = type; | ||
144 | lval->token = tokens[type - lowestToken]; | ||
144 | lval->line = line; | 145 | lval->line = line; |
145 | lval->column = column; | 146 | lval->column = column; |
146 | 147 | ||
@@ -149,24 +150,25 @@ void common(YYSTYPE *lval, char *text, boolean checkIgnorable) | |||
149 | lval->ignorableText = ignorableText; | 150 | lval->ignorableText = ignorableText; |
150 | ignorableText = NULL; | 151 | ignorableText = NULL; |
151 | } | 152 | } |
153 | else | ||
154 | { | ||
155 | if (ignorableText) | ||
156 | { | ||
157 | int lenI = strlen(ignorableText); | ||
158 | int lenT = strlen(text); | ||
159 | |||
160 | ignorableText = realloc(ignorableText, lenI + lenT + 1); | ||
161 | sprintf(&ignorableText[lenI], "%s", text); | ||
162 | } | ||
163 | else | ||
164 | ignorableText = strdup(text); | ||
165 | } | ||
152 | 166 | ||
153 | #ifdef LUASL_DEBUG | 167 | #ifdef LUASL_DEBUG |
154 | printf ("%04d, %04d [%s]\n", line, column, text); | 168 | printf ("%04d, %04d [%s]\n", line, column, text); |
155 | #endif | 169 | #endif |
156 | } | ||
157 | |||
158 | void ignorable(char *text) | ||
159 | { | ||
160 | if (ignorableText) | ||
161 | { | ||
162 | int lenI = strlen(ignorableText); | ||
163 | int lenT = strlen(text); | ||
164 | 170 | ||
165 | ignorableText = realloc(ignorableText, lenI + lenT + 1); | 171 | return type; |
166 | sprintf(&ignorableText[lenI], "%s", text); | ||
167 | } | ||
168 | else | ||
169 | ignorableText = strdup(text); | ||
170 | } | 172 | } |
171 | 173 | ||
172 | int yyerror(const char *msg) | 174 | int yyerror(const char *msg) |