Skip to content

Commit e8da930

Browse files
authored
Merge pull request #1095 from CodaFi/library-card
Prepare SwiftParser for Library Evolution
2 parents 2e978f3 + 7fe5dd1 commit e8da930

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
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.

Sources/SwiftSyntax/Raw/RawSyntax.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public struct RawSyntax {
133133
}
134134

135135
internal var payload: RawSyntaxData.Payload {
136-
_read { yield rawData.payload }
136+
get { rawData.payload }
137137
}
138138
}
139139

0 commit comments

Comments
 (0)