-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2666 from ahoppen/6.0/identifier
[6.0] Add Identifier wrapper that strips backticks from token text
- Loading branch information
Showing
5 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// A canonicalized representation of an identifier that strips away backticks. | ||
public struct Identifier: Equatable, Hashable, Sendable { | ||
/// The sanitized `text` of a token. | ||
public var name: String { | ||
String(syntaxText: raw.name) | ||
} | ||
|
||
@_spi(RawSyntax) | ||
public let raw: RawIdentifier | ||
|
||
private let arena: SyntaxArenaRef | ||
|
||
public init?(_ token: TokenSyntax) { | ||
guard case .identifier = token.tokenKind else { | ||
return nil | ||
} | ||
|
||
self.raw = RawIdentifier(token.tokenView) | ||
self.arena = token.tokenView.raw.arenaReference | ||
} | ||
} | ||
|
||
@_spi(RawSyntax) | ||
public struct RawIdentifier: Equatable, Hashable, Sendable { | ||
public let name: SyntaxText | ||
|
||
@_spi(RawSyntax) | ||
fileprivate init(_ raw: RawSyntaxTokenView) { | ||
let backtick = SyntaxText("`") | ||
if raw.rawText.count > 2 && raw.rawText.hasPrefix(backtick) && raw.rawText.hasSuffix(backtick) { | ||
let startIndex = raw.rawText.index(after: raw.rawText.startIndex) | ||
let endIndex = raw.rawText.index(before: raw.rawText.endIndex) | ||
self.name = SyntaxText(rebasing: raw.rawText[startIndex..<endIndex]) | ||
} else { | ||
self.name = raw.rawText | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@_spi(RawSyntax) import SwiftSyntax | ||
import XCTest | ||
|
||
class IdentifierTests: XCTestCase { | ||
public func testIdentifierInit() { | ||
let someToken = TokenSyntax(stringLiteral: "someToken") | ||
XCTAssertNotNil(Identifier(someToken)) | ||
|
||
let nonIdentifierToken = DeclSyntax("let a = 1").firstToken(viewMode: .all)! | ||
XCTAssertNil(Identifier(nonIdentifierToken)) | ||
} | ||
|
||
public func testName() { | ||
let basicToken = TokenSyntax(stringLiteral: "basicToken") | ||
XCTAssertEqual(Identifier(basicToken)?.name, "basicToken") | ||
|
||
let backtickedToken = TokenSyntax(stringLiteral: "`backtickedToken`") | ||
XCTAssertEqual(Identifier(backtickedToken)?.name, "backtickedToken") | ||
|
||
let multiBacktickedToken = TokenSyntax(stringLiteral: "```multiBacktickedToken```") | ||
XCTAssertEqual(Identifier(multiBacktickedToken)?.name, "``multiBacktickedToken``") | ||
|
||
let unicodeNormalizedToken = TokenSyntax(stringLiteral: "\u{e0}") // "a`" | ||
XCTAssertEqual(Identifier(unicodeNormalizedToken)?.name, "\u{61}\u{300}") // "à" | ||
} | ||
|
||
public func testIdentifier() { | ||
let token = TokenSyntax(stringLiteral: "sometoken") | ||
withExtendedLifetime(token) { token in | ||
XCTAssertEqual(token.identifier?.raw.name, SyntaxText("sometoken")) | ||
} | ||
} | ||
|
||
public func testTokenSyntaxIdentifier() throws { | ||
let tokenSyntax = TokenSyntax(stringLiteral: "sometoken") | ||
XCTAssertEqual(tokenSyntax.identifier, Identifier(tokenSyntax)) | ||
|
||
let nonIdentifierToken = try XCTUnwrap(DeclSyntax("let a = 1").firstToken(viewMode: .all)) | ||
XCTAssertNil(nonIdentifierToken.identifier) | ||
} | ||
} |