ZeParser JavaScript Parser
ZeParser is an early-stage JavaScript parser, last published as version 0.0.7 in June 2013. Developed by Peter van der Zee, it was designed to convert JavaScript input into a 'parse tree' (an array of arrays with tokens as leaves) or various token streams, including a 'white tree' (all tokens, including whitespace) and a 'black tree' (token stream without whitespace). The package also offered a `createParser` method to instantiate a parser with parsed input and access these tree structures directly. It featured simple and extended parsing modes, with the extended mode providing richer meta-information. While historically relevant for JavaScript parsing benchmarks, the project has been abandoned since its last release, meaning it does not support modern JavaScript syntax (ES2015+), lacks ongoing maintenance, and has no defined release cadence. An experimental v2 (`zeparser2`) was later introduced but also appears to be unmaintained.
Common errors
-
SyntaxError: Unexpected token 'const'
cause Attempting to parse JavaScript code containing `const`, `let`, arrow functions, classes, or other ES2015+ features.fixThis parser is only compatible with ES5 (ECMAScript 5) and older syntax. It cannot parse modern JavaScript. Use a different, actively maintained parser. -
TypeError: ZeParser.parse is not a function
cause Incorrectly importing the `ZeParser` module or attempting to call `parse` on a non-module object.fixEnsure you are using `const ZeParser = require('zeparser');` and then calling `ZeParser.parse(yourCode);`. This package is CommonJS, and its methods are accessed directly from the required module object. -
ReferenceError: require is not defined (in browser environments)
cause Using `require('zeparser')` directly in a web browser without a CommonJS-compatible bundler.fixThis package is intended for Node.js environments. For browser usage, you would need a tool like Webpack or Browserify to bundle it, but given its abandonment and lack of modern JS support, it's not recommended for new web projects.
Warnings
- breaking ZeParser is an abandoned package, last published in 2013 (v0.0.7). It does not support modern JavaScript syntax (ES2015+), including `const`/`let`, arrow functions, classes, modules, or other contemporary language features. Attempting to parse modern JS will result in parsing errors or incorrect trees.
- gotcha The package is CommonJS-only and relies on `require()`. It does not provide ES Module exports (`import`). Using `import` statements directly will lead to module resolution errors in ESM-only environments.
- gotcha The documentation mentions 'simple' and 'extended' parsing modes, with the extended mode being default (`.ast` property true). The structure of the parse tree and token objects, particularly meta-information, might be less intuitive or well-documented compared to contemporary AST standards like ESTree.
Install
-
npm install zeparser -
yarn add zeparser -
pnpm add zeparser
Imports
- ZeParser
import { ZeParser } from 'zeparser';const ZeParser = require('zeparser'); - parse
import { parse } from 'zeparser';const ZeParser = require('zeparser'); const tree = ZeParser.parse('var a = 1;'); - createParser
const { createParser } = require('zeparser');const ZeParser = require('zeparser'); const parserInstance = ZeParser.createParser('var a = 1;');
Quickstart
const ZeParser = require('zeparser');
const inputCode = `
function greet(name) {
// A simple comment
return 'Hello, ' + name + '!';
}
var message = greet('World');
`;
console.log('--- Parsing with ZeParser.parse() ---');
const parseTree = ZeParser.parse(inputCode);
// The parse tree is an array of arrays representing the AST structure.
// It includes tokens as leaf nodes.
console.log('Parse Tree (excerpt):', JSON.stringify(parseTree, null, 2).substring(0, 500) + '...');
console.log('\n--- Using ZeParser.createParser() for detailed token streams ---');
const parserInstance = ZeParser.createParser(inputCode);
// .wtree (white tree) includes all tokens, including whitespace and comments
console.log('White Tree (first 5 tokens):', parserInstance.wtree.slice(0, 5));
// .btree (black tree) is the token stream without whitespace, line terminators, or comments
console.log('Black Tree (first 5 tokens):', parserInstance.btree.slice(0, 5));
// Accessing a token's properties, e.g., the first token in the black tree
if (parserInstance.btree.length > 0) {
const firstToken = parserInstance.btree[0];
console.log('First token in black tree (type, value):', firstToken.type, firstToken.value);
}