Skip to main content
Home

Built and signed on GitHub Actions

An extremely simple JavaScript RegExp-based lexer.

This package works with DenoIt is unknown whether this package works with Cloudflare Workers, Node.js, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
100%
Published
3 months ago (0.2.0)

Simple Lexer

An extremely simple JavaScript RegExp-based lexer. It has no advanced capabilities. It will never have them, else it'd no longer be "simple". But sometimes, what it can do is all that you need.

Read the documentation.

Example

Creating a lexer for JSON:

import { SimpleLexer } from "@seally/simple-lexer";

const lexer = new SimpleLexer({
    tokens: {
        "whitespace": /\s+/v,
        "openBrace": "{",
        "closeBrace": "}",
        "openBracket": "[",
        "closeBracket": "]",
        "comma": ",",
        "colon": ":",
        "null": "null",
        "boolean": /true|false/v,
        "number": /-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?/v,
        "string": /"(?:[^"\\]|\\(?:["\\\/bfnrt]|u[0-9A-Fa-f]{4}))*"/v,
    },
});

// Note that `tokenize()` returns a generator, so spread the result into an
// array if you need a list of tokens.
console.log([
    ...lexer.tokenize(
        '{ "foo": "bar", "fruits": ["apple", "cherry", "kiwi"] }',
    ),
]);

This should print something like:

[
    { type: "openBrace", image: "{", startOffset: 0, nextOffset: 1 },
    { type: "whitespace", image: " ", startOffset: 1, nextOffset: 2 },
    { type: "string", image: '"foo"', startOffset: 2, nextOffset: 7 },
    { type: "colon", image: ":", startOffset: 7, nextOffset: 8 },
    { type: "whitespace", image: " ", startOffset: 8, nextOffset: 9 },
    { type: "string", image: '"bar"', startOffset: 9, nextOffset: 14 },
    { type: "comma", image: ",", startOffset: 14, nextOffset: 15 },
    { type: "whitespace", image: " ", startOffset: 15, nextOffset: 16 },
    {
        type: "string",
        image: '"fruits"',
        startOffset: 16,
        nextOffset: 24,
    },
    { type: "colon", image: ":", startOffset: 24, nextOffset: 25 },
    { type: "whitespace", image: " ", startOffset: 25, nextOffset: 26 },
    { type: "openBracket", image: "[", startOffset: 26, nextOffset: 27 },
    { type: "string", image: '"apple"', startOffset: 27, nextOffset: 34 },
    { type: "comma", image: ",", startOffset: 34, nextOffset: 35 },
    { type: "whitespace", image: " ", startOffset: 35, nextOffset: 36 },
    {
        type: "string",
        image: '"cherry"',
        startOffset: 36,
        nextOffset: 44,
    },
    { type: "comma", image: ",", startOffset: 44, nextOffset: 45 },
    { type: "whitespace", image: " ", startOffset: 45, nextOffset: 46 },
    { type: "string", image: '"kiwi"', startOffset: 46, nextOffset: 52 },
    { type: "closeBracket", image: "]", startOffset: 52, nextOffset: 53 },
    { type: "whitespace", image: " ", startOffset: 53, nextOffset: 54 },
    { type: "closeBrace", image: "}", startOffset: 54, nextOffset: 55 },
];
Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@seally/simple-lexer

Import symbol

import * as simple_lexer from "@seally/simple-lexer";
or

Import directly with a jsr specifier

import * as simple_lexer from "jsr:@seally/simple-lexer";

Add Package

pnpm i jsr:@seally/simple-lexer
or (using pnpm 10.8 or older)
pnpm dlx jsr add @seally/simple-lexer

Import symbol

import * as simple_lexer from "@seally/simple-lexer";

Add Package

yarn add jsr:@seally/simple-lexer
or (using Yarn 4.8 or older)
yarn dlx jsr add @seally/simple-lexer

Import symbol

import * as simple_lexer from "@seally/simple-lexer";

Add Package

vlt install jsr:@seally/simple-lexer

Import symbol

import * as simple_lexer from "@seally/simple-lexer";

Add Package

npx jsr add @seally/simple-lexer

Import symbol

import * as simple_lexer from "@seally/simple-lexer";

Add Package

bunx jsr add @seally/simple-lexer

Import symbol

import * as simple_lexer from "@seally/simple-lexer";