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

Unified Diff: vm/object.cc

Issue 10823269: - Improve generated sources: (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « vm/object.h ('k') | vm/object_test.cc » ('j') | vm/raw_object_snapshot.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: vm/object.cc
===================================================================
--- vm/object.cc (revision 10270)
+++ vm/object.cc (working copy)
@@ -4593,24 +4593,48 @@
}
+RawString* TokenStream::PrivateKey() const {
+ return raw_ptr()->private_key_;
+}
+
+
+void TokenStream::SetPrivateKey(const String& value) const {
+ StorePointer(&raw_ptr()->private_key_, value.raw());
+}
+
+
RawString* TokenStream::GenerateSource() const {
Iterator iterator(*this, 0);
- GrowableObjectArray& literals =
+ const GrowableObjectArray& literals =
GrowableObjectArray::Handle(GrowableObjectArray::New(Length()));
- String& literal = String::Handle();
+ const String& private_key = String::Handle(PrivateKey());
+ intptr_t private_len = private_key.Length();
+
String& blank = String::Handle(String::New(" "));
String& newline = String::Handle(String::New("\n"));
String& two_newlines = String::Handle(String::New("\n\n"));
String& double_quotes = String::Handle(String::New("\""));
String& dollar = String::Handle(String::New("$"));
String& two_spaces = String::Handle(String::New(" "));
+
+ Token::Kind curr = iterator.CurrentTokenKind();
+ Token::Kind prev = Token::kILLEGAL;
+ // Handles used in the loop.
Object& obj = Object::Handle();
- Token::Kind kind = iterator.CurrentTokenKind();
+ String& literal = String::Handle();
+ // Current indentation level.
int indent = 0;
- while (kind != Token::kEOS) {
+
+ while (curr != Token::kEOS) {
+ // Remember current values for this token.
obj = iterator.CurrentToken();
literal = iterator.MakeLiteralToken(obj);
- if (kind == Token::kSTRING) {
+ // Advance to be able to use next token kind.
+ iterator.Advance();
+ Token::Kind next = iterator.CurrentTokenKind();
+
+ // Handle the current token.
+ if (curr == Token::kSTRING) {
bool escape_quotes = false;
for (intptr_t i = 0; i < literal.Length(); i++) {
if (literal.CharAt(i) == '"') {
@@ -4618,31 +4642,32 @@
break;
}
}
- literals.Add(double_quotes);
+ if ((prev != Token::kINTERPOL_VAR) && (prev != Token::kINTERPOL_END)) {
+ literals.Add(double_quotes);
+ }
if (escape_quotes) {
literal = String::EscapeDoubleQuotes(literal);
literals.Add(literal);
} else {
literals.Add(literal);
}
- literals.Add(double_quotes);
- } else if (kind == Token::kINTERPOL_VAR) {
- literals.Add(double_quotes);
+ if ((next != Token::kINTERPOL_VAR) && (next != Token::kINTERPOL_START)) {
+ literals.Add(double_quotes);
+ }
+ } else if (curr == Token::kINTERPOL_VAR) {
literals.Add(dollar);
literals.Add(literal);
- literals.Add(double_quotes);
- } else if (kind == Token::kINTERPOL_START) {
- literals.Add(double_quotes);
+ } else if (curr == Token::kIDENT) {
+ if (literal.CharAt(0) == Scanner::kPrivateIdentifierStart) {
+ literal = String::SubString(literal, 0, literal.Length() - private_len);
+ }
literals.Add(literal);
- } else if (kind == Token::kINTERPOL_END) {
- literals.Add(literal);
- literals.Add(double_quotes);
} else {
literals.Add(literal);
}
// Determine the separation text based on this current token.
const String* separator = NULL;
- switch (kind) {
+ switch (curr) {
case Token::kLBRACE:
indent++;
separator = &newline;
@@ -4659,17 +4684,19 @@
break;
case Token::kPERIOD:
case Token::kLPAREN:
+ case Token::kLBRACK:
+ case Token::kTIGHTADD:
+ case Token::kINTERPOL_VAR:
+ case Token::kINTERPOL_START:
+ case Token::kINTERPOL_END:
break;
default:
separator = &blank;
break;
}
- // Advance the iterator.
- iterator.Advance();
- kind = iterator.CurrentTokenKind();
// Determine whether the separation text needs to be updated based on the
// next token.
- switch (kind) {
+ switch (next) {
case Token::kRBRACE:
indent--;
break;
@@ -4678,12 +4705,28 @@
case Token::kCOMMA:
case Token::kLPAREN:
case Token::kRPAREN:
+ case Token::kLBRACK:
+ case Token::kRBRACK:
+ case Token::kINTERPOL_VAR:
+ case Token::kINTERPOL_START:
+ case Token::kINTERPOL_END:
separator = NULL;
break;
+ case Token::kELSE:
+ separator = &blank;
default:
// Do nothing.
break;
}
+ // Update the few cases where both tokens need to be taken into account.
+ if (((curr == Token::kIF) || (curr == Token::kFOR)) &&
+ (next == Token::kLPAREN)) {
+ separator = &blank;
+ } else if ((curr == Token::kASSIGN) && (next == Token::kLPAREN)) {
+ separator = & blank;
+ } else if ((curr == Token::kLBRACE) && (next == Token::kRBRACE)) {
+ separator = NULL;
+ }
if (separator != NULL) {
literals.Add(*separator);
if (separator == &newline) {
@@ -4692,6 +4735,9 @@
}
}
}
+ // Setup for next iteration.
+ prev = curr;
+ curr = next;
}
const Array& source = Array::Handle(Array::MakeArray(literals));
return String::ConcatAll(source);
@@ -4879,7 +4925,8 @@
};
-RawTokenStream* TokenStream::New(const Scanner::GrowableTokenStream& tokens) {
+RawTokenStream* TokenStream::New(const Scanner::GrowableTokenStream& tokens,
+ const String& private_key) {
// Copy the relevant data out of the scanner into a compressed stream of
// tokens.
CompressedTokenStreamData data;
@@ -4908,6 +4955,7 @@
// Create and setup the token stream object.
const TokenStream& result = TokenStream::Handle(New(data.Length()));
+ result.SetPrivateKey(private_key);
{
NoGCScope no_gc;
memmove(result.EntryAddr(0), data.GetStream(), data.Length());
@@ -5111,7 +5159,8 @@
TimerScope timer(FLAG_compiler_stats, &CompilerStats::scanner_timer);
const String& src = String::Handle(Source());
Scanner scanner(src, private_key);
- set_tokens(TokenStream::Handle(TokenStream::New(scanner.GetStream())));
+ set_tokens(TokenStream::Handle(TokenStream::New(scanner.GetStream(),
+ private_key)));
if (FLAG_compiler_stats) {
CompilerStats::src_length += src.Length();
}
« no previous file with comments | « vm/object.h ('k') | vm/object_test.cc » ('j') | vm/raw_object_snapshot.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698