diff options
author | David Walter Seikel | 2012-01-12 08:58:03 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-01-12 08:58:03 +1000 |
commit | f4ca01557b072577bb6ba6dc5992d72988f13589 (patch) | |
tree | 5ddf0aff36485562a8531fe781003d7bbab0adfe | |
parent | Make the lexer more reentrant, and piss off yyerror. (diff) | |
download | SledjHamr-f4ca01557b072577bb6ba6dc5992d72988f13589.zip SledjHamr-f4ca01557b072577bb6ba6dc5992d72988f13589.tar.gz SledjHamr-f4ca01557b072577bb6ba6dc5992d72988f13589.tar.bz2 SledjHamr-f4ca01557b072577bb6ba6dc5992d72988f13589.tar.xz |
A failed attempt to handle multiple files.
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.c | 95 | ||||
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.h | 6 | ||||
-rw-r--r-- | LuaSL/src/LuaSL_lexer.l | 14 |
3 files changed, 60 insertions, 55 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c index 65ccbc8..8eed579 100644 --- a/LuaSL/src/LuaSL_LSL_tree.c +++ b/LuaSL/src/LuaSL_LSL_tree.c | |||
@@ -394,9 +394,29 @@ static void convertLeaf2Lua(FILE *file, LSL_Leaf *leaf) | |||
394 | } | 394 | } |
395 | } | 395 | } |
396 | 396 | ||
397 | int nextFile(LuaSL_yyparseExtra *extra) | ||
398 | { | ||
399 | if (NULL != extra->file) | ||
400 | fclose(extra->file); | ||
401 | if (--(extra->argc) > 0 && *++(extra->argv) != '\0') | ||
402 | { | ||
403 | strncpy(extra->fileName, *(extra->argv), PATH_MAX - 1); | ||
404 | extra->fileName[PATH_MAX - 1] = '\0'; | ||
405 | extra->file = fopen(extra->fileName, "r"); | ||
406 | if (NULL == extra->file) | ||
407 | { | ||
408 | fprintf(stderr, "Error opening file %s.\n", extra->fileName); | ||
409 | return 1; | ||
410 | } | ||
411 | printf("Opened %s.\n", extra->fileName); | ||
412 | return(0); | ||
413 | } | ||
414 | return(1); | ||
415 | } | ||
416 | |||
397 | int main(int argc, char **argv) | 417 | int main(int argc, char **argv) |
398 | { | 418 | { |
399 | char *programName = argv[0]; | 419 | // char *programName = argv[0]; |
400 | int i; | 420 | int i; |
401 | 421 | ||
402 | // Figure out what numbers yacc gave to our tokens. | 422 | // Figure out what numbers yacc gave to our tokens. |
@@ -412,9 +432,7 @@ int main(int argc, char **argv) | |||
412 | char fileName[PATH_MAX]; | 432 | char fileName[PATH_MAX]; |
413 | LuaSL_yyparseParam param; | 433 | LuaSL_yyparseParam param; |
414 | LuaSL_yyparseExtra extra; | 434 | LuaSL_yyparseExtra extra; |
415 | int file; | ||
416 | int count; | 435 | int count; |
417 | boolean badArgs = FALSE; | ||
418 | 436 | ||
419 | // Sort the token table. | 437 | // Sort the token table. |
420 | for (i = 0; LSL_Tokens[i].token != NULL; i++) | 438 | for (i = 0; LSL_Tokens[i].token != NULL; i++) |
@@ -425,44 +443,18 @@ int main(int argc, char **argv) | |||
425 | } | 443 | } |
426 | 444 | ||
427 | fileName[0] = '\0'; | 445 | fileName[0] = '\0'; |
428 | 446 | param.ast = NULL; | |
429 | // get the arguments passed in | 447 | param.lval = calloc(1, sizeof(LSL_Leaf)); |
430 | while (--argc > 0 && *++argv != '\0') | 448 | memset(&extra, 0, sizeof(extra)); |
431 | { | 449 | extra.argc = argc; |
432 | if (*argv[0] == '-') | 450 | extra.argv = argv; |
433 | { | 451 | extra.fileName = fileName; |
434 | // point to the characters after the '-' sign | 452 | extra.file = NULL; |
435 | char *s = argv[0] + 1; | 453 | // Grab the first file name, if any. |
436 | 454 | if (1 == nextFile(&extra)) | |
437 | switch (*s) | ||
438 | { | ||
439 | case 'f': // file | ||
440 | { | ||
441 | if (--argc > 0 && *++argv != '\0') | ||
442 | { | ||
443 | strncpy(fileName, *argv, PATH_MAX - 1); | ||
444 | fileName[PATH_MAX - 1] = '\0';; | ||
445 | } | ||
446 | else | ||
447 | badArgs = TRUE; | ||
448 | break; | ||
449 | } | ||
450 | default: | ||
451 | badArgs = TRUE; | ||
452 | } | ||
453 | } | ||
454 | else | ||
455 | badArgs = TRUE; | ||
456 | } | ||
457 | |||
458 | if (badArgs) | ||
459 | { | ||
460 | printf("Usage: %s [-f filename]\n", programName); | ||
461 | printf(" -f: Script file to run.\n"); | ||
462 | printf("Or pass filenames in stdin.\n"); | ||
463 | return 1; | 455 | return 1; |
464 | } | ||
465 | 456 | ||
457 | /* | ||
466 | if ('\0' == fileName[0]) | 458 | if ('\0' == fileName[0]) |
467 | { | 459 | { |
468 | //strcpy(fileName, "test.lsl"); | 460 | //strcpy(fileName, "test.lsl"); |
@@ -485,40 +477,37 @@ int main(int argc, char **argv) | |||
485 | } | 477 | } |
486 | 478 | ||
487 | } | 479 | } |
488 | else | 480 | */ |
489 | printf("Filename %s in argument.\n", fileName); | ||
490 | 481 | ||
491 | file = open(fileName, 0); | 482 | if (yylex_init_extra(&extra, &(param.scanner))) |
492 | if (-1 == file) | ||
493 | { | ||
494 | printf("Error opening file %s.\n", fileName); | ||
495 | return 1; | 483 | return 1; |
496 | } | 484 | |
497 | #ifdef LUASL_DEBUG | 485 | #ifdef LUASL_DEBUG |
498 | // yydebug= 5; | 486 | // yydebug= 5; |
499 | #endif | 487 | #endif |
500 | 488 | ||
501 | param.ast = NULL; | ||
502 | param.lval = calloc(1, sizeof(LSL_Leaf)); | ||
503 | memset(&extra, 0, sizeof(extra)); | ||
504 | if (yylex_init_extra(&extra, &(param.scanner))) | ||
505 | return 1; | ||
506 | 489 | ||
507 | #ifdef LUASL_DEBUG | 490 | #ifdef LUASL_DEBUG |
508 | yyset_debug(1, param.scanner); | 491 | yyset_debug(1, param.scanner); |
509 | #endif | 492 | #endif |
510 | // yyset_in(file, param.scanner); | ||
511 | 493 | ||
494 | #ifdef LUASL_FILES | ||
495 | yyset_in(extra.file, &(param.scanner)); | ||
496 | #endif | ||
512 | { | 497 | { |
513 | void *pParser = ParseAlloc(malloc); | 498 | void *pParser = ParseAlloc(malloc); |
514 | int yv; | 499 | int yv; |
515 | 500 | ||
516 | ParseTrace(stdout, "LSL_lemon "); | 501 | ParseTrace(stdout, "LSL_lemon "); |
517 | 502 | ||
518 | while ((i = read(file, buffer, PATH_MAX - 1)) > 0) | 503 | #ifndef LUASL_FILES |
504 | while ((i = fread(buffer, 1, PATH_MAX - 1, extra.file)) > 0) | ||
505 | #endif | ||
519 | { | 506 | { |
507 | #ifndef LUASL_FILES | ||
520 | buffer[i] = '\0'; | 508 | buffer[i] = '\0'; |
521 | yy_scan_string(buffer, param.scanner); | 509 | yy_scan_string(buffer, param.scanner); |
510 | #endif | ||
522 | // on EOF yylex will return 0 | 511 | // on EOF yylex will return 0 |
523 | while((yv = yylex(param.lval, param.scanner)) != 0) | 512 | while((yv = yylex(param.lval, param.scanner)) != 0) |
524 | { | 513 | { |
diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index 533237a..558dc8d 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h | |||
@@ -3,6 +3,7 @@ | |||
3 | #define __LSL_TREE_H__ | 3 | #define __LSL_TREE_H__ |
4 | 4 | ||
5 | #define LUASL_DEBUG | 5 | #define LUASL_DEBUG |
6 | //#define LUASL_FILES | ||
6 | 7 | ||
7 | 8 | ||
8 | #include <stddef.h> // So we can have NULL defined. | 9 | #include <stddef.h> // So we can have NULL defined. |
@@ -173,6 +174,10 @@ typedef struct | |||
173 | char *ignorableText; | 174 | char *ignorableText; |
174 | int column; | 175 | int column; |
175 | int line; | 176 | int line; |
177 | int argc; | ||
178 | char **argv; | ||
179 | char *fileName; | ||
180 | FILE *file; | ||
176 | } LuaSL_yyparseExtra; | 181 | } LuaSL_yyparseExtra; |
177 | 182 | ||
178 | 183 | ||
@@ -201,6 +206,7 @@ typedef struct | |||
201 | //#define ParseARG_PDECL , LuaSL_yyparseParam *param | 206 | //#define ParseARG_PDECL , LuaSL_yyparseParam *param |
202 | 207 | ||
203 | void burnLeaf(LSL_Leaf *leaf); | 208 | void burnLeaf(LSL_Leaf *leaf); |
209 | int nextFile(LuaSL_yyparseExtra *extra); | ||
204 | LSL_Leaf *addExpression(LSL_Leaf *exp); | 210 | LSL_Leaf *addExpression(LSL_Leaf *exp); |
205 | LSL_Leaf *addOperation(LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right); | 211 | LSL_Leaf *addOperation(LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right); |
206 | LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Leaf *rval); | 212 | LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Leaf *rval); |
diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l index b4a1444..93df410 100644 --- a/LuaSL/src/LuaSL_lexer.l +++ b/LuaSL/src/LuaSL_lexer.l | |||
@@ -151,6 +151,11 @@ int common(YYSTYPE *lval, char *text, LuaSL_yyparseExtra *extra, boolean checkIg | |||
151 | 151 | ||
152 | int yywrap(yyscan_t yyscanner) | 152 | int yywrap(yyscan_t yyscanner) |
153 | { | 153 | { |
154 | #ifdef LUASL_FILES | ||
155 | LuaSL_yyparseExtra *extra = yyget_extra(yyscanner); | ||
156 | #endif | ||
157 | int result = 1; | ||
158 | |||
154 | #ifdef FLEX_SCANNER | 159 | #ifdef FLEX_SCANNER |
155 | #ifndef LL_WINDOWS | 160 | #ifndef LL_WINDOWS |
156 | // Get gcc to stop complaining about lack of use of yyunput and input. | 161 | // Get gcc to stop complaining about lack of use of yyunput and input. |
@@ -158,7 +163,12 @@ int yywrap(yyscan_t yyscanner) | |||
158 | (void) input; | 163 | (void) input; |
159 | #endif | 164 | #endif |
160 | #endif | 165 | #endif |
161 | // TODO - If we are getting files from stdin, or multiple -f arguments, we should loop through them and return 0. Return 1 when there are no more files. | 166 | |
162 | return(1); | 167 | #ifdef LUASL_FILES |
168 | result = nextFile(extra); | ||
169 | if (0 == result) | ||
170 | yyset_in(extra->file, yyscanner); | ||
171 | #endif | ||
172 | return result; | ||
163 | } | 173 | } |
164 | 174 | ||