| OLD | NEW |
| 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 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 } | 180 } |
| 181 | 181 |
| 182 | 182 |
| 183 // ---------------------------------------------------------------------------- | 183 // ---------------------------------------------------------------------------- |
| 184 // Regular expressions | 184 // Regular expressions |
| 185 | 185 |
| 186 #define MAKE_ACCEPT(Name) \ | 186 #define MAKE_ACCEPT(Name) \ |
| 187 void* RegExp##Name::Accept(RegExpVisitor* visitor, void* data) { \ | 187 void* RegExp##Name::Accept(RegExpVisitor* visitor, void* data) { \ |
| 188 return visitor->Visit##Name(this, data); \ | 188 return visitor->Visit##Name(this, data); \ |
| 189 } | 189 } |
| 190 FOR_EACH_REG_EXP_NODE_TYPE(MAKE_ACCEPT) | 190 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ACCEPT) |
| 191 #undef MAKE_ACCEPT | 191 #undef MAKE_ACCEPT |
| 192 | 192 |
| 193 #define MAKE_CONVERSION(Name) \ | 193 #define MAKE_CONVERSION(Name) \ |
| 194 RegExp##Name* RegExpTree::As##Name() { \ | 194 RegExp##Name* RegExpTree::As##Name() { \ |
| 195 return NULL; \ | 195 return NULL; \ |
| 196 } | 196 } |
| 197 FOR_EACH_REG_EXP_NODE_TYPE(MAKE_CONVERSION) | 197 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CONVERSION) |
| 198 #undef MAKE_CONVERSION | 198 #undef MAKE_CONVERSION |
| 199 | 199 |
| 200 #define MAKE_CONVERSION(Name) \ | 200 #define MAKE_CONVERSION(Name) \ |
| 201 RegExp##Name* RegExp##Name::As##Name() { \ | 201 RegExp##Name* RegExp##Name::As##Name() { \ |
| 202 return this; \ | 202 return this; \ |
| 203 } | 203 } |
| 204 FOR_EACH_REG_EXP_NODE_TYPE(MAKE_CONVERSION) | 204 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CONVERSION) |
| 205 #undef MAKE_CONVERSION | 205 #undef MAKE_CONVERSION |
| 206 | 206 |
| 207 RegExpEmpty RegExpEmpty::kInstance; | 207 RegExpEmpty RegExpEmpty::kInstance; |
| 208 | 208 |
| 209 | 209 |
| 210 // Convert regular expression trees to a simple sexp representation. | 210 // Convert regular expression trees to a simple sexp representation. |
| 211 // This representation should be different from the input grammar | 211 // This representation should be different from the input grammar |
| 212 // in as many cases as possible, to make it more difficult for incorrect | 212 // in as many cases as possible, to make it more difficult for incorrect |
| 213 // parses to look as correct ones which is likely if the input and | 213 // parses to look as correct ones which is likely if the input and |
| 214 // output formats are alike. | 214 // output formats are alike. |
| 215 class RegExpUnparser: public RegExpVisitor { | 215 class RegExpUnparser: public RegExpVisitor { |
| 216 public: | 216 public: |
| 217 RegExpUnparser(); | 217 RegExpUnparser(); |
| 218 void VisitCharacterRange(CharacterRange that); | 218 void VisitCharacterRange(CharacterRange that); |
| 219 SmartPointer<const char> ToString() { return stream_.ToCString(); } | 219 SmartPointer<const char> ToString() { return stream_.ToCString(); } |
| 220 #define MAKE_CASE(Name) virtual void* Visit##Name(RegExp##Name*, void* data); | 220 #define MAKE_CASE(Name) virtual void* Visit##Name(RegExp##Name*, void* data); |
| 221 FOR_EACH_REG_EXP_NODE_TYPE(MAKE_CASE) | 221 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE) |
| 222 #undef MAKE_CASE | 222 #undef MAKE_CASE |
| 223 private: | 223 private: |
| 224 StringStream* stream() { return &stream_; } | 224 StringStream* stream() { return &stream_; } |
| 225 HeapStringAllocator alloc_; | 225 HeapStringAllocator alloc_; |
| 226 StringStream stream_; | 226 StringStream stream_; |
| 227 }; | 227 }; |
| 228 | 228 |
| 229 | 229 |
| 230 RegExpUnparser::RegExpUnparser() : stream_(&alloc_) { | 230 RegExpUnparser::RegExpUnparser() : stream_(&alloc_) { |
| 231 } | 231 } |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 return NULL; | 301 return NULL; |
| 302 } | 302 } |
| 303 | 303 |
| 304 | 304 |
| 305 void* RegExpUnparser::VisitAtom(RegExpAtom* that, void* data) { | 305 void* RegExpUnparser::VisitAtom(RegExpAtom* that, void* data) { |
| 306 stream()->Add("'%w'", that->data()); | 306 stream()->Add("'%w'", that->data()); |
| 307 return NULL; | 307 return NULL; |
| 308 } | 308 } |
| 309 | 309 |
| 310 | 310 |
| 311 void* RegExpUnparser::VisitText(RegExpText* that, void* data) { |
| 312 if (that->elements()->length() == 1) { |
| 313 that->elements()->at(0).data.u_atom->Accept(this, data); |
| 314 } else { |
| 315 stream()->Add("(!"); |
| 316 for (int i = 0; i < that->elements()->length(); i++) { |
| 317 stream()->Add(" "); |
| 318 that->elements()->at(i).data.u_atom->Accept(this, data); |
| 319 } |
| 320 stream()->Add(")"); |
| 321 } |
| 322 return NULL; |
| 323 } |
| 324 |
| 325 |
| 311 void* RegExpUnparser::VisitQuantifier(RegExpQuantifier* that, void* data) { | 326 void* RegExpUnparser::VisitQuantifier(RegExpQuantifier* that, void* data) { |
| 312 stream()->Add("(# %i ", that->min()); | 327 stream()->Add("(# %i ", that->min()); |
| 313 if (that->max() == RegExpQuantifier::kInfinity) { | 328 if (that->max() == RegExpQuantifier::kInfinity) { |
| 314 stream()->Add("- "); | 329 stream()->Add("- "); |
| 315 } else { | 330 } else { |
| 316 stream()->Add("%i ", that->max()); | 331 stream()->Add("%i ", that->max()); |
| 317 } | 332 } |
| 318 stream()->Add(that->is_greedy() ? "g " : "n "); | 333 stream()->Add(that->is_greedy() ? "g " : "n "); |
| 319 that->body()->Accept(this, data); | 334 that->body()->Accept(this, data); |
| 320 stream()->Add(")"); | 335 stream()->Add(")"); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 | 368 |
| 354 | 369 |
| 355 SmartPointer<const char> RegExpTree::ToString() { | 370 SmartPointer<const char> RegExpTree::ToString() { |
| 356 RegExpUnparser unparser; | 371 RegExpUnparser unparser; |
| 357 Accept(&unparser, NULL); | 372 Accept(&unparser, NULL); |
| 358 return unparser.ToString(); | 373 return unparser.ToString(); |
| 359 } | 374 } |
| 360 | 375 |
| 361 | 376 |
| 362 } } // namespace v8::internal | 377 } } // namespace v8::internal |
| OLD | NEW |