@pehotin/parker
v1.0.17
Published
Parker is a small JavaScript parser and tokenizer that turns JavaScript/TypeScript-like source code into an abstract syntax tree (AST). It is designed for tooling, code analysis, and experimentation rather than being a full drop-in replacement for product
Readme
Parker
Parker is a small JavaScript parser and tokenizer that turns JavaScript/TypeScript-like source code into an abstract syntax tree (AST). It is designed for tooling, code analysis, and experimentation rather than being a full drop-in replacement for production parsers. Work in progress.
Features
- Lexer: Converts source text into a stream of tokens.
- Parser: Produces a rich AST using node classes from
src/node.js. - Options support: Optional location metadata on nodes (
line/column). - Playground: Simple CLI playground under
playground/for quick experiments. - Go prototype: Early Go-based lexer implementation under
go/src/.
Getting started
Prerequisites
- Node.js (recommended: current LTS)
- npm (or another Node package manager)
Installation
From the parker directory:
npm installParker is published as a Node module inside this repo (see package.json). You can either:
- Use it directly from this repo (
require('./src')), or - Publish it / link it locally and depend on it from another project.
Usage
Basic parsing
const Parker = require('parker'); // or: require('./src')
const source = `
export class Renderer {
}
`;
const ast = Parker.parse(source, { locations: true });
console.log(JSON.stringify(ast, null, 2));Tokenizing
const Parker = require('parker'); // or: require('./src')
const source = `
export class Renderer {
}
`;
const tokens = Parker.tokenizeAll(source, { locations: true });
console.log(tokens);Options
Parker accepts an options object in both parse and tokenizeAll:
locations:boolean
Whentrue, the parser and lexer attach location information to tokens and AST nodes.
Example:
const ast = Parker.parse(source, { locations: true });API
Parker.parse(source, options?)
source:string– Source code to parse.options:object(optional)locations?: boolean
Returns: An AST NodeProgram instance representing the full program.
Parker.tokenizeAll(source, options?)
source:string– Source code to tokenize.options:object(optional)locations?: boolean
Returns: An array of token objects from the underlying lexer.
Playground
This repository includes a simple playground script to inspect the AST for a sample file.
From the parker directory:
npm run playThis will:
- Read
playground/test.js - Parse it with
Parker.parse - Print the resulting AST using Node’s
util.inspect.
You can change the sample code by editing playground/test.js.
Development
- Main entry:
src/index.js(exports theParkerclass fromsrc/parker.js). - Lexer:
src/lexer.js - Parser:
src/parser.js - Nodes:
src/node.js - Options:
src/options.js
For local hacking:
# Install dependencies
npm install
# Run playground
npm run playLicense
MIT
