[SwiftOperators] Expose precedence-group comparison on OperatorTable#3380
Open
devdattatalele wants to merge 1 commit into
Open
[SwiftOperators] Expose precedence-group comparison on OperatorTable#3380devdattatalele wants to merge 1 commit into
devdattatalele wants to merge 1 commit into
Conversation
Add a public precedence(of:relativeTo:referencedFrom:errorHandler:) method to OperatorTable that reports the relationship between two precedence groups, and make the Precedence enum (and its `flipped` member) public so callers can consume the result. The underlying comparison already lived on the internal PrecedenceGraph.precedence(relating:to:startSyntax:endSyntax:) helper; this change simply lifts that capability to the public surface without altering the algorithm. Motivated by an existing use case in swift-format where clients want to check the precedence of an operator relative to another one (see issue swiftlang#2258 and swiftlang/swift-format#647). Prior to this PR, callers had no supported way to obtain that information short of re-implementing the traversal. Tests exercise the new API in three regimes on the standard operator table: a direct higherThan/lowerThan relationship (MultiplicationPrecedence vs AdditionPrecedence), the same-group case, and an unrelated-groups case (DefaultPrecedence vs LogicalDisjunctionPrecedence, both of which sit above TernaryPrecedence with no other links between them). A separate test covers Precedence.flipped for all three cases. Resolves swiftlang#2258.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a public API to
OperatorTablethat reports the relationship between two precedence groups, so callers can determine whether one group has higher, lower, or unrelated precedence to another without re-implementing the traversal:The
Precedenceenum (and itsflippedaccessor) are also made public so callers can consume the result.Motivation
This resolves #2258. The existing use case is swift-format PR apple/swift-format#647, where a caller wanted to compare the precedence of two operators. That comparison lived on the internal
PrecedenceGraph.precedence(relating:to:startSyntax:endSyntax:)helper, so callers had no supported way to obtain the information short of re-implementing the traversal.This PR does not change the algorithm — it lifts the existing internal capability to the public surface.
Design notes
OperatorTable+Semantics.swiftbecause that file already declarespublic import SwiftSyntax, so its public API can acceptSyntaxin the signature (needed to preserve the same error-reporting semantics as the internal helper).referencedFrom:parameter mirrors the pattern used by the existing internallookupOperatorPrecedenceGroupName(_:referencedFrom:errorHandler:)helper onOperatorTable. Callers that already have a syntax node (e.g. the operator token being compared) simply pass it through.errorHandlerdefault matches the rest of the module:{ throw \$0 }(re-throw the underlyingOperatorError).Test plan
Three unit tests in
OperatorTableTests:testPrecedenceGroupComparisonexercises the new API on the standard operator table across four scenarios:higherThanrelationship (MultiplicationPrecedencevsAdditionPrecedence)..lowerThan..unrelated.TernaryPrecedencewith no other links between them (DefaultPrecedencevsLogicalDisjunctionPrecedence) returning.unrelated.testPrecedenceGroupFlippedcoversPrecedence.flippedfor all three cases.Files changed
Sources/SwiftOperators/PrecedenceGraph.swift: markPrecedenceandPrecedence.flippedpublic— +2 / -2.Sources/SwiftOperators/OperatorTable+Semantics.swift: addprecedence(of:relativeTo:referencedFrom:errorHandler:)— +29 / -0.Tests/SwiftOperatorsTest/OperatorTableTests.swift: add two new tests — +55 / -0.Total: +86 / -2.
Resolves #2258.