[Syntax] Parse associatedtype declaration

Also, added generic where clause to typealias declaration.
This commit is contained in:
Rintaro Ishizaki
2018-02-05 18:25:17 +09:00
parent 62eb27110d
commit ba58a2994d
5 changed files with 51 additions and 6 deletions

View File

@@ -2456,6 +2456,7 @@ Parser::parseDecl(ParseDeclOptions Flags,
MayNeedOverrideCompletion = true;
break;
case tok::kw_associatedtype:
DeclParsingContext.setCreateSyntax(SyntaxKind::AssociatedtypeDecl);
DeclResult = parseDeclAssociatedType(Flags, Attributes);
break;
case tok::kw_enum:
@@ -3498,6 +3499,8 @@ ParserResult<TypeDecl> Parser::parseDeclAssociatedType(Parser::ParseDeclOptions
ParserResult<TypeRepr> UnderlyingTy;
if (Tok.is(tok::equal)) {
SyntaxParsingContext InitContext(SyntaxContext,
SyntaxKind::TypeInitializerClause);
consumeToken(tok::equal);
UnderlyingTy = parseType(diag::expected_type_in_associatedtype);
Status |= UnderlyingTy;

View File

@@ -99,7 +99,17 @@ class C <MemberDeclBlock>{<FunctionDecl>
internal </DeclModifier>subscript<ParameterClause>(<FunctionParameter>x: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionParameter>) </ParameterClause><ReturnClause>-> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier></ReturnClause><AccessorBlock>{ <AccessorDecl>get <CodeBlock>{} </CodeBlock></AccessorDecl><AccessorDecl>set <CodeBlock>{} </CodeBlock></AccessorDecl>}</AccessorBlock></SubscriptDecl><SubscriptDecl>
subscript<ParameterClause>() </ParameterClause><ReturnClause>-> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier></ReturnClause><AccessorBlock>{ <ReturnStmt>return <IntegerLiteralExpr>1 </IntegerLiteralExpr></ReturnStmt>}</AccessorBlock></SubscriptDecl>
}</MemberDeclBlock></ClassDecl>
}</MemberDeclBlock></ClassDecl><ProtocolDecl>
protocol PP <MemberDeclBlock>{<AssociatedtypeDecl>
associatedtype A</AssociatedtypeDecl><AssociatedtypeDecl>
associatedtype B<TypeInheritanceClause>: <InheritedType><SimpleTypeIdentifier>Sequence</SimpleTypeIdentifier></InheritedType></TypeInheritanceClause></AssociatedtypeDecl><AssociatedtypeDecl>
associatedtype C <TypeInitializerClause>= <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></TypeInitializerClause></AssociatedtypeDecl><AssociatedtypeDecl>
associatedtype D<TypeInheritanceClause>: <InheritedType><SimpleTypeIdentifier>Sequence </SimpleTypeIdentifier></InheritedType></TypeInheritanceClause><TypeInitializerClause>= <ArrayType>[<SimpleTypeIdentifier>Int</SimpleTypeIdentifier>]</ArrayType></TypeInitializerClause></AssociatedtypeDecl><AssociatedtypeDecl>
associatedtype E<TypeInheritanceClause>: <InheritedType><SimpleTypeIdentifier>Sequence </SimpleTypeIdentifier></InheritedType></TypeInheritanceClause><TypeInitializerClause>= <ArrayType>[<ArrayType>[<SimpleTypeIdentifier>Int</SimpleTypeIdentifier>]</ArrayType>] </ArrayType></TypeInitializerClause><GenericWhereClause>where <ConformanceRequirement><MemberTypeIdentifier><SimpleTypeIdentifier>A</SimpleTypeIdentifier>.Element </MemberTypeIdentifier>: <SimpleTypeIdentifier>Sequence</SimpleTypeIdentifier></ConformanceRequirement></GenericWhereClause></AssociatedtypeDecl><AssociatedtypeDecl><DeclModifier>
private </DeclModifier>associatedtype F</AssociatedtypeDecl><AssociatedtypeDecl><Attribute>
@objc </Attribute>associatedtype G</AssociatedtypeDecl>
}</MemberDeclBlock></ProtocolDecl>
#endif</IfConfigDecl><IfConfigDecl>

View File

@@ -101,6 +101,16 @@ class C {
subscript() -> Int { return 1 }
}
protocol PP {
associatedtype A
associatedtype B: Sequence
associatedtype C = Int
associatedtype D: Sequence = [Int]
associatedtype E: Sequence = [[Int]] where A.Element : Sequence
private associatedtype F
@objc associatedtype G
}
#endif
#if blah

View File

@@ -106,7 +106,7 @@ TEST(DeclSyntaxTests, TypealiasMakeAPIs) {
auto TypeInit = SyntaxFactory::makeTypeInitializerClause(Assignment,
Array_Int);
SyntaxFactory::makeTypealiasDecl(None, None, Typealias,
Subsequence, GenericParams, TypeInit)
Subsequence, GenericParams, TypeInit, None)
.print(OS);
ASSERT_EQ(OS.str().str(),
"typealias MyCollection<Element> = Array<Element>");

View File

@@ -4,7 +4,7 @@ from Node import Node
DECL_NODES = [
# initializer -> '=' type
# type-assignment -> '=' type
Node('TypeInitializerClause', kind='Syntax',
children=[
Child('Equal', kind='EqualToken'),
@@ -13,9 +13,8 @@ DECL_NODES = [
# typealias-declaration -> attributes? access-level-modifier? 'typealias'
# typealias-name generic-parameter-clause?
# typealias-assignment
# type-assignment
# typealias-name -> identifier
# typealias-assignment -> = type
Node('TypealiasDecl', kind='Decl',
children=[
Child('Attributes', kind='AttributeList',
@@ -27,7 +26,30 @@ DECL_NODES = [
Child('GenericParameterClause', kind='GenericParameterClause',
is_optional=True),
Child('Initializer', kind='TypeInitializerClause',
is_optional=True)
is_optional=True),
Child('GenericWhereClause', kind='GenericWhereClause',
is_optional=True),
]),
# associatedtype-declaration -> attributes? access-level-modifier?
# 'associatedtype' associatedtype-name
# inheritance-clause? type-assignment?
# generic-where-clause?
# associatedtype-name -> identifier
Node('AssociatedtypeDecl', kind='Decl',
children=[
Child('Attributes', kind='AttributeList',
is_optional=True),
Child('AccessLevelModifier', kind='DeclModifier',
is_optional=True),
Child('AssociatedtypeKeyword', kind='AssociatedtypeToken'),
Child('Identifier', kind='IdentifierToken'),
Child('InheritanceClause', kind='TypeInheritanceClause',
is_optional=True),
Child('Initializer', kind='TypeInitializerClause',
is_optional=True),
Child('GenericWhereClause', kind='GenericWhereClause',
is_optional=True),
]),
Node('FunctionParameterList', kind='SyntaxCollection',