How Swift has brought new life to parsing

Doug Ross
Skills Matter
Published in
3 min readJan 30, 2019

--

Author of iOS Core Animation: Advanced Techniques and the creator of iCarousel and SwiftFormat, Nick Lockwood is well acquainted with the benefits of parsing, especially in the opportunities open to those who know how to write parsers.

Recently speaking at FrenchKit in Paris, he is headed to London in March to talk at iOSCon on writing parsers using Swift, Apple’s statically typed multi-paradigm language.

Since writing iOS Core, Nick has seen how Swift has already (and will increasingly) open up parsing to a wider range of iOS developers.

“Unlike natural (spoken) languages, most programming languages have so-called ‘regular’ grammars, making it possible to parse them easily and unambiguously using a technique called ‘recursive descent’,” he explains.

“There are generally two steps to parsing: lexical analysis (breaking the input into individual words and symbols) and syntactic analysis (forming those words into meaningful statements or commands).

“Swift is excellent for performing both of these steps (much better than Objective-C) because of its combination of very good unicode support, fast and efficient stack-allocated data structures, and strong typing.

“If you are comfortable with writing parsers it opens up a lot of exciting possibilities for how you write apps. For example, you are no longer restricted to using built-in formats such as Plists and JSON to store data, but can instead easily add support for esoteric formats that don’t currently have Swift implementations or even create your own domain-specific-languages (DSLs), specifically optimised for your application.

“The same applies to scripting. Instead of choosing between hard-coding all your business logic in Swift (with a long recompile cycle for every small change) or using a heavyweight, bloated scripting language like JavaScript, you can invent a new language that is perfect for describing the specific data structures and operations in your app, and which can be interpreted with much less overhead than the JSVM.”

Despite recursive descent parsing having an extensive history, Nick points out that the introduction of Swift has opened the technique up to more iOS developers, saving them the need to learn a second language like C++.

“I also feel that the very creation of Swift and the openness with which its creators and maintainers have been willing to discuss its development with the community have opened our eyes to the idea that programming languages are not something that are handed down from an ivory tower, but are in fact something that ordinary developers can understand, contribute to, and even create from scratch.

“I hope that creating DSLs will become more popular as a way to solve everyday coding problems, because the idea that you can create new tools that exactly suit the problem at hand, rather than trying to bend pre-existing solutions to fit, is very liberating.”

🌆 Nick Lockwood is discussing parsing with Swift at iOSCon London in March. 👍 For news and articles from Skills Matter, subscribe to our newsletter here.

--

--