npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

elder-parse

v0.5.2

Published

Lexer and parser library of a hand-written style.

Readme

Elder-Parse

A TypeScript lexer and parser library with a hand-written style, designed for building custom parsers with concrete syntax tree (CST) support.

Features

  • Hand-written lexer and parser: Full control over tokenization and parsing logic
  • CST (Concrete Syntax Tree) support: Preserves all syntax information including whitespace and comments
  • CSS selector queries: Query and manipulate CST nodes using familiar CSS selectors
  • TypeScript support: Written in TypeScript with full type definitions
  • Dual module format: Supports both CommonJS and ES modules
  • Flexible token handling: Support for keywords, separators, markers, and trivia tokens
  • Error recovery: Built-in error handling and recovery mechanisms

Installation

npm install elder-parse

Quick Start

import { Lexer, TokenType, Parser, TokenReader, CstBuilder } from 'elder-parse';

// Define token types
class MyTokens {
  static Identifier = new TokenType('Identifier');
  static Number = new TokenType('Number');
  static Space = new TokenType('Space', { skip: true });
  static Plus = new TokenType('Plus');
}

// Create a lexer
class MyLexer extends Lexer {
  constructor(options = {}) {
    super('mylang', [
      { type: MyTokens.Space, re: /\s+/y },
      { type: MyTokens.Number, re: /\d+/y },
      { type: MyTokens.Identifier, re: /[a-zA-Z]\w*/y },
      { type: MyTokens.Plus, re: /\+/y }
    ], options);
  }
}

// Create a parser
class MyParser extends Parser<MyLexer> {
  constructor(options) {
    super(new MyLexer(options), options);
  }
  
  protected parseTokens(reader: TokenReader, builder: CstBuilder) {
    builder.start('expression');
    // Parse your grammar here
    builder.end();
  }
}

// Use the parser
const parser = new MyParser();
const cst = parser.parse('1 + 2');

CST Node Operations

elder-parse provides powerful CST manipulation capabilities using CSS selectors:

import { CstNode } from 'elder-parse';

// Parse or create a CST
const cst = CstNode.parseJSON(
  ['node', { type: 'root' },
    ['node', { type: 'child', value: '1' }],
    ['node', { type: 'child', value: '2' }],
  ]
);

// Query nodes using CSS selectors
const firstChild = cst.selectOne('> [type=child]');
const findAllChildren = cst.selectAll('[type=child]');

// Check if a node matches a selector
if (cst.is(':has(> [type=child])')) {
  console.log('Root has children');
}

// Manipulate the tree
cst.append(new CstNode('node', { type: 'child', value: '3' }));
cst.remove(firstChild);

// Convert to JSON
console.log(cst.toJSONString());

// Convert to XML
console.log(cst.toXMLString());

Advanced Features

Keywords

Define reserved words and keywords with case sensitivity options:

const IF = MyTokens.Identifier.newKeyword('if', { reserved: true });
const FUNCTION = MyTokens.Identifier.newKeyword('function', { 
  reserved: true,
  ignoreCase: true 
});

Token Reader

The TokenReader provides convenient methods for parsing:

// Peek at the next token
if (reader.peekIf(TokenType)) {
  const token = reader.consume();
}

// Consume with expectation
const token = reader.consume(ExpectedTokenType);

// Handle errors
if (!reader.peekIf(ExpectedType)) {
  throw reader.createParseError('Expected something');
}

Builder Options

Control what information is preserved in the CST:

const parser = new MyParser({
  meta: true,    // Include metadata
  token: true,   // Include token information
  trivia: true,  // Include whitespace and comments
  marker: true   // Include marker tokens
});

API Reference

Core Classes

  • Lexer: Base class for creating lexers
  • Parser<L>: Base class for creating parsers
  • TokenType: Define token types with various options
  • Token: Represents a lexed token
  • TokenReader: Utilities for reading token streams
  • CstNode: Concrete syntax tree node with selector support
  • CstBuilder: Builder for constructing CST nodes

Development

# Install dependencies
npm install

# Build the project
npm run build

# Run tests
npm test

# Format code
npm run format

License

MIT License - see LICENSE file for details

Author

Hidekatsu Izuno [email protected]

Links