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 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
|
//===----------------------------------------------------------------------===//
//
// 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)
@_spi(RawSyntax) public import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif
// MARK: - TokenSpecSet
@_spi(Diagnostics)
public enum AsyncEffectSpecifier: TokenSpecSet {
case async
case await
case reasync
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
// We'll take 'await' too for recovery but they have to be on the same line
// as the declaration they're modifying.
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.async): self = .async
case TokenSpec(.await, allowAtStartOfLine: false): self = .await
case TokenSpec(.reasync): self = .reasync
default: return nil
}
}
@_spi(Diagnostics)
public init?(token: TokenSyntax) {
switch token.tokenKind {
case .keyword(.async): self = .async
case .keyword(.await): self = .await
case .keyword(.reasync): self = .reasync
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .async: return .keyword(.async)
case .await: return TokenSpec(.await, allowAtStartOfLine: false)
case .reasync: return .keyword(.reasync)
}
}
}
@_spi(Diagnostics)
public enum ThrowsEffectSpecifier: TokenSpecSet {
case `rethrows`
case `throw`
case `throws`
case `try`
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
// We'll take 'throw' and 'try' too for recovery but they have to
// be on the same line as the declaration they're modifying.
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.rethrows): self = .rethrows
case TokenSpec(.throw, allowAtStartOfLine: false): self = .throw
case TokenSpec(.throws): self = .throws
case TokenSpec(.try, allowAtStartOfLine: false): self = .try
default: return nil
}
}
@_spi(Diagnostics)
public init?(token: TokenSyntax) {
switch token.tokenKind {
case .keyword(.rethrows): self = .rethrows
case .keyword(.throw): self = .throw
case .keyword(.throws): self = .throws
case .keyword(.try): self = .try
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .rethrows: return .keyword(.rethrows)
case .throw: return TokenSpec(.throw, allowAtStartOfLine: false)
case .throws: return .keyword(.throws)
case .try: return TokenSpec(.try, allowAtStartOfLine: false)
}
}
}
@_spi(Diagnostics)
public enum EffectSpecifier: TokenSpecSet {
case asyncSpecifier(AsyncEffectSpecifier)
case throwsSpecifier(ThrowsEffectSpecifier)
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
if let subset = AsyncEffectSpecifier(lexeme: lexeme, experimentalFeatures: experimentalFeatures) {
self = .asyncSpecifier(subset)
} else if let subset = ThrowsEffectSpecifier(lexeme: lexeme, experimentalFeatures: experimentalFeatures) {
self = .throwsSpecifier(subset)
} else {
return nil
}
}
@_spi(Diagnostics)
public init?(token: TokenSyntax) {
if let subset = AsyncEffectSpecifier(token: token) {
self = .asyncSpecifier(subset)
} else if let subset = ThrowsEffectSpecifier(token: token) {
self = .throwsSpecifier(subset)
} else {
return nil
}
}
@_spi(Diagnostics)
public static var allCases: [EffectSpecifier] {
return AsyncEffectSpecifier.allCases.map(Self.asyncSpecifier)
+ ThrowsEffectSpecifier.allCases.map(Self.throwsSpecifier)
}
var spec: TokenSpec {
switch self {
case .asyncSpecifier(let underlyingKind): return underlyingKind.spec
case .throwsSpecifier(let underlyingKind): return underlyingKind.spec
}
}
var isThrowsSpecifier: Bool {
switch self {
case .asyncSpecifier: return false
case .throwsSpecifier: return true
}
}
}
// MARK: - EffectSpecifiersTrait
/// Raw syntax nodes don't have traits (because usually we don't need them).
/// Specify the effect specifiers trait manually as a one off.
protocol RawMisplacedEffectSpecifiersTrait {
/// The token kinds that should be consumed as misspelled `asyncSpecifier`.
/// Should be a subset of ``AsyncEffectSpecifier``.
associatedtype MisspelledAsyncTokenKinds: TokenSpecSet
/// The token kinds that we can consume as a correct `asyncSpecifier`.
/// Should be a subset of ``AsyncEffectSpecifier``.
associatedtype CorrectAsyncTokenKinds: TokenSpecSet
/// The token kinds that should be consumed as misspelled `throwsSpecifier`.
/// Should be a subset of ``ThrowsEffectSpecifier``.
associatedtype MisspelledThrowsTokenKinds: TokenSpecSet
/// The token kinds that we can consume as a correct `throwsSpecifier`.
/// Should be a subset of ``ThrowsEffectSpecifier``.
associatedtype CorrectThrowsTokenKinds: TokenSpecSet
var asyncSpecifier: RawTokenSyntax? { get }
var throwsClause: RawThrowsClauseSyntax? { get }
init(
asyncSpecifier: RawTokenSyntax?,
throwsClause: RawThrowsClauseSyntax?,
arena: __shared SyntaxArena
)
func withMisplaced(
async misplacedAsyncKeyword: RawTokenSyntax?,
throws misplacedThrowsClause: RawThrowsClauseSyntax?,
arena: __shared SyntaxArena
) -> Self
}
protocol RawEffectSpecifiersTrait: RawMisplacedEffectSpecifiersTrait {
var unexpectedBeforeAsyncSpecifier: RawUnexpectedNodesSyntax? { get }
var unexpectedBetweenAsyncSpecifierAndThrowsClause: RawUnexpectedNodesSyntax? { get }
var unexpectedAfterThrowsClause: RawUnexpectedNodesSyntax? { get }
init(
_ unexpectedBeforeAsyncSpecifier: RawUnexpectedNodesSyntax?,
asyncSpecifier: RawTokenSyntax?,
_ unexpectedBetweenAsyncSpecifierAndThrowsClause: RawUnexpectedNodesSyntax?,
throwsClause: RawThrowsClauseSyntax?,
_ unexpectedAfterThrowsClause: RawUnexpectedNodesSyntax?,
arena: __shared SyntaxArena
)
}
extension RawEffectSpecifiersTrait {
init(
asyncSpecifier: RawTokenSyntax?,
throwsClause: RawThrowsClauseSyntax?,
arena: __shared SyntaxArena
) {
self.init(
nil,
asyncSpecifier: asyncSpecifier,
nil,
throwsClause: throwsClause,
nil,
arena: arena
)
}
func withMisplaced(
async misplacedAsyncKeyword: RawTokenSyntax?,
throws misplacedThrowsClause: RawThrowsClauseSyntax?,
arena: __shared SyntaxArena
) -> Self {
return Self.init(
self.unexpectedBeforeAsyncSpecifier,
asyncSpecifier: self.asyncSpecifier ?? misplacedAsyncKeyword,
self.unexpectedBetweenAsyncSpecifierAndThrowsClause,
throwsClause: self.throwsClause ?? misplacedThrowsClause,
self.unexpectedAfterThrowsClause,
arena: arena
)
}
}
extension RawFunctionEffectSpecifiersSyntax: RawEffectSpecifiersTrait {
enum MisspelledAsyncTokenKinds: TokenSpecSet {
case await
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.await, allowAtStartOfLine: false): self = .await
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .await: return TokenSpec(.await, allowAtStartOfLine: false)
}
}
}
enum CorrectAsyncTokenKinds: TokenSpecSet {
case async
case reasync
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.async): self = .async
case TokenSpec(.reasync): self = .reasync
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .async: return .keyword(.async)
case .reasync: return .keyword(.reasync)
}
}
}
enum MisspelledThrowsTokenKinds: TokenSpecSet {
case `try`
case `throw`
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.try, allowAtStartOfLine: false): self = .try
case TokenSpec(.throw, allowAtStartOfLine: false): self = .throw
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .try: return TokenSpec(.try, allowAtStartOfLine: false)
case .throw: return TokenSpec(.throw, allowAtStartOfLine: false)
}
}
}
enum CorrectThrowsTokenKinds: TokenSpecSet {
case `rethrows`
case `throws`
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.rethrows): self = .rethrows
case TokenSpec(.throws): self = .throws
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .rethrows: return .keyword(.rethrows)
case .throws: return .keyword(.throws)
}
}
}
}
extension RawTypeEffectSpecifiersSyntax: RawEffectSpecifiersTrait {
enum MisspelledAsyncTokenKinds: TokenSpecSet {
case await
case reasync
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.await, allowAtStartOfLine: false): self = .await
case TokenSpec(.reasync): self = .reasync
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .await: return TokenSpec(.await, allowAtStartOfLine: false)
case .reasync: return .keyword(.reasync)
}
}
}
enum CorrectAsyncTokenKinds: TokenSpecSet {
case async
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.async): self = .async
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .async: return .keyword(.async)
}
}
}
enum MisspelledThrowsTokenKinds: TokenSpecSet {
case `rethrows`
case `try`
case `throw`
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.rethrows): self = .rethrows
case TokenSpec(.try, allowAtStartOfLine: false): self = .try
case TokenSpec(.throw, allowAtStartOfLine: false): self = .throw
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .rethrows: return .keyword(.rethrows)
case .try: return TokenSpec(.try, allowAtStartOfLine: false)
case .throw: return TokenSpec(.throw, allowAtStartOfLine: false)
}
}
}
enum CorrectThrowsTokenKinds: TokenSpecSet {
case `throws`
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.throws): self = .throws
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .throws: return .keyword(.throws)
}
}
}
}
extension RawAccessorEffectSpecifiersSyntax: RawEffectSpecifiersTrait {
enum MisspelledAsyncTokenKinds: TokenSpecSet {
case await
case reasync
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.await, allowAtStartOfLine: false): self = .await
case TokenSpec(.reasync): self = .reasync
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .await: return TokenSpec(.await, allowAtStartOfLine: false)
case .reasync: return .keyword(.reasync)
}
}
}
enum CorrectAsyncTokenKinds: TokenSpecSet {
case async
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.async): self = .async
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .async: return .keyword(.async)
}
}
}
enum MisspelledThrowsTokenKinds: TokenSpecSet {
case `rethrows`
case `try`
case `throw`
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.rethrows): self = .rethrows
case TokenSpec(.try, allowAtStartOfLine: false): self = .try
case TokenSpec(.throw, allowAtStartOfLine: false): self = .throw
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .rethrows: return .keyword(.rethrows)
case .try: return TokenSpec(.try, allowAtStartOfLine: false)
case .throw: return TokenSpec(.throw, allowAtStartOfLine: false)
}
}
}
enum CorrectThrowsTokenKinds: TokenSpecSet {
case `throws`
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.throws): self = .throws
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .throws: return .keyword(.throws)
}
}
}
}
extension RawDeinitializerEffectSpecifiersSyntax: RawMisplacedEffectSpecifiersTrait {
enum MisspelledAsyncTokenKinds: TokenSpecSet {
case await
case reasync
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.await, allowAtStartOfLine: false): self = .await
case TokenSpec(.reasync): self = .reasync
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .await: return TokenSpec(.await, allowAtStartOfLine: false)
case .reasync: return .keyword(.reasync)
}
}
}
enum CorrectAsyncTokenKinds: TokenSpecSet {
case async
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.async): self = .async
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .async: return .keyword(.async)
}
}
}
enum MisspelledThrowsTokenKinds: TokenSpecSet {
case `rethrows`
case `try`
case `throw`
case `throws`
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.rethrows): self = .rethrows
case TokenSpec(.try, allowAtStartOfLine: false): self = .try
case TokenSpec(.throw, allowAtStartOfLine: false): self = .throw
case TokenSpec(.throws): self = .throws
default: return nil
}
}
var spec: TokenSpec {
switch self {
case .rethrows: return .keyword(.rethrows)
case .try: return TokenSpec(.try, allowAtStartOfLine: false)
case .throw: return TokenSpec(.throw, allowAtStartOfLine: false)
case .throws: return .keyword(.throws)
}
}
}
enum CorrectThrowsTokenKinds: TokenSpecSet {
// Uninhabited
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
return nil
}
var spec: TokenSpec {
switch self {
}
}
}
var throwsClause: RawThrowsClauseSyntax? { nil }
init(
asyncSpecifier: RawTokenSyntax?,
throwsClause: RawThrowsClauseSyntax?,
arena: __shared SwiftSyntax.SyntaxArena
) {
// `throwsSpecifier` should never be present because `parseMisplacedEffectSpecifiers()` only creates missing tokens
// and `CorrectThrowsTokenKinds` is an empty `TokenSpecSet`.
//
// We don't want to insert missing `throws` for deinit case,
// so if `parseMisplacedEffectSpecifiers()` creates one it will be discarded here.
precondition(throwsClause?.throwsSpecifier.isMissing ?? true)
self.init(
nil,
asyncSpecifier: asyncSpecifier,
arena: arena
)
}
func withMisplaced(
async misplacedAsyncKeyword: RawTokenSyntax?,
throws misplacedThrowsClause: RawThrowsClauseSyntax?,
arena: SyntaxArena
) -> RawDeinitializerEffectSpecifiersSyntax {
// `throwsClause` should never be present because `parseMisplacedEffectSpecifiers()` only creates missing tokens
// and `CorrectThrowsTokenKinds` is an empty `TokenSpecSet`.
//
// We don't want to insert missing `throws` for deinit case,
// so if `parseMisplacedEffectSpecifiers()` creates one it will be discarded here.
precondition(throwsClause?.throwsSpecifier.isMissing ?? true)
return Self.init(
self.unexpectedBeforeAsyncSpecifier,
asyncSpecifier: self.asyncSpecifier ?? misplacedAsyncKeyword,
self.unexpectedAfterAsyncSpecifier,
arena: arena
)
}
}
extension TokenConsumer {
mutating func at<SpecSet1: TokenSpecSet, SpecSet2: TokenSpecSet>(
anyIn specSet1: SpecSet1.Type,
or specSet2: SpecSet2.Type
) -> (spec: TokenSpec, handle: TokenConsumptionHandle, matchedSubset: Any.Type)? {
if let (spec, handle) = self.at(anyIn: specSet1) {
return (spec.spec, handle, SpecSet1.self)
} else if let (spec, handle) = self.at(anyIn: specSet2) {
return (spec.spec, handle, SpecSet2.self)
} else {
return nil
}
}
}
// MARK: - Parsing effect specifiers
extension Parser {
/// Parse a throws clause after we've already parsed the 'throws' keyword to introduce it.
mutating func parseThrowsClause(after throwsKeyword: RawTokenSyntax) -> RawThrowsClauseSyntax {
guard let leftParen = self.consume(if: .leftParen) else {
return RawThrowsClauseSyntax(
throwsSpecifier: throwsKeyword,
leftParen: nil,
type: nil,
rightParen: nil,
arena: self.arena
)
}
let type = self.parseType()
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
return RawThrowsClauseSyntax(
throwsSpecifier: throwsKeyword,
leftParen: leftParen,
type: type,
unexpectedBeforeRightParen,
rightParen: rightParen,
arena: self.arena
)
}
private mutating func parseEffectSpecifiers<S: RawEffectSpecifiersTrait>(_: S.Type) -> S? {
var unexpectedBeforeAsync: [RawSyntax] = []
var asyncKeyword: RawTokenSyntax? = nil
var unexpectedBeforeThrows: [RawSyntax] = []
var throwsClause: RawThrowsClauseSyntax?
var unexpectedAfterThrowsClause: [RawSyntax] = []
while let misspelledAsync = self.consume(ifAnyIn: S.MisspelledAsyncTokenKinds.self) {
unexpectedBeforeAsync.append(RawSyntax(misspelledAsync))
if asyncKeyword == nil {
// Let's synthesize a missing 'async'. If we find a real async specifier
// later, we will replace the missing token by the present token.
asyncKeyword = missingToken(.keyword(.async))
}
}
if let (_, handle) = self.canRecoverTo(anyIn: S.CorrectAsyncTokenKinds.self) {
let (unexpected, async) = self.eat(handle)
unexpectedBeforeAsync.append(contentsOf: unexpected?.elements ?? [])
asyncKeyword = async
}
var unexpectedBeforeThrowsLoopProgress = LoopProgressCondition()
while self.hasProgressed(&unexpectedBeforeThrowsLoopProgress) {
if let misspelledAsync = self.consume(ifAnyIn: S.MisspelledAsyncTokenKinds.self) {
unexpectedBeforeThrows.append(RawSyntax(misspelledAsync))
} else if let misspelledThrows = self.consume(ifAnyIn: S.MisspelledThrowsTokenKinds.self) {
unexpectedBeforeThrows.append(RawSyntax(misspelledThrows))
if throwsClause == nil {
// Let's synthesize a missing 'throws'. If we find a real throws specifier
// later, we will replace the missing token by the present token.
throwsClause = RawThrowsClauseSyntax(
throwsSpecifier: missingToken(.keyword(.throws)),
leftParen: nil,
type: nil,
rightParen: nil,
arena: self.arena
)
}
} else {
break
}
}
if let (_, handle) = self.canRecoverTo(anyIn: S.CorrectThrowsTokenKinds.self) {
let (unexpected, throwsKw) = self.eat(handle)
unexpectedBeforeThrows.append(contentsOf: unexpected?.elements ?? [])
throwsClause = parseThrowsClause(after: throwsKw)
}
var unexpectedAfterThrownErrorLoopProgress = LoopProgressCondition()
while self.hasProgressed(&unexpectedAfterThrownErrorLoopProgress) {
if let (_, handle, _) = self.at(anyIn: S.MisspelledAsyncTokenKinds.self, or: S.CorrectAsyncTokenKinds.self) {
let misspelledAsync = self.eat(handle)
unexpectedAfterThrowsClause.append(RawSyntax(misspelledAsync))
if asyncKeyword == nil {
// Handle `async` after `throws`
asyncKeyword = missingToken(.keyword(.async))
}
} else if let (_, handle, _) = self.at(
anyIn: S.MisspelledThrowsTokenKinds.self,
or: S.CorrectThrowsTokenKinds.self
) {
let misspelledThrows = self.eat(handle)
unexpectedAfterThrowsClause.append(RawSyntax(misspelledThrows))
} else {
break
}
}
if unexpectedBeforeAsync.isEmpty && asyncKeyword == nil && unexpectedBeforeThrows.isEmpty && throwsClause == nil
&& unexpectedAfterThrowsClause.isEmpty
{
return nil
}
return S(
RawUnexpectedNodesSyntax(unexpectedBeforeAsync, arena: self.arena),
asyncSpecifier: asyncKeyword,
RawUnexpectedNodesSyntax(unexpectedBeforeThrows, arena: self.arena),
throwsClause: throwsClause,
RawUnexpectedNodesSyntax(unexpectedAfterThrowsClause, arena: self.arena),
arena: self.arena
)
}
mutating func parseTypeEffectSpecifiers() -> RawTypeEffectSpecifiersSyntax? {
return parseEffectSpecifiers(RawTypeEffectSpecifiersSyntax.self)
}
mutating func parseFunctionEffectSpecifiers() -> RawFunctionEffectSpecifiersSyntax? {
return parseEffectSpecifiers(RawFunctionEffectSpecifiersSyntax.self)
}
mutating func parseAccessorEffectSpecifiers() -> RawAccessorEffectSpecifiersSyntax? {
return parseEffectSpecifiers(RawAccessorEffectSpecifiersSyntax.self)
}
mutating func parseDeinitEffectSpecifiers() -> RawDeinitializerEffectSpecifiersSyntax? {
// Note that parseEffectSpecifiers() consumes deinit name as unexpected token
// But we want it to be handled on the higher level.
// So we parseEffectSpecifiers() is not reused here.
var unexpectedBeforeAsync: [RawSyntax?] = []
var asyncKeyword: RawTokenSyntax?
var unexpectedAfterAsync: [RawSyntax?] = []
while let (specifier, handle) = self.at(anyIn: EffectSpecifiers.self) {
let beforeAsync = asyncKeyword?.isMissing ?? true
switch specifier {
case .async:
if beforeAsync {
asyncKeyword = self.eat(handle)
} else {
unexpectedAfterAsync.append(RawSyntax(self.eat(handle)))
}
case .await, .reasync:
if beforeAsync {
// Let's synthesize a missing 'async'. If we find a real async specifier
// later, we will replace the missing token by the present token.
asyncKeyword = missingToken(.async)
}
fallthrough
default:
if beforeAsync {
unexpectedBeforeAsync.append(RawSyntax(self.eat(handle)))
} else {
unexpectedAfterAsync.append(RawSyntax(self.eat(handle)))
}
}
}
if unexpectedBeforeAsync.isEmpty && asyncKeyword == nil && unexpectedAfterAsync.isEmpty {
return nil
}
return RawDeinitializerEffectSpecifiersSyntax(
RawUnexpectedNodesSyntax(unexpectedBeforeAsync, arena: self.arena),
asyncSpecifier: asyncKeyword,
RawUnexpectedNodesSyntax(unexpectedAfterAsync, arena: self.arena),
arena: self.arena
)
}
/// Consume any misplaced effect specifiers and return them in as unexpected tokens.
/// When a misplaced effect specifier is consumed and `effectSpecifiers`
/// doesn't have an effect specifier of that kind, modify `effectSpecifiers`
/// to have a missing specifier of that kind.
mutating func parseMisplacedEffectSpecifiers<S: RawMisplacedEffectSpecifiersTrait>(
_ effectSpecifiers: inout S?
) -> RawUnexpectedNodesSyntax? {
var synthesizedAsync: RawTokenSyntax? = nil
var synthesizedThrowsClause: RawThrowsClauseSyntax? = nil
var unexpected: [RawSyntax] = []
var loopProgress = LoopProgressCondition()
while self.hasProgressed(&loopProgress) {
if let (spec, handle, matchedSubset) = self.at(
anyIn: S.MisspelledAsyncTokenKinds.self,
or: S.CorrectAsyncTokenKinds.self
) {
let misspelledAsync = self.eat(handle)
unexpected.append(RawSyntax(misspelledAsync))
if effectSpecifiers?.asyncSpecifier == nil {
if matchedSubset == S.CorrectAsyncTokenKinds.self {
synthesizedAsync = missingToken(spec)
} else {
synthesizedAsync = missingToken(.async)
}
}
} else if let (spec, handle, matchedSubset) = self.at(
anyIn: S.MisspelledThrowsTokenKinds.self,
or: S.CorrectThrowsTokenKinds.self
) {
let misspelledThrows = self.eat(handle)
unexpected.append(RawSyntax(misspelledThrows))
guard effectSpecifiers?.throwsClause == nil else {
continue
}
if matchedSubset == S.CorrectThrowsTokenKinds.self {
let synthesizedThrows = missingToken(spec)
// avoid generating diagnotics that suggest moving `throws` and the following type if there is any trivia
// between `throws` and the following left parenthesis, because the following type is likely to be the
// return type.
// e.g. -> throws (Int, Int)
//
// prefer pessimistic diagnostics even if they might be suboptimal.
// e.g. -> throws (any Error) Int
guard misspelledThrows.trailingTriviaByteLength == 0 && self.currentToken.leadingTriviaByteLength == 0 else {
synthesizedThrowsClause = RawThrowsClauseSyntax(
throwsSpecifier: synthesizedThrows,
leftParen: nil,
type: nil,
rightParen: nil,
arena: self.arena
)
continue
}
guard
withLookahead({ lookahead in
lookahead.consume(if: .leftParen) != nil && lookahead.canParseType()
})
else {
synthesizedThrowsClause = RawThrowsClauseSyntax(
throwsSpecifier: synthesizedThrows,
leftParen: nil,
type: nil,
rightParen: nil,
arena: self.arena
)
continue
}
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen)
if let unexpectedBeforeLeftParen {
unexpected += unexpectedBeforeLeftParen.elements
}
unexpected.append(RawSyntax(leftParen))
let type = self.parseType()
unexpected.append(RawSyntax(type))
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
if let unexpectedBeforeRightParen {
unexpected += unexpectedBeforeRightParen.elements
}
if !rightParen.isMissing {
unexpected.append(RawSyntax(rightParen))
}
synthesizedThrowsClause = RawThrowsClauseSyntax(
throwsSpecifier: synthesizedThrows,
leftParen: missingToken(.leftParen),
type: self.withAllTokensMarkedMissing(syntax: type),
rightParen: rightParen.isMissing ? nil : missingToken(.rightParen),
arena: self.arena
)
} else {
synthesizedThrowsClause = RawThrowsClauseSyntax(
throwsSpecifier: missingToken(.throws),
leftParen: nil,
type: nil,
rightParen: nil,
arena: self.arena
)
}
} else {
break
}
}
if synthesizedAsync != nil || synthesizedThrowsClause != nil {
if let specifiers = effectSpecifiers {
effectSpecifiers = specifiers.withMisplaced(
async: synthesizedAsync,
throws: synthesizedThrowsClause,
arena: self.arena
)
} else {
effectSpecifiers = S(
asyncSpecifier: synthesizedAsync,
throwsClause: synthesizedThrowsClause,
arena: self.arena
)
}
}
return RawUnexpectedNodesSyntax(unexpected, arena: self.arena)
}
}
|