Skip to content

Replace where parameter in consume(if:) and friends by allowTokenAtStartOfLine #1175

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 1 commit into from
Jan 13, 2023
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
4 changes: 2 additions & 2 deletions Sources/SwiftParser/Attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ extension Parser {
case .required:
shouldParseArgument = true
case .customAttribute:
shouldParseArgument = self.lookahead().isCustomAttributeArgument() && self.at(.leftParen, where: { !$0.isAtStartOfLine })
shouldParseArgument = self.lookahead().isCustomAttributeArgument() && self.at(.leftParen, allowTokenAtStartOfLine: false)
case .optional:
shouldParseArgument = self.at(.leftParen)
}
Expand Down Expand Up @@ -1157,7 +1157,7 @@ extension Parser.Lookahead {
return false
}

if self.at(.leftParen, where: { !$0.isAtStartOfLine }) && self.lookahead().isCustomAttributeArgument() {
if self.at(.leftParen, allowTokenAtStartOfLine: false) && self.lookahead().isCustomAttributeArgument() {
self.skipSingle()
}

Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ extension Parser {
let (unexpectedBeforeName, name) = self.expectIdentifier(allowIdentifierLikeKeywords: false, keywordRecovery: true)

let associatedValue: RawParameterClauseSyntax?
if self.at(.leftParen, where: { !$0.isAtStartOfLine }) {
if self.at(.leftParen, allowTokenAtStartOfLine: false) {
associatedValue = self.parseParameterClause(for: .enumCase)
} else {
associatedValue = nil
Expand Down Expand Up @@ -1016,7 +1016,7 @@ extension Parser {
) -> RawDeinitializerDeclSyntax {
let (unexpectedBeforeDeinitKeyword, deinitKeyword) = self.eat(handle)
var unexpectedNameAndSignature: [RawSyntax?] = []
unexpectedNameAndSignature.append(self.consume(if: .identifier, where: { !$0.isAtStartOfLine }).map(RawSyntax.init))
unexpectedNameAndSignature.append(self.consume(if: .identifier, allowTokenAtStartOfLine: false).map(RawSyntax.init))
if self.at(.leftParen) && !self.currentToken.isAtStartOfLine {
unexpectedNameAndSignature.append(RawSyntax(parseFunctionSignature()))
}
Expand Down Expand Up @@ -1596,7 +1596,7 @@ extension Parser {
}

// diagnose 'throw'/'try'.
if let throwTry = self.consume(ifAny: [.keyword(.throw), .keyword(.try)], where: { !$0.isAtStartOfLine }) {
if let throwTry = self.consume(ifAny: [.keyword(.throw), .keyword(.try)], allowTokenAtStartOfLine: false) {
return throwTry
}

Expand Down Expand Up @@ -1853,7 +1853,7 @@ extension Parser {
case (_, let handle)?:
(unexpectedBeforeName, name) = self.eat(handle)
default:
if let identifier = self.consume(ifAny: [.identifier, .dollarIdentifier], where: { !$0.isAtStartOfLine }) {
if let identifier = self.consume(ifAny: [.identifier, .dollarIdentifier], allowTokenAtStartOfLine: false) {
// Recover if the developer tried to use an identifier as the operator name
unexpectedBeforeName = RawUnexpectedNodesSyntax([identifier], arena: self.arena)
} else {
Expand Down Expand Up @@ -2041,7 +2041,7 @@ extension Parser {
let (unexpectedBeforeColon, colon) = self.expect(.colon)
let (unexpectedBeforeFlag, flag) = self.expectAny([.keyword(.true), .keyword(.false)], default: .keyword(.true))
let unexpectedAfterFlag: RawUnexpectedNodesSyntax?
if flag.isMissing, let unexpectedIdentifier = self.consume(if: .identifier, where: { !$0.isAtStartOfLine }) {
if flag.isMissing, let unexpectedIdentifier = self.consume(if: .identifier, allowTokenAtStartOfLine: false) {
unexpectedAfterFlag = RawUnexpectedNodesSyntax([unexpectedIdentifier], arena: self.arena)
} else {
unexpectedAfterFlag = nil
Expand Down Expand Up @@ -2267,7 +2267,7 @@ extension Parser {
}

// Parse the optional parenthesized argument list.
let leftParen = self.consume(if: .leftParen, where: { !$0.isAtStartOfLine })
let leftParen = self.consume(if: .leftParen, allowTokenAtStartOfLine: false)
let args: [RawTupleExprElementSyntax]
let unexpectedBeforeRightParen: RawUnexpectedNodesSyntax?
let rightParen: RawTokenSyntax?
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ extension Parser {
}

// If there is an expr-call-suffix, parse it and form a call.
if let lparen = self.consume(if: .leftParen, where: { !$0.isAtStartOfLine }) {
if let lparen = self.consume(if: .leftParen, allowTokenAtStartOfLine: false) {
let args = self.parseArgumentListElements(pattern: pattern)
let (unexpectedBeforeRParen, rparen) = self.expect(.rightParen)

Expand Down Expand Up @@ -756,7 +756,7 @@ extension Parser {

// Check for a [expr] suffix.
// Note that this cannot be the start of a new line.
if let lsquare = self.consume(if: .leftSquareBracket, where: { !$0.isAtStartOfLine }) {
if let lsquare = self.consume(if: .leftSquareBracket, allowTokenAtStartOfLine: false) {
let args: [RawTupleExprElementSyntax]
if self.at(.rightSquareBracket) {
args = []
Expand Down Expand Up @@ -997,7 +997,7 @@ extension Parser {
while loopCondition.evaluate(currentToken) {
// Check for a [] or .[] suffix. The latter is only permitted when there
// are no components.
if self.at(.leftSquareBracket, where: { !$0.isAtStartOfLine })
if self.at(.leftSquareBracket, allowTokenAtStartOfLine: false)
|| (components.isEmpty && self.at(.period) && self.peek().rawTokenKind == .leftSquareBracket)
{
// Consume the '.', if it's allowed here.
Expand Down Expand Up @@ -1326,7 +1326,7 @@ extension Parser {
}

// Parse the optional parenthesized argument list.
let leftParen = self.consume(if: .leftParen, where: { !$0.isAtStartOfLine })
let leftParen = self.consume(if: .leftParen, allowTokenAtStartOfLine: false)
let args: [RawTupleExprElementSyntax]
let unexpectedBeforeRightParen: RawUnexpectedNodesSyntax?
let rightParen: RawTokenSyntax?
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftParser/Names.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ extension Parser {
}

// Is the current token a left paren?
guard self.at(.leftParen, where: { !$0.isAtStartOfLine }) else {
guard self.at(.leftParen, allowTokenAtStartOfLine: false) else {
return nil
}

Expand Down
29 changes: 16 additions & 13 deletions Sources/SwiftParser/TokenConsumer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ extension TokenConsumer {
/// - Returns: `true` if the given `kind` matches the current token's kind.
public func at(
_ kind: RawTokenKind,
where condition: (Lexer.Lexeme) -> Bool = { _ in true }
allowTokenAtStartOfLine: Bool = true
) -> Bool {
return RawTokenKindMatch(kind) ~= self.currentToken && condition(self.currentToken)
if !allowTokenAtStartOfLine && self.currentToken.isAtStartOfLine {
return false
}
return RawTokenKindMatch(kind) ~= self.currentToken
}

/// Returns whether the current token is an operator with the given `name`.
Expand All @@ -80,9 +83,12 @@ extension TokenConsumer {
@_spi(RawSyntax)
public func at(
any kinds: [RawTokenKind],
where condition: (Lexer.Lexeme) -> Bool = { _ in true }
allowTokenAtStartOfLine: Bool = true
) -> Bool {
return kinds.contains(where: { RawTokenKindMatch($0) ~= self.currentToken }) && condition(self.currentToken)
if !allowTokenAtStartOfLine && self.currentToken.isAtStartOfLine {
return false
}
return kinds.contains(where: { RawTokenKindMatch($0) ~= self.currentToken })
}

/// Checks whether the parser is currently positioned at any token in `Subset`.
Expand Down Expand Up @@ -139,9 +145,9 @@ extension TokenConsumer {
public mutating func consume(
if kind: RawTokenKind,
remapping: RawTokenKind? = nil,
where condition: (Lexer.Lexeme) -> Bool = { _ in true }
allowTokenAtStartOfLine: Bool = true
) -> Token? {
if self.at(kind, where: condition) {
if self.at(kind, allowTokenAtStartOfLine: allowTokenAtStartOfLine) {
if let remapping = remapping {
return self.consumeAnyToken(remapping: remapping)
} else if case .keyword = kind {
Expand Down Expand Up @@ -173,9 +179,9 @@ extension TokenConsumer {
@_spi(RawSyntax)
public mutating func consume(
ifAny kinds: [RawTokenKind],
where condition: (Lexer.Lexeme) -> Bool = { _ in true }
allowTokenAtStartOfLine: Bool = true
) -> Token? {
if self.at(any: kinds, where: condition) {
if self.at(any: kinds, allowTokenAtStartOfLine: allowTokenAtStartOfLine) {
return self.consumeAnyToken()
}
return nil
Expand Down Expand Up @@ -230,11 +236,8 @@ extension TokenConsumer {
/// - Parameter condition: An additional condition that must be satisfied for
/// the token to be consumed.
/// - Returns: A token of the given kind.
public mutating func expectWithoutRecovery(
_ kind: RawTokenKind,
where condition: (Lexer.Lexeme) -> Bool = { _ in true }
) -> Token {
if let token = self.consume(if: kind, where: condition) {
public mutating func expectWithoutRecovery(_ kind: RawTokenKind) -> Token {
if let token = self.consume(if: kind) {
return token
} else {
return missingToken(kind, text: nil)
Expand Down