Skip to content

Commit 580734b

Browse files
committed
[NFC] Refactor parseQualifiedTypeIdentifier()
Changes it to share code with `parseTypeIdentifier()` and clean up the member type parsing a little. Also tweaks call sites of `parseTypeIdentifier()`.
1 parent 9d89b32 commit 580734b

File tree

2 files changed

+28
-48
lines changed

2 files changed

+28
-48
lines changed

Sources/SwiftParser/Names.swift

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -188,36 +188,24 @@ extension Parser {
188188
}
189189

190190
mutating func parseQualifiedTypeIdentifier() -> RawTypeSyntax {
191-
if self.at(.keyword(.Any)) {
192-
return RawTypeSyntax(self.parseAnyType())
193-
}
194191

195-
let (unexpectedBeforeName, name) = self.expect(anyIn: IdentifierTypeSyntax.NameOptions.self, default: .identifier)
196-
let generics: RawGenericArgumentClauseSyntax?
197-
if self.at(prefix: "<") {
198-
generics = self.parseGenericArguments()
199-
} else {
200-
generics = nil
192+
let identifierType = self.parseTypeIdentifier()
193+
var result = RawTypeSyntax(identifierType)
194+
195+
// There are no nested types inside `Any`.
196+
if case TokenSpec.keyword(.Any) = identifierType.name {
197+
return result
201198
}
202199

203-
var result = RawTypeSyntax(
204-
RawIdentifierTypeSyntax(
205-
moduleSelector: nil,
206-
unexpectedBeforeName,
207-
name: name,
208-
genericArgumentClause: generics,
209-
arena: self.arena
210-
)
211-
)
200+
func hasAnotherMember() -> Bool {
201+
// If qualified name base type cannot be parsed from the current
202+
// point (i.e. the next type identifier is not followed by a '.'),
203+
// then the next identifier is the final declaration name component.
204+
var lookahead = self.lookahead()
205+
return lookahead.consume(ifPrefix: ".", as: .period) != nil && lookahead.canParseBaseTypeForQualifiedDeclName()
206+
}
212207

213-
// If qualified name base type cannot be parsed from the current
214-
// point (i.e. the next type identifier is not followed by a '.'),
215-
// then the next identifier is the final declaration name component.
216-
var lookahead = self.lookahead()
217-
guard
218-
lookahead.consume(ifPrefix: ".", as: .period) != nil,
219-
lookahead.canParseBaseTypeForQualifiedDeclName()
220-
else {
208+
guard hasAnotherMember() else {
221209
return result
222210
}
223211

@@ -248,14 +236,7 @@ extension Parser {
248236
)
249237
)
250238

251-
// If qualified name base type cannot be parsed from the current
252-
// point (i.e. the next type identifier is not followed by a '.'),
253-
// then the next identifier is the final declaration name component.
254-
var lookahead = self.lookahead()
255-
guard
256-
lookahead.consume(ifPrefix: ".", as: .period) != nil,
257-
lookahead.canParseBaseTypeForQualifiedDeclName()
258-
else {
239+
guard hasAnotherMember() else {
259240
break
260241
}
261242

Sources/SwiftParser/Types.swift

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ extension Parser {
248248
var base: RawTypeSyntax
249249
switch self.at(anyIn: TypeBaseStart.self)?.spec {
250250
case .Self, .Any, .identifier:
251-
base = self.parseTypeIdentifier()
251+
base = RawTypeSyntax(self.parseTypeIdentifier())
252252
case .leftParen:
253253
base = RawTypeSyntax(self.parseTupleTypeBody())
254254
case .leftSquare:
@@ -363,9 +363,9 @@ extension Parser {
363363
}
364364

365365
/// Parse a type identifier.
366-
mutating func parseTypeIdentifier() -> RawTypeSyntax {
366+
mutating func parseTypeIdentifier() -> RawIdentifierTypeSyntax {
367367
if self.at(.keyword(.Any)) {
368-
return RawTypeSyntax(self.parseAnyType())
368+
return self.parseAnyType()
369369
}
370370

371371
let (unexpectedBeforeName, name) = self.expect(anyIn: IdentifierTypeSyntax.NameOptions.self, default: .identifier)
@@ -376,14 +376,12 @@ extension Parser {
376376
generics = nil
377377
}
378378

379-
return RawTypeSyntax(
380-
RawIdentifierTypeSyntax(
381-
moduleSelector: nil,
382-
unexpectedBeforeName,
383-
name: name,
384-
genericArgumentClause: generics,
385-
arena: self.arena
386-
)
379+
return RawIdentifierTypeSyntax(
380+
moduleSelector: nil,
381+
unexpectedBeforeName,
382+
name: name,
383+
genericArgumentClause: generics,
384+
arena: self.arena
387385
)
388386
}
389387

@@ -1318,9 +1316,7 @@ extension Parser {
13181316

13191317
extension Parser {
13201318
mutating func parseResultType() -> RawTypeSyntax {
1321-
if self.currentToken.isEditorPlaceholder {
1322-
return self.parseTypeIdentifier()
1323-
} else if self.at(prefix: "<") {
1319+
if self.at(prefix: "<") && !self.currentToken.isEditorPlaceholder {
13241320
let generics = self.parseGenericParameters()
13251321
let baseType = self.parseType()
13261322
return RawTypeSyntax(
@@ -1337,6 +1333,9 @@ extension Parser {
13371333
return result
13381334
}
13391335

1336+
// The rest of this tries to recover from a missing left square bracket like ` -> [Int]]? {`. We can do this for
1337+
// result types because we know there isn't an enclosing expression context.
1338+
13401339
// If the right square bracket is at a new line, we should just return the result
13411340
if let rightSquare = self.consume(if: TokenSpec(.rightSquare, allowAtStartOfLine: false)) {
13421341
result = RawTypeSyntax(

0 commit comments

Comments
 (0)