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

Side by Side Diff: runtime/vm/object.cc

Issue 10823028: - Improve the formatting of generated source text. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 4 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/object.h" 5 #include "vm/object.h"
6 6
7 #include "platform/assert.h" 7 #include "platform/assert.h"
8 #include "vm/assembler.h" 8 #include "vm/assembler.h"
9 #include "vm/bigint_operations.h" 9 #include "vm/bigint_operations.h"
10 #include "vm/bootstrap.h" 10 #include "vm/bootstrap.h"
(...skipping 4544 matching lines...) Expand 10 before | Expand all | Expand 10 after
4555 } 4555 }
4556 4556
4557 4557
4558 RawString* TokenStream::GenerateSource() const { 4558 RawString* TokenStream::GenerateSource() const {
4559 Iterator iterator(*this, 0); 4559 Iterator iterator(*this, 0);
4560 GrowableObjectArray& literals = 4560 GrowableObjectArray& literals =
4561 GrowableObjectArray::Handle(GrowableObjectArray::New(Length())); 4561 GrowableObjectArray::Handle(GrowableObjectArray::New(Length()));
4562 String& literal = String::Handle(); 4562 String& literal = String::Handle();
4563 String& blank = String::Handle(String::New(" ")); 4563 String& blank = String::Handle(String::New(" "));
4564 String& newline = String::Handle(String::New("\n")); 4564 String& newline = String::Handle(String::New("\n"));
4565 String& two_newlines = String::Handle(String::New("\n\n"));
4565 String& double_quotes = String::Handle(String::New("\"")); 4566 String& double_quotes = String::Handle(String::New("\""));
4567 String& dollar = String::Handle(String::New("$"));
4568 String& two_spaces = String::Handle(String::New(" "));
4566 Object& obj = Object::Handle(); 4569 Object& obj = Object::Handle();
4567 Token::Kind kind = iterator.CurrentTokenKind(); 4570 Token::Kind kind = iterator.CurrentTokenKind();
4571 int indent = 0;
4568 while (kind != Token::kEOS) { 4572 while (kind != Token::kEOS) {
4569 obj = iterator.CurrentToken(); 4573 obj = iterator.CurrentToken();
4570 literal = iterator.MakeLiteralToken(obj); 4574 literal = iterator.MakeLiteralToken(obj);
4571 if (kind == Token::kSTRING) { 4575 if (kind == Token::kSTRING) {
4572 bool escape_quotes = false; 4576 bool escape_quotes = false;
4573 for (intptr_t i = 0; i < literal.Length(); i++) { 4577 for (intptr_t i = 0; i < literal.Length(); i++) {
4574 if (literal.CharAt(i) == '"') { 4578 if (literal.CharAt(i) == '"') {
4575 escape_quotes = true; 4579 escape_quotes = true;
4576 break; 4580 break;
4577 } 4581 }
4578 } 4582 }
4579 literals.Add(double_quotes); 4583 literals.Add(double_quotes);
4580 if (escape_quotes) { 4584 if (escape_quotes) {
4581 literal = String::EscapeDoubleQuotes(literal); 4585 literal = String::EscapeDoubleQuotes(literal);
4582 literals.Add(literal); 4586 literals.Add(literal);
4583 } else { 4587 } else {
4584 literals.Add(literal); 4588 literals.Add(literal);
4585 } 4589 }
4586 literals.Add(double_quotes); 4590 literals.Add(double_quotes);
4591 } else if (kind == Token::kINTERPOL_VAR) {
4592 literals.Add(double_quotes);
4593 literals.Add(dollar);
4594 literals.Add(literal);
4595 literals.Add(double_quotes);
4596 } else if (kind == Token::kINTERPOL_START) {
4597 literals.Add(double_quotes);
4598 literals.Add(literal);
4599 } else if (kind == Token::kINTERPOL_END) {
4600 literals.Add(literal);
4601 literals.Add(double_quotes);
4587 } else { 4602 } else {
4588 literals.Add(literal); 4603 literals.Add(literal);
4589 } 4604 }
4590 if (kind == Token::kLBRACE) { 4605 // Determine the separation text based on this current token.
4591 literals.Add(newline); 4606 const String* separator = NULL;
4592 } else { 4607 switch (kind) {
4593 literals.Add(blank); 4608 case Token::kLBRACE:
4609 indent++;
4610 separator = &newline;
4611 break;
4612 case Token::kRBRACE:
4613 if (indent == 0) {
4614 separator = &two_newlines;
4615 } else {
4616 separator = &newline;
4617 }
4618 break;
4619 case Token::kSEMICOLON:
4620 separator = &newline;
4621 break;
4622 case Token::kPERIOD:
4623 case Token::kLPAREN:
4624 break;
4625 default:
4626 separator = &blank;
4627 break;
4594 } 4628 }
4629 // Advance the iterator.
4595 iterator.Advance(); 4630 iterator.Advance();
4596 kind = iterator.CurrentTokenKind(); 4631 kind = iterator.CurrentTokenKind();
4632 // Determine whether the separation text needs to be updated based on the
4633 // next token.
4634 switch (kind) {
4635 case Token::kRBRACE:
4636 indent--;
4637 break;
4638 case Token::kSEMICOLON:
4639 case Token::kPERIOD:
4640 case Token::kCOMMA:
4641 case Token::kLPAREN:
4642 case Token::kRPAREN:
4643 separator = NULL;
4644 break;
4645 default:
4646 // Do nothing.
4647 break;
4648 }
4649 if (separator != NULL) {
4650 literals.Add(*separator);
4651 if (separator == &newline) {
4652 for (int i = 0; i < indent; i++) {
4653 literals.Add(two_spaces);
4654 }
4655 }
4656 }
4597 } 4657 }
4598 const Array& source = Array::Handle(Array::MakeArray(literals)); 4658 const Array& source = Array::Handle(Array::MakeArray(literals));
4599 return String::ConcatAll(source); 4659 return String::ConcatAll(source);
4600 } 4660 }
4601 4661
4602 4662
4603 intptr_t TokenStream::ComputeSourcePosition(intptr_t tok_pos) const { 4663 intptr_t TokenStream::ComputeSourcePosition(intptr_t tok_pos) const {
4604 Iterator iterator(*this, 0); 4664 Iterator iterator(*this, 0);
4605 intptr_t src_pos = 0; 4665 intptr_t src_pos = 0;
4606 Token::Kind kind = iterator.CurrentTokenKind(); 4666 Token::Kind kind = iterator.CurrentTokenKind();
(...skipping 6163 matching lines...) Expand 10 before | Expand all | Expand 10 after
10770 const String& str = String::Handle(pattern()); 10830 const String& str = String::Handle(pattern());
10771 const char* format = "JSRegExp: pattern=%s flags=%s"; 10831 const char* format = "JSRegExp: pattern=%s flags=%s";
10772 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); 10832 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags());
10773 char* chars = reinterpret_cast<char*>( 10833 char* chars = reinterpret_cast<char*>(
10774 Isolate::Current()->current_zone()->Allocate(len + 1)); 10834 Isolate::Current()->current_zone()->Allocate(len + 1));
10775 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); 10835 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags());
10776 return chars; 10836 return chars;
10777 } 10837 }
10778 10838
10779 } // namespace dart 10839 } // namespace dart
OLDNEW
« runtime/vm/dart.cc ('K') | « runtime/vm/dart.cc ('k') | runtime/vm/token.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698