Skip to content

Commit 7fe5dd1

Browse files
committed
Lift @_spi To TokenConsumer
When Library Evolution is enabled, this protocol requires its @_spi members to have default implementations. For one, there's not really a reasonable default implementation we could provide. For another, there's not really a way one could conform to this protocol without also having SPI access to raw tokens from the lexer. Lift the SPI restriction to the entire protocol and refactor the conformances a bit.
1 parent 3786720 commit 7fe5dd1

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

Sources/SwiftParser/Lookahead.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extension Parser {
2020
/// arbitrary number of tokens ahead in the input stream. Instances of
2121
/// ``Lookahead`` are distinct from their parent ``Parser`` instances, so
2222
/// any tokens they consume will not be reflected in the parent parser.
23-
public struct Lookahead: TokenConsumer {
23+
public struct Lookahead {
2424
var lexemes: Lexer.LexemeSequence
2525
@_spi(RawSyntax)
2626
public var currentToken: Lexer.Lexeme
@@ -58,6 +58,21 @@ extension Parser {
5858
}
5959
}
6060

61+
@_spi(RawSyntax)
62+
extension Parser.Lookahead: TokenConsumer {
63+
/// Consumes the current token, and asserts that the kind of token that was
64+
/// consumed matches the given kind.
65+
///
66+
/// If the token kind did not match, this function will abort. It is useful
67+
/// to insert structural invariants during parsing.
68+
///
69+
/// - Parameter kind: The kind of token to consume.
70+
/// - Returns: A token of the given kind.
71+
public mutating func eat(_ kind: RawTokenKind) -> Token {
72+
return self.consume(if: kind)!
73+
}
74+
}
75+
6176
extension Parser.Lookahead {
6277
@_spi(RawSyntax)
6378
public func peek() -> Lexer.Lexeme {
@@ -164,18 +179,6 @@ extension Parser.Lookahead {
164179
_ = self.canParseCustomAttribute()
165180
return
166181
}
167-
168-
/// Consumes the current token, and asserts that the kind of token that was
169-
/// consumed matches the given kind.
170-
///
171-
/// If the token kind did not match, this function will abort. It is useful
172-
/// to insert structural invariants during parsing.
173-
///
174-
/// - Parameter kind: The kind of token to consume.
175-
/// - Returns: A token of the given kind.
176-
public mutating func eat(_ kind: RawTokenKind) -> Token {
177-
return self.consume(if: kind)!
178-
}
179182
}
180183

181184
extension Parser.Lookahead {

Sources/SwiftParser/Parser.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ extension Parser {
116116
/// The exception to this is parser lookahead, which is allowed to skip as many
117117
/// tokens as needed to disambiguate a parse. However, because lookahead
118118
/// operates on a copy of the lexical stream, no input tokens are lost..
119-
public struct Parser: TokenConsumer {
119+
public struct Parser {
120120
@_spi(RawSyntax)
121121
public var arena: ParsingSyntaxArena
122122
/// A view of the sequence of lexemes in the input.
@@ -223,6 +223,9 @@ extension Parser {
223223

224224
// MARK: Consuming Tokens
225225

226+
@_spi(RawSyntax)
227+
extension Parser: TokenConsumer {}
228+
226229
extension Parser {
227230
/// Consumes the current token and sets its kind to the given `TokenKind`,
228231
/// then advances the lexer to the next token.

Sources/SwiftParser/TokenConsumer.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
@_spi(RawSyntax) import SwiftSyntax
1414

1515
/// A type that consumes instances of `TokenSyntax`.
16+
@_spi(RawSyntax)
1617
public protocol TokenConsumer {
1718
associatedtype Token
1819
/// The current token syntax being examined by the consumer
19-
@_spi(RawSyntax)
2020
var currentToken: Lexer.Lexeme { get }
2121
/// Whether the current token matches the given kind.
2222
mutating func consumeAnyToken() -> Token
@@ -26,7 +26,6 @@ public protocol TokenConsumer {
2626

2727
/// Synthesize a missing token with `kind`.
2828
/// If `text` is not `nil`, use it for the token's text, otherwise use the token's default text.
29-
@_spi(RawSyntax)
3029
mutating func missingToken(_ kind: RawTokenKind, text: SyntaxText?) -> Token
3130

3231
/// Return the lexeme that will be parsed next.

0 commit comments

Comments
 (0)