libSyntax: specialize import declaration. (#13437)

This commit is contained in:
Xi Ge
2017-12-14 12:27:52 -08:00
committed by GitHub
parent 65be47ef71
commit a6b3559a8a
4 changed files with 43 additions and 2 deletions

View File

@@ -2418,6 +2418,7 @@ Parser::parseDecl(ParseDeclOptions Flags,
// Unambiguous top level decls.
case tok::kw_import:
DeclParsingContext.setCreateSyntax(SyntaxKind::ImportDecl);
DeclResult = parseDeclImport(Flags, Attributes);
break;
case tok::kw_extension:
@@ -2683,7 +2684,10 @@ ParserResult<ImportDecl> Parser::parseDeclImport(ParseDeclOptions Flags,
}
std::vector<std::pair<Identifier, SourceLoc>> ImportPath;
bool HasNext;
do {
SyntaxParsingContext AccessCompCtx(SyntaxContext,
SyntaxKind::AccessPathComponent);
if (Tok.is(tok::code_complete)) {
consumeToken();
if (CodeCompletion) {
@@ -2695,7 +2699,11 @@ ParserResult<ImportDecl> Parser::parseDeclImport(ParseDeclOptions Flags,
if (parseAnyIdentifier(ImportPath.back().first,
diag::expected_identifier_in_decl, "import"))
return nullptr;
} while (consumeIf(tok::period));
HasNext = consumeIf(tok::period);
} while (HasNext);
// Collect all access path components to an access path.
SyntaxContext->collectNodesInPlace(SyntaxKind::AccessPath);
if (Tok.is(tok::code_complete)) {
// We omit the code completion token if it immediately follows the module

View File

@@ -1,8 +1,14 @@
// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t
<ImportDecl>// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t
// RUN: diff -u %s %t
// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -print-node-kind > %t.withkinds
// RUN: diff -u %S/Outputs/round_trip_parse_gen.swift.withkinds %t.withkinds
import <AccessPathComponent>ABC</AccessPathComponent></ImportDecl><ImportDecl>
import <AccessPathComponent>A.</AccessPathComponent><AccessPathComponent>B.</AccessPathComponent><AccessPathComponent>C</AccessPathComponent></ImportDecl><ImportDecl><Attribute>
@objc </Attribute>import <AccessPathComponent>A.</AccessPathComponent><AccessPathComponent>B</AccessPathComponent></ImportDecl><ImportDecl><Attribute>
@objc </Attribute>import typealias <AccessPathComponent>A.</AccessPathComponent><AccessPathComponent>B</AccessPathComponent></ImportDecl><ImportDecl>
import struct <AccessPathComponent>A.</AccessPathComponent><AccessPathComponent>B</AccessPathComponent></ImportDecl>
class C {<FunctionDecl>
func bar<FunctionSignature><ParameterClause>(<FunctionParameter>_ a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionParameter>) </ParameterClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl>
func bar1<FunctionSignature><ParameterClause>(<FunctionParameter>_ a: <SimpleTypeIdentifier>Float</SimpleTypeIdentifier></FunctionParameter>) </ParameterClause><ReturnClause>-> <SimpleTypeIdentifier>Float </SimpleTypeIdentifier></ReturnClause></FunctionSignature><CodeBlock>{ <ReturnStmt>return <SequenceExpr><PrefixOperatorExpr>-<FloatLiteralExpr>0.6 </FloatLiteralExpr></PrefixOperatorExpr><BinaryOperatorExpr>+ </BinaryOperatorExpr><FloatLiteralExpr>0.1 </FloatLiteralExpr><BinaryOperatorExpr>- </BinaryOperatorExpr><FloatLiteralExpr>0.3 </FloatLiteralExpr></SequenceExpr></ReturnStmt>}</CodeBlock></FunctionDecl><FunctionDecl>

View File

@@ -3,6 +3,12 @@
// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -print-node-kind > %t.withkinds
// RUN: diff -u %S/Outputs/round_trip_parse_gen.swift.withkinds %t.withkinds
import ABC
import A.B.C
@objc import A.B
@objc import typealias A.B
import struct A.B
class C {
func bar(_ a: Int) {}
func bar1(_ a: Float) -> Float { return -0.6 + 0.1 - 0.3 }

View File

@@ -279,4 +279,25 @@ DECL_NODES = [
Child('CloseParen', kind='RightParenToken',
is_optional=True),
]),
Node('AccessPathComponent', kind='Syntax',
children=[
Child('Name', kind='IdentifierToken'),
Child('TrailingDot', kind='PeriodToken', is_optional=True),
]),
Node('AccessPath', kind='SyntaxCollection', element='AccessPathComponent'),
Node('ImportDecl', kind='Decl',
children=[
Child('Attributes', kind='AttributeList', is_optional=True),
Child('ImportTok', kind='ImportToken'),
Child('ImportKind', kind='Token', is_optional=True,
token_choices=[
'TypealiasToken', 'StructToken', 'ClassToken',
'EnumToken', 'ProtocolToken', 'VarToken', 'LetToken',
'FuncToken',
]),
Child('Path', kind='AccessPath'),
]),
]