Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/lexer/lexer.re

Issue 27347002: Add early exit on EOS to lexer. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Fix re2c in makefile. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/lexer/lexer.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include <fcntl.h> 1 #include <fcntl.h>
2 #include <stdio.h> 2 #include <stdio.h>
3 #include <stddef.h> 3 #include <stddef.h>
4 #include <stdlib.h> 4 #include <stdlib.h>
5 #include <string.h> 5 #include <string.h>
6 6
7 // TODO: 7 // TODO:
8 // - SpiderMonkey compatibility hack: " --> something" is treated 8 // - SpiderMonkey compatibility hack: " --> something" is treated
9 // as a single line comment. 9 // as a single line comment.
10 // - An identifier cannot start immediately after a number. 10 // - An identifier cannot start immediately after a number.
(...skipping 30 matching lines...) Expand all
41 #endif 41 #endif
42 42
43 #endif // defined(WIN32) 43 #endif // defined(WIN32)
44 44
45 #include "lexer.h" 45 #include "lexer.h"
46 46
47 using namespace v8::internal; 47 using namespace v8::internal;
48 48
49 #define PUSH_TOKEN(T) { send(T); SKIP(); } 49 #define PUSH_TOKEN(T) { send(T); SKIP(); }
50 #define PUSH_TOKEN_LOOKAHEAD(T) { --cursor_; send(T); SKIP(); } 50 #define PUSH_TOKEN_LOOKAHEAD(T) { --cursor_; send(T); SKIP(); }
51 #define PUSH_EOF_AND_RETURN() { send(Token::EOS); eof_ = true; return 1;}
51 #define PUSH_LINE_TERMINATOR() { SKIP(); } 52 #define PUSH_LINE_TERMINATOR() { SKIP(); }
52 #define TERMINATE_ILLEGAL() { return 1; } 53 #define TERMINATE_ILLEGAL() { return 1; }
53 54
54 class PushScanner { 55 class PushScanner {
55 56
56 public: 57 public:
57 PushScanner(ExperimentalScanner* sink): 58 PushScanner(ExperimentalScanner* sink):
58 eof_(false), 59 eof_(false),
59 state_(-1), 60 state_(-1),
60 condition_(kConditionNormal), 61 condition_(kConditionNormal),
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 // ^ ^ ^ ^ ^ ^ 119 // ^ ^ ^ ^ ^ ^
119 // buffer_ start_ marker_ cursor_ limit_ buffer_en d_ 120 // buffer_ start_ marker_ cursor_ limit_ buffer_en d_
120 // 121 //
121 // We need to stretch the buffer_ and concatenate the new chunk of input to it 122 // We need to stretch the buffer_ and concatenate the new chunk of input to it
122 123
123 size_t used = limit_ - buffer_; 124 size_t used = limit_ - buffer_;
124 size_t needed = used + input_size; 125 size_t needed = used + input_size;
125 size_t allocated = buffer_end_ - buffer_; 126 size_t allocated = buffer_end_ - buffer_;
126 if(allocated < needed) { 127 if(allocated < needed) {
127 size_t limit__offset = limit_ - buffer_; 128 size_t limit__offset = limit_ - buffer_;
128 size_t start__offset = start_ - buffer_; 129 size_t start_offset = start_ - buffer_;
129 size_t marker__offset = marker_ - buffer_; 130 size_t marker__offset = marker_ - buffer_;
130 size_t cursor__offset = cursor_ - buffer_; 131 size_t cursor__offset = cursor_ - buffer_;
131 132
132 buffer_ = (uint8_t*)realloc(buffer_, needed); 133 buffer_ = (uint8_t*)realloc(buffer_, needed);
133 buffer_end_ = needed + buffer_; 134 buffer_end_ = needed + buffer_;
134 135
135 marker_ = marker__offset + buffer_; 136 marker_ = marker__offset + buffer_;
136 cursor_ = cursor__offset + buffer_; 137 cursor_ = cursor__offset + buffer_;
137 start_ = buffer_ + start__offset; 138 start_ = buffer_ + start_offset;
138 limit_ = limit__offset + buffer_; 139 limit_ = limit__offset + buffer_;
139 } 140 }
140 memcpy(limit_, input, input_size); 141 memcpy(limit_, input, input_size);
141 limit_ += input_size; 142 limit_ += input_size;
142 143
143 // The scanner start_s here 144 // The scanner start_s here
144 #define YYLIMIT limit_ 145 #define YYLIMIT limit_
145 #define YYCURSOR cursor_ 146 #define YYCURSOR cursor_
146 #define YYMARKER marker_ 147 #define YYMARKER marker_
147 #define YYCTYPE uint8_t 148 #define YYCTYPE uint8_t
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 <Normal> "," { PUSH_TOKEN(Token::COMMA); } 287 <Normal> "," { PUSH_TOKEN(Token::COMMA); }
287 288
288 <Normal> line_terminator+ { PUSH_LINE_TERMINATOR(); } 289 <Normal> line_terminator+ { PUSH_LINE_TERMINATOR(); }
289 <Normal> whitespace { SKIP(); } 290 <Normal> whitespace { SKIP(); }
290 291
291 <Normal> ["] :=> DoubleQuoteString 292 <Normal> ["] :=> DoubleQuoteString
292 <Normal> ['] :=> SingleQuoteString 293 <Normal> ['] :=> SingleQuoteString
293 294
294 <Normal> identifier_start_ :=> Identifier 295 <Normal> identifier_start_ :=> Identifier
295 296
296 <Normal> eof { PUSH_TOKEN(Token::EOS); return 1; } 297 <Normal> eof { PUSH_EOF_AND_RETURN();}
297 <Normal> any { TERMINATE_ILLEGAL(); } 298 <Normal> any { TERMINATE_ILLEGAL(); }
298 299
299 <DoubleQuoteString> "\\\"" { goto yy0; } 300 <DoubleQuoteString> "\\\"" { goto yy0; }
300 <DoubleQuoteString> '"' { PUSH_TOKEN(Token::STRING);} 301 <DoubleQuoteString> '"' { PUSH_TOKEN(Token::STRING);}
301 <DoubleQuoteString> any { goto yy0; } 302 <DoubleQuoteString> any { goto yy0; }
302 303
303 <SingleQuoteString> "\\'" { goto yy0; } 304 <SingleQuoteString> "\\'" { goto yy0; }
304 <SingleQuoteString> "'" { PUSH_TOKEN(Token::STRING);} 305 <SingleQuoteString> "'" { PUSH_TOKEN(Token::STRING);}
305 <SingleQuoteString> any { goto yy0; } 306 <SingleQuoteString> any { goto yy0; }
306 307
307 <Identifier> identifier_char+ { goto yy0; } 308 <Identifier> identifier_char+ { goto yy0; }
308 <Identifier> any { PUSH_TOKEN_LOOKAHEAD(Token::IDENTIFIER); } 309 <Identifier> any { PUSH_TOKEN_LOOKAHEAD(Token::IDENTIFIER); }
309 310
310 <SingleLineComment> line_terminator { PUSH_LINE_TERMINATOR();} 311 <SingleLineComment> line_terminator { PUSH_LINE_TERMINATOR();}
311 <SingleLineComment> eof { PUSH_LINE_TERMINATOR();} 312 <SingleLineComment> eof { PUSH_LINE_TERMINATOR();}
312 <SingleLineComment> any { goto yy0; } 313 <SingleLineComment> any { goto yy0; }
313 314
314 <MultiLineComment> [*][//] { PUSH_LINE_TERMINATOR();} 315 <MultiLineComment> [*][//] { PUSH_LINE_TERMINATOR();}
315 <MultiLineComment> eof { TERMINATE_ILLEGAL(); } 316 <MultiLineComment> eof { TERMINATE_ILLEGAL(); }
316 <MultiLineComment> any { goto yy0; } 317 <MultiLineComment> any { goto yy0; }
317 318
318 <HtmlComment> eof { TERMINATE_ILLEGAL(); } 319 <HtmlComment> eof { TERMINATE_ILLEGAL(); }
319 <HtmlComment> "-->" { PUSH_LINE_TERMINATOR();} 320 <HtmlComment> "-->" { PUSH_LINE_TERMINATOR();}
320 <HtmlComment> any { goto yy0; } 321 <HtmlComment> any { goto yy0; }
321 */ 322 */
322 323
323 fill: 324 fill:
324 int unfinished_size = cursor_-start_; 325 int unfinished_size = cursor_ - start_;
325 if (FLAG_trace_lexer) { 326 if (FLAG_trace_lexer) {
326 printf( 327 printf(
327 "scanner needs a refill. Exiting for now with:\n" 328 "scanner needs a refill. Exiting for now with:\n"
328 " saved fill state_ = %d\n" 329 " saved fill state_ = %d\n"
329 " unfinished token size = %d\n", 330 " unfinished token size = %d\n",
330 state_, 331 state_,
331 unfinished_size 332 unfinished_size
332 ); 333 );
333 if(0 < unfinished_size && start_ < limit_) { 334 if(0 < unfinished_size && start_ < limit_) {
334 printf(" unfinished token is: "); 335 printf(" unfinished token is: ");
335 fwrite(start_, 1, cursor_-start_, stdout); 336 fwrite(start_, 1, cursor_ - start_, stdout);
336 putchar('\n'); 337 putchar('\n');
337 } 338 }
338 putchar('\n'); 339 putchar('\n');
339 } 340 }
340 341
341 if (eof_) goto start_; 342 if (eof_) goto start_;
342 343
343 // Once we get here, we can get rid of 344 // Once we get here, we can get rid of
344 // everything before start_ and after limit_. 345 // everything before start_ and after limit_.
345 346
346 if (buffer_ < start_) { 347 if (buffer_ < start_) {
347 size_t start__offset = start_ - buffer_; 348 size_t start_offset = start_ - buffer_;
348 memmove(buffer_, start_, limit_ - start_); 349 memmove(buffer_, start_, limit_ - start_);
349 marker_ -= start__offset; 350 marker_ -= start_offset;
350 cursor_ -= start__offset; 351 cursor_ -= start_offset;
351 limit_ -= start__offset; 352 limit_ -= start_offset;
352 start_ -= start__offset; 353 start_ -= start_offset;
353 real_start_ += start__offset; 354 real_start_ += start_offset;
354 } 355 }
355 return 0; 356 return 0;
356 } 357 }
357 358
358 private: 359 private:
359 bool eof_; 360 bool eof_;
360 int32_t state_; 361 int32_t state_;
361 int32_t condition_; 362 int32_t condition_;
362 363
363 uint8_t* limit_; 364 uint8_t* limit_;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 } 414 }
414 415
415 416
416 void ExperimentalScanner::Record(Token::Value token, int beg, int end) { 417 void ExperimentalScanner::Record(Token::Value token, int beg, int end) {
417 if (token == Token::EOS) end--; 418 if (token == Token::EOS) end--;
418 token_[fetched_] = token; 419 token_[fetched_] = token;
419 beg_[fetched_] = beg; 420 beg_[fetched_] = beg;
420 end_[fetched_] = end; 421 end_[fetched_] = end;
421 fetched_++; 422 fetched_++;
422 } 423 }
OLDNEW
« no previous file with comments | « src/lexer/lexer.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698