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

Unified Diff: frog/leg/scanner/listener.dart

Issue 9642001: Make string juxtaposition combine properly with string interpolations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments. Created 8 years, 9 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
Index: frog/leg/scanner/listener.dart
diff --git a/frog/leg/scanner/listener.dart b/frog/leg/scanner/listener.dart
index 5b6dac4f983ada0282d0f70d25aa1ca455332b54..6c6b1aea12391f2e7edaf73344198b0e0835af7e 100644
--- a/frog/leg/scanner/listener.dart
+++ b/frog/leg/scanner/listener.dart
@@ -837,13 +837,65 @@ class ElementListener extends Listener {
}
void handleLiteralStringJuxtaposition(int literalCount) {
- Link<Expression> literals = const EmptyLink<Expression>();
+ assert(literalCount != 0);
ahe 2012/03/12 09:31:04 literalCount > 0
+ if (literalCount == 1) {
+ compiler.internalError("Unnecessary juxtaposition production");
ahe 2012/03/12 09:31:04 I don't think there is an instance of the compiler
+ }
+ // A sequence of string literals may contain string interpolations.
ahe 2012/03/12 09:31:04 This approach seems really complicated. I do not u
+ // Collect adjacent non-interpolations in [accumulator], and
+ // collect interpolation parts in [interpolationParts], merging
+ // adjacent literal string parts into one literal to build
+ // a final StringInterpolation.
+ Link<Node> interpolationParts = const EmptyLink<Node>();
+ Link<LiteralString> accumulator = const EmptyLink<LiteralString>();
+
+ // Traverse backwards through the parts of a StringInterpolation
+ // and prepend them to [interpolationParts].
+ // Combine the last part's string with the accumulator.
+ void prependParts(Link<Node> parts) {
+ StringInterpolationPart part = parts.head;
+ if (!parts.tail.isEmpty()) {
+ prependParts(parts.tail);
+ } else if (!accumulator.isEmpty()) {
+ // Create a single StringLiteral from accumulator and the last
+ // part's [string], and create a new part from that.
+ LiteralString newString =
+ new LiteralStringJuxtaposition(accumulator.prepend(part.string));
+ accumulator = const EmptyLink<LiteralString>();
+ part = new StringInterpolationPart(part.expression, newString);
+ }
+ interpolationParts = interpolationParts.prepend(part);
+ }
+
while (literalCount > 0) {
Expression expression = popNode();
- literals = literals.prepend(expression);
+ if (expression is StringInterpolation) {
+ StringInterpolation interpolation = expression;
+ assert(!interpolation.parts.nodes.isEmpty());
+ prependParts(interpolation.parts.nodes);
+ accumulator = accumulator.prepend(interpolation.string);
+ } else {
+ StringLiteral literal = expression;
+ accumulator = accumulator.prepend(literal);
+ }
literalCount -= 1;
}
- pushNode(new LiteralStringJuxtaposition(literals));
+ // Combine the accumulator into a single literal if necessary.
+ LiteralString string;
+ // Accumulator cannot be empty. Whether an expression is a literal string
+ // or a string interpolation, the loop above always leaves something in
+ // the accumulator, and literalCount coudln't start out as zero.
+ if (accumulator.tail.isEmpty()) {
+ string = accumulator.head;
+ } else {
+ string = new LiteralStringJuxtaposition(accumulator);
+ }
+ Expression result = string;
+ if (!interpolationParts.isEmpty()) {
+ NodeList partList = new NodeList(null, interpolationParts, null, null);
+ result = new StringInterpolation(string, partList);
+ }
+ pushNode(result);
}
}

Powered by Google App Engine
This is Rietveld 408576698