@atlisp/parser
v0.1.7
Published
S-expression parser for AutoLISP — AST types, tree walker, visitor pattern
Downloads
1,638
Readme
@atlisp/parser
S-expression parser for AutoLISP — AST types, tree walker, visitor pattern.
Install
npm install @atlisp/parserUsage
import { parseAst, astWalk, astIsList, astIsSymbol, AstVisitor } from '@atlisp/parser'
const ast = parseAst('(+ 1 2 (* 3 4))')
// Walk all nodes
for (const node of astWalk(ast)) {
console.log(node.type, node.pos)
}
// Check node types
if (astIsList(ast)) { /* ... */ }
if (astIsSymbol(node, 'defun')) { /* ... */ }
// Visitor pattern
const visitor = new AstVisitor()
visitor.on('defun', (node) => {
console.log('Found defun:', node.children?.[1]?.name)
})
visitor.run(ast)API
| Function | Description |
|----------|-------------|
| parseAst(content) | Parse entire input into an AST root node |
| parseContent(content) | Parse input, return top-level forms as array |
| astChildren(node) | Get children of a node |
| astIsSymbol(node, name?) | Type guard for symbol nodes |
| astIsList(node, head?) | Type guard for list nodes |
| astWalk(node) | Generator yielding all descendants depth-first |
| astFindAll(node, predicate) | Find all nodes matching a predicate |
| astFindListWithHead(node, headName) | Find lists with a given head symbol |
| AstVisitor | Class with on(name, handler) / run(root) |
Build
npm run build # tsc
npm run typecheck # tsc --noEmit