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
パスワード再設定メールのURLについて知っておこう
uyuki234
0
180
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
300
[2026-10-14](C)Re:ゼロから始める異世界生活〜始まりの終わりと終わりの始まり〜
tosite
0
150
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
370
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
28
35k
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
170
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
520
No More NoSuchMethodError
exoego
1
110
Kotlinize Is Not Just Converting to Kotlin
chigichan24
1
190
Kotlinの15年とServer-Side Kotlin
n_takehata
1
160
作って学ぶSpring BootのAuto Configuration / JJUG Night Seminar 2026-10-19
rshindo
0
200
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Kaigi on Rails 2026, Tokyo, Japan
marcoroth
2
320
Featured
See All Featured
Believing is Seeing
oripsolob
1
250
Writing Fast Ruby
sferik
630
63k
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
My Coaching Mixtape
mlcsv
0
360
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Into the Great Unknown - MozCon
thekraken
41
2.8k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
360
The Spectacular Lies of Maps
axbom
PRO
1
1.1k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
22k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
43k
Automating Front-end Workflow
addyosmani
1370
210k
Large-scale JavaScript Application Architecture
addyosmani
516
110k
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押してください!!
ご清聴ありがとうございました!!