1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if swift(>=6)
public import SwiftSyntax
#else
import SwiftSyntax
#endif
extension ExprSyntax {
// Is this an unresolved explicit cast?
fileprivate var isUnresolvedExplicitCast: Bool {
self.is(UnresolvedIsExprSyntax.self) || self.is(UnresolvedAsExprSyntax.self)
}
}
extension OperatorTable {
private struct PrecedenceBound {
let groupName: PrecedenceGroupName?
let isStrict: Bool
let syntax: Syntax?
}
/// Determine whether we should consider an operator in the given group
/// based on the specified bound.
private func shouldConsiderOperator(
fromGroup groupName: PrecedenceGroupName?,
in bound: PrecedenceBound,
operatorSyntax: Syntax,
errorHandler: OperatorErrorHandler = { throw $0 }
) rethrows -> Bool {
guard let boundGroupName = bound.groupName else {
return true
}
guard let groupName = groupName else {
return false
}
if groupName == boundGroupName {
return !bound.isStrict
}
return try precedenceGraph.precedence(
relating: groupName,
to: boundGroupName,
startSyntax: operatorSyntax,
endSyntax: bound.syntax!,
errorHandler: errorHandler
) != .lowerThan
}
/// Look up the precedence group for the given expression syntax.
private func lookupPrecedence(
of expr: ExprSyntax,
errorHandler: OperatorErrorHandler = { throw $0 }
) rethrows -> PrecedenceGroupName? {
// A binary operator.
if let binaryExpr = expr.as(BinaryOperatorExprSyntax.self) {
let operatorName = binaryExpr.operator.text
return try lookupOperatorPrecedenceGroupName(
operatorName,
referencedFrom: Syntax(binaryExpr.operator),
errorHandler: errorHandler
)
}
// The ternary operator has a fixed precedence group name.
if expr.is(UnresolvedTernaryExprSyntax.self) {
return "TernaryPrecedence"
}
// An assignment operator has fixed precedence.
if expr.is(AssignmentExprSyntax.self) {
return "AssignmentPrecedence"
}
// Cast operators have fixed precedence.
if expr.isUnresolvedExplicitCast {
return "CastingPrecedence"
}
// The arrow operator has fixed precedence.
if expr.is(ArrowExprSyntax.self) {
return "FunctionArrowPrecedence"
}
return nil
}
/// Form a binary operation expression for, e.g., a + b.
@_spi(Testing)
public static func makeBinaryOperationExpr(
lhs: ExprSyntax,
op: ExprSyntax,
rhs: ExprSyntax
) -> ExprSyntax {
// If the left-hand side is a "try" or "await", hoist it up to encompass
// the right-hand side as well.
if let tryExpr = lhs.as(TryExprSyntax.self) {
return ExprSyntax(
TryExprSyntax(
tryExpr.unexpectedBeforeTryKeyword,
tryKeyword: tryExpr.tryKeyword,
tryExpr.unexpectedBetweenTryKeywordAndQuestionOrExclamationMark,
questionOrExclamationMark: tryExpr.questionOrExclamationMark,
tryExpr.unexpectedBetweenQuestionOrExclamationMarkAndExpression,
expression: makeBinaryOperationExpr(
lhs: tryExpr.expression,
op: op,
rhs: rhs
)
)
)
}
if let awaitExpr = lhs.as(AwaitExprSyntax.self) {
return ExprSyntax(
AwaitExprSyntax(
awaitExpr.unexpectedBeforeAwaitKeyword,
awaitKeyword: awaitExpr.awaitKeyword,
awaitExpr.unexpectedBetweenAwaitKeywordAndExpression,
expression: makeBinaryOperationExpr(
lhs: awaitExpr.expression,
op: op,
rhs: rhs
)
)
)
}
// The form of the binary operation depends on the operator itself,
// which will be one of the unresolved infix operators.
// An operator such as '+'.
if let binaryOperatorExpr = op.as(BinaryOperatorExprSyntax.self) {
return ExprSyntax(
InfixOperatorExprSyntax(
leftOperand: lhs,
operator: ExprSyntax(binaryOperatorExpr),
rightOperand: rhs
)
)
}
// A ternary operator x ? y : z.
if let ternaryExpr = op.as(UnresolvedTernaryExprSyntax.self) {
return ExprSyntax(
TernaryExprSyntax(
condition: lhs,
ternaryExpr.unexpectedBeforeQuestionMark,
questionMark: ternaryExpr.questionMark,
ternaryExpr.unexpectedBetweenQuestionMarkAndThenExpression,
thenExpression: ternaryExpr.thenExpression,
ternaryExpr.unexpectedBetweenThenExpressionAndColon,
colon: ternaryExpr.colon,
elseExpression: rhs
)
)
}
// An assignment operator x = y.
if let assignExpr = op.as(AssignmentExprSyntax.self) {
return ExprSyntax(
InfixOperatorExprSyntax(
leftOperand: lhs,
operator: ExprSyntax(assignExpr),
rightOperand: rhs
)
)
}
// An "is" type check.
if let isExpr = op.as(UnresolvedIsExprSyntax.self) {
// FIXME: Do we actually have a guarantee that the right-hand side is a
// type expression here?
return ExprSyntax(
IsExprSyntax(
expression: lhs,
isExpr.unexpectedBeforeIsKeyword,
isKeyword: isExpr.isKeyword,
type: rhs.as(TypeExprSyntax.self)!.type
)
)
}
// An "as" cast.
if let asExpr = op.as(UnresolvedAsExprSyntax.self) {
// FIXME: Do we actually have a guarantee that the right-hand side is a
// type expression here?
return ExprSyntax(
AsExprSyntax(
expression: lhs,
asExpr.unexpectedBeforeAsKeyword,
asKeyword: asExpr.asKeyword,
asExpr.unexpectedBetweenAsKeywordAndQuestionOrExclamationMark,
questionOrExclamationMark: asExpr.questionOrExclamationMark,
type: rhs.as(TypeExprSyntax.self)!.type
)
)
}
// An arrow expression (->).
if let arrowExpr = op.as(ArrowExprSyntax.self) {
return ExprSyntax(
InfixOperatorExprSyntax(
leftOperand: lhs,
operator: ExprSyntax(arrowExpr),
rightOperand: rhs
)
)
}
// FIXME: Fallback that we should never need
fatalError("Unknown binary operator")
}
/// Determine the associativity between two precedence groups.
private func associativity(
firstGroup: PrecedenceGroupName?,
firstOperatorSyntax: Syntax,
secondGroup: PrecedenceGroupName?,
secondOperatorSyntax: Syntax,
errorHandler: OperatorErrorHandler = { throw $0 }
) rethrows -> Associativity {
guard let firstGroup = firstGroup, let secondGroup = secondGroup else {
return .none
}
// If we have the same group, query its associativity.
if firstGroup == secondGroup {
guard let group = precedenceGraph.lookupGroup(firstGroup) else {
try errorHandler(
.missingGroup(firstGroup, referencedFrom: firstOperatorSyntax)
)
return .none
}
return group.associativity
}
let prec = try precedenceGraph.precedence(
relating: firstGroup,
to: secondGroup,
startSyntax: firstOperatorSyntax,
endSyntax: secondOperatorSyntax,
errorHandler: errorHandler
)
switch prec {
case .higherThan:
return .left
case .lowerThan:
return .right
case .unrelated:
return .none
}
}
/// "Fold" an expression sequence where the left-hand side has been broken
/// out and (potentially) folded somewhat, and the "rest" of the sequence is
/// consumed along the way
private func fold(
_ lhs: ExprSyntax,
rest: inout Slice<ExprListSyntax>,
bound: PrecedenceBound,
errorHandler: OperatorErrorHandler = { throw $0 }
) rethrows -> ExprSyntax {
if rest.isEmpty { return lhs }
// We mutate the left-hand side in place as we fold the sequence.
var lhs = lhs
/// Get the operator, if appropriate to this pass.
func getNextOperator() throws -> (ExprSyntax, PrecedenceGroupName?)? {
let op = rest.first!
// If the operator's precedence is lower than the minimum, stop here.
let opPrecedence = try lookupPrecedence(
of: op,
errorHandler: errorHandler
)
if try !shouldConsiderOperator(
fromGroup: opPrecedence,
in: bound,
operatorSyntax: Syntax(op)
) {
return nil
}
return (op, opPrecedence)
}
// Extract out the first operator.
guard var (op1, op1Precedence) = try getNextOperator() else {
return lhs
}
// We will definitely be consuming at least one operator.
// Pull out the prospective RHS and slice off the first two elements.
rest = rest.dropFirst()
var rhs = rest.first!
rest = rest.dropFirst()
while !rest.isEmpty {
// If the operator is a cast operator, the RHS can't extend past the type
// that's part of the cast production.
if op1.isUnresolvedExplicitCast {
lhs = Self.makeBinaryOperationExpr(lhs: lhs, op: op1, rhs: rhs)
guard let (newOp1, newOp1Precedence) = try getNextOperator() else {
return lhs
}
op1 = newOp1
op1Precedence = newOp1Precedence
rest = rest.dropFirst()
rhs = rest.first!
rest = rest.dropFirst()
continue
}
// Pull out the next binary operator.
let op2 = rest.first!
let op2Precedence = try lookupPrecedence(
of: op2,
errorHandler: errorHandler
)
// If the second operator's precedence is lower than the
// precedence bound, break out of the loop.
if try !shouldConsiderOperator(
fromGroup: op2Precedence,
in: bound,
operatorSyntax: Syntax(op1),
errorHandler: errorHandler
) {
break
}
let associativity = try associativity(
firstGroup: op1Precedence,
firstOperatorSyntax: Syntax(op1),
secondGroup: op2Precedence,
secondOperatorSyntax: Syntax(op2),
errorHandler: errorHandler
)
switch associativity {
case .left:
// Apply left-associativity immediately by folding the first two
// operands.
lhs = Self.makeBinaryOperationExpr(lhs: lhs, op: op1, rhs: rhs)
op1 = op2
op1Precedence = op2Precedence
rest = rest.dropFirst()
rhs = rest.first!
rest = rest.dropFirst()
case .right where op1Precedence != op2Precedence:
// If the first operator's precedence is lower than the second
// operator's precedence, recursively fold all such
// higher-precedence operators starting from this point, then
// repeat.
rhs = try fold(
rhs,
rest: &rest,
bound: PrecedenceBound(
groupName: op1Precedence,
isStrict: true,
syntax: Syntax(op1)
),
errorHandler: errorHandler
)
case .right:
// Apply right-associativity by recursively folding operators
// starting from this point, then immediately folding the LHS and RHS.
rhs = try fold(
rhs,
rest: &rest,
bound: PrecedenceBound(
groupName: op1Precedence,
isStrict: false,
syntax: Syntax(op1)
),
errorHandler: errorHandler
)
lhs = Self.makeBinaryOperationExpr(lhs: lhs, op: op1, rhs: rhs)
// If we've drained the entire sequence, we're done.
if rest.isEmpty {
return lhs
}
// Otherwise, start all over with our new LHS.
return try fold(
lhs,
rest: &rest,
bound: bound,
errorHandler: errorHandler
)
case .none:
// If we ended up here, it's because we're either:
// - missing precedence groups,
// - have unordered precedence groups, or
// - have the same precedence group with no associativity.
if let op1Precedence,
let op2Precedence
{
try errorHandler(
.incomparableOperators(
leftOperator: op1,
leftPrecedenceGroup: op1Precedence,
rightOperator: op2,
rightPrecedenceGroup: op2Precedence
)
)
}
// Recover by folding arbitrarily at this operator, then continuing.
lhs = Self.makeBinaryOperationExpr(lhs: lhs, op: op1, rhs: rhs)
return try fold(lhs, rest: &rest, bound: bound, errorHandler: errorHandler)
}
}
// Fold LHS and RHS together and declare completion.
return Self.makeBinaryOperationExpr(lhs: lhs, op: op1, rhs: rhs)
}
/// "Fold" a sequence expression into a structured syntax tree.
///
/// A sequence expression results from parsing an expression involving
/// infix operators, such as `x + y * z`. Swift's grammar does not
/// involve operator precedence, so a sequence expression is a flat list
/// of all of the terms `x`, `+`, `y`, `*`, and `z`. This operation folds
/// a single sequence expression into a structured syntax tree that
/// represents the same source code, but describes the order of operations
/// as if the expression has been parenthesized `x + (y * z)`.
public func foldSingle(
_ sequence: SequenceExprSyntax,
errorHandler: OperatorErrorHandler = { throw $0 }
) rethrows -> ExprSyntax {
let lhs = sequence.elements.first!
var rest = sequence.elements.dropFirst()
return try fold(
lhs,
rest: &rest,
bound: PrecedenceBound(groupName: nil, isStrict: false, syntax: nil),
errorHandler: errorHandler
)
}
/// Syntax rewriter that folds all of the sequence expressions it
/// encounters.
private class SequenceFolder: SyntaxRewriter {
/// The first operator precedence that caused the error handler to
/// also throw.
var firstFatalError: OperatorError? = nil
let opPrecedence: OperatorTable
let errorHandler: OperatorErrorHandler
init(
opPrecedence: OperatorTable,
errorHandler: @escaping OperatorErrorHandler
) {
self.opPrecedence = opPrecedence
self.errorHandler = errorHandler
super.init(viewMode: .fixedUp)
}
override func visitAny(_ node: Syntax) -> Syntax? {
/// Return a non-nil value from `visitAny` to indicate to the SyntaxRewriter
/// that it shouldn't visit any of these node's children.
if !node.hasSequenceExpr {
return node
}
return nil
}
override func visit(_ node: SequenceExprSyntax) -> ExprSyntax {
// If the error handler threw in response to an error, don't
// continue folding.
if firstFatalError != nil {
return ExprSyntax(node)
}
let newNode = super.visit(node).as(SequenceExprSyntax.self)!
// If the error handler threw in response to an error, don't
// continue folding.
if firstFatalError != nil {
return ExprSyntax(newNode)
}
// Try to fold this sequence expression.
do {
return try opPrecedence.foldSingle(newNode) { origError in
do {
try errorHandler(origError)
} catch {
firstFatalError = origError
throw error
}
}
} catch {
return ExprSyntax(newNode)
}
}
}
/// Fold all sequence expressions within the given syntax tree into a
/// structured syntax tree.
///
/// This operation replaces all sequence expressions in the given syntax
/// tree with structured syntax trees, by walking the tree and invoking
/// `foldSingle` on each sequence expression it encounters. Use this to
/// provide structure to an entire tree.
///
/// Due to the inability to express the implementation of this rethrowing
/// function, a throwing error handler will end up being called twice with
/// the first error that causes it to be thrown. The first call will stop
/// the operation, then the second must also throw.
public func foldAll(
_ node: some SyntaxProtocol,
errorHandler: OperatorErrorHandler = { throw $0 }
) rethrows -> Syntax {
return try withoutActuallyEscaping(errorHandler) { errorHandler in
let folder = SequenceFolder(
opPrecedence: self,
errorHandler: errorHandler
)
let result = folder.rewrite(node)
// If the sequence folder encountered an error that caused the error
// handler to throw, invoke the error handler again with the original
// error.
if let origFatalError = folder.firstFatalError {
try errorHandler(origFatalError)
fatalError("error handler did not throw again after \(origFatalError)")
}
return result
}
}
}
|