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

parser-transform

v1.0.4

Published

Streaming+Async lexer and parser

Downloads

16

Readme

Streaming and Async Lexer and Parser

Usage

npm install parser-transform

For a complete example you might want to look at the test suite, implementing a fully-streaming JSON parser implemented as a JSON tokenizer that can be piped into a streaming JSON parser with a selector. Notice that the parser acts as a Stream Transform and doesn't even use the Async possibilities offered by this package.

For a simpler example, the same tokenizer can be piped into a collecting JSON parser which builds the data in memory. Compare with the streaming parser to see how to inject operations in the middle of a language when streaming.

Streaming Tokenizer

The lexer/tokenizer is implemented as a Node.js Stream transform that translates an incoming text stream into a stream of lexical tokens.

For example, assuming lexer is a String containing the textual description of your tokenizer:

{LexerParser,LexerTransform} = require('parser-transform')

text_stream.setEncoding('utf8')

lexical_stream = text_stream.pipe( new LexerTransform(LexerParser.parse(lexer)) )

LexerParser.parse

LexerParser.parse(text) converts the textual description into a Map of Deterministic Finite Automaton (DFA) (one DFA per start condition described in the text).

The lexical parser supports the usual two sections of a lex file, separated by %%:

  • defining names for regular expressions:
digit    [0-9]
  • generating lexical tokens:
true    return 'TRUE'

However, since the tokenizer handles streams, there are operations (such as lookaheads) that it cannot perform. (Other projects implementing tokenizers for Node.js use Regular Expressions to implement their tokenizers, and conversely cannot be used to handle streams of arbitrary lengths.)

The current operations are:

  • * (zero or more)
  • + (one or more)
  • ? (zero or one)
  • | (alternative)
  • concatenation
  • start conditions <…>

The code that generates lexical tokens can access the current text as this.text (and modify it before it is passed down to the stream).

It may also use this.begin(start_condition) and this.pop() to switch in- and out-of start conditions.

Finally, it may also use this.yy (a regular Object) to store data that must be persisted between actions.

At this time, the lexer only supports single-line actions. (This is a limitation in the parser that handles the actions, not a limitation of the lexer in itself.)

new LexerTransform

new LexerTransform(dfas) creates a Stream transform based on a Map of DFAs.

The lexical tokens generated contain {token,text,line,column,eof}; the token is either the value returned by the code that generated the token, ERROR in case of error (for example if the input does not match any pattern in the current start condition), or null at the end of the stream (in which case eof is set to true).

Streaming Parser

The parser is implemented as a Node.js Stream transform that receives an incoming stream of lexical tokens. You can send messages out on the stream by using emit(data) inside the parser's actions, or perform arbitrary actions, including asynchronous actions; the stream will be paused while asynchronous actions are performed.

For example, assuming parser is a String containing the textual description of your parser:

{Grammar,ParserTransform} = require('parser-transform')

lexical_stream.pipe( new ParserTransform(Grammar.fromString(parser,{mode:'LALR1'},'bnf')) )

Grammar

Grammar is imported as-is from syntax-cli and supports LL and LR parsing, precedence, etc.

Notice that the lex section is not used.