JavaScript API

createParser

Create a builtin parser over fixed source text and options.

function createParser(code: string, options: ParserOptions): Parser;

type Parser = {
    parse: () => Promise<AstResult>;
};

Creates a builtin parser over code and options.

The returned parser calls the builtin parser directly and never runs the plugin chain. Use parse when plugins should be able to replace the parse result.

Parameters

NameTypeDescription
codestringThe source text to parse.
optionsParserOptionsThe parser's fixed, resolved options.

ParserOptions

Every field is required. In a parse hook, the stage options are available as args.options.

FieldTypeDescription
cwdstringCurrent working directory used to locate the source file; a relative input map path resolves against the source file's directory, and absolute paths are used as-is.
filestringThe file being parsed.
languageLanguageThe grammar of the code.
sourceTypeSourceTypeThe module system of the code.
sourceMapSourceMapOptionsThe source-map inputs and outputs requested for the step.

The parser resolves sourceMap.input and attaches it to the returned AST. Output source-map delivery is applied by the pipeline, not by the builtin parser.

Returns

A Parser whose parse() method returns Promise<AstResult>.

AstResult is { ast: Ast }. ast.language is the grammar from the parser options.

Parse failures reject with a CompileError.

Example

import type { AstResult, Parser } from "telarel";

import { createParser } from "telarel";

const parser: Parser = createParser("const x: number = 1;", {
    cwd: process.cwd(),
    file: "index.ts",
    language: "ts",
    sourceType: "module",
    sourceMap: {},
});

const result: AstResult = await parser.parse();

Last updated on

Copyright © 2026, Alpheus

On this page