Changeset 798 in webkit for trunk/JavaScriptCore/kjs/lexer.cpp
- Timestamp:
- Mar 21, 2002, 4:31:57 PM (23 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/lexer.cpp
r6 r798 1 // -*- c-basic-offset: 2 -*- 1 2 /* 2 3 * This file is part of the KDE libraries … … 17 18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 19 * Boston, MA 02111-1307, USA. 20 * 21 * $Id$ 19 22 */ 20 23 … … 29 32 #include <assert.h> 30 33 31 #include "kjs.h" 34 #include "value.h" 35 #include "object.h" 36 #include "types.h" 37 #include "interpreter.h" 32 38 #include "nodes.h" 33 39 #include "lexer.h" … … 39 45 using namespace KJS; 40 46 47 static Lexer *currLexer = 0; 48 41 49 #ifndef KDE_USE_FINAL 42 50 #include "grammar.h" … … 45 53 #include "lexer.lut.h" 46 54 47 #ifdef KJS_DEBUGGER48 55 extern YYLTYPE yylloc; // global bison variable holding token info 49 #endif50 56 51 57 // a bridge for yacc from the C world to C++ … … 58 64 : yylineno(0), 59 65 size8(128), size16(128), restrKeyword(false), 60 stackToken(-1), pos(0),66 eatNextIdentifier(false), stackToken(-1), lastToken(-1), pos(0), 61 67 code(0), length(0), 62 68 #ifndef KJS_PURE_ECMA … … 68 74 buffer8 = new char[size8]; 69 75 buffer16 = new UChar[size16]; 76 currLexer = this; 70 77 71 78 } … … 79 86 Lexer *Lexer::curr() 80 87 { 81 assert(KJScriptImp::current()); 82 return KJScriptImp::current()->lex; 88 if (!currLexer) { 89 // create singleton instance 90 currLexer = new Lexer(); 91 } 92 return currLexer; 83 93 } 84 94 … … 88 98 restrKeyword = false; 89 99 delimited = false; 100 eatNextIdentifier = false; 90 101 stackToken = -1; 102 lastToken = -1; 91 103 pos = 0; 92 104 code = c; 93 105 length = len; 106 skipLF = false; 107 skipCR = false; 94 108 #ifndef KJS_PURE_ECMA 95 109 bol = true; … … 128 142 done = false; 129 143 terminator = false; 144 skipLF = false; 145 skipCR = false; 130 146 131 147 // did we push a token on the stack previously ? … … 138 154 139 155 while (!done) { 156 if (skipLF && current != '\n') // found \r but not \n afterwards 157 skipLF = false; 158 if (skipCR && current != '\r') // found \n but not \r afterwards 159 skipCR = false; 160 if (skipLF || skipCR) // found \r\n or \n\r -> eat the second one 161 { 162 skipLF = false; 163 skipCR = false; 164 shift(1); 165 } 140 166 switch (state) { 141 167 case Start: … … 316 342 record8(current); 317 343 state = InOctal; 344 } else if (isDecimalDigit(current)) { 345 record8(current); 346 state = InDecimal; 318 347 } else { 319 348 setDone(Number); … … 330 359 if (isOctalDigit(current)) { 331 360 record8(current); 361 } 362 else if (isDecimalDigit(current)) { 363 record8(current); 364 state = InDecimal; 332 365 } else 333 366 setDone(Octal); … … 434 467 #endif 435 468 469 if (state != Identifier && eatNextIdentifier) 470 eatNextIdentifier = false; 471 436 472 restrKeyword = false; 437 473 delimited = false; 438 #ifdef KJS_DEBUGGER439 474 yylloc.first_line = yylineno; // ??? 440 475 yylloc.last_line = yylineno; 441 #endif442 476 443 477 switch (state) { 444 478 case Eof: 445 return 0; 479 token = 0; 480 break; 446 481 case Other: 447 482 if(token == '}' || token == ';') { 448 483 delimited = true; 449 484 } 450 return token;485 break; 451 486 case Identifier: 452 487 if ((token = Lookup::find(&mainTable, buffer16, pos16)) < 0) { 488 // Lookup for keyword failed, means this is an identifier 489 // Apply anonymous-function hack below (eat the identifier) 490 if (eatNextIdentifier) { 491 eatNextIdentifier = false; 492 UString debugstr(buffer16, pos16); fprintf(stderr,"Anonymous function hack: eating identifier %s\n",debugstr.ascii()); 493 token = lex(); 494 break; 495 } 453 496 /* TODO: close leak on parse error. same holds true for String */ 454 497 kjsyylval.ustr = new UString(buffer16, pos16); 455 return IDENT; 498 token = IDENT; 499 break; 456 500 } 501 502 eatNextIdentifier = false; 503 // Hack for "f = function somename() { ... }", too hard to get into the grammar 504 if (token == FUNCTION && lastToken == '=' ) 505 eatNextIdentifier = true; 506 457 507 if (token == CONTINUE || token == BREAK || 458 508 token == RETURN || token == THROW) 459 509 restrKeyword = true; 460 return token;510 break; 461 511 case String: 462 kjsyylval.ustr = new UString(buffer16, pos16); return STRING; 512 kjsyylval.ustr = new UString(buffer16, pos16); 513 token = STRING; 514 break; 463 515 case Number: 464 516 kjsyylval.dval = dval; 465 return NUMBER; 517 token = NUMBER; 518 break; 466 519 case Bad: 467 520 fprintf(stderr, "yylex: ERROR.\n"); … … 471 524 return -1; 472 525 } 526 lastToken = token; 527 return token; 473 528 } 474 529 … … 479 534 } 480 535 481 bool Lexer::isLineTerminator() const 482 { 483 return (current == '\n' || current == '\r'); 536 bool Lexer::isLineTerminator() 537 { 538 bool cr = (current == '\r'); 539 bool lf = (current == '\n'); 540 if (cr) 541 skipLF = true; 542 else if (lf) 543 skipCR = true; 544 return cr || lf; 484 545 } 485 546 … … 541 602 } else if (c1 == '+' && c2 == '+') { 542 603 shift(2); 543 if (terminator) { 544 // automatic semicolon insertion 545 stackToken = PLUSPLUS; 546 return AUTO; 547 } else 604 if (terminator) 605 return AUTOPLUSPLUS; 606 else 548 607 return PLUSPLUS; 549 608 } else if (c1 == '-' && c2 == '-') { 550 609 shift(2); 551 if (terminator) { 552 // automatic semicolon insertion 553 stackToken = MINUSMINUS; 554 return AUTO; 555 } else 610 if (terminator) 611 return AUTOMINUSMINUS; 612 else 556 613 return MINUSMINUS; 557 614 } else if (c1 == '=' && c2 == '=') { … … 716 773 pos16 = 0; 717 774 bool lastWasEscape = false; 775 bool inBrackets = false; 718 776 719 777 while (1) { 720 778 if (isLineTerminator() || current == 0) 721 779 return false; 722 else if (current != '/' || lastWasEscape == true )780 else if (current != '/' || lastWasEscape == true || inBrackets == true) 723 781 { 782 // keep track of '[' and ']' 783 if ( !lastWasEscape ) { 784 if ( current == '[' && !inBrackets ) 785 inBrackets = true; 786 if ( current == ']' && inBrackets ) 787 inBrackets = false; 788 } 724 789 record16(current); 725 790 lastWasEscape = 726 791 !lastWasEscape && (current == '\\'); 727 792 } 728 else { 793 else { // end of regexp 729 794 pattern = UString(buffer16, pos16); 730 795 pos16 = 0;
Note:
See TracChangeset
for help on using the changeset viewer.