Skip to content

Implement parsing for _forget statement #1362

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
Mar 1, 2023
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
1 change: 1 addition & 0 deletions CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public let KEYWORDS: [KeywordSpec] = [
KeywordSpec("fileprivate", isLexerClassified: true, requiresTrailingSpace: true),
KeywordSpec("final"),
KeywordSpec("for", isLexerClassified: true, requiresTrailingSpace: true),
KeywordSpec("_forget"),
KeywordSpec("forward"),
KeywordSpec("func", isLexerClassified: true, requiresTrailingSpace: true),
KeywordSpec("get"),
Expand Down
2 changes: 1 addition & 1 deletion CodeGeneration/Sources/SyntaxSupport/StmtNodes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# -*- mode: Swift -*-
# Ignore the following admonition it applies to the resulting .swift file only
}%
//// Automatically Generated From GenericNodes.swift.gyb.
//// Automatically Generated From StmtNodes.swift.gyb.
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//// Automatically Generated From GenericNodes.swift.gyb.
//// Automatically Generated From StmtNodes.swift.gyb.
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
//
Expand Down Expand Up @@ -211,6 +211,16 @@ public let STMT_NODES: [Node] = [
nameForDiagnostics: "body")
]),

Node(name: "ForgetStmt",
nameForDiagnostics: "'forget' statement",
kind: "Stmt",
children: [
Child(name: "ForgetKeyword",
kind: .token(choices: [.keyword(text: "_forget")])),
Child(name: "Expression",
kind: .node(kind: "Expr"))
]),

Node(name: "GuardStmt",
nameForDiagnostics: "'guard' statement",
kind: "Stmt",
Expand Down
38 changes: 38 additions & 0 deletions Sources/SwiftParser/Statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ extension Parser {
/// statement → control-transfer-statement ';'?
/// statement → defer-statement ';'?
/// statement → do-statement ';'?
/// statement → forget-statement ';'?
///
/// loop-statement → for-in-statement
/// loop-statement → while-statement
Expand Down Expand Up @@ -123,6 +124,8 @@ extension Parser {
return label(self.parseContinueStatement(continueHandle: handle), with: optLabel)
case (.fallthroughKeyword, let handle)?:
return label(self.parseFallthroughStatement(fallthroughHandle: handle), with: optLabel)
case (.forgetKeyword, let handle)?:
return label(self.parseForgetStatement(forgetHandle: handle), with: optLabel)
case (.returnKeyword, let handle)?:
return label(self.parseReturnStatement(returnHandle: handle), with: optLabel)
case (.throwKeyword, let handle)?:
Expand Down Expand Up @@ -374,6 +377,28 @@ extension Parser {
}
}

// MARK: Forget Statements

extension Parser {
/// Parse a forget statement.
///
/// Grammar
/// =======
///
/// forget-statement → 'forget' expression
@_spi(RawSyntax)
public mutating func parseForgetStatement(forgetHandle: RecoveryConsumptionHandle) -> RawForgetStmtSyntax {
let (unexpectedBeforeForgetKeyword, forgetKeyword) = self.eat(forgetHandle)
let expr = self.parseExpression()
return RawForgetStmtSyntax(
unexpectedBeforeForgetKeyword,
forgetKeyword: forgetKeyword,
expression: expr,
arena: self.arena
)
}
}

// MARK: Defer Statements

extension Parser {
Expand Down Expand Up @@ -946,6 +971,19 @@ extension Parser.Lookahead {
// yield statement of some singular expression.
return !self.peek().isAtStartOfLine
}
case .forgetKeyword?:
switch peek().rawTokenKind {
case .identifier, .keyword:
// Since some identifiers like "self" are classified as keywords,
// we want to recognize those too, to handle "forget self". We also
// accept any identifier since we want to emit a nice error message
// later on during type checking.
return true
default:
// any other token following "forget" means it's not the statement.
// For example, could be the function call "forget()".
return false
}
case nil:
return false
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftParser/TokenSpecSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ enum CanBeStatementStart: TokenSpecSet {
case doKeyword
case fallthroughKeyword
case forKeyword
case forgetKeyword
case guardKeyword
case ifKeyword
case repeatKeyword
Expand All @@ -97,6 +98,7 @@ enum CanBeStatementStart: TokenSpecSet {
case TokenSpec(.do): self = .doKeyword
case TokenSpec(.fallthrough): self = .fallthroughKeyword
case TokenSpec(.for): self = .forKeyword
case TokenSpec(._forget): self = .forgetKeyword
case TokenSpec(.guard): self = .guardKeyword
case TokenSpec(.if): self = .ifKeyword
case TokenSpec(.repeat): self = .repeatKeyword
Expand All @@ -117,6 +119,7 @@ enum CanBeStatementStart: TokenSpecSet {
case .doKeyword: return .keyword(.do)
case .fallthroughKeyword: return .keyword(.fallthrough)
case .forKeyword: return .keyword(.for)
case .forgetKeyword: return TokenSpec(._forget, recoveryPrecedence: .stmtKeyword)
case .guardKeyword: return .keyword(.guard)
case .ifKeyword: return .keyword(.if)
case .repeatKeyword: return .keyword(.repeat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
- <doc:SwiftSyntax/FallthroughStmtSyntax>
- <doc:SwiftSyntax/BreakStmtSyntax>
- <doc:SwiftSyntax/ThrowStmtSyntax>
- <doc:SwiftSyntax/ForgetStmtSyntax>

### Expressions

Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftSyntax/generated/Keyword.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public enum Keyword: UInt8, Hashable {
case `fileprivate`
case final
case `for`
case _forget
case forward
case `func`
case get
Expand Down Expand Up @@ -428,6 +429,8 @@ public enum Keyword: UInt8, Hashable {
self = .`default`
case "dynamic":
self = .dynamic
case "_forget":
self = ._forget
case "forward":
self = .forward
case "message":
Expand Down Expand Up @@ -946,6 +949,7 @@ public enum Keyword: UInt8, Hashable {
"fileprivate",
"final",
"for",
"_forget",
"forward",
"func",
"get",
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftSyntax/generated/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ extension Syntax {
.node(FloatLiteralExprSyntax.self),
.node(ForInStmtSyntax.self),
.node(ForcedValueExprSyntax.self),
.node(ForgetStmtSyntax.self),
.node(FunctionCallExprSyntax.self),
.node(FunctionDeclSyntax.self),
.node(FunctionParameterListSyntax.self),
Expand Down Expand Up @@ -493,6 +494,8 @@ extension SyntaxKind {
return ForInStmtSyntax.self
case .forcedValueExpr:
return ForcedValueExprSyntax.self
case .forgetStmt:
return ForgetStmtSyntax.self
case .functionCallExpr:
return FunctionCallExprSyntax.self
case .functionDecl:
Expand Down Expand Up @@ -1020,6 +1023,8 @@ extension SyntaxKind {
return "'for' statement"
case .forcedValueExpr:
return "force unwrap"
case .forgetStmt:
return "'forget' statement"
case .functionCallExpr:
return "function call"
case .functionDecl:
Expand Down
8 changes: 8 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxAnyVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,14 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
visitAnyPost(node._syntaxNode)
}

override open func visit(_ node: ForgetStmtSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}

override open func visitPost(_ node: ForgetStmtSyntax) {
visitAnyPost(node._syntaxNode)
}

override open func visit(_ node: FunctionCallExprSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/SwiftSyntax/generated/SyntaxBaseNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public struct StmtSyntax: StmtSyntaxProtocol, SyntaxHashable {

public init?<S: SyntaxProtocol>(_ node: S) {
switch node.raw.kind {
case .breakStmt, .continueStmt, .deferStmt, .doStmt, .expressionStmt, .fallthroughStmt, .forInStmt, .guardStmt, .labeledStmt, .missingStmt, .repeatWhileStmt, .returnStmt, .throwStmt, .whileStmt, .yieldStmt:
case .breakStmt, .continueStmt, .deferStmt, .doStmt, .expressionStmt, .fallthroughStmt, .forInStmt, .forgetStmt, .guardStmt, .labeledStmt, .missingStmt, .repeatWhileStmt, .returnStmt, .throwStmt, .whileStmt, .yieldStmt:
self._syntaxNode = node._syntaxNode
default:
return nil
Expand All @@ -535,7 +535,7 @@ public struct StmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
internal init(_ data: SyntaxData) {
#if DEBUG
switch data.raw.kind {
case .breakStmt, .continueStmt, .deferStmt, .doStmt, .expressionStmt, .fallthroughStmt, .forInStmt, .guardStmt, .labeledStmt, .missingStmt, .repeatWhileStmt, .returnStmt, .throwStmt, .whileStmt, .yieldStmt:
case .breakStmt, .continueStmt, .deferStmt, .doStmt, .expressionStmt, .fallthroughStmt, .forInStmt, .forgetStmt, .guardStmt, .labeledStmt, .missingStmt, .repeatWhileStmt, .returnStmt, .throwStmt, .whileStmt, .yieldStmt:
break
default:
fatalError("Unable to create StmtSyntax from \(data.raw.kind)")
Expand Down Expand Up @@ -578,6 +578,7 @@ public struct StmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
.node(ExpressionStmtSyntax.self),
.node(FallthroughStmtSyntax.self),
.node(ForInStmtSyntax.self),
.node(ForgetStmtSyntax.self),
.node(GuardStmtSyntax.self),
.node(LabeledStmtSyntax.self),
.node(MissingStmtSyntax.self),
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public enum SyntaxEnum {
case floatLiteralExpr(FloatLiteralExprSyntax)
case forInStmt(ForInStmtSyntax)
case forcedValueExpr(ForcedValueExprSyntax)
case forgetStmt(ForgetStmtSyntax)
case functionCallExpr(FunctionCallExprSyntax)
case functionDecl(FunctionDeclSyntax)
case functionParameterList(FunctionParameterListSyntax)
Expand Down Expand Up @@ -492,6 +493,8 @@ public extension Syntax {
return .forInStmt(ForInStmtSyntax(self)!)
case .forcedValueExpr:
return .forcedValueExpr(ForcedValueExprSyntax(self)!)
case .forgetStmt:
return .forgetStmt(ForgetStmtSyntax(self)!)
case .functionCallExpr:
return .functionCallExpr(FunctionCallExprSyntax(self)!)
case .functionDecl:
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftSyntax/generated/SyntaxKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public enum SyntaxKind {
case floatLiteralExpr
case forInStmt
case forcedValueExpr
case forgetStmt
case functionCallExpr
case functionDecl
case functionParameterList
Expand Down
25 changes: 25 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxRewriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,13 @@ open class SyntaxRewriter {
return ExprSyntax(visitChildren(node))
}

/// Visit a `ForgetStmtSyntax`.
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
open func visit(_ node: ForgetStmtSyntax) -> StmtSyntax {
return StmtSyntax(visitChildren(node))
}

/// Visit a `FunctionCallExprSyntax`.
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
Expand Down Expand Up @@ -3375,6 +3382,20 @@ open class SyntaxRewriter {
return Syntax(visit(node))
}

/// Implementation detail of visit(_:). Do not call directly.
private func visitImplForgetStmtSyntax(_ data: SyntaxData) -> Syntax {
let node = ForgetStmtSyntax(data)
// Accessing _syntaxNode directly is faster than calling Syntax(node)
visitPre(node._syntaxNode)
defer {
visitPost(node._syntaxNode)
}
if let newNode = visitAny(node._syntaxNode) {
return newNode
}
return Syntax(visit(node))
}

/// Implementation detail of visit(_:). Do not call directly.
private func visitImplFunctionCallExprSyntax(_ data: SyntaxData) -> Syntax {
let node = FunctionCallExprSyntax(data)
Expand Down Expand Up @@ -5809,6 +5830,8 @@ open class SyntaxRewriter {
return visitImplForInStmtSyntax
case .forcedValueExpr:
return visitImplForcedValueExprSyntax
case .forgetStmt:
return visitImplForgetStmtSyntax
case .functionCallExpr:
return visitImplFunctionCallExprSyntax
case .functionDecl:
Expand Down Expand Up @@ -6339,6 +6362,8 @@ open class SyntaxRewriter {
return visitImplForInStmtSyntax(data)
case .forcedValueExpr:
return visitImplForcedValueExprSyntax(data)
case .forgetStmt:
return visitImplForgetStmtSyntax(data)
case .functionCallExpr:
return visitImplFunctionCallExprSyntax(data)
case .functionDecl:
Expand Down
14 changes: 14 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,11 @@ public protocol SyntaxTransformVisitor {
/// - Returns: the sum of whatever the child visitors return.
func visit(_ node: ForcedValueExprSyntax) -> ResultType

/// Visiting `ForgetStmtSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: the sum of whatever the child visitors return.
func visit(_ node: ForgetStmtSyntax) -> ResultType

/// Visiting `FunctionCallExprSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: the sum of whatever the child visitors return.
Expand Down Expand Up @@ -2053,6 +2058,13 @@ extension SyntaxTransformVisitor {
visitAny(Syntax(node))
}

/// Visiting `ForgetStmtSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: nil by default.
public func visit(_ node: ForgetStmtSyntax) -> ResultType {
visitAny(Syntax(node))
}

/// Visiting `FunctionCallExprSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: nil by default.
Expand Down Expand Up @@ -3357,6 +3369,8 @@ extension SyntaxTransformVisitor {
return visit(derived)
case .forcedValueExpr(let derived):
return visit(derived)
case .forgetStmt(let derived):
return visit(derived)
case .functionCallExpr(let derived):
return visit(derived)
case .functionDecl(let derived):
Expand Down
25 changes: 25 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,18 @@ open class SyntaxVisitor {
open func visitPost(_ node: ForcedValueExprSyntax) {
}

/// Visiting `ForgetStmtSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: how should we continue visiting.
open func visit(_ node: ForgetStmtSyntax) -> SyntaxVisitorContinueKind {
return .visitChildren
}

/// The function called after visiting `ForgetStmtSyntax` and its descendents.
/// - node: the node we just finished visiting.
open func visitPost(_ node: ForgetStmtSyntax) {
}

/// Visiting `FunctionCallExprSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: how should we continue visiting.
Expand Down Expand Up @@ -4310,6 +4322,17 @@ open class SyntaxVisitor {
visitPost(node)
}

/// Implementation detail of doVisit(_:_:). Do not call directly.
private func visitImplForgetStmtSyntax(_ data: SyntaxData) {
let node = ForgetStmtSyntax(data)
let needsChildren = (visit(node) == .visitChildren)
// Avoid calling into visitChildren if possible.
if needsChildren && !node.raw.layoutView!.children.isEmpty {
visitChildren(node)
}
visitPost(node)
}

/// Implementation detail of doVisit(_:_:). Do not call directly.
private func visitImplFunctionCallExprSyntax(_ data: SyntaxData) {
let node = FunctionCallExprSyntax(data)
Expand Down Expand Up @@ -6241,6 +6264,8 @@ open class SyntaxVisitor {
visitImplForInStmtSyntax(data)
case .forcedValueExpr:
visitImplForcedValueExprSyntax(data)
case .forgetStmt:
visitImplForgetStmtSyntax(data)
case .functionCallExpr:
visitImplFunctionCallExprSyntax(data)
case .functionDecl:
Expand Down
Loading