# ExSwiftParser
An Elixir wrapper for Swift AST parsing using [swift-ast-explorer](https://github.com/kishikawakatsumi/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`:
```elixir
def deps do
[
{:ex_swift_parser, "~> 0.1.0"}
]
end
```
## Prerequisites
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
```elixir
# 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:
```bash
# Build the escript
mix escript.build
# Use it from command line
./ex_swift_parser "5.10" "let x = 5"
```
### Example: Parsing a Swift Function
```elixir
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:
```bash
# Initialize and build all parsers
make all
# Or build individual components
make submodules # Initialize git submodules
make # Build all parsers
```
## Architecture
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:
1. Validates the requested Swift version
2. Spawns the appropriate Swift parser executable
3. Sends Swift source code to the parser via stdin
4. Receives JSON AST output and parses it
5. Returns structured Elixir data
## Error Handling
The library returns `{:ok, result}` on success or `{:error, reason}` on failure:
```elixir
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)}")
end
```
## Development
```bash
# Get dependencies
mix deps.get
# Build native parsers
make all
# Run tests
mix test
# Generate documentation
mix docs
```
## Contributing
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Acknowledgments
- Built on top of [swift-ast-explorer](https://github.com/kishikawakatsumi/swift-ast-explorer)
- Inspired by the need for Swift AST parsing in Elixir ecosystems