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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
120
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
200
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
160
大喜利で理解するLLM as a Judge / Understanding LLM-as-a-Judge through Ogiri
rockname
0
190
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.4k
WebRTC映像をAirPlayに対応させる挑戦.pdf
monolithic_adam
0
340
Simple Storage Service(S3) is not simple
iwatsukayura
0
210
Are APIs Still Relevant in the AI Era?
soyuka
0
430
ソニーのクラウド共通基盤の変遷とAI時代の開発スタイルに合わせた進化 / The Journey of Sony’s Common Cloud Platform and Its Evolution for AI-Native Development
kenjiyoneyama
0
230
Heart of Swift Concurrency
koher
0
1.2k
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
920
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
550
Featured
See All Featured
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
3.3k
Claude Code のすすめ
schroneko
67
230k
Designing for Timeless Needs
cassininazir
1
520
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
460
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
Docker and Python
trallard
47
4.2k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.7k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
450
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
550
The untapped power of vector embeddings
frankvandijk
2
1.9k
Typedesign – Prime Four
hannesfritz
42
3.2k
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押してください!!
ご清聴ありがとうございました!!