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

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

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

Powered by Google App Engine
This is Rietveld 408576698