Skip to main content

Crate elm_ast

Crate elm_ast 

Source
Expand description

A syn-quality Rust library for parsing and constructing Elm 0.19.1 ASTs.

elm-ast-rs provides a complete, strongly-typed representation of Elm source code as a Rust AST, along with a parser, pretty-printer, and visitor/fold traits for traversal and transformation.

§Quick start

use elm_ast::{parse, print};

let source = r#"
module Main exposing (..)

add : Int -> Int -> Int
add x y =
    x + y
"#;

let module = parse(source).expect("valid Elm source parses");
assert_eq!(module.declarations.len(), 1);

let output = print(&module);
assert!(output.contains("add x y"));

§Features

All features are enabled by default via full. Disable default-features and pick what you need to reduce compile times.

FeatureDescription
fullEnables all features below (default)
parsingparse() and parse_recovering() functions
printingprint(), Display impls, Printer struct
visitVisit trait for immutable traversal
visit-mutVisitMut for in-place mutation
foldFold for owned transformation
serdeSerialize/Deserialize on all AST types
wasmWASM bindings via wasm-bindgen

§AST overview

The root type is ElmModule, representing a complete .elm file. Every AST node is wrapped in Spanned<T>, which carries source location info (Span) alongside the value.

Key types: Expr, Pattern, TypeAnnotation, Declaration, Import, ModuleHeader.

Re-exports§

pub use file::ElmModule;
pub use lexer::Lexer;
pub use node::Spanned;
pub use span::Position;
pub use span::Span;
pub use token::Token;
pub use parse::parse;
pub use parse::parse_recovering;
pub use print::pretty_print;
pub use print::pretty_print_converged;
pub use print::print;

Modules§

builder
Builder helpers for constructing Elm AST nodes programmatically.
comment
declaration
display
Display implementations for AST types.
exposing
expr
file
fold
Owned transformation trait for rewriting Elm ASTs.
ident
import
lexer
literal
module_header
node
operator
parse
pattern
print
Elm pretty-printer.
span
token
type_annotation
visit
Immutable visitor trait for traversing Elm ASTs.
visit_mut
Mutable visitor trait for in-place AST transformation.