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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@porifa/tokenizer

v0.2.0

Published

Regex Based Tokenizer used in parsers developed by Porifa

Readme

@porifa/tokenizer

A Regex Based Tokenizer for Parsers

Description

This package provides a flexible and efficient tokenizer that utilizes regular expressions to break down text into tokens for parsing. It's primarily designed for use within parsers developed by Porifa, but can be adapted for other parsing tasks as well.

Features

  • Regex-based tokenization: Define token patterns using regular expressions for precise control.
  • Token types: Assign a unique type to each token for identification and processing.
  • Trivia handling: Attach additional data to tokens, such as comments or whitespace, for custom handling.
  • Skippable tokens: Specify token types to be ignored during parsing.
  • Customizable tokenization logic: Inject custom tokenization logic through a trivia function.
  • EOF handling: Detects the end of the input stream.
  • Peeking: Preview upcoming tokens without consuming them.
  • Utility functions: Build regular expressions from special characters for convenience.

Installation

npm install @porifa/tokenizer

Usage

  1. Import the Tokenizer class:
import { Tokenizer, TokenDefinition } from '@porifa/tokenizer';
  1. Define token definitions:
enum TokenKind {
    EOF = 'eof',
    UNRECOGNIZED = 'unrecognized',
    WHITESPACE = 'whitespace',
    IDENTIFIER = 'identifier',

    IF = 'if',
    ELSE = 'else',
    WHILE = 'while',
    FOR = 'for',
}

const keywordMap: Record<string, TokenKind> = {
    if: TokenKind.IF,
    else: TokenKind.ELSE,
    while: TokenKind.WHILE,
    for: TokenKind.FOR,
};

const tokenDefinitions: TokenDefinition<TokenKind>[] = [
    { regex: /if|else|while|for/, tokenMap: keywordMap },
    { regex: /[a-zA-Z_][a-zA-Z0-9_]*/, kind: 'identifier' },
    { regex: /\s+/, kind: 'whitespace' },
    // ... more token definitions
];
  1. Create a tokenizer instance:
const tokenizer = new Tokenizer<TokenKind, { text: string }>(
    tokenDefinitions,
    [TokenKind.WHITESPACE],
    TokenKind.EOF,
    TokenKind.UNRECOGNIZED,
    (token, tokenizer) => ({ text: tokenizer.code.substring(token.start, token.start + token.length) })
);
  1. Provide input code:
tokenizer.setInput(code);
  1. Iterate through tokens:
while (!tokenizer.isEndOfFile()) {
    const token = tokenizer.nextToken();
    console.log(token.kind, token.triviaData?.text);
}

Additional Information

  • Explore the Token and Tokenizer classes for detailed properties and methods.
  • Refer to the examples directory for usage in different parsing scenarios.
  • Consider contributing to the package for enhancements and bug fixes.

Contributing

We welcome contributions to this package! Please follow our contribution guidelines.

License

This package is licensed under the MIT License.