Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
SwiftSyntaxが面白い
Search
Ryu
May 22, 2023
Programming
3
1k
SwiftSyntaxが面白い
Ryu
May 22, 2023
Tweet
Share
More Decks by Ryu
See All by Ryu
TCAのようなアーキテクチャを作ってみた話
ryu_hu03
2
470
SwiftのKeyPathを使いこなす
ryu_hu03
0
780
Other Decks in Programming
See All in Programming
Webサーバーサイド言語としてのRustについて
kouyuume
1
5k
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
570
kiroとCodexで最高のSpec駆動開発を!!数時間で web3ネイティブなミニゲームを作ってみたよ!
mashharuki
0
960
bootcamp2025_バックエンド研修_WebAPIサーバ作成.pdf
geniee_inc
0
140
開発組織の戦略的な役割と 設計スキル向上の効果
masuda220
PRO
10
1.8k
スキーマ駆動で、Zod OpenAPI Honoによる、API開発するために、Hono Takibiというライブラリを作っている
nakita628
0
330
コードとあなたと私の距離 / The Distance Between Code, You, and I
hiro_y
0
200
CSC305 Lecture 11
javiergs
PRO
0
310
AI 駆動開発におけるコミュニティと AWS CDK の価値
konokenj
5
290
AkarengaLT vol.38
hashimoto_kei
1
130
Amazon ECS Managed Instances が リリースされた!キャッチアップしよう!! / Let's catch up Amazon ECS Managed Instances
cocoeyes02
0
110
iOSでSVG画像を扱う
kishikawakatsumi
0
170
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
31
2.7k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
285
14k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
253
22k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.7k
The Art of Programming - Codeland 2020
erikaheidi
56
14k
The World Runs on Bad Software
bkeepers
PRO
72
11k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
34
2.3k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
9.7k
The Invisible Side of Design
smashingmag
302
51k
What's in a price? How to price your products and services
michaelherold
246
12k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
116
20k
YesSQL, Process and Tooling at Scale
rocio
173
15k
Transcript
SwiftSyntaxが⾯⽩い ~ Enumのcaseをアルファベット順でsortしよう ~
About Me! • りゅう • 法政⼤学電気電⼦⼯学科 3年 • NewsPicksでインターン •
最近SwiftSyntaxに⼊⾨した • CLIツールを作るのが好き @Ryu0118 @ryu_hu03 @Ryu0118 @ryu_hu03
• Apple公式のライブラリ • ソースコードを解析、⽣成、変換することができるライブラリ • swift-formatやSwiftLint, mockolo, Sourceryなどのライブラリ で使⽤されている。 SwiftSyntaxとは
今回はSwiftSyntaxを⽤いてEnumのcaseをアルファベット 順にソートするプログラムを書いてみる =>
コードを抽象構⽂⽊(AST)に変換してみる
enum Hoge { case b case a } ASTに変換すると… (使⽤したツール
https://swift-ast-explorer.com)
この⼆つを並び替えるコードを 書いたらいけそう🧐
SwiftSyntaxを少し学ぶ
SyntaxRewriter • 構⽂⽊を書き換えることができる • descriptionプロパティで書き換えたSwiftコードを出⼒できる ⚠ 本スライドのSwiftSyntaxはmainブランチを指定して使⽤しています。 最新バージョンとは書き⽅が違うのでご了承ください。
この2つが対応している。 つまり、コード内にEnumがあった場合、 このvisitメソッドが呼び出される。
それぞれの対応関係はこんな感じで、 node.with(_:_:)で⼦ノードの値を書き換えることができる。
membersを空のListに置き換えた! これでcaseがないenumができるはず 実際にEnumEmptyRewriterを動かしてみよう!
ソースコードを構⽂⽊に変換して
SyntaxRewriterを使って構⽂⽊を書き換える
構⽂⽊からSwiftのコードに変換し、出⼒
実⾏すると…
期待通りの出⼒になった
enumのcaseをアルファ ベット順に並び替える
None
None
None
None
None
None
None
実⾏すると… let `enum` = """ enum E1 { case b
case a } """ let syntax = Parser.parse(source: `enum`) let formatted = EnumRewriter().visit(syntax) print(formatted.description)
並び変わった!!
enum E1 { case b case a case j case
h case i case e case d case g case f case c } 別のenumでもちゃんと並びかわる!
ただ、、 enum E1 { case b, a, j, h, i,
e, d } このenumには⾮対応
https://github.com/Ryu0118/Sorter それにも対応したコードがこちらにあるの で、 よかったらStar押してください!!
ご清聴ありがとうございました!!