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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
Transcript
30 slides
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押してください!!
ご清聴ありがとうございました!!
More Decks by Ryu
See All by Ryu
TCAのようなアーキテクチャを作ってみた話
ryu_hu03
2
510
SwiftのKeyPathを使いこなす
ryu_hu03
0
890
Other Decks in Programming
See All in Programming
AI Augmentation Techniques with Liberty
ivargrimstad
1
560
Secure Your Coding Agent Like It’s Malware (v1.5) 🇬🇧 @Code.talks 2026
richargh
0
200
Distance field of Julia set
kinakomoti321
0
330
flutter_test のその先へ — Flutter アプリの LLM 機能に「評価」という品質保証を
kosukesaigusa
0
210
クラウドゲーミングのパフォーマンス最適化に挑む / Cloud Gaming Performance Tuning
vivion
0
360
Materialが消えた日 〜真っ白なFlutterキャンバスにRenderObjectで絵を描く〜
umigishiaoi
0
170
AppFunctionsの内部実装を紐解く
hiroaki404
0
120
マルチプロダクトSaaSの アプリ一覧を速くする——複数プロダクトにまたがる「権限行脚」を減らすまで #techramen26conf
mktakuya
1
200
なぜ私たちは「現時点では」KMPの導入を見送ったのか
chigichan24
1
630
そのデザイントークン、Flutterでも同じ見た目になりますか?
nakamizu
0
470
AI時代のシニアエンジニアとは
yosuke_furukawa
PRO
15
12k
Enforcing Your Architecture with Unit Tests in the AI Era (Swift Connection X)
steliosf
PRO
1
190
Featured
See All Featured
Designing for humans not robots
tammielis
254
26k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
500
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.2k
Mind Mapping
helmedeiros
1
450
A better future with KSS
kneath
240
18k
Why Our Code Smells
bkeepers
PRO
340
58k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3.1k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
790
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
540
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
The Limits of Empathy - UXLibs8
cassininazir
2
800