Rust API

parse

Parse source text into an AST.

pub async fn parse(
    code: String,
    options: ParseOptions,
    plugins: Vec<SharedPluginable>,
) -> Result<AstResult, CompileError>

Parses code into an AST.

The builtin parser resolves any requested input source map and is the terminal fallback after every parse hook defers.

Parameters

NameTypeDescription
codeStringThe source text to parse.
optionsParseOptionsThe parse options.
pluginsVec<SharedPluginable>The plugins to run.

ParseOptions

Every field is optional and resolved against defaults before use.

FieldTypeDefaultDescription
cwdOption<PathBuf>process working dirFilesystem root used to resolve relative paths.
fileOption<String>index.tsThe file being parsed. The language is inferred from its extension.
languageOption<Language>inferredThe grammar of the code. Overrides the extension inference.
source_typeOption<SourceType>inferredThe module system of the code. Overrides the grammar's module kind.
input_source_mapOption<InputSourceMap>offThe input source-map request for the step.

parse produces an AST, so it has no output source map. The shared plugin options bag presents this request as source_map.input; a plugin-set source_map.output or top-level output is ignored by parse.

Returns

Result<AstResult, CompileError>, where AstResult is { ast: Ast }. See Ast for accessors, clone semantics, and mutation.

Example

use telarel::{parse, ParseOptions, AstResult};

let result: AstResult = parse(
    String::from("const x: number = 1;"),
    ParseOptions::default(),
    vec![],
).await?;

assert_eq!(result.ast.source().as_ref(), "const x: number = 1;");

Last updated on

Copyright © 2026, Alpheus

On this page