Parsing .swiftinterface

I want to introduce a very basic static analyzing tool for my personal usage and for that I want to parse .swiftinterface file. I wonder if it possible to obtain any parsing-friendly representation of it? Like human-readable AST? Or maybe JSON representation but with semantic markup (not basic tokens, I wish I can have info about functions, their signatures, availability, attributes etc).

A .swiftinterface file uses the same syntax as Swift source code, so I think you should be able to use the SwiftSyntax library to parse it.

2 Likes

You can use SwiftSemantics to parse Swift code and .swiftinterface files into a convenient data model:

import SwiftSyntax
import SwiftSemantics

let source = #"""
func greet() {}
"""#

var collector = DeclarationCollector()
let tree = try SyntaxParser.parse(source: source)
tree.walk(&collector)

collector.functions.first?.identifier // "greet"

Wow, this is really great. I will definitely try it out! Thanks!

1 Like