Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public let AVAILABILITY_NODES: [Node] = [
kind: .nodeChoices(choices: [
Child(
name: "String",
kind: .node(kind: .stringLiteralExpr)
kind: .node(kind: .simpleStringLiteralExpr)
),
Child(
name: "Version",
Expand Down
2 changes: 1 addition & 1 deletion CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,7 @@ public let DECL_NODES: [Node] = [
),
Child(
name: "FileName",
kind: .node(kind: .stringLiteralExpr),
kind: .node(kind: .simpleStringLiteralExpr),
nameForDiagnostics: "file name"
),
Child(
Expand Down
32 changes: 32 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,38 @@ public let EXPR_NODES: [Node] = [
elementChoices: [.stringSegment, .expressionSegment]
),

Node(
kind: .simpleStringLiteralExpr,
base: .expr,
nameForDiagnostics: "simple string literal",
documentation: "A simple string that can’t contain string interpolation and cannot have raw string delimiters.",
children: [
Child(
name: "OpeningQuote",
kind: .token(choices: [.token(.stringQuote), .token(.multilineStringQuote)]),
documentation: "Open quote for the string literal"
),
Child(
name: "Segments",
kind: .collection(kind: .simpleStringLiteralSegmentList, collectionElementName: "Segment"),
documentation: "String content"
),
Child(
name: "ClosingQuote",
kind: .token(choices: [.token(.stringQuote), .token(.multilineStringQuote)]),
documentation: "Close quote for the string literal"
),
]
),

Node(
kind: .simpleStringLiteralSegmentList,
base: .syntaxCollection,
nameForDiagnostics: nil,
documentation: "String literal segments that only can contain non string interpolated or extended escaped strings",
elementChoices: [.stringSegment]
),

// string literal segment in a string interpolation expression.
Node(
kind: .stringSegment,
Expand Down
2 changes: 2 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ public enum SyntaxNodeKind: String, CaseIterable {
case specializeAvailabilityArgument
case specializeTargetFunctionArgument
case stmt
case simpleStringLiteralExpr
case simpleStringLiteralSegmentList
case stringLiteralExpr
case stringLiteralSegmentList
case stringSegment
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftBasicFormat/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ open class BasicFormat: SyntaxRewriter {
case \ExpressionSegmentSyntax.backslash,
\ExpressionSegmentSyntax.rightParen,
\DeclNameArgumentSyntax.colon,
\SimpleStringLiteralExprSyntax.openingQuote,
\StringLiteralExprSyntax.openingQuote,
\RegexLiteralExprSyntax.openingSlash:
return false
Expand Down
3 changes: 1 addition & 2 deletions Sources/SwiftParser/Availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ extension Parser {
(.renamed, let handle)?:
let argumentLabel = self.eat(handle)
let (unexpectedBeforeColon, colon) = self.expect(.colon)
// FIXME: Make sure this is a string literal with no interpolation.
let stringValue = self.parseStringLiteral()
let stringValue = self.parseSimpleString()

entry = .availabilityLabeledArgument(
RawAvailabilityLabeledArgumentSyntax(
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftParser/Directives.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ extension Parser {
if !self.at(.rightParen) {
let (unexpectedBeforeFile, file) = self.expect(.keyword(.file))
let (unexpectedBeforeFileColon, fileColon) = self.expect(.colon)
let fileName = self.parseStringLiteral()
let fileName = self.parseSimpleString()
let (unexpectedBeforeComma, comma) = self.expect(.comma)

let (unexpectedBeforeLine, line) = self.expect(.keyword(.line))
Expand Down
102 changes: 101 additions & 1 deletion Sources/SwiftParser/StringLiterals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ extension Parser {
return RawTokenSyntax(
kind: token.tokenKind,
text: SyntaxText(rebasing: token.tokenText.dropFirst(reclassifyLeading.count).dropLast(reclassifyTrailing.count)),
leadingTriviaPieces: token.leadingTriviaPieces + TriviaParser.parseTrivia(reclassifyLeading, position: .trailing),
leadingTriviaPieces: token.leadingTriviaPieces + TriviaParser.parseTrivia(reclassifyLeading, position: .leading),
trailingTriviaPieces: TriviaParser.parseTrivia(reclassifyTrailing, position: .trailing) + token.trailingTriviaPieces,
presence: token.presence,
tokenDiagnostic: token.tokenView.tokenDiagnostic ?? tokenDiagnostic,
Expand Down Expand Up @@ -595,10 +595,110 @@ extension Parser {
)
}
}

mutating func parseSimpleString() -> RawSimpleStringLiteralExprSyntax {
let openDelimiter = self.consume(if: .rawStringPoundDelimiter)
let (unexpectedBeforeOpenQuote, openQuote) = self.expect(anyIn: SimpleStringLiteralExprSyntax.OpeningQuoteOptions.self, default: .stringQuote)

/// Parse segments.
var segments: [RawStringSegmentSyntax] = []
var loopProgress = LoopProgressCondition()
while hasProgressed(&loopProgress) {
// If we encounter a token with leading trivia, we're no longer in the
// string literal.
guard currentToken.leadingTriviaText.isEmpty else { break }

if let stringSegment = self.consume(if: .stringSegment, TokenSpec(.identifier, remapping: .stringSegment)) {
var unexpectedAfterContent: RawUnexpectedNodesSyntax?

if let (backslash, leftParen) = self.consume(if: .backslash, followedBy: .leftParen) {
var unexpectedTokens: [RawSyntax] = [RawSyntax(backslash), RawSyntax(leftParen)]

let (unexpectedBeforeRightParen, rightParen) = self.expect(TokenSpec(.rightParen, allowAtStartOfLine: false))
unexpectedTokens += unexpectedBeforeRightParen?.elements ?? []
unexpectedTokens.append(RawSyntax(rightParen))

unexpectedAfterContent = RawUnexpectedNodesSyntax(
unexpectedTokens,
arena: self.arena
)
}

segments.append(RawStringSegmentSyntax(content: stringSegment, unexpectedAfterContent, arena: self.arena))
} else {
break
}
}

let (unexpectedBetweenSegmentAndCloseQuote, closeQuote) = self.expect(
anyIn: SimpleStringLiteralExprSyntax.ClosingQuoteOptions.self,
default: openQuote.closeTokenKind
)
let closeDelimiter = self.consume(if: .rawStringPoundDelimiter)

if openQuote.tokenKind == .multilineStringQuote, !openQuote.isMissing, !closeQuote.isMissing {
let postProcessed = postProcessMultilineStringLiteral(
rawStringDelimitersToken: openDelimiter,
openQuote: openQuote,
segments: segments.compactMap { RawStringLiteralSegmentListSyntax.Element.stringSegment($0) },
closeQuote: closeQuote
)

return RawSimpleStringLiteralExprSyntax(
RawUnexpectedNodesSyntax(
combining: openDelimiter,
unexpectedBeforeOpenQuote,
postProcessed.unexpectedBeforeOpeningQuote,
arena: self.arena
),
openingQuote: postProcessed.openingQuote,
segments: RawSimpleStringLiteralSegmentListSyntax(
// `RawSimpleStringLiteralSegmentListSyntax` only accepts `RawStringSegmentSyntax`.
// So we can safely cast.
elements: postProcessed.segments.map { $0.cast(RawStringSegmentSyntax.self) },
arena: self.arena
),
RawUnexpectedNodesSyntax(
combining: unexpectedBetweenSegmentAndCloseQuote,
postProcessed.unexpectedBeforeClosingQuote,
arena: self.arena
),
closingQuote: postProcessed.closingQuote,
RawUnexpectedNodesSyntax(
[closeDelimiter],
arena: self.arena
),
arena: self.arena
)
} else {
return RawSimpleStringLiteralExprSyntax(
RawUnexpectedNodesSyntax(combining: unexpectedBeforeOpenQuote, openDelimiter, arena: self.arena),
openingQuote: openQuote,
segments: RawSimpleStringLiteralSegmentListSyntax(elements: segments, arena: self.arena),
unexpectedBetweenSegmentAndCloseQuote,
closingQuote: closeQuote,
RawUnexpectedNodesSyntax([closeDelimiter], arena: self.arena),
arena: self.arena
)
}
}
}

// MARK: - Utilities

fileprivate extension RawTokenSyntax {
var closeTokenKind: SimpleStringLiteralExprSyntax.ClosingQuoteOptions {
switch self {
case .multilineStringQuote:
return .multilineStringQuote
case .stringQuote:
return .stringQuote
default:
return .stringQuote
}
}
}

fileprivate extension SyntaxText {
private func hasSuffix(_ other: String) -> Bool {
var other = other
Expand Down
82 changes: 82 additions & 0 deletions Sources/SwiftParser/generated/Parser+TokenSpecSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2585,6 +2585,88 @@ extension SameTypeRequirementSyntax {
}
}

extension SimpleStringLiteralExprSyntax {
@_spi(Diagnostics)
public enum OpeningQuoteOptions: TokenSpecSet {
case stringQuote
case multilineStringQuote

init?(lexeme: Lexer.Lexeme) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.stringQuote):
self = .stringQuote
case TokenSpec(.multilineStringQuote):
self = .multilineStringQuote
default:
return nil
}
}

var spec: TokenSpec {
switch self {
case .stringQuote:
return .stringQuote
case .multilineStringQuote:
return .multilineStringQuote
}
}

/// Returns a token that satisfies the `TokenSpec` of this case.
///
/// If the token kind of this spec has variable text, e.g. for an identifier, this returns a token with empty text.
@_spi(Diagnostics)
public var tokenSyntax: TokenSyntax {
switch self {
case .stringQuote:
return .stringQuoteToken()
case .multilineStringQuote:
return .multilineStringQuoteToken()
}
}
}
}

extension SimpleStringLiteralExprSyntax {
@_spi(Diagnostics)
public enum ClosingQuoteOptions: TokenSpecSet {
case stringQuote
case multilineStringQuote

init?(lexeme: Lexer.Lexeme) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.stringQuote):
self = .stringQuote
case TokenSpec(.multilineStringQuote):
self = .multilineStringQuote
default:
return nil
}
}

var spec: TokenSpec {
switch self {
case .stringQuote:
return .stringQuote
case .multilineStringQuote:
return .multilineStringQuote
}
}

/// Returns a token that satisfies the `TokenSpec` of this case.
///
/// If the token kind of this spec has variable text, e.g. for an identifier, this returns a token with empty text.
@_spi(Diagnostics)
public var tokenSyntax: TokenSyntax {
switch self {
case .stringQuote:
return .stringQuoteToken()
case .multilineStringQuote:
return .multilineStringQuoteToken()
}
}
}
}

extension SomeOrAnyTypeSyntax {
@_spi(Diagnostics)
public enum SomeOrAnySpecifierOptions: TokenSpecSet {
Expand Down
50 changes: 50 additions & 0 deletions Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,56 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
return .visitChildren
}

public override func visit(_ node: SimpleStringLiteralExprSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
}

var rawDelimiters: [TokenSyntax] = []

if let unexpectedBeforeOpenQuote = node.unexpectedBeforeOpeningQuote?.onlyPresentToken(where: { $0.tokenKind.isRawStringDelimiter }) {
rawDelimiters += [unexpectedBeforeOpenQuote]
}

if let unexpectedAfterCloseQuote = node.unexpectedAfterClosingQuote?.onlyPresentToken(where: { $0.tokenKind.isRawStringDelimiter }) {
rawDelimiters += [unexpectedAfterCloseQuote]
}

if !rawDelimiters.isEmpty {
addDiagnostic(
node,
.forbiddenExtendedEscapingString,
fixIts: [
FixIt(
message: RemoveNodesFixIt(rawDelimiters),
changes: rawDelimiters.map { .makeMissing($0) }
)
],
handledNodes: rawDelimiters.map { $0.id }
)
}

return .visitChildren
}

public override func visit(_ node: SimpleStringLiteralSegmentListSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
}

for segment in node {
if let unexpectedAfterContent = segment.unexpectedAfterContent {
addDiagnostic(
node,
.forbiddenInterpolatedString,
handledNodes: [unexpectedAfterContent.id]
)
}
}

return .visitChildren
}

public override func visit(_ node: SourceFileSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ extension DiagnosticMessage where Self == StaticParserError {
public static var extraRightBracket: Self {
.init("unexpected ']' in type; did you mean to write an array type?")
}
public static var forbiddenExtendedEscapingString: Self {
.init("argument cannot be an extended escaping string literal")
}
public static var forbiddenInterpolatedString: Self {
return .init("argument cannot be an interpolated string literal")
}
public static var initializerInPattern: Self {
.init("unexpected initializer in pattern; did you mean to use '='?")
}
Expand Down
9 changes: 9 additions & 0 deletions Sources/SwiftParserDiagnostics/SyntaxExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ extension TokenKind {
return false
}
}

var isRawStringDelimiter: Bool {
switch self {
case .rawStringPoundDelimiter:
return true
default:
return false
}
}
}

public extension TriviaPiece {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ extension SyntaxKind {
return "'return' statement"
case .sameTypeRequirement:
return "same type requirement"
case .simpleStringLiteralExpr:
return "simple string literal"
case .someOrAnyType:
return "type"
case .sourceFile:
Expand Down
Loading