Skip to content

Commit 08eba17

Browse files
committed
Add support for parsing annotations with Fasta.
[email protected], [email protected] BUG= Review-Url: https://codereview.chromium.org/2748763003 .
1 parent 1cb1b70 commit 08eba17

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

pkg/analyzer/test/generated/parser_test.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13315,6 +13315,56 @@ abstract class TopLevelParserTestMixin implements AbstractParserTestCase {
1331513315
expect(declaration.typeParameters, isNull);
1331613316
}
1331713317

13318+
void test_parseClassDeclaration_metadata() {
13319+
createParser('@A @B(2) @C.foo(3) @d.E.bar(4, 5) class X {}');
13320+
var declaration = parseFullCompilationUnitMember() as ClassDeclaration;
13321+
expect(declaration.metadata, hasLength(4));
13322+
13323+
{
13324+
var annotation = declaration.metadata[0];
13325+
expect(annotation.atSign, isNotNull);
13326+
expect(annotation.name, new isInstanceOf<SimpleIdentifier>());
13327+
expect(annotation.name.name, 'A');
13328+
expect(annotation.period, isNull);
13329+
expect(annotation.constructorName, isNull);
13330+
expect(annotation.arguments, isNull);
13331+
}
13332+
13333+
{
13334+
var annotation = declaration.metadata[1];
13335+
expect(annotation.atSign, isNotNull);
13336+
expect(annotation.name, new isInstanceOf<SimpleIdentifier>());
13337+
expect(annotation.name.name, 'B');
13338+
expect(annotation.period, isNull);
13339+
expect(annotation.constructorName, isNull);
13340+
expect(annotation.arguments, isNotNull);
13341+
expect(annotation.arguments.arguments, hasLength(1));
13342+
}
13343+
13344+
{
13345+
var annotation = declaration.metadata[2];
13346+
expect(annotation.atSign, isNotNull);
13347+
expect(annotation.name, new isInstanceOf<PrefixedIdentifier>());
13348+
expect(annotation.name.name, 'C.foo');
13349+
expect(annotation.period, isNull);
13350+
expect(annotation.constructorName, isNull);
13351+
expect(annotation.arguments, isNotNull);
13352+
expect(annotation.arguments.arguments, hasLength(1));
13353+
}
13354+
13355+
{
13356+
var annotation = declaration.metadata[3];
13357+
expect(annotation.atSign, isNotNull);
13358+
expect(annotation.name, new isInstanceOf<PrefixedIdentifier>());
13359+
expect(annotation.name.name, 'd.E');
13360+
expect(annotation.period, isNotNull);
13361+
expect(annotation.constructorName, isNotNull);
13362+
expect(annotation.constructorName.name, 'bar');
13363+
expect(annotation.arguments, isNotNull);
13364+
expect(annotation.arguments.arguments, hasLength(2));
13365+
}
13366+
}
13367+
1331813368
void test_parseClassDeclaration_native() {
1331913369
createParser('class A native "nativeValue" {}');
1332013370
CompilationUnitMember member = parseFullCompilationUnitMember();

pkg/front_end/lib/src/fasta/analyzer/ast_builder.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,21 @@ class AstBuilder extends ScopeListener {
14701470
}
14711471
}
14721472

1473+
@override
1474+
void endMetadata(Token beginToken, Token periodBeforeName, Token endToken) {
1475+
debugEvent("Metadata");
1476+
MethodInvocation invocation = pop();
1477+
SimpleIdentifier constructorName = periodBeforeName != null ? pop() : null;
1478+
pop(); // Type arguments, not allowed.
1479+
Identifier name = pop();
1480+
push(ast.annotation(
1481+
toAnalyzerToken(beginToken),
1482+
name,
1483+
toAnalyzerToken(periodBeforeName),
1484+
constructorName,
1485+
invocation?.argumentList));
1486+
}
1487+
14731488
ParameterKind _toAnalyzerParameterKind(FormalParameterType type) {
14741489
if (type == FormalParameterType.POSITIONAL) {
14751490
return ParameterKind.POSITIONAL;

0 commit comments

Comments
 (0)