Skip to content
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

Add SimpleStringLiteralExprSyntax.representedStringValue #2892

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions Sources/SwiftParser/StringLiteralRepresentedLiteralValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
@_spi(RawSyntax) @_spi(BumpPtrAllocator) import SwiftSyntax
#endif

private func getStringLiteralKind(quoteToken: TokenSyntax) -> StringLiteralKind? {
switch quoteToken.tokenKind {
case .stringQuote:
return .singleLine
case .multilineStringQuote:
return .multiLine
case .singleQuote:
return .singleQuote
default:
return nil
}
}

extension StringLiteralExprSyntax {

/// Returns the string value of the literal as the parsed program would see
Expand Down Expand Up @@ -55,16 +68,7 @@ extension StringLiteralExprSyntax {

@_spi(Compiler)
public var stringLiteralKind: StringLiteralKind? {
switch openingQuote.tokenKind {
case .stringQuote:
return .singleLine
case .multilineStringQuote:
return .multiLine
case .singleQuote:
return .singleQuote
default:
return nil
}
getStringLiteralKind(quoteToken: openingQuote)
}

@_spi(Compiler)
Expand All @@ -73,6 +77,23 @@ extension StringLiteralExprSyntax {
}
}

extension SimpleStringLiteralExprSyntax {
public var representedLiteralValue: String? {
guard let stringLiteralKind else { return nil }

var results = ""
for segment in segments {
segment.appendUnescapedLiteralValue(stringLiteralKind: stringLiteralKind, delimiterLength: 0, to: &results)
}
return results
}

@_spi(Compiler)
public var stringLiteralKind: StringLiteralKind? {
getStringLiteralKind(quoteToken: openingQuote)
}
}

extension StringSegmentSyntax {
@_spi(Compiler)
public func appendUnescapedLiteralValue(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 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
//
//===----------------------------------------------------------------------===//

import SwiftParser
import SwiftSyntax
import SwiftSyntaxBuilder
import XCTest

class SimpleStringLiteralRepresentedLiteralValueTests: ParserTestCase {
func testBasic() {
test(###"""
#sourceLocation(file: "foo.swift", line: 0)
"""###, "foo.swift")
}

func testBasicUnicode() {
test(###"""
#sourceLocation(file: "foo\u{002E}swift", line: 0)
"""###, "foo.swift")
}

func testMultiline() {
test(###"""
#sourceLocation(file: """
foo.swift
""", line: 0)
"""###, "foo.swift")
}

func testMultilineEscaped() {
test(###"""
#sourceLocation(file: """
foo.\
swift
""", line: 0)
"""###, "foo.swift")
}

func test(_ source: String, _ expectedValue: String, file: StaticString = #filePath, line: UInt = #line) {
let parsed = Parser.parse(source: source)
guard let literalExpr: SimpleStringLiteralExprSyntax = parsed.statements.first?.item.as(PoundSourceLocationSyntax.self)?.arguments?.fileName else {
return XCTFail("target literal expression not found", file: file, line: line)
}
XCTAssertEqual(literalExpr.representedLiteralValue, expectedValue, file: file, line: line)
}
}