explaingit

kach/nearley

Analysis updated 2026-07-03

3,738JavaScriptAudience · developerComplexity · 3/5Setup · moderate

TLDR

A JavaScript parser toolkit that lets you define a grammar in a .ne file and compile it into a parser that handles left-recursive and ambiguous grammars where other JS parser tools fall short.

Mindmap

mindmap
  root((repo))
    What it does
      Builds JS parsers
      Compiles grammars
      Handles ambiguity
    Tech Stack
      JavaScript
      Node.js
      moo lexer
    Features
      Earley algorithm
      Streaming input
      Railroad diagrams
      Fuzz testing
    Use Cases
      Custom languages
      File format parsers
      Markup compilers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

What do people build with it?

USE CASE 1

Build a parser for a custom file format or mini programming language by writing a grammar file and compiling it with nearley

USE CASE 2

Handle left-recursive grammar rules that popular JavaScript parser generators like PEG.js cannot process

USE CASE 3

Parse streaming input that arrives incrementally without waiting for the full text to be available

USE CASE 4

Generate railroad diagrams and fuzz tests from your grammar file to document and stress-test your parser automatically

What is it built with?

JavaScriptNode.js

How does it compare?

kach/nearleywebrtchacks/adapterzhblue/hustoj
Stars3,7383,7383,738
LanguageJavaScriptJavaScriptJavaScript
Setup difficultymoderateeasyhard
Complexity3/52/54/5
Audiencedeveloperdeveloperdeveloper

Figures from each repo's GitHub metadata at analysis time.

How do you get it running?

Difficulty · moderate Time to first run · 30min

Requires Node.js, grammar compilation adds a build step that needs to be integrated into your project workflow.

In plain English

Nearley is a JavaScript library for building parsers. A parser is a program that reads text written in a specific format and turns it into structured data your code can work with. If you have ever needed to interpret a custom file format, a mini programming language, or a structured command syntax, a parser is what you would build, and nearley is a toolkit that makes that job much easier. The library is built around the Earley parsing algorithm, which is a technique known for being able to handle a very wide range of grammar definitions, including some patterns that trip up other popular JavaScript parser tools. Most other parsers in the JavaScript ecosystem have restrictions on the kinds of grammars they accept, such as struggling with left-recursive rules (where a grammar rule refers back to itself on the left side). Nearley handles these without issue. You describe your language using a purpose-built grammar syntax, write those rules in a .ne file, and nearley compiles that into a parser you can use in Node.js or in the browser. The library also includes a set of supporting tools: you can generate railroad diagrams (visual flowcharts of your grammar), create fuzz testers to throw random inputs at your parser, and write grammar-aware tests. It integrates with several editors and works with external lexers, with moo being the recommended option. Nearley supports streaming input, meaning it can parse data that arrives incrementally rather than all at once. It also handles ambiguous grammars, cases where a single input can be interpreted in more than one valid way, by returning all possible parsings. The README notes real-world uses including university AI and linguistics courses, file format parsers, markup language compilers, and compilers for real programming languages. The nearley compiler itself is built with nearley.

Copy-paste prompts

Prompt 1
Write a nearley grammar file that parses simple arithmetic expressions with addition and multiplication, and show me how to run the compiled parser in Node.js.
Prompt 2
How do I integrate nearley with the moo lexer to parse a custom domain-specific language that has keywords and string literals?
Prompt 3
Generate a railroad diagram from my nearley grammar file and explain how to read it as documentation for my team.
Prompt 4
How does nearley handle an ambiguous grammar that can match a single input in more than one valid way, and how do I get all parse results?
Prompt 5
Show me how to use nearley to parse streaming input that arrives in chunks rather than all at once.

Frequently asked questions

What is nearley?

A JavaScript parser toolkit that lets you define a grammar in a .ne file and compile it into a parser that handles left-recursive and ambiguous grammars where other JS parser tools fall short.

What language is nearley written in?

Mainly JavaScript. The stack also includes JavaScript, Node.js.

How hard is nearley to set up?

Setup difficulty is rated moderate, with roughly 30min to a first successful run.

Who is nearley for?

Mainly developer.

Open on GitHub → Explain another repo

This repo across BitVibe Labs

Scan in gitsafehub Deploy in gitdeployhub kach on gitmyhub

Verify against the repo before relying on details.