treegrep-query-lang
v0.2.0
Published
A tiny S-expression query language (tree-grep) with a Lezer grammar and AST parser.
Downloads
6
Readme
treegrep-query-lang
A tiny, Lisp-like query language intended for tree-grep style matching of parse trees.
This package includes:
src/query.grammar: a Lezer grammar for the query language itself- a small library that parses query strings into a compact AST
Install
npm i treegrep-query-langBuild / publish
This repo is set up so that publishing runs:
npm run generate
npm run buildgenerateuseslezer-generatorto writesrc/generated/parser.jsbuildruns TypeScript and (withallowJs) copies the generated JS intodist/
Main API
parseQuery(query: string): Promise<ParseResult>
Parses a query into an AST.
- If a generated Lezer parser is present (after
npm run generate), it uses that. - Otherwise it falls back to the built-in manual parser.
parseQuerySync(query: string): ParseResult
Always uses the manual parser.
Query language summary
(Kind P*)— structural match (head + ordered children)[P]— capture$P— capture prefix (captures whateverPmatches)#A— kind-variable (unification by kind, for match engines)_— wildcard node...— sibling wildcard (0+)(@op P*)— operator form (operator names start with@)
Operator semantics are implemented by your matching engine; this library only parses.
Output types
export type ParseResult =
| { ok: true; ast: Pattern }
| { ok: false; error: string };
export type Pattern =
| { type: "list"; head: Pattern; args: Pattern[] }
| { type: "symbol"; name: string }
| { type: "kvar"; name: string }
| { type: "string"; value: string }
| { type: "wild" }
| { type: "many" }
| { type: "capture"; inner: Pattern };Autocomplete
See AUTOCOMPLETE.md.
