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

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

Issue 24272007: Use newline tokens when generating source from tokenstream. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: added comment Created 7 years, 3 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
« no previous file with comments | « no previous file | runtime/vm/snapshot_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/assembler.h" 9 #include "vm/assembler.h"
10 #include "vm/cpu.h" 10 #include "vm/cpu.h"
(...skipping 5730 matching lines...) Expand 10 before | Expand all | Expand 10 after
5741 return raw_ptr()->private_key_; 5741 return raw_ptr()->private_key_;
5742 } 5742 }
5743 5743
5744 5744
5745 void TokenStream::SetPrivateKey(const String& value) const { 5745 void TokenStream::SetPrivateKey(const String& value) const {
5746 StorePointer(&raw_ptr()->private_key_, value.raw()); 5746 StorePointer(&raw_ptr()->private_key_, value.raw());
5747 } 5747 }
5748 5748
5749 5749
5750 RawString* TokenStream::GenerateSource() const { 5750 RawString* TokenStream::GenerateSource() const {
5751 Iterator iterator(*this, 0); 5751 Iterator iterator(*this, 0, Iterator::kAllTokens);
5752 const ExternalTypedData& data = ExternalTypedData::Handle(GetStream()); 5752 const ExternalTypedData& data = ExternalTypedData::Handle(GetStream());
5753 const GrowableObjectArray& literals = 5753 const GrowableObjectArray& literals =
5754 GrowableObjectArray::Handle(GrowableObjectArray::New(data.Length())); 5754 GrowableObjectArray::Handle(GrowableObjectArray::New(data.Length()));
5755 const String& private_key = String::Handle(PrivateKey()); 5755 const String& private_key = String::Handle(PrivateKey());
5756 intptr_t private_len = private_key.Length(); 5756 intptr_t private_len = private_key.Length();
5757 5757
5758 Token::Kind curr = iterator.CurrentTokenKind(); 5758 Token::Kind curr = iterator.CurrentTokenKind();
5759 Token::Kind prev = Token::kILLEGAL; 5759 Token::Kind prev = Token::kILLEGAL;
5760 // Handles used in the loop. 5760 // Handles used in the loop.
5761 Object& obj = Object::Handle(); 5761 Object& obj = Object::Handle();
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
5802 literal = String::SubString(literal, 0, literal.Length() - private_len); 5802 literal = String::SubString(literal, 0, literal.Length() - private_len);
5803 } 5803 }
5804 literals.Add(literal); 5804 literals.Add(literal);
5805 } else { 5805 } else {
5806 literals.Add(literal); 5806 literals.Add(literal);
5807 } 5807 }
5808 // Determine the separation text based on this current token. 5808 // Determine the separation text based on this current token.
5809 const String* separator = NULL; 5809 const String* separator = NULL;
5810 switch (curr) { 5810 switch (curr) {
5811 case Token::kLBRACE: 5811 case Token::kLBRACE:
5812 indent++;
5813 separator = &Symbols::NewLine();
5814 break;
5815 case Token::kRBRACE: 5812 case Token::kRBRACE:
5816 if (indent == 0) {
5817 separator = &Symbols::TwoNewlines();
5818 } else {
5819 separator = &Symbols::NewLine();
5820 }
5821 break;
5822 case Token::kSEMICOLON:
5823 separator = &Symbols::NewLine();
5824 break;
5825 case Token::kPERIOD: 5813 case Token::kPERIOD:
5826 case Token::kLPAREN:
5827 case Token::kLBRACK: 5814 case Token::kLBRACK:
5828 case Token::kINTERPOL_VAR: 5815 case Token::kINTERPOL_VAR:
5829 case Token::kINTERPOL_START: 5816 case Token::kINTERPOL_START:
5830 case Token::kINTERPOL_END: 5817 case Token::kINTERPOL_END:
5818 case Token::kBIT_NOT:
5819 break;
5820 // In case we see an opening parentheses '(' we increase the indent to
5821 // align multi-line parameters accordingly. The indent will be removed as
5822 // soon as we see the matching closing parentheses ')'.
5823 //
5824 // Example:
5825 // SomeVeryLongMethod(
5826 // "withVeryLongParameter",
5827 // "andAnotherVeryLongParameter",
5828 // "andAnotherVeryLongParameter2") { ...
5829 case Token::kLPAREN:
5830 indent += 2;
5831 break;
5832 case Token::kRPAREN:
5833 indent -= 2;
5834 separator = &Symbols::Blank();
5835 break;
5836 case Token::kNEWLINE:
5837 if (prev == Token::kLBRACE) {
5838 indent++;
5839 }
5840 if (next == Token::kRBRACE) {
5841 indent--;
5842 }
5831 break; 5843 break;
5832 default: 5844 default:
5833 separator = &Symbols::Blank(); 5845 separator = &Symbols::Blank();
5834 break; 5846 break;
5835 } 5847 }
5848
5836 // Determine whether the separation text needs to be updated based on the 5849 // Determine whether the separation text needs to be updated based on the
5837 // next token. 5850 // next token.
5838 switch (next) { 5851 switch (next) {
5839 case Token::kRBRACE: 5852 case Token::kRBRACE:
5840 indent--;
5841 break; 5853 break;
5842 case Token::kSEMICOLON: 5854 case Token::kSEMICOLON:
5843 case Token::kPERIOD: 5855 case Token::kPERIOD:
5844 case Token::kCOMMA: 5856 case Token::kCOMMA:
5845 case Token::kLPAREN: 5857 case Token::kLPAREN:
5846 case Token::kRPAREN: 5858 case Token::kRPAREN:
5847 case Token::kLBRACK: 5859 case Token::kLBRACK:
5848 case Token::kRBRACK: 5860 case Token::kRBRACK:
5849 case Token::kINTERPOL_VAR: 5861 case Token::kINTERPOL_VAR:
5850 case Token::kINTERPOL_START: 5862 case Token::kINTERPOL_START:
5851 case Token::kINTERPOL_END: 5863 case Token::kINTERPOL_END:
5852 separator = NULL; 5864 separator = NULL;
5853 break; 5865 break;
5854 case Token::kELSE: 5866 case Token::kELSE:
5855 separator = &Symbols::Blank(); 5867 separator = &Symbols::Blank();
5868 break;
5856 default: 5869 default:
5857 // Do nothing. 5870 // Do nothing.
5858 break; 5871 break;
5859 } 5872 }
5873
5860 // Update the few cases where both tokens need to be taken into account. 5874 // Update the few cases where both tokens need to be taken into account.
5861 if (((curr == Token::kIF) || (curr == Token::kFOR)) && 5875 if (((curr == Token::kIF) || (curr == Token::kFOR)) &&
5862 (next == Token::kLPAREN)) { 5876 (next == Token::kLPAREN)) {
5863 separator = &Symbols::Blank(); 5877 separator = &Symbols::Blank();
5864 } else if ((curr == Token::kASSIGN) && (next == Token::kLPAREN)) { 5878 } else if ((curr == Token::kASSIGN) && (next == Token::kLPAREN)) {
5865 separator = &Symbols::Blank(); 5879 separator = &Symbols::Blank();
5880 } else if ((curr == Token::kRETURN ||
5881 curr == Token::kCONDITIONAL ||
5882 Token::IsBinaryOperator(curr) ||
5883 Token::IsEqualityOperator(curr)) && (next == Token::kLPAREN)) {
5884 separator = &Symbols::Blank();
5866 } else if ((curr == Token::kLBRACE) && (next == Token::kRBRACE)) { 5885 } else if ((curr == Token::kLBRACE) && (next == Token::kRBRACE)) {
5867 separator = NULL; 5886 separator = NULL;
5887 } else if ((curr == Token::kSEMICOLON) && (next != Token::kNEWLINE)) {
5888 separator = &Symbols::Blank();
5868 } 5889 }
5890
5891 // Add the separator.
5869 if (separator != NULL) { 5892 if (separator != NULL) {
5870 literals.Add(*separator); 5893 literals.Add(*separator);
5871 if (separator == &Symbols::NewLine()) { 5894 }
5895
5896 // Account for indentation in case we printed a newline.
5897 if (curr == Token::kNEWLINE) {
5872 for (int i = 0; i < indent; i++) { 5898 for (int i = 0; i < indent; i++) {
5873 literals.Add(Symbols::TwoSpaces()); 5899 literals.Add(Symbols::TwoSpaces());
5874 } 5900 }
5875 }
5876 } 5901 }
5902
5877 // Setup for next iteration. 5903 // Setup for next iteration.
5878 prev = curr; 5904 prev = curr;
5879 curr = next; 5905 curr = next;
5880 } 5906 }
5881 const Array& source = Array::Handle(Array::MakeArray(literals)); 5907 const Array& source = Array::Handle(Array::MakeArray(literals));
5882 return String::ConcatAll(source); 5908 return String::ConcatAll(source);
5883 } 5909 }
5884 5910
5885 5911
5886 intptr_t TokenStream::ComputeSourcePosition(intptr_t tok_pos) const { 5912 intptr_t TokenStream::ComputeSourcePosition(intptr_t tok_pos) const {
(...skipping 9123 matching lines...) Expand 10 before | Expand all | Expand 10 after
15010 return "_MirrorReference"; 15036 return "_MirrorReference";
15011 } 15037 }
15012 15038
15013 15039
15014 void MirrorReference::PrintToJSONStream(JSONStream* stream, bool ref) const { 15040 void MirrorReference::PrintToJSONStream(JSONStream* stream, bool ref) const {
15015 JSONObject jsobj(stream); 15041 JSONObject jsobj(stream);
15016 } 15042 }
15017 15043
15018 15044
15019 } // namespace dart 15045 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/vm/snapshot_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698