Skip to content

Commit e048c1c

Browse files
authored
Merge pull request #1155 from DougGregor/simplify-syntax-macros-api
2 parents 88cec89 + 05b48c6 commit e048c1c

File tree

8 files changed

+271
-263
lines changed

8 files changed

+271
-263
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ let package = Package(
150150
),
151151
.executableTarget(
152152
name: "swift-parser-cli",
153-
dependencies: ["SwiftDiagnostics", "SwiftSyntax", "SwiftParser", "SwiftParserDiagnostics", "SwiftOperators", "_SwiftSyntaxMacros",
153+
dependencies: ["SwiftDiagnostics", "SwiftSyntax", "SwiftParser", "SwiftParserDiagnostics", "SwiftOperators",
154154
.product(name: "ArgumentParser", package: "swift-argument-parser")]
155155
),
156156
.testTarget(name: "IDEUtilsTest", dependencies: ["_SwiftSyntaxTestSupport", "SwiftParser", "SwiftSyntax", "IDEUtils"]),

Sources/_SwiftSyntaxMacros/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ add_swift_host_library(_SwiftSyntaxMacros
1010
ExpressionMacro.swift
1111
Macro.swift
1212
MacroExpansionContext.swift
13-
MacroSystem+Builtin.swift
14-
MacroSystem+Examples.swift
1513
MacroSystem.swift
1614
Syntax+MacroEvaluation.swift
1715
)

Sources/_SwiftSyntaxMacros/MacroSystem+Builtin.swift

Lines changed: 0 additions & 106 deletions
This file was deleted.

Sources/_SwiftSyntaxMacros/MacroSystem+Examples.swift

Lines changed: 0 additions & 37 deletions
This file was deleted.

Sources/_SwiftSyntaxMacros/MacroSystem.swift

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import SwiftDiagnostics
1414
import SwiftSyntax
1515

1616
/// Describes the kinds of errors that can occur within a macro system.
17-
public enum MacroSystemError: Error {
17+
enum MacroSystemError: Error {
1818
/// Indicates that a macro with the given name has already been defined.
1919
case alreadyDefined(new: Macro.Type, existing: Macro.Type)
2020

@@ -36,16 +36,16 @@ public enum MacroSystemError: Error {
3636
}
3737

3838
/// A system of known macros that can be expanded syntactically
39-
public struct MacroSystem {
39+
struct MacroSystem {
4040
var macros: [String: Macro.Type] = [:]
4141

4242
/// Create an empty macro system.
43-
public init() {}
43+
init() {}
4444

4545
/// Add a macro to the system.
4646
///
4747
/// Throws an error if there is already a macro with this name.
48-
public mutating func add(_ macro: Macro.Type, name: String) throws {
48+
mutating func add(_ macro: Macro.Type, name: String) throws {
4949
if let knownMacro = macros[name] {
5050
throw MacroSystemError.alreadyDefined(new: macro, existing: knownMacro)
5151
}
@@ -54,7 +54,7 @@ public struct MacroSystem {
5454
}
5555

5656
/// Look for a macro with the given name.
57-
public func lookup(_ macroName: String) -> Macro.Type? {
57+
func lookup(_ macroName: String) -> Macro.Type? {
5858
return macros[macroName]
5959
}
6060
}
@@ -101,20 +101,28 @@ class MacroApplication: SyntaxRewriter {
101101
}
102102
}
103103

104-
extension MacroSystem {
105-
/// Expand all macros encountered within the given syntax tree.
106-
///
107-
/// - Parameter node: The syntax node in which macros will be evaluated.
108-
/// - Parameter context: The context in which macros are evaluated.
109-
/// - Returns: the syntax tree with all macros evaluated.
110-
public func evaluateMacros<Node: SyntaxProtocol>(
111-
node: Node,
104+
extension SyntaxProtocol {
105+
/// Expand all uses of the given set of macros within this syntax
106+
/// node.
107+
public func expand(
108+
macros: [String: Macro.Type],
112109
in context: inout MacroExpansionContext
113110
) -> Syntax {
111+
// Build the macro system.
112+
var system = MacroSystem()
113+
for (macroName, macroType) in macros {
114+
try! system.add(macroType, name: macroName)
115+
}
116+
114117
let applier = MacroApplication(
115-
macroSystem: self,
118+
macroSystem: system,
116119
context: context
117120
)
118-
return applier.visit(Syntax(node))
121+
122+
defer {
123+
context = applier.context
124+
}
125+
126+
return applier.visit(Syntax(self))
119127
}
120128
}

Sources/_SwiftSyntaxMacros/Syntax+MacroEvaluation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ extension Syntax {
9595
/// Determine the name of the macro that is evaluated by this syntax node,
9696
/// if indeed it is a macro evaluation. For example, "#stringify(x)" has the
9797
/// name "stringify".
98-
public var evaluatedMacroName: String? {
98+
var evaluatedMacroName: String? {
9999
switch self.as(SyntaxEnum.self) {
100100
case .macroExpansionDecl(let expansion):
101101
return expansion.macro.text
@@ -114,7 +114,7 @@ extension Syntax {
114114
/// This operation only makes sense when `evaluatedMacroName` produces a
115115
/// non-nil value, indicating that this syntax node is a macro evaluation of
116116
/// some kind.
117-
public func evaluateMacro(
117+
func evaluateMacro(
118118
with macroSystem: MacroSystem,
119119
context: inout MacroExpansionContext
120120
) -> Syntax {

Sources/swift-parser-cli/swift-parser-cli.swift

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import SwiftDiagnostics
1414
import SwiftSyntax
15-
@_spi(Testing) import _SwiftSyntaxMacros
1615
import SwiftParser
1716
import SwiftParserDiagnostics
1817
import SwiftOperators
@@ -82,7 +81,6 @@ class SwiftParserCli: ParsableCommand {
8281
subcommands: [
8382
PrintDiags.self,
8483
PrintTree.self,
85-
ExpandMacros.self,
8684
Reduce.self,
8785
VerifyRoundTrip.self,
8886
]
@@ -219,51 +217,6 @@ class PrintTree: ParsableCommand {
219217
}
220218
}
221219

222-
class ExpandMacros: ParsableCommand {
223-
static var configuration = CommandConfiguration(
224-
commandName: "expand-macros",
225-
abstract: "Expand example macros and print the result source code"
226-
)
227-
228-
required init() {}
229-
230-
@Argument(help: "The source file that should be parsed; if omitted, use stdin")
231-
var sourceFile: String
232-
233-
@Flag(name: .long, help: "Perform sequence folding with the standard operators")
234-
var foldSequences: Bool = false
235-
236-
func run() throws {
237-
let source = try getContentsOfSourceFile(at: sourceFile)
238-
239-
source.withUnsafeBufferPointer { sourceBuffer in
240-
let tree = Parser.parse(source: sourceBuffer)
241-
242-
let resultTree: Syntax
243-
if foldSequences {
244-
resultTree = foldAllSequences(tree).0
245-
} else {
246-
resultTree = Syntax(tree)
247-
}
248-
249-
var context = MacroExpansionContext(
250-
moduleName: "MyModule",
251-
fileName: self.sourceFile.withoutPath()
252-
)
253-
var diags = ParseDiagnosticsGenerator.diagnostics(for: tree)
254-
let transformedTree = MacroSystem.exampleSystem.evaluateMacros(
255-
node: resultTree,
256-
in: &context
257-
)
258-
259-
diags.append(contentsOf: context.diagnostics)
260-
261-
print(transformedTree)
262-
print(DiagnosticsFormatter.annotatedSource(tree: tree, diags: diags))
263-
}
264-
}
265-
}
266-
267220
class Reduce: ParsableCommand {
268221
static var configuration = CommandConfiguration(
269222
commandName: "reduce",

0 commit comments

Comments
 (0)