This page documents @lanexio/parser-query, the LexQuery pattern matching engine for querying nodes in a LexTree by kind name, field, and error status.
Version: Stable
Module name: parser-query
Package: @lanexio/parser-query
Import path: @lanexio/parser-query
Layer: 3 (Query)
Runtime: Universal
Module format: ESM
Stability: Stable
Primary use case: Query nodes in a LexTree by pattern.
You need to find nodes in a parsed tree by kind name or pattern.
You need to filter nodes by field, error status, or wildcard.
You want to build a kind index for faster repeated lookups.
Boundary Description Inputs Pattern string, LexQueryKindResolver Outputs LexQuery (compiled), IterableIterator<LexNode> from matches()Side effects None Determinism Yes (same pattern + same resolver produce same compiled query) External dependencies @lanexio/parser-coreNever-throw guarantee No: LexQuery.compile() throws LexQuerySyntaxError on invalid patterns. query.matches() never throws. Security surface None (no output generation)
Install the package.
pnpm add @lanexio/parser-query
npm install @lanexio/parser-query
yarn add @lanexio/parser-query
Import the named export.
import { LexQuery, type LexQueryKindResolver } from ' @lanexio/parser-query ' ;
Requirement Required Layer Notes @lanexio/parser-coreYes 1 ^1.0.0
import { LexQuery } from ' @lanexio/parser-query ' ;
import { HtmlKind } from ' @lanexio/parser-grammar-html ' ;
const resolver : LexQueryKindResolver = ( name : string ) =>
(HtmlKind as Record < string , number | undefined > )[name];
const query = LexQuery . compile ( ' Element ' , resolver);
const encoder = new TextEncoder ();
const tree = parseHtml (encoder . encode ( ' <div><p>text</p></div> ' ));
for ( const node of query . matches (tree)) {
console . log (node . kind , node . range );
Export Type Description LexQueryclassCompiled pattern query. Created via LexQuery.compile(). LexQuerySyntaxErrorclassError thrown for invalid patterns. Has code and offset properties. LexQuerySyntaxErrorCodeconst objectError code constants: EmptyPattern, UnexpectedToken, UnknownKind, UnsupportedAttribute. createKindIndex(resolver: LexQueryKindResolver, kindIds: number[]) => KindIndexCreate a precomputed kind index for faster lookups.
No options object. LexQuery.compile(pattern, resolver) accepts the pattern string and resolver directly.
Method Return Type Description matches(tree: LexTree)IterableIterator<LexNode>Iterate matching nodes in preorder DFS. testNode(node: LexNode)booleanTest a single node against the pattern. sourcestringThe original pattern string.
Type Purpose Notes LexQueryKindResolverMaps a kind name string to numeric kind ID or undefined (name: string) => number | undefinedKindIndexPrecomputed kind index Used with createKindIndex CompiledSelectorInternal compiled selector Not typically used directly CompiledLevelInternal compiled level Not typically used directly CompiledPatternInternal compiled pattern Not typically used directly
Pattern Description KindNameMatch nodes of the named kind. *Match any node. @fieldNameMatch nodes at the named field slot. [error]Match only LexError nodes. A, BMatch either A or B (disjunction).
The LexQueryKindResolver must be provided by the caller. Typically it is created by casting a grammar’s kind object (like HtmlKind, MdKind) as a Record<string, number>.
No direct accessibility surface. The query engine operates on AST data structures.
Concern Status Generated output semantics Not applicable (query engine) ARIA attributes in serialized output Not applicable Semantic element round-trip Not applicable
Invalid patterns throw LexQuerySyntaxError. The error message includes the pattern text.
query.matches() never throws on any tree or node.
Threat Mitigation Status Invalid pattern syntax Throws LexQuerySyntaxError with code and offset Implemented Unknown kind names in pattern Throws LexQuerySyntaxError with code UnknownKind Implemented
Package Relationship Layer Notes @lanexio/parser-coreRequires 1 Provides LexTree, LexNode, LexCursor @lanexio/parser-grammar-htmlEnhances 2 Provides HtmlKind for resolvers @lanexio/parser-grammar-markdownEnhances 2 Provides MdKind for resolvers @lanexio/parser-cliConsumes 6 Uses LexQuery in the query subcommand
Version Date Status Notable changes 1.0.02026-05-29Current Initial stable release. Apache-2.0.
LexQuery Guide: pattern-based tree queries with LexQuery. Flat AST How the 16-byte node layout works.