Skip to content

Commit 4b4ae9c

Browse files
committed
A couple of miscellaneous implementation improvements in the parser
# Conflicts: # Sources/SwiftParser/Expressions.swift
1 parent ad449ac commit 4b4ae9c

File tree

5 files changed

+51
-23
lines changed

5 files changed

+51
-23
lines changed

Sources/SwiftParser/Expressions.swift

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -588,21 +588,18 @@ extension Parser {
588588
// First check to see if we have the start of a regex literal `/.../`.
589589
// tryLexRegexLiteral(/*forUnappliedOperator*/ false)
590590

591-
switch self.currentToken {
592591
// Try parse an 'if' or 'switch' as an expression. Note we do this here in
593592
// parseUnaryExpression as we don't allow postfix syntax to hang off such
594593
// expressions to avoid ambiguities such as postfix '.member', which can
595594
// currently be parsed as a static dot member for a result builder.
596-
case TokenSpec(.switch):
595+
if self.at(.keyword(.switch)) {
597596
return RawExprSyntax(
598597
parseSwitchExpression(switchHandle: .constant(.keyword(.switch)))
599598
)
600-
case TokenSpec(.if):
599+
} else if self.at(.keyword(.if)) {
601600
return RawExprSyntax(
602601
parseIfExpression(ifHandle: .constant(.keyword(.if)))
603602
)
604-
default:
605-
break
606603
}
607604

608605
switch self.at(anyIn: ExpressionPrefixOperator.self) {
@@ -950,7 +947,7 @@ extension Parser {
950947
// Check if the first '#if' body starts with '.' <identifier>, and parse
951948
// it as a "postfix ifconfig expression".
952949
do {
953-
var backtrack = self.lookahead()
950+
var lookahead = self.lookahead()
954951
// Skip to the first body. We may need to skip multiple '#if' directives
955952
// since we support nested '#if's. e.g.
956953
// baseExpr
@@ -959,13 +956,13 @@ extension Parser {
959956
// .someMember
960957
var loopProgress = LoopProgressCondition()
961958
repeat {
962-
backtrack.eat(.poundIfKeyword)
963-
while !backtrack.at(.endOfFile) && !backtrack.currentToken.isAtStartOfLine {
964-
backtrack.skipSingle()
959+
lookahead.eat(.poundIfKeyword)
960+
while !lookahead.at(.endOfFile) && !lookahead.atStartOfLine {
961+
lookahead.skipSingle()
965962
}
966-
} while backtrack.at(.poundIfKeyword) && loopProgress.evaluate(backtrack.currentToken)
963+
} while lookahead.at(.poundIfKeyword) && loopProgress.evaluate(backtrack.currentToken)
967964

968-
guard backtrack.isAtStartOfPostfixExprSuffix() else {
965+
guard lookahead.isAtStartOfPostfixExprSuffix() else {
969966
break
970967
}
971968
}

Sources/SwiftParser/Nominals.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ extension Parser {
223223
inheritanceClause: nil,
224224
genericWhereClause: nil,
225225
memberBlock: RawMemberDeclBlockSyntax(
226-
leftBrace: RawTokenSyntax(missing: .leftBrace, arena: self.arena),
226+
leftBrace: missingToken(.leftBrace),
227227
members: RawMemberDeclListSyntax(elements: [], arena: self.arena),
228-
rightBrace: RawTokenSyntax(missing: .rightBrace, arena: self.arena),
228+
rightBrace: missingToken(.rightBrace),
229229
arena: self.arena
230230
),
231231
arena: self.arena

Sources/SwiftParser/Statements.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ extension Parser {
921921

922922
// label-name → identifier
923923
mutating func parseOptionalControlTransferTarget() -> RawTokenSyntax? {
924-
guard !self.currentToken.isAtStartOfLine else {
924+
guard !self.atStartOfLine else {
925925
return nil
926926
}
927927

Sources/SwiftParser/TokenConsumer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ extension TokenConsumer {
121121
/// If this is the case, return the `Subset` case that the parser is positioned in
122122
/// as well as a handle to consume that token.
123123
@inline(__always)
124-
mutating func at<SpecSet: TokenSpecSet>(anyIn specSet: SpecSet.Type) -> (SpecSet, TokenConsumptionHandle)? {
124+
mutating func at<SpecSet: TokenSpecSet>(anyIn specSet: SpecSet.Type) -> (spec: SpecSet, handle: TokenConsumptionHandle)? {
125125
#if SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION
126126
if shouldRecordAlternativeTokenChoices {
127127
recordAlternativeTokenChoice(for: self.currentToken, choices: specSet.allCases.map(\.spec))

Sources/SwiftParser/Types.swift

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,50 @@ extension Parser {
235235
mutating func parseSimpleType(
236236
stopAtFirstPeriod: Bool = false
237237
) -> RawTypeSyntax {
238+
enum TypeBaseStart: TokenSpecSet {
239+
case `Self`
240+
case `Any`
241+
case identifier
242+
case leftParen
243+
case leftSquare
244+
case wildcard
245+
246+
init?(lexeme: Lexer.Lexeme) {
247+
switch PrepareForKeywordMatch(lexeme) {
248+
case .keyword(.Self): self = .Self
249+
case .keyword(.Any): self = .Any
250+
case .identifier: self = .identifier
251+
case .leftParen: self = .leftParen
252+
case .leftSquare: self = .leftSquare
253+
case .wildcard: self = .wildcard
254+
default: return nil
255+
}
256+
}
257+
258+
var spec: TokenSpec {
259+
switch self {
260+
case .Self: return .keyword(.Self)
261+
case .Any: return .keyword(.Any)
262+
case .identifier: return .identifier
263+
case .leftParen: return .leftParen
264+
case .leftSquare: return .leftSquare
265+
case .wildcard: return .wildcard
266+
}
267+
}
268+
269+
}
270+
238271
var base: RawTypeSyntax
239-
switch self.currentToken {
240-
case TokenSpec(.Self),
241-
TokenSpec(.Any),
242-
TokenSpec(.identifier):
272+
switch self.at(anyIn: TypeBaseStart.self)?.spec {
273+
case .Self, .Any, .identifier:
243274
base = self.parseTypeIdentifier()
244-
case TokenSpec(.leftParen):
275+
case .leftParen:
245276
base = RawTypeSyntax(self.parseTupleTypeBody())
246-
case TokenSpec(.leftSquare):
277+
case .leftSquare:
247278
base = RawTypeSyntax(self.parseCollectionType())
248-
case TokenSpec(.wildcard):
279+
case .wildcard:
249280
base = RawTypeSyntax(self.parsePlaceholderType())
250-
default:
281+
case nil:
251282
return RawTypeSyntax(RawMissingTypeSyntax(arena: self.arena))
252283
}
253284

0 commit comments

Comments
 (0)