Skip to content

@lanexio/parser

This page documents @lanexio/parser, the main entry point package that provides a unified API across all Lanexio Parser grammars: parse, createParser, register, and detectLanguage.

  • Version: Stable
  • Module name: parser
  • Package: @lanexio/parser
  • Import path: @lanexio/parser
  • Layer: 6 (Entry)
  • Runtime: Universal
  • Module format: ESM
  • Stability: Stable
  • Primary use case: Unified parsing API across all grammar packs.
  • You want a single parse() function that auto-detects the grammar.
  • You want to register multiple grammars and switch between them by language, file extension, or MIME type.
  • You want a single import instead of importing each grammar pack individually.
BoundaryDescription
InputsUint8Array (source bytes) + ParseOptions (language, grammar)
OutputsLexTree
Side effectsregister() mutates grammarRegistry. grammarRegistry is a shared global.
DeterminismYes (same input + same registrations produce same tree)
External dependencies@lanexio/parser-core, @lanexio/parser-wasm (optional)
Never-throw guaranteeNo: parse() throws LanexioParseError when auto-detection fails and no grammar is specified. Grammar-specific parse functions within a created parser are never-throw.
Security surfaceNone
  1. Install the package.

    Terminal window
    pnpm add @lanexio/parser
  2. Import the named export.

    import { parse, createParser, register } from '@lanexio/parser';
RequirementRequiredLayerNotes
@lanexio/parser-coreYes1^1.0.0
@lanexio/parser-wasmNo4Optional WASM bridge
Grammar packagesNo2Must be registered before use
import { parse, register } from '@lanexio/parser';
import { htmlRegistration } from '@lanexio/parser-grammar-html';
register(htmlRegistration);
const encoder = new TextEncoder();
const tree = parse(encoder.encode('<p>Hello</p>'), { language: 'html' });
console.log(tree.nodeCount);
// Detects grammar from file extension hints in ParseOptions
const tree = parse(encoder.encode('<p>Hello</p>'), { grammar: htmlGrammar });
import { createParser } from '@lanexio/parser';
import { htmlGrammar } from '@lanexio/parser-grammar-html';
const parser = createParser(htmlGrammar);
const tree = parser.parse(encoder.encode('<p>Hello</p>'));
ExportTypeDescription
parse(source: Uint8Array, options?: ParseOptions) => LexTreeUnified parse entry point. Throws LanexioParseError on detection failure.
createParser(grammar: LanexioParserPureGrammar) => LanexioParserCreate a parser handle bound to a specific grammar.
register(registration: GrammarRegistration) => voidRegister a grammar in the unified registry.
detectLanguage(source: Uint8Array, hints?: DetectionHints) => DetectionResultAuto-detect the grammar language from source bytes.
actionableError(error: LanexioParseError) => stringGenerate a human-readable error message.
KNOWN_GRAMMARSreadonly KnownGrammarEntry[]List of pre-registered known grammars.
LanexioParserErrorclassParser error base class.
LanexioParserErrorCodeconst objectError code constants.
LanexioParseErrorclassError thrown when parsing fails (e.g., unknown grammar).
grammarRegistryobjectGlobal grammar registry (re-exported from parser-core).
LexCursorclassRe-export from parser-core.
LexNodeclassRe-export from parser-core.
LexToyKindconst objectRe-export from parser-core.
LexTreeclassRe-export from parser-core.
PROTOCOL_VERSIONnumberRe-export from parser-core.
LexEditErrorclassRe-export from parser-core.
LexEditErrorCodeconst objectRe-export from parser-core.
applyEditfunctionRe-export from parser-core.
toyParsefunctionRe-export from parser-core (toy grammar parse).
createParseStreamfunctionRe-export from parser-core.
FieldTypeRequiredDefaultDescription
languagestringNoundefinedLanguage name for grammar lookup (e.g., "html", "markdown").
grammarLanexioParserPureGrammarNoundefinedExplicit grammar descriptor (skips registry lookup).
PropertyTypeDescription
rootLexNodeRoot node of the parsed tree.
nodeCountnumberTotal nodes in the tree.
sourceUint8ArrayOriginal parsed bytes.
TypePurposeNotes
ParseOptionsOptions for parse(){ language?: string; grammar?: LanexioParserPureGrammar }
LanexioParserParser handle returned by createParser{ parse: (bytes: Uint8Array) => LexTree }
DetectionResultResult of language detectionLanguage and confidence
DetectionHintsHints for language detectionExtension, MIME type
KnownGrammarEntryEntry in KNOWN_GRAMMARSGrammar metadata
LexRangeByte rangeRe-export from parser-core
LanexioParserPureGrammarGrammar descriptorRe-export from parser-core
LexParseStreamStreaming parse interfaceRe-export from parser-core
GrammarRegistrationGrammar registrationRe-export from parser-core
LexEditEdit descriptorRe-export from parser-core

Register grammars before calling parse(). The registry supports lookup by language name, file extension, and MIME type.

KNOWN_GRAMMARS lists the pre-registered grammar metadata. This is a read-only list for reference.

  • No direct accessibility surface. The main entry point dispatches to grammar-specific parsers. Consuming code is responsible for semantic rendering.
ConcernStatus
Generated output semanticsNot applicable (dispatch layer)
ARIA attributes in serialized outputNot applicable
Semantic element round-tripNot applicable
  • The unified parse() function relies on auto-detection. Grammar-specific parse functions within created parsers are never-throw.
  • When auto-detection fails and no grammar is specified, parse() throws LanexioParseError.
ThreatMitigationStatus
Malformed input byte sequenceDelegated to grammar-specific parsers (all grammar parsers are never-throw)Implemented
Grammar detection failureThrows LanexioParseError with clear messageImplemented
PackageRelationshipLayerNotes
@lanexio/parser-coreRequires1Core tree and protocol. Re-exported.
@lanexio/parser-grammar-htmlDev dependency2Optional grammar.
@lanexio/parser-grammar-markdownDev dependency2Optional grammar.
@lanexio/parser-wasmRequires4WASM bridge support.
VersionDateStatusNotable changes
1.0.02026-05-29CurrentInitial stable release. Apache-2.0.
  • None.