Skip to content

Commit

Permalink
Update AstBuilder to build spread collection AST structures
Browse files Browse the repository at this point in the history
Change-Id: Ifde5047933d810bdda77fe1b46eb6f69b7eabc5a
Reviewed-on: https://dart-review.googlesource.com/c/90082
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Dan Rubel <danrubel@google.com>
  • Loading branch information
danrubel authored and commit-bot@chromium.org committed Jan 18, 2019
1 parent b10f179 commit 2faab1d
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 39 deletions.
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/dart/ast/ast.dart
Expand Up @@ -5142,7 +5142,7 @@ abstract class ListLiteral extends TypedLiteral {
*
* listLiteral ::=
* 'const'? ('<' [TypeAnnotation] '>')?
* '[' ([CollectionLiteralElement] ','?)? ']'
* '[' ([CollectionElement] ','?)? ']'
*
* This is the class that is used to represent a list literal when either the
* 'control-flow-collections' or 'spread-collections' experiments are enabled.
Expand Down
89 changes: 66 additions & 23 deletions pkg/analyzer/lib/src/fasta/ast_builder.dart
Expand Up @@ -279,9 +279,12 @@ class AstBuilder extends StackListener {

@override
void handleSpreadExpression(Token spreadToken) {
// TODO(danrubel): generate new AST structure
handleRecoverableError(templateUnexpectedToken.withArguments(spreadToken),
spreadToken, spreadToken);
if (enableSpreadCollections) {
push(ast.spreadElement(spreadOperator: spreadToken, expression: pop()));
} else {
handleRecoverableError(templateUnexpectedToken.withArguments(spreadToken),
spreadToken, spreadToken);
}
}

void handleStringJuxtaposition(int literalCount) {
Expand Down Expand Up @@ -865,10 +868,22 @@ class AstBuilder extends StackListener {
assert(optional(']', rightBracket));
debugEvent("LiteralList");

List<Expression> expressions = popTypedList(count);
TypeArgumentList typeArguments = pop();
push(ast.listLiteral(
constKeyword, typeArguments, leftBracket, expressions, rightBracket));
if (enableSpreadCollections) {
List<CollectionElement> elements = popTypedList(count);
TypeArgumentList typeArguments = pop();
push(ast.listLiteral2(
constKeyword: constKeyword,
typeArguments: typeArguments,
leftBracket: leftBracket,
elements: elements,
rightBracket: rightBracket,
));
} else {
List<Expression> expressions = popTypedList(count);
TypeArgumentList typeArguments = pop();
push(ast.listLiteral(
constKeyword, typeArguments, leftBracket, expressions, rightBracket));
}
}

void handleAsyncModifier(Token asyncToken, Token starToken) {
Expand Down Expand Up @@ -927,10 +942,23 @@ class AstBuilder extends StackListener {
assert(optional('}', rightBracket));
debugEvent("LiteralSet");

List<Expression> entries = popTypedList(count) ?? <Expression>[];
TypeArgumentList typeArguments = pop();
push(ast.setLiteral(
constKeyword, typeArguments, leftBracket, entries, rightBracket));
if (enableSpreadCollections) {
List<CollectionElement> elements =
popTypedList(count) ?? <CollectionElement>[];
TypeArgumentList typeArguments = pop();
push(ast.setLiteral2(
constKeyword: constKeyword,
typeArguments: typeArguments,
leftBracket: leftBracket,
elements: elements,
rightBracket: rightBracket,
));
} else {
List<Expression> entries = popTypedList(count) ?? <Expression>[];
TypeArgumentList typeArguments = pop();
push(ast.setLiteral(
constKeyword, typeArguments, leftBracket, entries, rightBracket));
}
}

void handleLiteralMap(
Expand All @@ -940,19 +968,34 @@ class AstBuilder extends StackListener {
assert(optional('}', rightBracket));
debugEvent("LiteralMap");

// TODO(danrubel): Revise once spread collection AST structures
// are in place. For now, drop those on the floor
// as error(s) have already been reported in handleSpreadExpression.
List<MapLiteralEntry> entries = <MapLiteralEntry>[];
popTypedList(count)?.forEach((entry) {
if (entry is MapLiteralEntry) {
entries.add(entry);
}
});
if (enableSpreadCollections) {
List<MapElement> entries = <MapElement>[];
popTypedList(count)?.forEach((entry) {
if (entry is MapElement) {
entries.add(entry);
}
});

TypeArgumentList typeArguments = pop();
push(ast.mapLiteral(
constKeyword, typeArguments, leftBracket, entries, rightBracket));
TypeArgumentList typeArguments = pop();
push(ast.mapLiteral2(
constKeyword: constKeyword,
typeArguments: typeArguments,
leftBracket: leftBracket,
entries: entries,
rightBracket: rightBracket,
));
} else {
List<MapLiteralEntry> entries = <MapLiteralEntry>[];
popTypedList(count)?.forEach((entry) {
if (entry is MapLiteralEntry) {
entries.add(entry);
}
});

TypeArgumentList typeArguments = pop();
push(ast.mapLiteral(
constKeyword, typeArguments, leftBracket, entries, rightBracket));
}
}

void handleLiteralMapEntry(Token colon, Token endToken) {
Expand Down

0 comments on commit 2faab1d

Please sign in to comment.