Syntax
Say goodbye to Scanner's and Abstract Syntax Trees. Syntax will take text, and turn it into the model that you actually need.
Syntax is a SwiftUI-like data-driven parser builder DSL. You use composition and functional programming to implement a top-down LL(n) parser with minimal effort. The result is a Parser tailor made to fit your desired output model ;)
Installation
Swift Package Manager
You can install Syntax via Swift Package Manager by adding the following line to your Package.swift
:
Usage
Syntax allows you to write parsers that perfectly fit the model that you want.
For example, let's say you want to parse the output of FizzBuzz.
With syntax you begin writing your model:
And then you can just write a parser. Parser's in Syntax are structs that return a body
much like in SwiftUI.
Let's break that down:
Repeat
signals that it should parse multiple values.Either
signals that you expect any of the following options.IntLiteral
will match the next integer literal it sees.Word
will match the word you give it, and only if it exists by itself.map
will map the value of a parser into something else.map(to:)
will map the value to a constant, useful for matching things like keywords.
To use this parser you can call the parse
function:
Syntax Tree's
Syntax supports outputing an Abstract Syntax Tree. All nodes in the Syntax Tree are annotated with a kind. The kind will be automatically derived from the name of a parser, but you can also specify it yourself:
To get the AST you can ask for it via the syntaxTree
function:
The AST is Encodable, so you can encode it into JSON. For example:
Syntax Highlighting
You can use your parser to highlight code on a Publish Site using this plugin.
More complex parsing
Alright. I hear you. FizzBuzz isn't exactly a challenge. So let's take it up a notch and parse JSON instead.
To be able to parse JSON, we have to understand what JSON even is. JSON consists of
a) the primitive values like strings, numbers, booleans
and b) any combinations of objects (dictionaries) and arrays.
So a possible model for JSON would be:
Syntax comes with constructs for most of these out of the box, like: StringLiteral
and IntLiteral
.
So we can rely on those. We can put most of our cases in an Either
which will try to parse whichever case works:
You will notice that we put a map
at the end of each line.
This is because parsers like StringLiteral
will return a String and not JSON. So we need to map that string to JSON.
So the rest of our job will go into parsing objects and literals. The first thing we notice though is that Arrays and Objects need to parse JSON again.
This recursion needs to be stated explicitely. For that we can wrap our Either
in a Recursive
that will give us a reference to the JSON Parser:
We can start with arrays. We can create an array parser that will parse multiple values of JSON
separated by commas, inside [
and ]
. In Syntax that looks like this:
Easy right. It's pretty much what we said in words. Dictionary is pretty similar, except that we have a key-value pairs separated by commas:
And for the final act, we add those two to our Either
for JSON: