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

Side by Side Diff: src/ast.cc

Issue 8188: Some new regexp infrastructure. (Closed)
Patch Set: Created 12 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
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 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 11 matching lines...) Expand all
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include "v8.h" 28 #include "v8.h"
29 29
30 #include "ast.h" 30 #include "ast.h"
31 #include "scopes.h" 31 #include "scopes.h"
32 #include "string-stream.h"
32 33
33 namespace v8 { namespace internal { 34 namespace v8 { namespace internal {
34 35
35 36
36 VariableProxySentinel VariableProxySentinel::this_proxy_(true); 37 VariableProxySentinel VariableProxySentinel::this_proxy_(true);
37 VariableProxySentinel VariableProxySentinel::identifier_proxy_(false); 38 VariableProxySentinel VariableProxySentinel::identifier_proxy_(false);
38 ValidLeftHandSideSentinel ValidLeftHandSideSentinel::instance_; 39 ValidLeftHandSideSentinel ValidLeftHandSideSentinel::instance_;
39 Property Property::this_property_(VariableProxySentinel::this_proxy(), NULL, 0); 40 Property Property::this_property_(VariableProxySentinel::this_proxy(), NULL, 0);
40 Call Call::sentinel_(NULL, NULL, false, 0); 41 Call Call::sentinel_(NULL, NULL, false, 0);
41 42
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 // The variable statement visiting code may pass NULL expressions 173 // The variable statement visiting code may pass NULL expressions
173 // to this code. Maybe this should be handled by introducing an 174 // to this code. Maybe this should be handled by introducing an
174 // undefined expression or literal? Revisit this code if this 175 // undefined expression or literal? Revisit this code if this
175 // changes 176 // changes
176 Expression* expression = expressions->at(i); 177 Expression* expression = expressions->at(i);
177 if (expression != NULL) Visit(expression); 178 if (expression != NULL) Visit(expression);
178 } 179 }
179 } 180 }
180 181
181 182
183 // ----------------------------------------------------------------------------
184 // Regular expressions
185
186 #define MAKE_ACCEPT(Name) \
187 void* RegExp##Name::Accept(RegExpVisitor* visitor, void* data) { \
188 return visitor->Visit##Name(this, data); \
189 }
190 FOR_EACH_REG_EXP_NODE_TYPE(MAKE_ACCEPT)
191 #undef MAKE_ACCEPT
192
193
194 RegExpEmpty RegExpEmpty::kInstance;
195
196
197 class RegExpUnparser: public RegExpVisitor {
Lasse Reichstein 2008/10/27 13:12:58 Would be nice with a comment describing the syntax
Christian Plesner Hansen 2008/10/27 18:57:02 I can't be bothered to write the full format but I
198 public:
199 RegExpUnparser();
200 void VisitCharacterRange(CharacterRange that);
201 SmartPointer<char> ToString() { return stream_.ToCString(); }
202 #define MAKE_CASE(Name) virtual void* Visit##Name(RegExp##Name*, void* data);
203 FOR_EACH_REG_EXP_NODE_TYPE(MAKE_CASE)
204 #undef MAKE_CASE
205 private:
206 StringStream* stream() { return &stream_; }
207 HeapStringAllocator alloc_;
208 StringStream stream_;
209 };
210
211
212 RegExpUnparser::RegExpUnparser() : stream_(&alloc_) {
213 }
214
215
216 void* RegExpUnparser::VisitDisjunction(RegExpDisjunction* that, void* data) {
217 stream()->Add("(|");
218 for (int i = 0; i < that->nodes()->length(); i++) {
219 stream()->Add(" ");
220 that->nodes()->at(i)->Accept(this, data);
221 }
222 stream()->Add(")");
223 return NULL;
224 }
225
226
227 void* RegExpUnparser::VisitAlternative(RegExpAlternative* that, void* data) {
228 stream()->Add("(:");
229 for (int i = 0; i < that->nodes()->length(); i++) {
230 stream()->Add(" ");
231 that->nodes()->at(i)->Accept(this, data);
232 }
233 stream()->Add(")");
234 return NULL;
235 }
236
237
238 void RegExpUnparser::VisitCharacterRange(CharacterRange that) {
239 if (that.is_special()) {
240 stream()->Add("&%c", that.from());
241 } else if (that.IsSingleton()) {
242 stream()->Add("%c", that.from());
243 } else {
244 stream()->Add("%c-%c", that.from(), that.to());
245 }
246 }
247
248
249
250 void* RegExpUnparser::VisitCharacterClass(RegExpCharacterClass* that,
251 void* data) {
252 if (that->is_negated())
253 stream()->Add("^");
Lasse Reichstein 2008/10/27 13:12:58 Needs sqiggly quotes around the then-statement or
Christian Plesner Hansen 2008/10/27 18:57:02 Nope, the style guide allows both forms. "In gene
254 stream()->Add("[");
255 bool first = true;
256 for (int i = 0; i < that->ranges()->length(); i++) {
257 if (first) first = false;
Lasse Reichstein 2008/10/27 13:12:58 "first" is equivalent to "i == 0", so it can be om
Christian Plesner Hansen 2008/10/27 18:57:02 Good point.
258 else stream()->Add(" ");
259 VisitCharacterRange(that->ranges()->at(i));
260 }
261 stream()->Add("]");
262 return NULL;
263 }
264
265
266 void* RegExpUnparser::VisitAssertion(RegExpAssertion* that, void* data) {
267 switch (that->type()) {
268 case RegExpAssertion::START:
269 stream()->Add("@^");
270 break;
271 case RegExpAssertion::END:
272 stream()->Add("@$");
273 break;
274 case RegExpAssertion::BOUNDARY:
275 stream()->Add("@b");
276 break;
277 case RegExpAssertion::NON_BOUNDARY:
278 stream()->Add("@B");
279 break;
280 }
281 return NULL;
282 }
283
284
285 void* RegExpUnparser::VisitAtom(RegExpAtom* that, void* data) {
286 stream()->Add("'%w'", that->data());
287 return NULL;
288 }
289
290
291 void* RegExpUnparser::VisitQuantifier(RegExpQuantifier* that, void* data) {
292 stream()->Add("(# %i ", that->min());
293 if (that->max() == RegExpQuantifier::kInfinity) {
294 stream()->Add("- ");
295 } else {
296 stream()->Add("%i ", that->max());
297 }
298 stream()->Add(that->is_greedy() ? "g " : "n ");
299 that->body()->Accept(this, data);
300 stream()->Add(")");
301 return NULL;
302 }
303
304
305 void* RegExpUnparser::VisitCapture(RegExpCapture* that, void* data) {
306 stream()->Add("(^ ");
307 that->body()->Accept(this, data);
308 stream()->Add(")");
309 return NULL;
310 }
311
312
313 void* RegExpUnparser::VisitLookahead(RegExpLookahead* that, void* data) {
314 stream()->Add("(-> ");
315 stream()->Add(that->is_positive() ? "+ " : "- ");
316 that->body()->Accept(this, data);
317 stream()->Add(")");
318 return NULL;
319 }
320
321
322 void* RegExpUnparser::VisitEmpty(RegExpEmpty* that, void* data) {
323 stream()->Put('%');
324 return NULL;
325 }
326
327
328 SmartPointer<char> RegExpTree::ToString() {
329 RegExpUnparser unparser;
330 Accept(&unparser, NULL);
331 return unparser.ToString();
332 }
333
334
182 } } // namespace v8::internal 335 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698