I am looking for the official Swift grammar, in machine readable form, which can be read easily and parsed.
Does it exist?
TSPL has a section for the grammar, but it is not presented in a way which is amenable to parsing.
I am looking for the official Swift grammar, in machine readable form, which can be read easily and parsed.
Does it exist?
TSPL has a section for the grammar, but it is not presented in a way which is amenable to parsing.
The TSPL repo contains a summary of the grammar
How accurate do you need it to be?
Perhaps you could look at one of the free syntax highlighting engines that support Swift as a starting point.
I don’t think anyone has written anything down which you can just feed into a parser generator and get something back that supports everything the C++ and Swift parser implementations do. The fundamental “source of truth” here are those two hand-coded implementations.
is the eventual goal to do semantic highlighting on the web?
i’m not aware of any client side highlighters that can accurately highlight Swift, the best i’ve seen is GitHub’s and it still struggles with moderately complex Swift code.
on the server side, SwiftSyntax is the way to go.
I have some experience with this since I've had the good fortune (?) of having to write a grammar/table-driven parser for Swift to integrate in some of my employer's internal tooling. It's not perfect yet (it successfully parses 98.7% of the .swift
files in our monorepo, and that includes test cases from the toolchain that intentionally include parse errors so the actual number is higher), but it does handle a lot of the more complex cases in the language to disambiguate things like trailing closures, keywords that are contextual based on subsequent whitespace, and things like that.
Unfortunately the open-sourcing process for that project is hung up on some approvals so I don't know when I'll be able to actually point people to it.
swift-syntax is absolutely the answer if you're able to run Swift in your environment because you're never going to find a better parser than the real one, but for folks with other constraints, I hope I'm able to post an update on my work in the near-ish future.