diff options
| -rw-r--r-- | lex.c | 143 |
1 files changed, 69 insertions, 74 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | char *rcs_lex = "$Id: lex.c,v 2.32 1996/03/21 16:33:47 roberto Exp roberto $"; | 1 | char *rcs_lex = "$Id: lex.c,v 2.33 1996/05/28 21:07:32 roberto Exp roberto $"; |
| 2 | 2 | ||
| 3 | 3 | ||
| 4 | #include <ctype.h> | 4 | #include <ctype.h> |
| 5 | #include <string.h> | 5 | #include <string.h> |
| @@ -14,32 +14,24 @@ char *rcs_lex = "$Id: lex.c,v 2.32 1996/03/21 16:33:47 roberto Exp roberto $"; | |||
| 14 | 14 | ||
| 15 | #define MINBUFF 260 | 15 | #define MINBUFF 260 |
| 16 | 16 | ||
| 17 | #define next() { current = input(); } | 17 | #define next() (current = input()) |
| 18 | #define save(x) { *yytextLast++ = (x); } | 18 | #define save(x) (yytext[tokensize++] = (x)) |
| 19 | #define save_and_next() { save(current); next(); } | 19 | #define save_and_next() (save(current), next()) |
| 20 | |||
| 20 | 21 | ||
| 21 | static int current; | 22 | static int current; /* look ahead character */ |
| 22 | static char *yytext = NULL; | 23 | static Input input; /* input function */ |
| 23 | static int textsize = 0; | ||
| 24 | static char *yytextLast; | ||
| 25 | 24 | ||
| 26 | static Input input; | ||
| 27 | 25 | ||
| 28 | void lua_setinput (Input fn) | 26 | void lua_setinput (Input fn) |
| 29 | { | 27 | { |
| 30 | current = ' '; | 28 | current = ' '; |
| 31 | input = fn; | 29 | input = fn; |
| 32 | if (yytext == NULL) | ||
| 33 | { | ||
| 34 | textsize = MINBUFF; | ||
| 35 | yytext = newvector(textsize, char); | ||
| 36 | } | ||
| 37 | } | 30 | } |
| 38 | 31 | ||
| 39 | char *lua_lasttext (void) | 32 | char *lua_lasttext (void) |
| 40 | { | 33 | { |
| 41 | *yytextLast = 0; | 34 | return luaI_buffer(1); |
| 42 | return yytext; | ||
| 43 | } | 35 | } |
| 44 | 36 | ||
| 45 | 37 | ||
| @@ -80,70 +72,69 @@ void luaI_addReserved (void) | |||
| 80 | } | 72 | } |
| 81 | 73 | ||
| 82 | 74 | ||
| 83 | static void growtext (void) | 75 | static int read_long_string (char *yytext, int buffsize) |
| 84 | { | ||
| 85 | int size = yytextLast - yytext; | ||
| 86 | textsize = growvector(&yytext, textsize, char, lexEM, MAX_WORD); | ||
| 87 | yytextLast = yytext + size; | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | static int read_long_string (void) | ||
| 92 | { | 76 | { |
| 93 | int cont = 0; | 77 | int cont = 0; |
| 94 | int spaceleft = textsize - (yytextLast - yytext); | 78 | int tokensize = 2; /* '[[' already stored */ |
| 95 | while (1) | 79 | while (1) |
| 96 | { | 80 | { |
| 97 | if (spaceleft <= 2) /* may read more than 1 char in one cicle */ | 81 | if (buffsize-tokensize <= 2) /* may read more than 1 char in one cicle */ |
| 98 | { | 82 | yytext = luaI_buffer(buffsize *= 2); |
| 99 | growtext(); | ||
| 100 | spaceleft = textsize - (yytextLast - yytext); | ||
| 101 | } | ||
| 102 | switch (current) | 83 | switch (current) |
| 103 | { | 84 | { |
| 104 | case EOF: | 85 | case EOF: |
| 105 | case 0: | 86 | case 0: |
| 87 | save(0); | ||
| 106 | return WRONGTOKEN; | 88 | return WRONGTOKEN; |
| 107 | case '[': | 89 | case '[': |
| 108 | save_and_next(); spaceleft--; | 90 | save_and_next(); |
| 109 | if (current == '[') | 91 | if (current == '[') |
| 110 | { | 92 | { |
| 111 | cont++; | 93 | cont++; |
| 112 | save_and_next(); spaceleft--; | 94 | save_and_next(); |
| 113 | } | 95 | } |
| 114 | continue; | 96 | continue; |
| 115 | case ']': | 97 | case ']': |
| 116 | save_and_next(); spaceleft--; | 98 | save_and_next(); |
| 117 | if (current == ']') | 99 | if (current == ']') |
| 118 | { | 100 | { |
| 119 | if (cont == 0) return STRING; | 101 | if (cont == 0) goto endloop; |
| 120 | cont--; | 102 | cont--; |
| 121 | save_and_next(); spaceleft--; | 103 | save_and_next(); |
| 122 | } | 104 | } |
| 123 | continue; | 105 | continue; |
| 124 | case '\n': | 106 | case '\n': |
| 125 | lua_linenumber++; /* goes through */ | 107 | lua_linenumber++; /* goes through */ |
| 126 | default: | 108 | default: |
| 127 | save_and_next(); spaceleft--; | 109 | save_and_next(); |
| 128 | } | 110 | } |
| 129 | } | 111 | } endloop: |
| 112 | save_and_next(); /* pass the second ']' */ | ||
| 113 | yytext[tokensize-2] = 0; /* erases ']]' */ | ||
| 114 | luaY_lval.vWord = luaI_findconstantbyname(yytext+2); | ||
| 115 | yytext[tokensize-2] = ']'; /* restores ']]' */ | ||
| 116 | save(0); | ||
| 117 | return STRING; | ||
| 130 | } | 118 | } |
| 131 | 119 | ||
| 132 | |||
| 133 | int luaY_lex (void) | 120 | int luaY_lex (void) |
| 134 | { | 121 | { |
| 135 | double a; | ||
| 136 | static int linelasttoken = 0; | 122 | static int linelasttoken = 0; |
| 123 | double a; | ||
| 124 | int buffsize = MINBUFF; | ||
| 125 | char *yytext = luaI_buffer(buffsize); | ||
| 126 | yytext[1] = yytext[2] = yytext[3] = 0; | ||
| 137 | if (lua_debug) | 127 | if (lua_debug) |
| 138 | luaI_codedebugline(linelasttoken); | 128 | luaI_codedebugline(linelasttoken); |
| 139 | linelasttoken = lua_linenumber; | 129 | linelasttoken = lua_linenumber; |
| 140 | while (1) | 130 | while (1) |
| 141 | { | 131 | { |
| 142 | yytextLast = yytext; | 132 | int tokensize = 0; |
| 143 | switch (current) | 133 | switch (current) |
| 144 | { | 134 | { |
| 145 | case EOF: | 135 | case EOF: |
| 146 | case 0: | 136 | case 0: |
| 137 | save(0); | ||
| 147 | return 0; | 138 | return 0; |
| 148 | case '\n': linelasttoken = ++lua_linenumber; | 139 | case '\n': linelasttoken = ++lua_linenumber; |
| 149 | case ' ': | 140 | case ' ': |
| @@ -153,16 +144,16 @@ int luaY_lex (void) | |||
| 153 | continue; | 144 | continue; |
| 154 | 145 | ||
| 155 | case '$': | 146 | case '$': |
| 156 | next(); | 147 | save_and_next(); |
| 157 | while (isalnum(current) || current == '_') | 148 | while (isalnum(current) || current == '_') |
| 158 | save_and_next(); | 149 | save_and_next(); |
| 159 | *yytextLast = 0; | 150 | save(0); |
| 160 | if (strcmp(yytext, "debug") == 0) | 151 | if (strcmp(yytext+1, "debug") == 0) |
| 161 | { | 152 | { |
| 162 | luaY_lval.vInt = 1; | 153 | luaY_lval.vInt = 1; |
| 163 | return DEBUG; | 154 | return DEBUG; |
| 164 | } | 155 | } |
| 165 | else if (strcmp(yytext, "nodebug") == 0) | 156 | else if (strcmp(yytext+1, "nodebug") == 0) |
| 166 | { | 157 | { |
| 167 | luaY_lval.vInt = 0; | 158 | luaY_lval.vInt = 0; |
| 168 | return DEBUG; | 159 | return DEBUG; |
| @@ -181,12 +172,7 @@ int luaY_lex (void) | |||
| 181 | else | 172 | else |
| 182 | { | 173 | { |
| 183 | save_and_next(); /* pass the second '[' */ | 174 | save_and_next(); /* pass the second '[' */ |
| 184 | if (read_long_string() == WRONGTOKEN) | 175 | return read_long_string(yytext, buffsize); |
| 185 | return WRONGTOKEN; | ||
| 186 | save_and_next(); /* pass the second ']' */ | ||
| 187 | *(yytextLast-2) = 0; /* erases ']]' */ | ||
| 188 | luaY_lval.vWord = luaI_findconstantbyname(yytext+2); | ||
| 189 | return STRING; | ||
| 190 | } | 176 | } |
| 191 | 177 | ||
| 192 | case '=': | 178 | case '=': |
| @@ -213,21 +199,17 @@ int luaY_lex (void) | |||
| 213 | case '\'': | 199 | case '\'': |
| 214 | { | 200 | { |
| 215 | int del = current; | 201 | int del = current; |
| 216 | int spaceleft = textsize - (yytextLast - yytext); | 202 | save_and_next(); |
| 217 | next(); /* skip the delimiter */ | ||
| 218 | while (current != del) | 203 | while (current != del) |
| 219 | { | 204 | { |
| 220 | if (spaceleft <= 2) /* may read more than 1 char in one cicle */ | 205 | if (buffsize-tokensize <= 2) /* may read more than 1 char in one cicle */ |
| 221 | { | 206 | yytext = luaI_buffer(buffsize *= 2); |
| 222 | growtext(); | ||
| 223 | spaceleft = textsize - (yytextLast - yytext); | ||
| 224 | } | ||
| 225 | spaceleft--; | ||
| 226 | switch (current) | 207 | switch (current) |
| 227 | { | 208 | { |
| 228 | case EOF: | 209 | case EOF: |
| 229 | case 0: | 210 | case 0: |
| 230 | case '\n': | 211 | case '\n': |
| 212 | save(0); | ||
| 231 | return WRONGTOKEN; | 213 | return WRONGTOKEN; |
| 232 | case '\\': | 214 | case '\\': |
| 233 | next(); /* do not save the '\' */ | 215 | next(); /* do not save the '\' */ |
| @@ -244,9 +226,11 @@ int luaY_lex (void) | |||
| 244 | save_and_next(); | 226 | save_and_next(); |
| 245 | } | 227 | } |
| 246 | } | 228 | } |
| 247 | next(); /* skip the delimiter */ | 229 | next(); /* skip delimiter */ |
| 248 | *yytextLast = 0; | 230 | save(0); |
| 249 | luaY_lval.vWord = luaI_findconstantbyname(yytext); | 231 | luaY_lval.vWord = luaI_findconstantbyname(yytext+1); |
| 232 | tokensize--; | ||
| 233 | save(del); save(0); /* restore delimiter */ | ||
| 250 | return STRING; | 234 | return STRING; |
| 251 | } | 235 | } |
| 252 | 236 | ||
| @@ -266,7 +250,7 @@ int luaY_lex (void) | |||
| 266 | { | 250 | { |
| 267 | TaggedString *ts; | 251 | TaggedString *ts; |
| 268 | do { save_and_next(); } while (isalnum(current) || current == '_'); | 252 | do { save_and_next(); } while (isalnum(current) || current == '_'); |
| 269 | *yytextLast = 0; | 253 | save(0); |
| 270 | ts = lua_createstring(yytext); | 254 | ts = lua_createstring(yytext); |
| 271 | if (ts->marked > 2) | 255 | if (ts->marked > 2) |
| 272 | return ts->marked; /* reserved word */ | 256 | return ts->marked; /* reserved word */ |
| @@ -285,8 +269,7 @@ int luaY_lex (void) | |||
| 285 | save_and_next(); | 269 | save_and_next(); |
| 286 | return DOTS; /* ... */ | 270 | return DOTS; /* ... */ |
| 287 | } | 271 | } |
| 288 | else | 272 | else return CONC; /* .. */ |
| 289 | return CONC; /* .. */ | ||
| 290 | } | 273 | } |
| 291 | else if (!isdigit(current)) return '.'; | 274 | else if (!isdigit(current)) return '.'; |
| 292 | /* current is a digit: goes through to number */ | 275 | /* current is a digit: goes through to number */ |
| @@ -296,12 +279,19 @@ int luaY_lex (void) | |||
| 296 | case '0': case '1': case '2': case '3': case '4': | 279 | case '0': case '1': case '2': case '3': case '4': |
| 297 | case '5': case '6': case '7': case '8': case '9': | 280 | case '5': case '6': case '7': case '8': case '9': |
| 298 | a=0.0; | 281 | a=0.0; |
| 299 | do { a=10.0*a+(current-'0'); save_and_next(); } while (isdigit(current)); | 282 | do { |
| 283 | a=10.0*a+(current-'0'); | ||
| 284 | save_and_next(); | ||
| 285 | } while (isdigit(current)); | ||
| 300 | if (current == '.') save_and_next(); | 286 | if (current == '.') save_and_next(); |
| 301 | fraction: | 287 | fraction: |
| 302 | { double da=0.1; | 288 | { double da=0.1; |
| 303 | while (isdigit(current)) | 289 | while (isdigit(current)) |
| 304 | {a+=(current-'0')*da; da/=10.0; save_and_next()}; | 290 | { |
| 291 | a+=(current-'0')*da; | ||
| 292 | da/=10.0; | ||
| 293 | save_and_next(); | ||
| 294 | } | ||
| 305 | if (current == 'e' || current == 'E') | 295 | if (current == 'e' || current == 'E') |
| 306 | { | 296 | { |
| 307 | int e=0; | 297 | int e=0; |
| @@ -310,15 +300,19 @@ fraction: | |||
| 310 | save_and_next(); | 300 | save_and_next(); |
| 311 | neg=(current=='-'); | 301 | neg=(current=='-'); |
| 312 | if (current == '+' || current == '-') save_and_next(); | 302 | if (current == '+' || current == '-') save_and_next(); |
| 313 | if (!isdigit(current)) return WRONGTOKEN; | 303 | if (!isdigit(current)) { save(0); return WRONGTOKEN; } |
| 314 | do { e=10.0*e+(current-'0'); save_and_next(); } while (isdigit(current)); | 304 | do { |
| 315 | for (ea=neg?0.1:10.0; e>0; e>>=1) | 305 | e=10.0*e+(current-'0'); |
| 306 | save_and_next(); | ||
| 307 | } while (isdigit(current)); | ||
| 308 | for (ea=neg?0.1:10.0; e>0; e>>=1) | ||
| 316 | { | 309 | { |
| 317 | if (e & 1) a*=ea; | 310 | if (e & 1) a*=ea; |
| 318 | ea*=ea; | 311 | ea*=ea; |
| 319 | } | 312 | } |
| 320 | } | 313 | } |
| 321 | luaY_lval.vFloat = a; | 314 | luaY_lval.vFloat = a; |
| 315 | save(0); | ||
| 322 | return NUMBER; | 316 | return NUMBER; |
| 323 | } | 317 | } |
| 324 | 318 | ||
| @@ -330,3 +324,4 @@ fraction: | |||
| 330 | } | 324 | } |
| 331 | } | 325 | } |
| 332 | } | 326 | } |
| 327 | |||
