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

@lingyi/syntax-parser

v0.1.0

Published

<p align="center"> <h2 align="center">syntax-parser</h2> <p align="center"> <i> syntax-parser is a parser using pure javascript, so it can both run in browser and nodejs. </i> <p> <p align="center"> <i>

Downloads

5

Readme

syntax-parser supports:

  • lexer.
  • parser.

Lexer

createLexer can help you create a lexer.

Example

import { createLexer } from 'syntax-parser';

const myLexer = createLexer([
  {
    type: 'whitespace',
    regexes: [/^(\s+)/],
    ignore: true
  },
  {
    type: 'word',
    regexes: [/^([a-zA-Z0-9]+)/]
  },
  {
    type: 'operator',
    regexes: [/^(\+)/]
  }
]);

myLexer('a + b');
// [
//   { "type": "word", "value": "a", "position": [0, 1] },
//   { "type": "operator", "value": "+", "position": [2, 3] },
//   { "type": "word", "value": "b", "position": [4, 5] }
// ]

type

Token type name, you can use any value here, and you will use it in the parser stage.

regexes

Regexes that use to be matched for each Token type.

ignore

The matching Token will not be added to the Token result queue.

In general, whitespace can be ignored in syntax parsing.

Parser

createParser can help you create a parser. Parser requires a lexer.

import { createParser, chain, matchTokenType, many } from 'syntax-parser';

const root = () => chain(addExpr)(ast => ast[0]);

const addExpr = () =>
  chain(matchTokenType('word'), many(addPlus))(ast => ({
    left: ast[0].value,
    operator: ast[1] && ast[1][0].operator,
    right: ast[1] && ast[1][0].term
  }));

const addPlus = () =>
  chain('+', root)(ast => ({
    operator: ast[0].value,
    term: ast[1]
  }));

const myParser = createParser(
  root, // Root grammar.
  myLexer // Created in lexer example.
);

myParser('a + b');
// ast:
// [{
//   "left": "a",
//   "operator": "+",
//   "right": {
//     "left": "b",
//     "operator": null,
//     "right": null
//   }
// }]

chain

Basic grammatical element, support four parameters:

string

String means match token:

chain('select', 'table'); // Match 'select table'

array

Array means 'or':

chain('select', ['table', 'chart']); // Match both 'select table' and 'select chart'

matchTokenType

matchTokenType allow you match Token type defined in lexer.

chain('select', matchTokenType('word')); // Match 'select [any word!]'

function

It's easy to call another chain function:

const a = () => chain('select', b);
const b = () => chain('table');

many/optional

Just as literal meaning:

const a = () => chain('select', optional('table')); // Match both 'select' and 'select table'
const b = () => chain('select', many(',', matchTokenType('word'))); // Match both 'select' and 'select a' and 'select a, b' .. and so on.

optional many can also use chain as parameter. many(chain(..))

The last callback allow partial redefin of local ast:

chain('select', 'table')(
  ast => ast[0] // return 'select'
);

Tests

npm test

Monaco Editor Sql Editor

If you want to see this demo, run this command:

npm run docs

Then select demo Monaco Editor.