Skip to content

Commit 9f549b7

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 82ae64a commit 9f549b7

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
@@ -249,7 +249,7 @@ extension Parser {
249249
var base: RawTypeSyntax
250250
switch self.at(anyIn: TypeBaseStart.self)?.spec {
251251
case .Self, .Any, .identifier:
252-
base = self.parseTypeIdentifier()
252+
base = RawTypeSyntax(self.parseTypeIdentifier())
253253
case .leftParen:
254254
base = RawTypeSyntax(self.parseTupleTypeBody())
255255
case .leftSquare:
@@ -364,9 +364,9 @@ extension Parser {
364364
}
365365

366366
/// Parse a type identifier.
367-
mutating func parseTypeIdentifier() -> RawTypeSyntax {
367+
mutating func parseTypeIdentifier() -> RawIdentifierTypeSyntax {
368368
if self.at(.keyword(.Any)) {
369-
return RawTypeSyntax(self.parseAnyType())
369+
return self.parseAnyType()
370370
}
371371

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

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

@@ -1342,9 +1340,7 @@ extension Parser {
13421340

13431341
extension Parser {
13441342
mutating func parseResultType() -> RawTypeSyntax {
1345-
if self.currentToken.isEditorPlaceholder {
1346-
return self.parseTypeIdentifier()
1347-
} else if self.at(prefix: "<") {
1343+
if self.at(prefix: "<") && !self.currentToken.isEditorPlaceholder {
13481344
let generics = self.parseGenericParameters()
13491345
let baseType = self.parseType()
13501346
return RawTypeSyntax(
@@ -1361,6 +1357,9 @@ extension Parser {
13611357
return result
13621358
}
13631359

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

0 commit comments

Comments
 (0)