Skip to content

Commit 31efb35

Browse files
committed
examples: add macros examples
1 parent 2e25582 commit 31efb35

File tree

7 files changed

+129
-0
lines changed

7 files changed

+129
-0
lines changed

examples/srcs/binary/swift_binaries/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ swift_binary(
88
],
99
deps = [
1010
"//srcs/shared/module_b:module_b",
11+
"//srcs/shared/macros:stringify",
1112
"@swift_argument_parser//:ArgumentParser"
1213
]
1314
)

examples/srcs/binary/swift_binaries/entrance.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import srcs_shared_module_b_module_b
33
import ArgumentParser
4+
import Stringify
45

56
@main
67
struct Entrance: ParsableCommand {
@@ -10,5 +11,6 @@ struct Entrance: ParsableCommand {
1011
func run() throws {
1112
print(name)
1213
print(greeting_from_module_b())
14+
print(#stringify(1 + 2))
1315
}
1416
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_binary", "swift_library", "swift_test")
2+
load("@build_bazel_rules_swift//swift:swift_compiler_plugin.bzl", "swift_compiler_plugin", "universal_swift_compiler_plugin")
3+
4+
5+
swift_library(
6+
name = "stringify",
7+
srcs = ["Stringify.swift"],
8+
module_name = "Stringify",
9+
plugins = [":stringify_macro"],
10+
visibility = ["//visibility:public"],
11+
)
12+
13+
swift_compiler_plugin(
14+
name = "stringify_macro",
15+
srcs = [
16+
"StringifyMacro.swift",
17+
"StringifyMacroPlugin.swift",
18+
],
19+
module_name = "StringifyMacroPlugin",
20+
deps = [
21+
"@SwiftSyntax",
22+
"@SwiftSyntax//:SwiftCompilerPlugin",
23+
"@SwiftSyntax//:SwiftSyntaxBuilder",
24+
"@SwiftSyntax//:SwiftSyntaxMacros",
25+
],
26+
)
27+
28+
swift_binary(
29+
name = "stringify_client",
30+
srcs = ["StringifyClient.swift"],
31+
deps = [":stringify"],
32+
)
33+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2023 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
@freestanding(expression)
16+
public macro stringify<T>(_ value: T) -> (T, String) =
17+
#externalMacro(module: "StringifyMacroPlugin", type: "StringifyMacro")
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2023 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Stringify
16+
17+
@main
18+
struct Main {
19+
static func main() {
20+
print(#stringify(1 + 2))
21+
}
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2023 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import SwiftSyntax
16+
import SwiftSyntaxBuilder
17+
import SwiftSyntaxMacros
18+
19+
public struct StringifyMacro: ExpressionMacro {
20+
public static func expansion(
21+
of node: some FreestandingMacroExpansionSyntax,
22+
in context: some MacroExpansionContext
23+
) -> ExprSyntax {
24+
guard let argument = node.argumentList.first?.expression else {
25+
fatalError("compiler bug: the macro does not have any arguments")
26+
}
27+
return "(\(argument), \(literal: argument.description))"
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2023 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#if canImport(SwiftCompilerPlugin)
16+
import SwiftCompilerPlugin
17+
import SwiftSyntaxMacros
18+
19+
@main
20+
struct StringifyMacroPlugin: CompilerPlugin {
21+
let providingMacros: [Macro.Type] = [
22+
StringifyMacro.self
23+
]
24+
}
25+
#endif

0 commit comments

Comments
 (0)