Skip to content

Commit 10a6ea9

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 aa9fa13 commit 10a6ea9

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
@@ -250,7 +250,7 @@ extension Parser {
250250
var base: RawTypeSyntax
251251
switch self.at(anyIn: TypeBaseStart.self)?.spec {
252252
case .Self, .Any, .identifier:
253-
base = self.parseTypeIdentifier()
253+
base = RawTypeSyntax(self.parseTypeIdentifier())
254254
case .leftParen:
255255
base = RawTypeSyntax(self.parseTupleTypeBody())
256256
case .leftSquare:
@@ -365,9 +365,9 @@ extension Parser {
365365
}
366366

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

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

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

@@ -1343,9 +1341,7 @@ extension Parser {
13431341

13441342
extension Parser {
13451343
mutating func parseResultType() -> RawTypeSyntax {
1346-
if self.currentToken.isEditorPlaceholder {
1347-
return self.parseTypeIdentifier()
1348-
} else if self.at(prefix: "<") {
1344+
if self.at(prefix: "<") && !self.currentToken.isEditorPlaceholder {
13491345
let generics = self.parseGenericParameters()
13501346
let baseType = self.parseType()
13511347
return RawTypeSyntax(
@@ -1362,6 +1358,9 @@ extension Parser {
13621358
return result
13631359
}
13641360

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

0 commit comments

Comments
 (0)