Skip to content

[🦩] Add proof of concept for ASTGen. #703

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

Closed
wants to merge 12 commits into from
Closed
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 Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ let package = Package(
"SyntaxKind.swift.gyb",
"SyntaxNodes.swift.gyb.template",
"SyntaxRewriter.swift.gyb",
"SyntaxTransform.swift.gyb",
"SyntaxTraits.swift.gyb",
"SyntaxVisitor.swift.gyb",
"TokenKind.swift.gyb",
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftDiagnostics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ add_library(SwiftDiagnostics STATIC

target_link_libraries(SwiftDiagnostics PUBLIC
SwiftSyntax)

target_include_directories(SwiftDiagnostics PUBLIC
"${CMAKE_CURRENT_BINARY_DIR}/../SwiftSyntax")

set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS SwiftDiagnostics)

Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftParser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ add_library(SwiftParser STATIC
target_link_libraries(SwiftParser PUBLIC
SwiftSyntax
SwiftDiagnostics)

target_include_directories(SwiftParser PUBLIC
"${CMAKE_CURRENT_BINARY_DIR}/../SwiftSyntax"
"${CMAKE_CURRENT_BINARY_DIR}/../SwiftDiagnostics")

set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS SwiftParser)

Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftSyntax/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ add_library(SwiftSyntax STATIC
gyb_generated/SyntaxKind.swift
gyb_generated/SyntaxRewriter.swift
gyb_generated/SyntaxTraits.swift
gyb_generated/SyntaxTransform.swift
gyb_generated/SyntaxVisitor.swift
gyb_generated/TokenKind.swift
gyb_generated/Tokens.swift
Expand Down
90 changes: 90 additions & 0 deletions Sources/SwiftSyntax/SyntaxTransform.swift.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
%{
from gyb_syntax_support import *
# -*- mode: Swift -*-
# Ignore the following admonition it applies to the resulting .swift file only
}%
//// Automatically Generated From SyntaxTransform.swift.gyb.
//// Do Not Edit Directly!
//===------ SyntaxTransform.swift - Syntax Transform Visitor Protocol -----===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 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
//
//===----------------------------------------------------------------------===//

public protocol SyntaxTransformVisitor {
associatedtype ResultType = Void

func visitAny(_ node: Syntax) -> ResultType

func visit(_ token: TokenSyntax) -> ResultType
func visit(_ node: UnknownSyntax) -> ResultType

% for node in SYNTAX_NODES:
% if is_visitable(node):
/// Visiting `${node.name}` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: the sum of whatever the child visitors return.
func visit(_ node: ${node.name}) -> ResultType
% end
% end
}

extension SyntaxTransformVisitor {
public func visit(_ token: TokenSyntax) -> ResultType {
visitAny(Syntax(token))
}

public func visit(_ node: UnknownSyntax) -> ResultType {
visitAny(Syntax(node))
}

% for node in SYNTAX_NODES:
% if is_visitable(node):
/// Visiting `${node.name}` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: nil by default.
public func visit(_ node: ${node.name}) -> ResultType {
visitAny(Syntax(node))
}
% end
% end

public func visit(_ node: Syntax) -> ResultType {
switch node.as(SyntaxEnum.self) {
case .token(let node):
return visit(node)
case .unknown(let node):
return visit(node)
% for node in NON_BASE_SYNTAX_NODES:
case .${node.swift_syntax_kind}(let derived):
return visit(derived)
% end
}
}

public func visit(_ node: ExprSyntax) -> ResultType {
visit(Syntax(node))
}

public func visit(_ node: PatternSyntax) -> ResultType {
visit(Syntax(node))
}

public func visit(_ node: TypeSyntax) -> ResultType {
visit(Syntax(node))
}

public func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) -> [ResultType] {
let syntaxNode = Syntax(node)
return NonNilRawSyntaxChildren(syntaxNode, viewMode: .sourceAccurate).map { rawChild in
let child = Syntax(SyntaxData(rawChild, parent: syntaxNode))
return visit(child)
}
}
}
Loading