ExSwiftParser
An Elixir wrapper for Swift AST parsing using swift-ast-explorer. This library allows you to parse Swift source code and extract its Abstract Syntax Tree (AST) from within Elixir applications.
Features
- Parse Swift source code into JSON AST format
- Support for multiple Swift versions (5.8, 5.9, 5.10)
- Command-line interface for standalone usage
- Escript support for easy distribution
Installation
Add ex_swift_parser to your list of dependencies in mix.exs:
def deps do
[
{:ex_swift_parser, "~> 0.1.0"}
]
endPrerequisites
This library requires Swift to be installed on your system for building the native parsers. The build process will compile Swift parser executables for different Swift versions.
Usage
Basic Parsing
# Parse Swift code with Swift 5.10
{:ok, result} = ExSwiftParser.parse("let x = 5", "5.10")
# The result contains version info and the parsed AST
%{
"version" => "5.10.0",
"ast" => %{
"kind" => "SourceFile",
"layout" => [...]
}
}Supported Swift Versions
"5.8"- Swift 5.8 parser"5.9"- Swift 5.9 parser"5.10"- Swift 5.10 parser
Command Line Usage
The library can also be used as an escript:
# Build the escript
mix escript.build
# Use it from command line
./ex_swift_parser "5.10" "let x = 5"Example: Parsing a Swift Function
swift_code = """
func greet(name: String) -> String {
return "Hello, \\(name)!"
}
"""
{:ok, result} = ExSwiftParser.parse(swift_code, "5.10")
IO.inspect(result["ast"])Building
The library uses native Swift parsers that need to be built:
# Initialize and build all parsers
make all
# Or build individual components
make submodules # Initialize git submodules
make # Build all parsersArchitecture
The library consists of:
- ExSwiftParser: Main API module for parsing Swift code
- ExSwiftParser.CLI: Command-line interface for escript usage
- Native Parsers: Swift executables for different Swift versions
- Makefile: Build system for compiling Swift parsers
The parsing process:
- Validates the requested Swift version
- Spawns the appropriate Swift parser executable
- Sends Swift source code to the parser via stdin
- Receives JSON AST output and parses it
- Returns structured Elixir data
Error Handling
The library returns {:ok, result} on success or {:error, reason} on failure:
case ExSwiftParser.parse("invalid swift code", "5.10") do
{:ok, ast} ->
# Process the AST
IO.inspect(ast)
{:error, reason} ->
# Handle parsing error
IO.puts("Parse error: #{inspect(reason)}")
endDevelopment
# Get dependencies
mix deps.get
# Build native parsers
make all
# Run tests
mix test
# Generate documentation
mix docsContributing
- Fork the repository
-
Create your feature branch (
git checkout -b feature/amazing-feature) -
Commit your changes (
git commit -m 'Add some amazing feature') -
Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Built on top of swift-ast-explorer
- Inspired by the need for Swift AST parsing in Elixir ecosystems