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 🙏

© 2024 – Pkg Stats / Ryan Hefner

parseltongue

v0.1.1

Published

S-expression parser

Downloads

16

Readme

Table of contents

Introduction

Parseltongue is an S-expression parser. It provides a single parse function capable of parsing symbols, numbers, booleans and strings — as well as lists and dotted pairs. Expressions may be quoted.

import { parse } from 'parseltongue';

parse(`(address (street "644 Glen Summit")
                (city "St. Charles")
                (state "Missouri")
                (zip 63304))`);
/*
[
  'address',
  [ 'street', '"644 Glen Summit"' ],
  [ 'city', '"St. Charles"' ],
  [ 'state', '"Missouri"' ],
  [ 'zip', 63304 ]
]
*/

Installation

Parseltongue can be installed via npm with the following command:

npm install parseltongue

Reference

Symbols

Atomic symbols are parsed into native JavaScript strings. As usual, symbols cannot start with a number.

import { parse } from 'parseltongue';

parse(`driver-loop`);
// 'driver-loop'

parse(`+`);
// '+'

Numbers

Atomic numbers are parsed into native JavaScript numbers. As such, they are subject to the same rules and limitations. There is no support for fractions (i.e., exact numbers). Exponential notation is also not supported at this time.

import { parse } from 'parseltongue';

parse(`42`);
// 42

parse(`12.8`);
// 12.8

Strings

Atomic strings are parsed into native JavaScript strings. The content is put in quotation marks (").

import { parse } from 'parseltongue';

parse(`"this is a string"`);
// '"this is a string"'

Booleans

Atomic booleans are parsed into native JavaScript booleans.

import { parse } from 'parseltongue';

parse(`#t`);
// true

parse(`#f`);
// false

Dotted pairs

JavaScript does not provide a native tuple data type. For this reason, dotted pairs are parsed to a custom Pair class.

import { parse, Pair } from 'parseltongue';

parse(`(abelson . sussman)`);
// Pair { car: 'abelson', cdr: 'sussman' }

Lists

Symbolic lists are parsed into native JavaScript arrays. Dotted pairs forming a proper list are also parsed to arrays.

import { parse } from 'parseltongue';

parse(`(roger dave nick rick)`);
// [ 'roger', 'dave', 'nick', 'rick' ]

parse(`(roger . (dave . (nick . (rick . ()))))`);
// [ 'roger', 'dave', 'nick', 'rick' ]

Square and curly brackets are also supported:

import { parse } from 'parseltongue';

parse(`{[roger bass] [dave guitar] [nick drums] [rick keyboard]}`);
/*
[
  [ 'roger', 'bass' ],
  [ 'dave', 'guitar' ],
  [ 'nick', 'drums' ],
  [ 'rick', 'keyboard' ]
]
*/

Quotations

S-expressions may be quoted.

import { parse } from 'parseltongue';

parse(`'42`);
// [ 'quote', 42 ]

parse(`'(1 2 3)`);
// [ 'quote', [ 1, 2, 3 ] ]

Partial Parsing

Parseltoungue has built-in support for partial parsing. That is, the parse function asks for more characters if the input string has to potential to result in a well-defined S-expresssion.

import { Partial, parse } from 'parseltongue';

parse(`(1 2 `);
// Partial {}

parse(`(1 2 `).complete(`3)`);
// [ 1, 2, 3 ]

A partial parsing can be turned into a failure using the .fail() method. When invoked, .fail() will throw a ParseError error.

import { Partial, ParseError, parse } from 'parseltongue';

parse(`(1 2`).fail();
/* throws
   ParseError {
     at: 4,
     expected: {
       oneOf: [
         '0', '1',     '2',
         '3', '4',     '5',
         '6', '7',     '8',
         '9', '/\\s/', ')',
         '.'
       ]
     }
   }
*/

Error Handling

When fed with a malformed input, parse throws a ParseError error detailing what valid characters were instead expected in the input string.

import { ParseError, parse } from 'parseltongue';

parse(`0.12q`);
/* throws
   ParseError {
     at: 4,
     expected: {
       oneOf: [
        '0',
        '1',
        '2',
        '3',
        '4',
        '5',
        '6',
        '7',
        '8',
        '9',
        'end-of-input'
      ]
    }
  }
*/

parse(`0.121`);
// 0.121

Snake icons created by Freepik - Flaticon