Contents

Artem-Goldenberg/SwiftStella

Parser and data structures for working with Stella language in Swift

Installation

Don't forget to install Swift if you don't have it. Use swift --version to check installation.

Dependency for swift package manager

To use this package inside another swift package, add the following dependency to your Package.swift file: ``Swift dependencies: [ .package(url: "https://github.com/Artem-Goldenberg/SwiftStella.git", from: "1.2.1") ], ``

Then you can include this package as a module in normal Swift files like this: ``Swift import Stella ``

Cloning for testing and development

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"

If you've already cloned without the --recurse-submodules,

use git submodule init and then git submodule update commands to download needed repository.

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 Stella source, you can run

swift run QuickParse <your stella source file> 

It will print the serialized parse tree and pretty printed source program back 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 the printed-trees folder it also saves pretty printed sources from the ast to printed-code forlder, this is how you can generate and update test files. To run it, switch to the repository root directory and execute swift run TestGenerator command.

How to use

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
    )
    
    // use the `code` variable on any syntax element to get 
    // the reprinted source text of that element
    let text = program.code 
    
    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.

Package Metadata

Repository: Artem-Goldenberg/SwiftStella

Stars: 1

Forks: 0

Open issues: 0

Default branch: main

Primary language: swift

License: MIT

Topics: language, parser-combinators, swift

README: README.md