OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 21 matching lines...) Expand all Loading... |
32 #include "utils.h" | 32 #include "utils.h" |
33 #include "list.h" | 33 #include "list.h" |
34 #include "scanner-base.h" | 34 #include "scanner-base.h" |
35 #include "preparse-data.h" | 35 #include "preparse-data.h" |
36 #include "preparser.h" | 36 #include "preparser.h" |
37 | 37 |
38 namespace v8 { | 38 namespace v8 { |
39 namespace internal { | 39 namespace internal { |
40 | 40 |
41 // UTF16Buffer based on a v8::UnicodeInputStream. | 41 // UTF16Buffer based on a v8::UnicodeInputStream. |
42 class InputStreamUTF16Buffer : public UTF16Buffer { | 42 class InputStreamUTF16Buffer : public UC16CharacterStream { |
43 public: | 43 public: |
44 explicit InputStreamUTF16Buffer(UnicodeInputStream* stream) | 44 explicit InputStreamUTF16Buffer(v8::UnicodeInputStream* stream) |
45 : UTF16Buffer(), | 45 : UC16CharacterStream(), |
46 stream_(stream) { } | 46 stream_(stream), |
| 47 pushback_active_(false) { |
| 48 buffer_cursor_ = buffer_end_ = buffer_ + kPushBackSize; |
| 49 } |
47 | 50 |
48 virtual ~InputStreamUTF16Buffer() { } | 51 virtual ~InputStreamUTF16Buffer() { } |
49 | 52 |
50 virtual void PushBack(uc32 ch) { | 53 virtual void PushBack(uc16 ch) { |
| 54 ASSERT(pos_ > 0); |
| 55 if (buffer_cursor_ > buffer_) { |
| 56 // While we can stay within the buffer, just do so. |
| 57 *--buffer_cursor_ = ch; |
| 58 pos_--; |
| 59 return; |
| 60 } |
| 61 if (!pushback_active_) { |
| 62 // Push back the entire buffer to the stream and let the |
| 63 // stream handle pushbacks from now. |
| 64 // We leave buffer_cursor_ == buffer_end_, so the next read |
| 65 // will fill the buffer from the current position. |
| 66 // This should happen exceedingly rarely. |
| 67 while (buffer_end_ > buffer_) { |
| 68 stream_->PushBack(*--buffer_end_); |
| 69 } |
| 70 buffer_cursor_ = buffer_end_; |
| 71 pushback_active_ = true; |
| 72 } |
51 stream_->PushBack(ch); | 73 stream_->PushBack(ch); |
52 pos_--; | 74 pos_--; |
53 } | 75 } |
54 | 76 |
55 virtual uc32 Advance() { | 77 protected: |
56 uc32 result = stream_->Next(); | 78 virtual bool ReadBlock() { |
57 if (result >= 0) pos_++; | 79 // Copy the top of the buffer into the pushback area. |
58 return result; | 80 pushback_active_ = false; |
| 81 int32_t value; |
| 82 uc16* buffer_start = buffer_ + kPushBackSize; |
| 83 buffer_cursor_ = buffer_end_ = buffer_start; |
| 84 while ((value = stream_->Next()) >= 0) { |
| 85 if (value > static_cast<int32_t>(unibrow::Utf8::kMaxThreeByteChar)) { |
| 86 value = unibrow::Utf8::kBadChar; |
| 87 } |
| 88 // buffer_end_ is a const pointer, but buffer_ is writable. |
| 89 buffer_start[buffer_end_++ - buffer_start] = static_cast<uc16>(value); |
| 90 if (buffer_end_ == buffer_ + kPushBackSize + kBufferSize) break; |
| 91 } |
| 92 return buffer_end_ > buffer_start; |
59 } | 93 } |
60 | 94 |
61 virtual void SeekForward(int pos) { | 95 virtual unsigned SlowSeekForward(unsigned pos) { |
62 // Seeking in the input is not used by preparsing. | 96 // Seeking in the input is not used by preparsing. |
63 // It's only used by the real parser based on preparser data. | 97 // It's only used by the real parser based on preparser data. |
64 UNIMPLEMENTED(); | 98 UNIMPLEMENTED(); |
| 99 return 0; |
65 } | 100 } |
66 | 101 |
67 private: | 102 private: |
| 103 static const unsigned kBufferSize = 512; |
| 104 static const unsigned kPushBackSize = 16; |
68 v8::UnicodeInputStream* const stream_; | 105 v8::UnicodeInputStream* const stream_; |
| 106 // Buffer holding first kPushBackSize characters of pushback buffer, |
| 107 // then kBufferSize chars of read-ahead. |
| 108 // The pushback buffer is only used if pushing back characters past |
| 109 // the start of a block. |
| 110 uc16 buffer_[kBufferSize + kPushBackSize]; |
| 111 bool pushback_active_; |
69 }; | 112 }; |
70 | 113 |
71 | 114 |
72 class StandAloneJavaScriptScanner : public JavaScriptScanner { | 115 class StandAloneJavaScriptScanner : public JavaScriptScanner { |
73 public: | 116 public: |
74 void Initialize(UTF16Buffer* source) { | 117 void Initialize(UC16CharacterStream* source) { |
75 source_ = source; | 118 source_ = source; |
76 literal_flags_ = kLiteralString | kLiteralIdentifier; | 119 literal_flags_ = kLiteralString | kLiteralIdentifier; |
77 Init(); | 120 Init(); |
78 // Skip initial whitespace allowing HTML comment ends just like | 121 // Skip initial whitespace allowing HTML comment ends just like |
79 // after a newline and scan first token. | 122 // after a newline and scan first token. |
80 has_line_terminator_before_next_ = true; | 123 has_line_terminator_before_next_ = true; |
81 SkipWhiteSpace(); | 124 SkipWhiteSpace(); |
82 Scan(); | 125 Scan(); |
83 } | 126 } |
84 }; | 127 }; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 return PreParserData(size, data); | 162 return PreParserData(size, data); |
120 } | 163 } |
121 | 164 |
122 } // namespace v8. | 165 } // namespace v8. |
123 | 166 |
124 | 167 |
125 // Used by ASSERT macros and other immediate exits. | 168 // Used by ASSERT macros and other immediate exits. |
126 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) { | 169 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) { |
127 exit(EXIT_FAILURE); | 170 exit(EXIT_FAILURE); |
128 } | 171 } |
OLD | NEW |