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

Side by Side Diff: preparser/preparser-process.cc

Issue 5302003: Working stand-alone preparser. (Closed)
Patch Set: Created 10 years 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
« no previous file with comments | « no previous file | src/checks.h » ('j') | src/v8checks.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2010 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 <stdio.h>
ricow1 2010/11/24 09:09:33 stdio.h included through ../include/v8stdint.h
Lasse Reichstein 2010/11/24 09:39:47 Removed.
29 #include <stdarg.h>
30 #include "../include/v8stdint.h"
31 #include "globals.h"
32 #include "checks.h"
33 #include "allocation.h"
34 #include "utils.h"
35 #include "list.h"
36 #include "smart-pointer.h"
37 #include "scanner-base.h"
38 #include "preparse-data.h"
39 #include "preparser.h"
40
41 namespace v8 {
42 namespace internal {
43
44 // UTF16Buffer based on an UTF-8 string in memory.
45 class UTF8UTF16Buffer : public UTF16Buffer {
46 public:
47 UTF8UTF16Buffer(uint8_t* buffer, size_t length)
48 : UTF16Buffer(),
49 buffer_(buffer),
50 offset_(0),
51 end_offset_(static_cast<int>(length)) { }
52
53 virtual void PushBack(uc32 ch) {
54 // Pushback assumes that the character pushed back is the
55 // one that was most recently read, and jumps back in the
56 // UTF-8 stream by the length of that character's encoding.
57 offset_ -= unibrow::Utf8::Length(ch);
58 pos_--;
59 #ifdef DEBUG
60 int tmp = 0;
61 ASSERT_EQ(ch, unibrow::Utf8::ValueOf(buffer_ + offset_,
62 end_offset_ - offset_,
63 &tmp);
64 #endif
65 }
66
67 virtual uc32 Advance() {
68 if (offset_ == end_offset_) return -1;
69 uint32_t character_count = 0;
70 uint8_t first_char = buffer_[offset_];
71 if (first_char <= unibrow::Utf8::kMaxOneByteChar) {
72 pos_++;
73 offset_++;
74 return static_cast<uc32>(first_char);
75 }
76 unibrow::uchar codepoint =
77 unibrow::Utf8::CalculateValue(buffer_ + offset_,
78 end_offset_ - offset_,
79 &offset_);
80 pos_++;
81 return static_cast<uc32>(codepoint);
82 }
83
84 virtual void SeekForward(int pos) {
85 while (pos_ < pos) {
86 uint8_t first_byte = buffer_[offset_++];
87 while (first_byte & 0x80u && offset_ < end_offset_) {
88 offset_++;
89 first_byte <<= 1;
90 }
91 pos_++;
92 }
93 }
94
95 private:
96 const uint8_t* buffer_;
97 unsigned offset_;
98 unsigned end_offset_;
99 };
100
101
102 class StandAloneJavaScriptScanner : public JavaScriptScanner {
103 public:
104 void Initialize(UTF16Buffer* source) {
105 source_ = source;
106 literal_flags_ = kLiteralString | kLiteralIdentifier;
107 Init();
108 // Skip initial whitespace allowing HTML comment ends just like
109 // after a newline and scan first token.
110 has_line_terminator_before_next_ = true;
111 SkipWhiteSpace();
112 Scan();
113 }
114 };
115
116
ricow1 2010/11/24 09:09:33 Add comment similar to ReadUInt32
Lasse Reichstein 2010/11/24 09:39:47 Done.
117 void WriteUInt32(FILE* dest, uint32_t value, bool* ok) {
118 for (int i = 3; i >= 0; i--) {
119 uint8_t byte = static_cast<uint8_t>(value >> (i << 3));
120 int result = fputc(byte, dest);
121 if (result == EOF) {
122 *ok = false;
123 return;
124 }
125 }
126 }
127
128 // Read number from FILE* in network byte order.
129 uint32_t ReadUInt32(FILE* source, bool* ok) {
130 uint32_t n = 0;
131 for (int i = 0; i < 4; i++) {
132 int c = fgetc(source);
133 if (c == EOF) {
134 *ok = false;
135 return 0;
136 }
137 n = (n << 8) + static_cast<uint32_t>(c);
138 }
139 return n;
140 }
141
142
143 bool ReadBuffer(FILE* source, void* buffer, size_t length) {
144 size_t actually_read = fread(buffer, 1, length, stdin);
145 return (actually_read == length);
146 }
147
148
149 bool WriteBuffer(FILE* dest, void* buffer, size_t length) {
150 size_t actually_written = fwrite(buffer, 1, length, dest);
151 return (actually_written == length);
152 }
153
154 // Preparse stdin and output result on stdout.
155 int PreParseIO() {
ricow1 2010/11/24 09:09:33 What is the meaning of the exit values? Are these
Lasse Reichstein 2010/11/24 09:39:47 No big meaning. 1 is error reading, 2 is error wri
156 fprintf(stderr, "LOG: Enter parsing loop\n");
157 bool ok = true;
158 uint32_t length = ReadUInt32(stdin, &ok);
159 if (!ok) return 1;
160 SmartPointer<byte> buffer(NewArray<byte>(length));
161 if (!ReadBuffer(stdin, *buffer, length)) {
162 return 1;
163 }
164 UTF8UTF16Buffer input_buffer(*buffer, static_cast<size_t>(length));
165 StandAloneJavaScriptScanner scanner;
166 scanner.Initialize(&input_buffer);
167 CompleteParserRecorder recorder;
168 preparser::PreParser preparser;
169
170 if (!preparser.PreParseProgram(&scanner, &recorder, true)) {
171 if (scanner.stack_overflow()) {
172 // Report stack overflow error/no-preparser-data.
173 WriteUInt32(stdout, 0, &ok);
174 if (!ok) return 2;
175 return 0;
176 }
177 }
178 Vector<unsigned> pre_data = recorder.ExtractData();
179
180 uint32_t size = static_cast<uint32_t>(pre_data.length() * sizeof(uint32_t));
181 WriteUInt32(stdout, length, &ok);
182 if (!ok) return 2;
183 if (!WriteBuffer(stdout,
184 reinterpret_cast<byte*>(pre_data.start()),
185 static_cast<size_t>(length))) {
186 return 2;
187 }
188 return 0;
189 }
190
191 // Functions declared by allocation.h
192
193 void FatalProcessOutOfMemory(const char* location) {
194 V8_Fatal("", 0, location);
195 }
196
197 bool EnableSlowAsserts() { return true; }
198
199 } } // namespace v8::internal
200
201
202 int main(int argc, char* argv[]) {
203 int status = 0;
204 do {
205 status = v8::internal::PreParseIO();
206 } while (status == 0);
207 fprintf(stderr, "EXIT: Failure %d\n", status);
208 return EXIT_FAILURE;
209 }
210
211
212 // Fatal error handling declared by checks.h.
213
214 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) {
215 fflush(stdout);
216 fflush(stderr);
217 va_list arguments;
218 va_start(arguments, format);
219 vfprintf(stderr, format, arguments);
220 va_end(arguments);
221 fputs("\n#\n\n", stderr);
222 exit(4);
223 }
OLDNEW
« no previous file with comments | « no previous file | src/checks.h » ('j') | src/v8checks.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698