Skip to content

Prepare SwiftParser for Library Evolution #1095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions Sources/SwiftParser/Lookahead.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension Parser {
/// arbitrary number of tokens ahead in the input stream. Instances of
/// ``Lookahead`` are distinct from their parent ``Parser`` instances, so
/// any tokens they consume will not be reflected in the parent parser.
public struct Lookahead: TokenConsumer {
public struct Lookahead {
var lexemes: Lexer.LexemeSequence
@_spi(RawSyntax)
public var currentToken: Lexer.Lexeme
Expand Down Expand Up @@ -58,6 +58,21 @@ extension Parser {
}
}

@_spi(RawSyntax)
extension Parser.Lookahead: TokenConsumer {
/// Consumes the current token, and asserts that the kind of token that was
/// consumed matches the given kind.
///
/// If the token kind did not match, this function will abort. It is useful
/// to insert structural invariants during parsing.
///
/// - Parameter kind: The kind of token to consume.
/// - Returns: A token of the given kind.
public mutating func eat(_ kind: RawTokenKind) -> Token {
return self.consume(if: kind)!
}
}

extension Parser.Lookahead {
@_spi(RawSyntax)
public func peek() -> Lexer.Lexeme {
Expand Down Expand Up @@ -164,18 +179,6 @@ extension Parser.Lookahead {
_ = self.canParseCustomAttribute()
return
}

/// Consumes the current token, and asserts that the kind of token that was
/// consumed matches the given kind.
///
/// If the token kind did not match, this function will abort. It is useful
/// to insert structural invariants during parsing.
///
/// - Parameter kind: The kind of token to consume.
/// - Returns: A token of the given kind.
public mutating func eat(_ kind: RawTokenKind) -> Token {
return self.consume(if: kind)!
}
}

extension Parser.Lookahead {
Expand Down
5 changes: 4 additions & 1 deletion Sources/SwiftParser/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ extension Parser {
/// The exception to this is parser lookahead, which is allowed to skip as many
/// tokens as needed to disambiguate a parse. However, because lookahead
/// operates on a copy of the lexical stream, no input tokens are lost..
public struct Parser: TokenConsumer {
public struct Parser {
@_spi(RawSyntax)
public var arena: ParsingSyntaxArena
/// A view of the sequence of lexemes in the input.
Expand Down Expand Up @@ -223,6 +223,9 @@ extension Parser {

// MARK: Consuming Tokens

@_spi(RawSyntax)
extension Parser: TokenConsumer {}

extension Parser {
/// Consumes the current token and sets its kind to the given `TokenKind`,
/// then advances the lexer to the next token.
Expand Down
3 changes: 1 addition & 2 deletions Sources/SwiftParser/TokenConsumer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
@_spi(RawSyntax) import SwiftSyntax

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

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

/// Return the lexeme that will be parsed next.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftSyntax/Raw/RawSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public struct RawSyntax {
}

internal var payload: RawSyntaxData.Payload {
_read { yield rawData.payload }
get { rawData.payload }
}
}

Expand Down