Parser and data structures library for working with the stella language in Swift.
Code generated by the ANTLR for Swift is terrible... So this package provides a more convenient interface to access Stella's syntax constructions as well as the parser built on top of the SwiftParsec library, which is a port of the original Haskell's Parsec library to Swfit.
Don't forget to install Swift if you don't have it.
To use this package inside another swift package,
add the following dependency to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/Artem-Goldenberg/SwiftStella", from: "1.1.0")
],
Then you can include this package as a module in normal Swift files like this:
import Stella
If you want to clone this package and run tests for the parser, you need to clone
the repository with this submodule repository
containing example Stella programs, on which the parser is being tested.
To do it you can simply use:
git clone --recurse-submodules "https://github.com/Artem-Goldenberg/SwiftStella.git"
After that, you can build the package or run tests using
swift build
swift test
Also, if you want to quickly see the parse tree for some file, you can run
swift run QuickParse <your stella source file>
It will print the serialized parse tree to the standard output.
For development version there is also a
TestGenerator
executable target, which parses current test files and prints serizlized program trees to theprinted-trees
folder, this is how you can generate new tests. To run it, switch to the repository root directory and executeswift run TestGenerator
command.
Quicker to demonstrate by an example:
import Stella
func parse(sourceText: String) throws {
// get Parsec parsers for Stella's syntactic elements by using
// the static attribute `parser` on them, see library sources
// (or explore with `Stella.` completions) for all syntax elements
let stellaParser = Stella.Program.parser // or just use Program.parser
// you can use Parsec parser's `run` method to run the parser on the specified text
// and source file name (for error messages). See docs for Swift's Parsec library, if
// you want to learn more about how to work with these parsers
let program: Program = try stellaParser.run(
sourceName: "somefilename.stella",
input: sourceText
)
let declarations = program.declarations // get the list of all declarations
// now you can do whatever you want with them:
for decl in declarations {
switch decl {
case .function(_, name, params, returnType, _, body, returnExpr):
...
case .genericFunction(...):
...
...
}
}
}
See Syntax.swift and Type.swift files for all cases and all syntax elements of the Stella language.