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

Side by Side Diff: src/lexer/experimental-scanner.cc

Issue 78463002: Experimental scanner: Remove the re2c version for real. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 7 years, 1 month 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/experimental-scanner.h ('k') | src/lexer/lexer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "v8.h"
31
32 #include "objects.h"
33 #include "objects-inl.h"
34 #include "spaces-inl.h"
35 #include "isolate.h"
36 #include "lexer.h"
37
38 namespace v8 {
39 namespace internal {
40
41
42 const byte* ReadFile(const char* name, Isolate* isolate,
43 int* size, int repeat) {
44 FILE* file = fopen(name, "rb");
45 *size = 0;
46 if (file == NULL) return NULL;
47
48 fseek(file, 0, SEEK_END);
49 int file_size = ftell(file);
50 rewind(file);
51
52 *size = file_size * repeat;
53
54 byte* chars = new byte[*size];
55 for (int i = 0; i < file_size;) {
56 int read = static_cast<int>(fread(&chars[i], 1, file_size - i, file));
57 i += read;
58 }
59 fclose(file);
60
61 for (int i = file_size; i < *size; i++) {
62 chars[i] = chars[i - file_size];
63 }
64
65 return chars;
66 }
67
68
69 ExperimentalScanner::ExperimentalScanner(const char* fname,
70 bool read_all_at_once,
71 Isolate* isolate,
72 int repeat)
73 : current_(0),
74 fetched_(0),
75 read_all_at_once_(read_all_at_once),
76 already_pushed_(false),
77 source_(0),
78 length_(0) {
79 CHECK(repeat == 1 || read_all_at_once);
80 file_ = fopen(fname, "rb");
81 scanner_ = new PushScanner(this, isolate->unicode_cache());
82 if (read_all_at_once_) {
83 source_ = ReadFile(fname, isolate, &length_, repeat);
84 token_.resize(length_);
85 } else {
86 token_.resize(BUFFER_SIZE);
87 }
88 }
89
90
91 ExperimentalScanner::~ExperimentalScanner() {
92 fclose(file_);
93 delete[] source_;
94 }
95
96
97 void ExperimentalScanner::FillTokens() {
98 current_ = 0;
99 fetched_ = 0;
100 if (read_all_at_once_) {
101 if (!already_pushed_) {
102 scanner_->push(source_, length_ + 1);
103 already_pushed_ = true;
104 } else {
105 scanner_->push(0, 0);
106 }
107 } else {
108 uint8_t chars[BUFFER_SIZE];
109 int n = static_cast<int>(fread(&chars, 1, BUFFER_SIZE, file_));
110 for (int i = n; i < BUFFER_SIZE; i++) chars[i] = 0;
111 scanner_->push(chars, BUFFER_SIZE);
112 }
113 }
114
115
116 Token::Value ExperimentalScanner::Next() {
117 while (current_ == fetched_)
118 FillTokens();
119 return token_[current_++].value;
120 }
121
122
123 Token::Value ExperimentalScanner::current_token() {
124 return token_[current_ - 1].value;
125 }
126
127
128 ExperimentalScanner::Location ExperimentalScanner::location() {
129 return Location(token_[current_ - 1].beg, token_[current_ - 1].end);
130 }
131
132
133 void ExperimentalScanner::Record(Token::Value token, int beg, int end) {
134 if (token == Token::EOS) end--;
135 if (fetched_ >= token_.size()) {
136 token_.resize(token_.size() * 2);
137 }
138 token_[fetched_].value = token;
139 token_[fetched_].beg = beg;
140 token_[fetched_].end = end;
141 fetched_++;
142 }
143
144 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/lexer/experimental-scanner.h ('k') | src/lexer/lexer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698