| 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 11 matching lines...) Expand all Loading... |
| 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, 0); |
| 42 CallEval CallEval::sentinel_(NULL, NULL, 0); |
| 41 | 43 |
| 42 | 44 |
| 43 // ---------------------------------------------------------------------------- | 45 // ---------------------------------------------------------------------------- |
| 44 // All the Accept member functions for each syntax tree node type. | 46 // All the Accept member functions for each syntax tree node type. |
| 45 | 47 |
| 46 #define DECL_ACCEPT(type) \ | 48 #define DECL_ACCEPT(type) \ |
| 47 void type::Accept(Visitor* v) { \ | 49 void type::Accept(AstVisitor* v) { \ |
| 48 if (v->CheckStackOverflow()) return; \ | 50 if (v->CheckStackOverflow()) return; \ |
| 49 v->Visit##type(this); \ | 51 v->Visit##type(this); \ |
| 50 } | 52 } |
| 51 NODE_LIST(DECL_ACCEPT) | 53 NODE_LIST(DECL_ACCEPT) |
| 52 #undef DECL_ACCEPT | 54 #undef DECL_ACCEPT |
| 53 | 55 |
| 54 | 56 |
| 55 // ---------------------------------------------------------------------------- | 57 // ---------------------------------------------------------------------------- |
| 56 // Implementation of other node functionality. | 58 // Implementation of other node functionality. |
| 57 | 59 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 // Add the label to the collector, but discard duplicates. | 152 // Add the label to the collector, but discard duplicates. |
| 151 int length = targets_->length(); | 153 int length = targets_->length(); |
| 152 for (int i = 0; i < length; i++) { | 154 for (int i = 0; i < length; i++) { |
| 153 if (targets_->at(i) == target) return; | 155 if (targets_->at(i) == target) return; |
| 154 } | 156 } |
| 155 targets_->Add(target); | 157 targets_->Add(target); |
| 156 } | 158 } |
| 157 | 159 |
| 158 | 160 |
| 159 // ---------------------------------------------------------------------------- | 161 // ---------------------------------------------------------------------------- |
| 160 // Implementation of Visitor | 162 // Implementation of AstVisitor |
| 161 | 163 |
| 162 | 164 |
| 163 void Visitor::VisitStatements(ZoneList<Statement*>* statements) { | 165 void AstVisitor::VisitStatements(ZoneList<Statement*>* statements) { |
| 164 for (int i = 0; i < statements->length(); i++) { | 166 for (int i = 0; i < statements->length(); i++) { |
| 165 Visit(statements->at(i)); | 167 Visit(statements->at(i)); |
| 166 } | 168 } |
| 167 } | 169 } |
| 168 | 170 |
| 169 | 171 |
| 170 void Visitor::VisitExpressions(ZoneList<Expression*>* expressions) { | 172 void AstVisitor::VisitExpressions(ZoneList<Expression*>* expressions) { |
| 171 for (int i = 0; i < expressions->length(); i++) { | 173 for (int i = 0; i < expressions->length(); i++) { |
| 172 // The variable statement visiting code may pass NULL expressions | 174 // The variable statement visiting code may pass NULL expressions |
| 173 // to this code. Maybe this should be handled by introducing an | 175 // to this code. Maybe this should be handled by introducing an |
| 174 // undefined expression or literal? Revisit this code if this | 176 // undefined expression or literal? Revisit this code if this |
| 175 // changes | 177 // changes |
| 176 Expression* expression = expressions->at(i); | 178 Expression* expression = expressions->at(i); |
| 177 if (expression != NULL) Visit(expression); | 179 if (expression != NULL) Visit(expression); |
| 178 } | 180 } |
| 179 } | 181 } |
| 180 | 182 |
| 181 | 183 |
| 184 // ---------------------------------------------------------------------------- |
| 185 // Regular expressions |
| 186 |
| 187 #define MAKE_ACCEPT(Name) \ |
| 188 void* RegExp##Name::Accept(RegExpVisitor* visitor, void* data) { \ |
| 189 return visitor->Visit##Name(this, data); \ |
| 190 } |
| 191 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ACCEPT) |
| 192 #undef MAKE_ACCEPT |
| 193 |
| 194 #define MAKE_TYPE_CASE(Name) \ |
| 195 RegExp##Name* RegExpTree::As##Name() { \ |
| 196 return NULL; \ |
| 197 } \ |
| 198 bool RegExpTree::Is##Name() { return false; } |
| 199 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE) |
| 200 #undef MAKE_TYPE_CASE |
| 201 |
| 202 #define MAKE_TYPE_CASE(Name) \ |
| 203 RegExp##Name* RegExp##Name::As##Name() { \ |
| 204 return this; \ |
| 205 } \ |
| 206 bool RegExp##Name::Is##Name() { return true; } |
| 207 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE) |
| 208 #undef MAKE_TYPE_CASE |
| 209 |
| 210 RegExpEmpty RegExpEmpty::kInstance; |
| 211 |
| 212 |
| 213 // Convert regular expression trees to a simple sexp representation. |
| 214 // This representation should be different from the input grammar |
| 215 // in as many cases as possible, to make it more difficult for incorrect |
| 216 // parses to look as correct ones which is likely if the input and |
| 217 // output formats are alike. |
| 218 class RegExpUnparser: public RegExpVisitor { |
| 219 public: |
| 220 RegExpUnparser(); |
| 221 void VisitCharacterRange(CharacterRange that); |
| 222 SmartPointer<const char> ToString() { return stream_.ToCString(); } |
| 223 #define MAKE_CASE(Name) virtual void* Visit##Name(RegExp##Name*, void* data); |
| 224 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE) |
| 225 #undef MAKE_CASE |
| 226 private: |
| 227 StringStream* stream() { return &stream_; } |
| 228 HeapStringAllocator alloc_; |
| 229 StringStream stream_; |
| 230 }; |
| 231 |
| 232 |
| 233 RegExpUnparser::RegExpUnparser() : stream_(&alloc_) { |
| 234 } |
| 235 |
| 236 |
| 237 void* RegExpUnparser::VisitDisjunction(RegExpDisjunction* that, void* data) { |
| 238 stream()->Add("(|"); |
| 239 for (int i = 0; i < that->alternatives()->length(); i++) { |
| 240 stream()->Add(" "); |
| 241 that->alternatives()->at(i)->Accept(this, data); |
| 242 } |
| 243 stream()->Add(")"); |
| 244 return NULL; |
| 245 } |
| 246 |
| 247 |
| 248 void* RegExpUnparser::VisitAlternative(RegExpAlternative* that, void* data) { |
| 249 stream()->Add("(:"); |
| 250 for (int i = 0; i < that->nodes()->length(); i++) { |
| 251 stream()->Add(" "); |
| 252 that->nodes()->at(i)->Accept(this, data); |
| 253 } |
| 254 stream()->Add(")"); |
| 255 return NULL; |
| 256 } |
| 257 |
| 258 |
| 259 void RegExpUnparser::VisitCharacterRange(CharacterRange that) { |
| 260 stream()->Add("%k", that.from()); |
| 261 if (!that.IsSingleton()) { |
| 262 stream()->Add("-%k", that.to()); |
| 263 } |
| 264 } |
| 265 |
| 266 |
| 267 |
| 268 void* RegExpUnparser::VisitCharacterClass(RegExpCharacterClass* that, |
| 269 void* data) { |
| 270 if (that->is_negated()) |
| 271 stream()->Add("^"); |
| 272 stream()->Add("["); |
| 273 for (int i = 0; i < that->ranges()->length(); i++) { |
| 274 if (i > 0) stream()->Add(" "); |
| 275 VisitCharacterRange(that->ranges()->at(i)); |
| 276 } |
| 277 stream()->Add("]"); |
| 278 return NULL; |
| 279 } |
| 280 |
| 281 |
| 282 void* RegExpUnparser::VisitAssertion(RegExpAssertion* that, void* data) { |
| 283 switch (that->type()) { |
| 284 case RegExpAssertion::START_OF_INPUT: |
| 285 stream()->Add("@^i"); |
| 286 break; |
| 287 case RegExpAssertion::END_OF_INPUT: |
| 288 stream()->Add("@$i"); |
| 289 break; |
| 290 case RegExpAssertion::START_OF_LINE: |
| 291 stream()->Add("@^l"); |
| 292 break; |
| 293 case RegExpAssertion::END_OF_LINE: |
| 294 stream()->Add("@$l"); |
| 295 break; |
| 296 case RegExpAssertion::BOUNDARY: |
| 297 stream()->Add("@b"); |
| 298 break; |
| 299 case RegExpAssertion::NON_BOUNDARY: |
| 300 stream()->Add("@B"); |
| 301 break; |
| 302 } |
| 303 return NULL; |
| 304 } |
| 305 |
| 306 |
| 307 void* RegExpUnparser::VisitAtom(RegExpAtom* that, void* data) { |
| 308 stream()->Add("'"); |
| 309 Vector<const uc16> chardata = that->data(); |
| 310 for (int i = 0; i < chardata.length(); i++) { |
| 311 stream()->Add("%k", chardata[i]); |
| 312 } |
| 313 stream()->Add("'"); |
| 314 return NULL; |
| 315 } |
| 316 |
| 317 |
| 318 void* RegExpUnparser::VisitText(RegExpText* that, void* data) { |
| 319 if (that->elements()->length() == 1) { |
| 320 that->elements()->at(0).data.u_atom->Accept(this, data); |
| 321 } else { |
| 322 stream()->Add("(!"); |
| 323 for (int i = 0; i < that->elements()->length(); i++) { |
| 324 stream()->Add(" "); |
| 325 that->elements()->at(i).data.u_atom->Accept(this, data); |
| 326 } |
| 327 stream()->Add(")"); |
| 328 } |
| 329 return NULL; |
| 330 } |
| 331 |
| 332 |
| 333 void* RegExpUnparser::VisitQuantifier(RegExpQuantifier* that, void* data) { |
| 334 stream()->Add("(# %i ", that->min()); |
| 335 if (that->max() == RegExpQuantifier::kInfinity) { |
| 336 stream()->Add("- "); |
| 337 } else { |
| 338 stream()->Add("%i ", that->max()); |
| 339 } |
| 340 stream()->Add(that->is_greedy() ? "g " : "n "); |
| 341 that->body()->Accept(this, data); |
| 342 stream()->Add(")"); |
| 343 return NULL; |
| 344 } |
| 345 |
| 346 |
| 347 void* RegExpUnparser::VisitCapture(RegExpCapture* that, void* data) { |
| 348 stream()->Add("(^ "); |
| 349 that->body()->Accept(this, data); |
| 350 stream()->Add(")"); |
| 351 return NULL; |
| 352 } |
| 353 |
| 354 |
| 355 void* RegExpUnparser::VisitLookahead(RegExpLookahead* that, void* data) { |
| 356 stream()->Add("(-> "); |
| 357 stream()->Add(that->is_positive() ? "+ " : "- "); |
| 358 that->body()->Accept(this, data); |
| 359 stream()->Add(")"); |
| 360 return NULL; |
| 361 } |
| 362 |
| 363 |
| 364 void* RegExpUnparser::VisitBackReference(RegExpBackReference* that, |
| 365 void* data) { |
| 366 stream()->Add("(<- %i)", that->index()); |
| 367 return NULL; |
| 368 } |
| 369 |
| 370 |
| 371 void* RegExpUnparser::VisitEmpty(RegExpEmpty* that, void* data) { |
| 372 stream()->Put('%'); |
| 373 return NULL; |
| 374 } |
| 375 |
| 376 |
| 377 SmartPointer<const char> RegExpTree::ToString() { |
| 378 RegExpUnparser unparser; |
| 379 Accept(&unparser, NULL); |
| 380 return unparser.ToString(); |
| 381 } |
| 382 |
| 383 |
| 182 } } // namespace v8::internal | 384 } } // namespace v8::internal |
| OLD | NEW |