Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
SwiftSyntaxが面白い
Search
Ryu
May 22, 2023
Programming
1.1k
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
SwiftSyntaxが面白い
Ryu
May 22, 2023
More Decks by Ryu
See All by Ryu
TCAのようなアーキテクチャを作ってみた話
ryu_hu03
2
510
SwiftのKeyPathを使いこなす
ryu_hu03
0
880
Other Decks in Programming
See All in Programming
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
140
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
420
Are APIs Still Relevant in the AI Era?
soyuka
0
310
Go × SIMDで高速化するベクトル検索 ~ルーフラインモデルでSIMDが効く境界を探れ! ~
po3rin
1
3.7k
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
520
wkhtmltopdfの次どうするか問題2026
willnet
2
1.7k
JRuby: Past, Present, and Future
headius
0
200
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
740
カツオ、ご期待ください
suneo3476
0
100
Ghostty + Neovimで作る 透明でカッコ良い開発環境
j341nono
0
130
大喜利で理解するLLM as a Judge / Understanding LLM-as-a-Judge through Ogiri
rockname
0
160
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
220
Featured
See All Featured
[SF Ruby Conf 2025] Rails X
palkan
3
1.4k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
330
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
300
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.3k
Marketing to machines
jonoalderson
1
5.8k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
440
Leo the Paperboy
mayatellez
10
2.3k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
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押してください!!
ご清聴ありがとうございました!!