Skip to content

Allow postfix #if to be empty #963

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
merged 1 commit into from
Oct 18, 2022
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 @@ -183,6 +183,7 @@ public let DECL_NODES: [Node] = [
classification: "BuildConfigId"),
Child(name: "Elements",
kind: "Syntax",
isOptional: true,
nodeChoices: [
Child(name: "Statements",
kind: "CodeBlockItemList"),
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftBasicFormat/generated/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ open class BasicFormat: SyntaxRewriter {
let unexpectedBetweenPoundKeywordAndCondition = node.unexpectedBetweenPoundKeywordAndCondition.map(self.visit)?.cast(UnexpectedNodesSyntax.self)
let condition = node.condition.map(self.visit)?.cast(ExprSyntax.self)
let unexpectedBetweenConditionAndElements = node.unexpectedBetweenConditionAndElements.map(self.visit)?.cast(UnexpectedNodesSyntax.self)
let elements = self.visit(node.elements).cast(Syntax.self)
let elements = node.elements.map(self.visit)?.cast(Syntax.self)
return Syntax(IfConfigClauseSyntax(unexpectedBeforePoundKeyword, poundKeyword: poundKeyword, unexpectedBetweenPoundKeywordAndCondition, condition: condition, unexpectedBetweenConditionAndElements, elements: elements))
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftParser/Directives.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ extension Parser {
public mutating func parsePoundIfDirective<Element: RawSyntaxNodeProtocol>(
_ parseElement: (inout Parser) -> Element?,
addSemicolonIfNeeded: (_ lastElement: Element, _ newItemAtStartOfLine: Bool, _ parser: inout Parser) -> Element? = { _, _, _ in nil },
syntax: (inout Parser, [Element]) -> RawSyntax
syntax: (inout Parser, [Element]) -> RawSyntax?
) -> RawIfConfigDeclSyntax {
var clauses = [RawIfConfigClauseSyntax]()
do {
Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -592,13 +592,13 @@ extension Parser {
// TODO: diagnose and skip the remaining token in the current clause.
return result
}
syntax: { parser, elements in
guard elements.count == 1 else {
assert(elements.isEmpty)
return RawSyntax(RawMissingExprSyntax(arena: parser.arena))
}
return RawSyntax(elements.first!)
syntax: { (parser, elements) -> RawSyntax? in
switch elements.count {
case 0: return nil
case 1: return RawSyntax(elements.first!)
default: fatalError("Postfix #if should only have one element")
}
}

return RawExprSyntax(RawPostfixIfConfigExprSyntax(
base: start, config: config,
Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftSyntax/Raw/gyb_generated/RawSyntaxNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7952,7 +7952,7 @@ public struct RawIfConfigClauseSyntax: RawSyntaxNodeProtocol, RawSyntaxToSyntax
_ unexpectedBetweenPoundKeywordAndCondition: RawUnexpectedNodesSyntax? = nil,
condition: RawExprSyntax?,
_ unexpectedBetweenConditionAndElements: RawUnexpectedNodesSyntax? = nil,
elements: RawSyntax,
elements: RawSyntax?,
_ unexpectedAfterElements: RawUnexpectedNodesSyntax? = nil,
arena: __shared SyntaxArena
) {
Expand All @@ -7964,7 +7964,7 @@ public struct RawIfConfigClauseSyntax: RawSyntaxNodeProtocol, RawSyntaxToSyntax
layout[2] = unexpectedBetweenPoundKeywordAndCondition?.raw
layout[3] = condition?.raw
layout[4] = unexpectedBetweenConditionAndElements?.raw
layout[5] = elements.raw
layout[5] = elements?.raw
layout[6] = unexpectedAfterElements?.raw
}
self.init(raw: raw)
Expand Down Expand Up @@ -8000,11 +8000,11 @@ public struct RawIfConfigClauseSyntax: RawSyntaxNodeProtocol, RawSyntaxToSyntax
public func withUnexpectedBetweenConditionAndElements(_ unexpectedBetweenConditionAndElements: RawUnexpectedNodesSyntax?, arena: SyntaxArena) -> RawIfConfigClauseSyntax {
return layoutView.replacingChild(at: 4, with: unexpectedBetweenConditionAndElements.map(RawSyntax.init), arena: arena).as(RawIfConfigClauseSyntax.self)!
}
public var elements: RawSyntax {
layoutView.children[5]!
public var elements: RawSyntax? {
layoutView.children[5]
}
public func withElements(_ elements: RawSyntax, arena: SyntaxArena) -> RawIfConfigClauseSyntax {
return layoutView.replacingChild(at: 5, with: RawSyntax(elements), arena: arena).as(RawIfConfigClauseSyntax.self)!
public func withElements(_ elements: RawSyntax?, arena: SyntaxArena) -> RawIfConfigClauseSyntax {
return layoutView.replacingChild(at: 5, with: elements.map(RawSyntax.init), arena: arena).as(RawIfConfigClauseSyntax.self)!
}
public var unexpectedAfterElements: RawUnexpectedNodesSyntax? {
layoutView.children[6].map(RawUnexpectedNodesSyntax.init(raw:))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
_verify(layout[2], as: RawUnexpectedNodesSyntax?.self)
_verify(layout[3], as: RawExprSyntax?.self)
_verify(layout[4], as: RawUnexpectedNodesSyntax?.self)
_verify(layout[5], as: RawSyntax.self)
_verify(layout[5], as: RawSyntax?.self)
_verify(layout[6], as: RawUnexpectedNodesSyntax?.self)
break
case .ifConfigClauseList:
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2752,14 +2752,14 @@ public enum SyntaxFactory {
return FunctionSignatureSyntax(data)
}
@available(*, deprecated, message: "Use initializer on IfConfigClauseSyntax")
public static func makeIfConfigClause(_ unexpectedBeforePoundKeyword: UnexpectedNodesSyntax? = nil, poundKeyword: TokenSyntax, _ unexpectedBetweenPoundKeywordAndCondition: UnexpectedNodesSyntax? = nil, condition: ExprSyntax?, _ unexpectedBetweenConditionAndElements: UnexpectedNodesSyntax? = nil, elements: Syntax, _ unexpectedAfterElements: UnexpectedNodesSyntax? = nil) -> IfConfigClauseSyntax {
public static func makeIfConfigClause(_ unexpectedBeforePoundKeyword: UnexpectedNodesSyntax? = nil, poundKeyword: TokenSyntax, _ unexpectedBetweenPoundKeywordAndCondition: UnexpectedNodesSyntax? = nil, condition: ExprSyntax?, _ unexpectedBetweenConditionAndElements: UnexpectedNodesSyntax? = nil, elements: Syntax?, _ unexpectedAfterElements: UnexpectedNodesSyntax? = nil) -> IfConfigClauseSyntax {
let layout: [RawSyntax?] = [
unexpectedBeforePoundKeyword?.raw,
poundKeyword.raw,
unexpectedBetweenPoundKeywordAndCondition?.raw,
condition?.raw,
unexpectedBetweenConditionAndElements?.raw,
elements.raw,
elements?.raw,
unexpectedAfterElements?.raw,
]
let raw = RawSyntax.makeLayout(kind: SyntaxKind.ifConfigClause,
Expand All @@ -2777,7 +2777,7 @@ public enum SyntaxFactory {
nil,
nil,
nil,
RawSyntax.makeEmptyLayout(kind: SyntaxKind.unknown, arena: .default),
nil,
nil,
], arena: .default))
return IfConfigClauseSyntax(data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5589,7 +5589,7 @@ public struct IfConfigClauseSyntax: SyntaxProtocol, SyntaxHashable {
_ unexpectedBetweenPoundKeywordAndCondition: UnexpectedNodesSyntax? = nil,
condition: ExprSyntax?,
_ unexpectedBetweenConditionAndElements: UnexpectedNodesSyntax? = nil,
elements: Syntax,
elements: Syntax?,
_ unexpectedAfterElements: UnexpectedNodesSyntax? = nil
) {
let layout: [RawSyntax?] = [
Expand All @@ -5598,7 +5598,7 @@ public struct IfConfigClauseSyntax: SyntaxProtocol, SyntaxHashable {
unexpectedBetweenPoundKeywordAndCondition?.raw,
condition?.raw,
unexpectedBetweenConditionAndElements?.raw,
elements.raw,
elements?.raw,
unexpectedAfterElements?.raw,
]
let raw = RawSyntax.makeLayout(kind: SyntaxKind.ifConfigClause,
Expand Down Expand Up @@ -5711,9 +5711,10 @@ public struct IfConfigClauseSyntax: SyntaxProtocol, SyntaxHashable {
return IfConfigClauseSyntax(newData)
}

public var elements: Syntax {
public var elements: Syntax? {
get {
let childData = data.child(at: 5, parent: Syntax(self))
if childData == nil { return nil }
return Syntax(childData!)
}
set(value) {
Expand All @@ -5726,7 +5727,7 @@ public struct IfConfigClauseSyntax: SyntaxProtocol, SyntaxHashable {
/// current `elements`, if present.
public func withElements(
_ newChild: Syntax?) -> IfConfigClauseSyntax {
let raw = newChild?.raw ?? RawSyntax.makeEmptyLayout(kind: SyntaxKind.unknown, arena: .default)
let raw = newChild?.raw
let newData = data.replacingChild(raw, at: 5)
return IfConfigClauseSyntax(newData)
}
Expand Down Expand Up @@ -5782,7 +5783,7 @@ extension IfConfigClauseSyntax: CustomReflectable {
"unexpectedBetweenPoundKeywordAndCondition": unexpectedBetweenPoundKeywordAndCondition.map(Syntax.init)?.asProtocol(SyntaxProtocol.self) as Any,
"condition": condition.map(Syntax.init)?.asProtocol(SyntaxProtocol.self) as Any,
"unexpectedBetweenConditionAndElements": unexpectedBetweenConditionAndElements.map(Syntax.init)?.asProtocol(SyntaxProtocol.self) as Any,
"elements": Syntax(elements).asProtocol(SyntaxProtocol.self),
"elements": elements.map(Syntax.init)?.asProtocol(SyntaxProtocol.self) as Any,
"unexpectedAfterElements": unexpectedAfterElements.map(Syntax.init)?.asProtocol(SyntaxProtocol.self) as Any,
])
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftSyntaxBuilder/generated/BuildableNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7999,7 +7999,7 @@ public struct IfConfigClause: SyntaxBuildable, ExpressibleAsIfConfigClause {
var unexpectedBetweenPoundKeywordAndCondition: UnexpectedNodes?
var condition: ExprBuildable?
var unexpectedBetweenConditionAndElements: UnexpectedNodes?
var elements: SyntaxBuildable
var elements: SyntaxBuildable?
}
enum Data {
case buildable(BuildableData)
Expand All @@ -8014,9 +8014,9 @@ public struct IfConfigClause: SyntaxBuildable, ExpressibleAsIfConfigClause {
/// - condition:
/// - unexpectedBetweenConditionAndElements:
/// - elements:
public init (leadingTrivia: Trivia = [], trailingTrivia: Trivia = [], unexpectedBeforePoundKeyword: ExpressibleAsUnexpectedNodes? = nil, poundKeyword: Token, unexpectedBetweenPoundKeywordAndCondition: ExpressibleAsUnexpectedNodes? = nil, condition: ExpressibleAsExprBuildable? = nil, unexpectedBetweenConditionAndElements: ExpressibleAsUnexpectedNodes? = nil, elements: ExpressibleAsSyntaxBuildable) {
public init (leadingTrivia: Trivia = [], trailingTrivia: Trivia = [], unexpectedBeforePoundKeyword: ExpressibleAsUnexpectedNodes? = nil, poundKeyword: Token, unexpectedBetweenPoundKeywordAndCondition: ExpressibleAsUnexpectedNodes? = nil, condition: ExpressibleAsExprBuildable? = nil, unexpectedBetweenConditionAndElements: ExpressibleAsUnexpectedNodes? = nil, elements: ExpressibleAsSyntaxBuildable? = nil) {
assert(poundKeyword.text == #"#if"# || poundKeyword.text == #"#elseif"# || poundKeyword.text == #"#else"#)
self.data = .buildable(BuildableData(leadingTrivia: leadingTrivia, trailingTrivia: trailingTrivia, unexpectedBeforePoundKeyword: unexpectedBeforePoundKeyword?.createUnexpectedNodes(), poundKeyword: poundKeyword, unexpectedBetweenPoundKeywordAndCondition: unexpectedBetweenPoundKeywordAndCondition?.createUnexpectedNodes(), condition: condition?.createExprBuildable(), unexpectedBetweenConditionAndElements: unexpectedBetweenConditionAndElements?.createUnexpectedNodes(), elements: elements.createSyntaxBuildable()))
self.data = .buildable(BuildableData(leadingTrivia: leadingTrivia, trailingTrivia: trailingTrivia, unexpectedBeforePoundKeyword: unexpectedBeforePoundKeyword?.createUnexpectedNodes(), poundKeyword: poundKeyword, unexpectedBetweenPoundKeywordAndCondition: unexpectedBetweenPoundKeywordAndCondition?.createUnexpectedNodes(), condition: condition?.createExprBuildable(), unexpectedBetweenConditionAndElements: unexpectedBetweenConditionAndElements?.createUnexpectedNodes(), elements: elements?.createSyntaxBuildable()))
}
public init(_ constructedNode: IfConfigClauseSyntax) {
self.data = .constructed(constructedNode)
Expand All @@ -8027,7 +8027,7 @@ public struct IfConfigClause: SyntaxBuildable, ExpressibleAsIfConfigClause {
func buildIfConfigClause() -> IfConfigClauseSyntax {
switch data {
case .buildable(let buildableData):
var result = IfConfigClauseSyntax(buildableData.unexpectedBeforePoundKeyword?.buildUnexpectedNodes(), poundKeyword: buildableData.poundKeyword.buildToken(), buildableData.unexpectedBetweenPoundKeywordAndCondition?.buildUnexpectedNodes(), condition: buildableData.condition?.buildExpr(), buildableData.unexpectedBetweenConditionAndElements?.buildUnexpectedNodes(), elements: buildableData.elements.buildSyntax())
var result = IfConfigClauseSyntax(buildableData.unexpectedBeforePoundKeyword?.buildUnexpectedNodes(), poundKeyword: buildableData.poundKeyword.buildToken(), buildableData.unexpectedBetweenPoundKeywordAndCondition?.buildUnexpectedNodes(), condition: buildableData.condition?.buildExpr(), buildableData.unexpectedBetweenConditionAndElements?.buildUnexpectedNodes(), elements: buildableData.elements?.buildSyntax())
result.leadingTrivia = buildableData.leadingTrivia + (result.leadingTrivia ?? [])
result.trailingTrivia = buildableData.trailingTrivia + (result.trailingTrivia ?? [])
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
extension SyntaxParser {
static func verifyNodeDeclarationHash() -> Bool {
return String(cString: swiftparse_syntax_structure_versioning_identifier()!) ==
"82461208aad7b9c2a13fb8715fdddce431fa2f3f"
"2a5be09fdaf9642a740c7436fd887ffa1251fa7b"
}
}
14 changes: 4 additions & 10 deletions Tests/SwiftParserTest/translated/IfconfigExprTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ final class IfconfigExprTests: XCTestCase {
}
"""#,
diagnostics: [
// TODO: Old parser expected error on line 8: unary operator cannot be separated from its operand
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code '+ otherExpr' in conditional compilation block"),
// TODO: Old parser expected error on line 13: unexpected tokens in '#if' expression body
DiagnosticSpec(locationMarker: "2️⃣", message: #"unexpected code 'print("debug")' in conditional compilation block"#),
]
)
Expand All @@ -120,7 +118,6 @@ final class IfconfigExprTests: XCTestCase {
}
""",
diagnostics: [
// TODO: Old parser expected error on line 12: unexpected tokens in '#if' expression body
DiagnosticSpec(message: "unexpected code '+ 12' in conditional compilation block"),
]
)
Expand All @@ -133,22 +130,19 @@ final class IfconfigExprTests: XCTestCase {
baseExpr
#if CONDITION_1
.methodOne()
#elseif CONDITION_21️⃣
#elseif CONDITION_2
// OK. Do nothing.
#endif
baseExpr
#if CONDITION_1
.methodOne()
#elseif CONDITION_22️⃣
3️⃣return
#elseif CONDITION_2
1️⃣return
#endif
}
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected expression in conditional compilation clause"),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected expression in conditional compilation clause"),
// TODO: Old parser expected error on line 12: unexpected tokens in '#if' expression body
DiagnosticSpec(locationMarker: "3️⃣", message: "unexpected code 'return' in conditional compilation block"),
DiagnosticSpec(message: "unexpected code 'return' in conditional compilation block"),
]
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ final class ImplicitGetterIncompleteTests: XCTestCase {
var a : Int {
switch i {
}1️⃣
// expected-error@+1 2 {{expected '}'}}
"""#,
diagnostics: [
// TODO: Old parser expected error on line 4: 'switch' statement body must have at least one 'case'
DiagnosticSpec(message: "expected '}' to end variable"),
DiagnosticSpec(message: "expected '}' to end function"),
]
Expand Down
2 changes: 1 addition & 1 deletion gyb_syntax_support/DeclNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
]),
Child('Condition', kind='Expr', name_for_diagnostics='condition', classification='BuildConfigId',
is_optional=True),
Child('Elements', kind='Syntax',
Child('Elements', is_optional=True, kind='Syntax',
node_choices=[
Child('Statements', kind='CodeBlockItemList'),
Child('SwitchCases', kind='SwitchCaseList'),
Expand Down