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

Side by Side Diff: src/lexer/lexer-shell.cc

Issue 28763003: Experimental parser: Starting to unify the ExperimentalScanner iface with Scanner iface. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: 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.re ('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 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 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 24 matching lines...) Expand all
35 #include "api.h" 35 #include "api.h"
36 #include "ast.h" 36 #include "ast.h"
37 #include "char-predicates-inl.h" 37 #include "char-predicates-inl.h"
38 #include "messages.h" 38 #include "messages.h"
39 #include "platform.h" 39 #include "platform.h"
40 #include "runtime.h" 40 #include "runtime.h"
41 #include "scanner-character-streams.h" 41 #include "scanner-character-streams.h"
42 #include "scopeinfo.h" 42 #include "scopeinfo.h"
43 #include "string-stream.h" 43 #include "string-stream.h"
44 #include "scanner.h" 44 #include "scanner.h"
45
46 #include "experimental-scanner.h"
45 #include "lexer.h" 47 #include "lexer.h"
46 48
47 using namespace v8::internal; 49 using namespace v8::internal;
48 50
49 const byte* ReadFile(const char* name, Isolate* isolate, int* size) { 51 const byte* ReadFile(const char* name, Isolate* isolate, int* size) {
50 FILE* file = fopen(name, "rb"); 52 FILE* file = fopen(name, "rb");
51 *size = 0; 53 *size = 0;
52 if (file == NULL) return NULL; 54 if (file == NULL) return NULL;
53 55
54 fseek(file, 0, SEEK_END); 56 fseek(file, 0, SEEK_END);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 return res; 93 return res;
92 } 94 }
93 95
94 private: 96 private:
95 UnicodeCache* unicode_cache_; 97 UnicodeCache* unicode_cache_;
96 Scanner* scanner_; 98 Scanner* scanner_;
97 const byte* source_; 99 const byte* source_;
98 Utf8ToUtf16CharacterStream* stream_; 100 Utf8ToUtf16CharacterStream* stream_;
99 }; 101 };
100 102
101 ExperimentalScanner::ExperimentalScanner(const char* fname,
102 bool read_all_at_once)
103 : current_(0),
104 fetched_(0),
105 read_all_at_once_(read_all_at_once),
106 source_(0),
107 length_(0) {
108 file_ = fopen(fname, "rb");
109 scanner_ = new PushScanner(this);
110 if (read_all_at_once_) {
111 source_ = ReadFile(fname, NULL, &length_);
112 token_.resize(1500);
113 beg_.resize(1500);
114 end_.resize(1500);
115 } else {
116 token_.resize(BUFFER_SIZE);
117 beg_.resize(BUFFER_SIZE);
118 end_.resize(BUFFER_SIZE);
119 }
120 }
121
122
123 ExperimentalScanner::~ExperimentalScanner() {
124 fclose(file_);
125 delete[] source_;
126 }
127
128
129 void ExperimentalScanner::FillTokens() {
130 current_ = 0;
131 fetched_ = 0;
132 if (read_all_at_once_) {
133 scanner_->push(source_, length_ + 1);
134 } else {
135 uint8_t chars[BUFFER_SIZE];
136 int n = static_cast<int>(fread(&chars, 1, BUFFER_SIZE, file_));
137 for (int i = n; i < BUFFER_SIZE; i++) chars[i] = 0;
138 scanner_->push(chars, BUFFER_SIZE);
139 }
140 }
141
142
143 Token::Value ExperimentalScanner::Next(int* beg_pos, int* end_pos) {
144 while (current_ == fetched_)
145 FillTokens();
146 *beg_pos = beg_[current_];
147 *end_pos = end_[current_];
148 Token::Value res = token_[current_];
149 if (res != Token::Token::EOS)
150 current_++;
151 return res;
152 }
153
154
155 void ExperimentalScanner::Record(Token::Value token, int beg, int end) {
156 if (token == Token::EOS) end--;
157 if (fetched_ >= token_.size()) {
158 token_.resize(token_.size() * 2);
159 beg_.resize(beg_.size() * 2);
160 end_.resize(end_.size() * 2);
161 }
162 token_[fetched_] = token;
163 beg_[fetched_] = beg;
164 end_[fetched_] = end;
165 fetched_++;
166 }
167
168 103
169 int main(int argc, char* argv[]) { 104 int main(int argc, char* argv[]) {
170 v8::V8::InitializeICU(); 105 v8::V8::InitializeICU();
171 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); 106 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
172 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 107 v8::Isolate* isolate = v8::Isolate::GetCurrent();
173 { 108 {
174 v8::HandleScope handle_scope(isolate); 109 v8::HandleScope handle_scope(isolate);
175 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); 110 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
176 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); 111 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);
177 ASSERT(!context.IsEmpty()); 112 ASSERT(!context.IsEmpty());
(...skipping 19 matching lines...) Expand all
197 baseline_tokens.push_back(token); 132 baseline_tokens.push_back(token);
198 baseline_beg.push_back(beg); 133 baseline_beg.push_back(beg);
199 baseline_end.push_back(end); 134 baseline_end.push_back(end);
200 } while (token != Token::EOS); 135 } while (token != Token::EOS);
201 baseline_time = timer.Elapsed(); 136 baseline_time = timer.Elapsed();
202 } 137 }
203 138
204 { 139 {
205 timer.Start(); 140 timer.Start();
206 do { 141 do {
207 token = experimental.Next(&beg, &end); 142 token = experimental.Next();
208 experimental_tokens.push_back(token); 143 experimental_tokens.push_back(token);
209 experimental_beg.push_back(beg); 144 ExperimentalScanner::Location location = experimental.location();
210 experimental_end.push_back(end); 145 experimental_beg.push_back(location.beg_pos);
146 experimental_end.push_back(location.end_pos);
211 } while (token != Token::EOS); 147 } while (token != Token::EOS);
212 experimental_time = timer.Elapsed(); 148 experimental_time = timer.Elapsed();
213 } 149 }
214 150
215 for (size_t i = 0; i < experimental_tokens.size(); ++i) { 151 for (size_t i = 0; i < experimental_tokens.size(); ++i) {
216 printf("=> %11s at (%d, %d)\n", 152 printf("=> %11s at (%d, %d)\n",
217 Token::Name(experimental_tokens[i]), 153 Token::Name(experimental_tokens[i]),
218 experimental_beg[i], experimental_end[i]); 154 experimental_beg[i], experimental_end[i]);
219 if (experimental_tokens[i] != baseline_tokens[i] || 155 if (experimental_tokens[i] != baseline_tokens[i] ||
220 experimental_beg[i] != baseline_beg[i] || 156 experimental_beg[i] != baseline_beg[i] ||
(...skipping 10 matching lines...) Expand all
231 } 167 }
232 printf("No of tokens: %d\n", experimental_tokens.size()); 168 printf("No of tokens: %d\n", experimental_tokens.size());
233 printf("Baseline: %f ms\nExperimental %f ms\n", 169 printf("Baseline: %f ms\nExperimental %f ms\n",
234 baseline_time.InMillisecondsF(), 170 baseline_time.InMillisecondsF(),
235 experimental_time.InMillisecondsF()); 171 experimental_time.InMillisecondsF());
236 } 172 }
237 } 173 }
238 v8::V8::Dispose(); 174 v8::V8::Dispose();
239 return 0; 175 return 0;
240 } 176 }
OLDNEW
« no previous file with comments | « src/lexer/lexer.re ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698