-
Notifications
You must be signed in to change notification settings - Fork 440
Add Identifier wrapper that strips backticks from token text #2576
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
plemarquand
merged 15 commits into
swiftlang:main
from
adammcarter:adamcarter93/separate-backtick-tokens
Jun 3, 2024
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
23e48f1
Remove backticks when creating Identifier
adammcarter d5e150e
Added TokenSyntax identifier property
adammcarter 336b4b3
Conform Identifier to Sendable and Hashable
adammcarter a63ade8
Make Identifier initializer failable
adammcarter 21ba4fb
WIP - SyntaxText
adammcarter 1b4a841
Run formatter
plemarquand 85714ab
Address review comments
plemarquand d07cffd
Remove unused string extension method
plemarquand 3a8c221
Address comments
plemarquand 1e39d2d
Remove RetainedSyntaxArena
plemarquand e903d9a
Update release notes
plemarquand de2d770
Merge branch 'main' into adamcarter93/separate-backtick-tokens
plemarquand 2ce026f
Add pull request to changelog
plemarquand 8cc8dd3
Move release notes to 601
plemarquand 53234d8
Add Identifier to CMakeLists.txt
plemarquand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add Identifier.swift to CMakeLists.txt. swift-syntax is built using CMake when it’s linked into the compiler. |
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
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 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
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
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 { | ||
adammcarter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.