npm package: @pehotin/parker
Description: 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
Author: Artem P
Latest version: 1.0.17

## 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:

```bash
npm install
```

Parker 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

```js
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

```js
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`  
  When `true`, the parser and lexer attach location information to tokens and AST nodes.

Example:

```js
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:

```bash
npm run play
```

This 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 the `Parker` class from `src/parker.js`).
- **Lexer**: `src/lexer.js`
- **Parser**: `src/parser.js`
- **Nodes**: `src/node.js`
- **Options**: `src/options.js`

For local hacking:

```bash
# Install dependencies
npm install

# Run playground
npm run play
```

---

## License

MIT