Skip to content

@lanexio/parser-query

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.
BoundaryDescription
InputsPattern string, LexQueryKindResolver
OutputsLexQuery (compiled), IterableIterator<LexNode> from matches()
Side effectsNone
DeterminismYes (same pattern + same resolver produce same compiled query)
External dependencies@lanexio/parser-core
Never-throw guaranteeNo: LexQuery.compile() throws LexQuerySyntaxError on invalid patterns. query.matches() never throws.
Security surfaceNone (no output generation)
  1. Install the package.

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

    import { LexQuery, type LexQueryKindResolver } from '@lanexio/parser-query';
RequirementRequiredLayerNotes
@lanexio/parser-coreYes1^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);
}
ExportTypeDescription
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.

MethodReturn TypeDescription
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.
TypePurposeNotes
LexQueryKindResolverMaps a kind name string to numeric kind ID or undefined(name: string) => number | undefined
KindIndexPrecomputed kind indexUsed with createKindIndex
CompiledSelectorInternal compiled selectorNot typically used directly
CompiledLevelInternal compiled levelNot typically used directly
CompiledPatternInternal compiled patternNot typically used directly
PatternDescription
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.
ConcernStatus
Generated output semanticsNot applicable (query engine)
ARIA attributes in serialized outputNot applicable
Semantic element round-tripNot applicable
  • Invalid patterns throw LexQuerySyntaxError. The error message includes the pattern text.
  • query.matches() never throws on any tree or node.
ThreatMitigationStatus
Invalid pattern syntaxThrows LexQuerySyntaxError with code and offsetImplemented
Unknown kind names in patternThrows LexQuerySyntaxError with code UnknownKindImplemented
PackageRelationshipLayerNotes
@lanexio/parser-coreRequires1Provides LexTree, LexNode, LexCursor
@lanexio/parser-grammar-htmlEnhances2Provides HtmlKind for resolvers
@lanexio/parser-grammar-markdownEnhances2Provides MdKind for resolvers
@lanexio/parser-cliConsumes6Uses LexQuery in the query subcommand
VersionDateStatusNotable changes
1.0.02026-05-29CurrentInitial stable release. Apache-2.0.
  • None.