| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #include "experimental-scanner.h" |
| 29 |
| 30 #include "lexer.h" |
| 31 |
| 32 namespace v8 { |
| 33 namespace internal { |
| 34 |
| 35 namespace { |
| 36 |
| 37 // Will go away. |
| 38 const byte* ReadFile(const char* name, Isolate* isolate, int* size) { |
| 39 FILE* file = fopen(name, "rb"); |
| 40 *size = 0; |
| 41 if (file == NULL) return NULL; |
| 42 |
| 43 fseek(file, 0, SEEK_END); |
| 44 *size = ftell(file); |
| 45 rewind(file); |
| 46 |
| 47 byte* chars = new byte[*size + 1]; |
| 48 chars[*size] = 0; |
| 49 for (int i = 0; i < *size;) { |
| 50 int read = static_cast<int>(fread(&chars[i], 1, *size - i, file)); |
| 51 i += read; |
| 52 } |
| 53 fclose(file); |
| 54 return chars; |
| 55 } |
| 56 |
| 57 } |
| 58 |
| 59 ExperimentalScanner::ExperimentalScanner(const char* fname, |
| 60 bool read_all_at_once) |
| 61 : current_(0), |
| 62 fetched_(0), |
| 63 read_all_at_once_(read_all_at_once), |
| 64 source_(0), |
| 65 length_(0) { |
| 66 file_ = fopen(fname, "rb"); |
| 67 scanner_ = new PushScanner(this); |
| 68 if (read_all_at_once_) { |
| 69 source_ = ReadFile(fname, NULL, &length_); |
| 70 token_.resize(1500); |
| 71 beg_.resize(1500); |
| 72 end_.resize(1500); |
| 73 } else { |
| 74 token_.resize(BUFFER_SIZE); |
| 75 beg_.resize(BUFFER_SIZE); |
| 76 end_.resize(BUFFER_SIZE); |
| 77 } |
| 78 } |
| 79 |
| 80 |
| 81 ExperimentalScanner::~ExperimentalScanner() { |
| 82 fclose(file_); |
| 83 delete[] source_; |
| 84 } |
| 85 |
| 86 |
| 87 void ExperimentalScanner::FillTokens() { |
| 88 current_ = 0; |
| 89 fetched_ = 0; |
| 90 if (read_all_at_once_) { |
| 91 scanner_->push(source_, length_ + 1); |
| 92 } else { |
| 93 uint8_t chars[BUFFER_SIZE]; |
| 94 int n = static_cast<int>(fread(&chars, 1, BUFFER_SIZE, file_)); |
| 95 for (int i = n; i < BUFFER_SIZE; i++) chars[i] = 0; |
| 96 scanner_->push(chars, BUFFER_SIZE); |
| 97 } |
| 98 } |
| 99 |
| 100 |
| 101 Token::Value ExperimentalScanner::Next() { |
| 102 while (current_ == fetched_) |
| 103 FillTokens(); |
| 104 return token_[current_++]; |
| 105 } |
| 106 |
| 107 |
| 108 Token::Value ExperimentalScanner::current_token() { |
| 109 return token_[current_ - 1]; |
| 110 } |
| 111 |
| 112 |
| 113 ExperimentalScanner::Location ExperimentalScanner::location() { |
| 114 return Location(beg_[current_ - 1], end_[current_ - 1]); |
| 115 } |
| 116 |
| 117 |
| 118 void ExperimentalScanner::Record(Token::Value token, int beg, int end) { |
| 119 if (token == Token::EOS) end--; |
| 120 if (fetched_ >= token_.size()) { |
| 121 token_.resize(token_.size() * 2); |
| 122 beg_.resize(beg_.size() * 2); |
| 123 end_.resize(end_.size() * 2); |
| 124 } |
| 125 token_[fetched_] = token; |
| 126 beg_[fetched_] = beg; |
| 127 end_[fetched_] = end; |
| 128 fetched_++; |
| 129 } |
| 130 |
| 131 } } // namespace v8::internal |
| OLD | NEW |