Skip to content

Renamed isAt* funcs to at* in Parser and Parser.Lookahead #1996

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
Aug 5, 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
20 changes: 10 additions & 10 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ extension Parser {
/// `copy` etc. are only contextually a keyword if they are followed by an
/// identifier or keyword on the same line. We do this to ensure that we do
/// not break any copy functions defined by users.
private mutating func isContextualExpressionModifier() -> Bool {
private mutating func atContextualExpressionModifier() -> Bool {
return self.peek(
isAt: TokenSpec(.identifier, allowAtStartOfLine: false),
TokenSpec(.dollarIdentifier, allowAtStartOfLine: false),
Expand Down Expand Up @@ -444,7 +444,7 @@ extension Parser {
)

case (.copy, let handle)?:
if !isContextualExpressionModifier() {
if !atContextualExpressionModifier() {
break EXPR_PREFIX
}

Expand All @@ -463,7 +463,7 @@ extension Parser {
)

case (.consume, let handle)?:
if !isContextualExpressionModifier() {
if !atContextualExpressionModifier() {
break EXPR_PREFIX
}

Expand All @@ -486,7 +486,7 @@ extension Parser {
return RawExprSyntax(parsePackExpansionExpr(repeatHandle: handle, flavor, pattern: pattern))

case (.each, let handle)?:
if !isContextualExpressionModifier() {
if !atContextualExpressionModifier() {
break EXPR_PREFIX
}

Expand All @@ -501,7 +501,7 @@ extension Parser {
)

case (.any, _)?:
if !isContextualExpressionModifier() {
if !atContextualExpressionModifier() {
break EXPR_PREFIX
}

Expand Down Expand Up @@ -874,7 +874,7 @@ extension Parser {
}
} while lookahead.at(.poundIf) && lookahead.hasProgressed(&loopProgress)

guard lookahead.isAtStartOfPostfixExprSuffix() else {
guard lookahead.atStartOfPostfixExprSuffix() else {
break
}
}
Expand Down Expand Up @@ -2149,7 +2149,7 @@ extension Parser {
while !self.at(.endOfFile, .rightBrace) && !self.at(.poundEndif, .poundElseif, .poundElse)
&& self.hasProgressed(&elementsProgress)
{
if self.withLookahead({ $0.isAtStartOfSwitchCase(allowRecovery: false) }) {
if self.withLookahead({ $0.atStartOfSwitchCase(allowRecovery: false) }) {
elements.append(.switchCase(self.parseSwitchCase()))
} else if self.canRecoverTo(.poundIf) != nil {
// '#if' in 'case' position can enclose zero or more 'case' or 'default'
Expand Down Expand Up @@ -2206,7 +2206,7 @@ extension Parser {
)
)
)
} else if self.withLookahead({ $0.isAtStartOfSwitchCase(allowRecovery: true) }) {
} else if self.withLookahead({ $0.atStartOfSwitchCase(allowRecovery: true) }) {
elements.append(.switchCase(self.parseSwitchCase()))
} else {
break
Expand All @@ -2217,7 +2217,7 @@ extension Parser {

mutating func parseSwitchCaseBody() -> RawCodeBlockItemListSyntax {
parseCodeBlockItemList(until: {
$0.at(.rightBrace) || $0.at(.poundEndif, .poundElseif, .poundElse) || $0.withLookahead({ $0.isStartOfConditionalSwitchCases() })
$0.at(.rightBrace) || $0.at(.poundEndif, .poundElseif, .poundElse) || $0.withLookahead({ $0.atStartOfConditionalSwitchCases() })
})
}

Expand Down Expand Up @@ -2487,7 +2487,7 @@ extension Parser.Lookahead {
extension Parser.Lookahead {
// Helper function to see if we can parse member reference like suffixes
// inside '#if'.
fileprivate mutating func isAtStartOfPostfixExprSuffix() -> Bool {
fileprivate mutating func atStartOfPostfixExprSuffix() -> Bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep the functions in Lexer.Cursor in the isAt style. The main checking function of Lexer.Cursor also is is(offset: Int = 0, at: CharcterByte), so that fits with the isAt style. The parser just uses at(_: TokenSpec).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! One sec, I'll clean this up.

guard self.at(.period) else {
return false
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftParser/Nominals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ extension Parser {
}

let inheritance: RawInheritanceClauseSyntax?
if self.at(.colon) || self.isAtPythonStyleInheritanceClause() {
if self.at(.colon) || self.atPythonStyleInheritanceClause() {
inheritance = self.parseInheritance()
} else {
inheritance = nil
Expand Down Expand Up @@ -363,7 +363,7 @@ extension Parser {
}

extension Parser {
private mutating func isAtPythonStyleInheritanceClause() -> Bool {
private mutating func atPythonStyleInheritanceClause() -> Bool {
guard self.at(.leftParen) else { return false }
return self.withLookahead {
$0.consume(if: .leftParen)
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftParser/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@
/// operates on a copy of the lexical stream, no input tokens are lost..
public struct Parser {
var arena: ParsingSyntaxArena

/// A view of the sequence of lexemes in the input.
var lexemes: Lexer.LexemeSequence

/// The current token that should be consumed next.
///
/// If the end of the source file is reached, this is `.endOfFile`.
Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftParser/Statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension TokenConsumer {
// misplaced attributes.
_ = lookahead.consumeAttributeList()
}
return lookahead.isStartOfStatement(allowRecovery: allowRecovery)
return lookahead.atStartOfStatement(allowRecovery: allowRecovery)
}
}

Expand Down Expand Up @@ -808,7 +808,7 @@ extension Parser.Lookahead {
///
/// - Note: This function must be kept in sync with `parseStatement()`.
/// - Seealso: ``Parser/parseStatement()``
mutating func isStartOfStatement(allowRecovery: Bool = false) -> Bool {
mutating func atStartOfStatement(allowRecovery: Bool = false) -> Bool {
if (self.at(anyIn: SwitchCaseStart.self) != nil || self.at(.atSign)) && withLookahead({ $0.atStartOfSwitchCaseItem() }) {
// We consider SwitchCaseItems statements so we don't parse the start of a new case item as trailing parts of an expression.
return true
Expand Down Expand Up @@ -884,7 +884,7 @@ extension Parser.Lookahead {

/// Returns whether the parser's current position is the start of a switch case,
/// given that we're in the middle of a switch already.
mutating func isAtStartOfSwitchCase(allowRecovery: Bool = false) -> Bool {
mutating func atStartOfSwitchCase(allowRecovery: Bool = false) -> Bool {
// Check for and consume attributes. The only valid attribute is `@unknown`
// but that's a semantic restriction.
var lookahead = self.lookahead()
Expand Down Expand Up @@ -916,9 +916,9 @@ extension Parser.Lookahead {
}
}

mutating func isStartOfConditionalSwitchCases() -> Bool {
mutating func atStartOfConditionalSwitchCases() -> Bool {
guard self.at(.poundIf) else {
return self.isAtStartOfSwitchCase()
return self.atStartOfSwitchCase()
}

var lookahead = self.lookahead()
Expand All @@ -928,6 +928,6 @@ extension Parser.Lookahead {
// just find the end of the line
lookahead.skipUntilEndOfLine()
} while lookahead.at(.poundIf, .poundElseif, .poundElse) && lookahead.hasProgressed(&loopProgress)
return lookahead.isAtStartOfSwitchCase()
return lookahead.atStartOfSwitchCase()
}
}
8 changes: 4 additions & 4 deletions Sources/SwiftParser/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension Parser {
mutating func parseTypeScalar(misplacedSpecifiers: [RawTokenSyntax] = []) -> RawTypeSyntax {
let (specifier, unexpectedBeforeAttrList, attrList) = self.parseTypeAttributeList(misplacedSpecifiers: misplacedSpecifiers)
var base = RawTypeSyntax(self.parseSimpleOrCompositionType())
if self.withLookahead({ $0.isAtFunctionTypeArrow() }) {
if self.withLookahead({ $0.atFunctionTypeArrow() }) {
var effectSpecifiers = self.parseTypeEffectSpecifiers()
let returnClause = self.parseFunctionReturnClause(effectSpecifiers: &effectSpecifiers, allowNamedOpaqueResultType: false)

Expand Down Expand Up @@ -623,7 +623,7 @@ extension Parser.Lookahead {
return false
}

if self.isAtFunctionTypeArrow() {
if self.atFunctionTypeArrow() {
// Handle type-function if we have an '->' with optional
// 'async' and/or 'throws'.
var loopProgress = LoopProgressCondition()
Expand Down Expand Up @@ -781,7 +781,7 @@ extension Parser.Lookahead {
return self.consume(if: .rightParen) != nil
}

mutating func isAtFunctionTypeArrow() -> Bool {
mutating func atFunctionTypeArrow() -> Bool {
if self.at(.arrow) {
return true
}
Expand All @@ -795,7 +795,7 @@ extension Parser.Lookahead {
var backtrack = self.lookahead()
backtrack.consumeAnyToken()
backtrack.consumeAnyToken()
return backtrack.isAtFunctionTypeArrow()
return backtrack.atFunctionTypeArrow()
}

return false
Expand Down