explaingit

ohmjs/ohm

5,521JavaScriptAudience · developerComplexity · 3/5Setup · easy

TLDR

A JavaScript toolkit for building custom parsers and languages, describe your format's rules in a grammar file and Ohm handles all the parsing machinery, with an interactive visual debugger included.

Mindmap

mindmap
  root((repo))
    What it does
      Build parsers
      Build interpreters
    Key features
      Grammar language PEGs
      Left recursion support
      Grammar inheritance
    Tools
      Online visualizer
      Interactive editor
    Tech stack
      JavaScript
      Node.js
    Setup
      npm install
      Or script tag
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

Things people build with this

USE CASE 1

Build a parser for a custom configuration file format or domain-specific language without writing low-level parsing code.

USE CASE 2

Create an interpreter for a small programming language and use Ohm's interactive editor to debug the grammar in real time.

USE CASE 3

Extend an existing grammar to add new syntax rules without rewriting the original, using Ohm's inheritance system.

USE CASE 4

Add a formula or expression language to a web app that runs entirely in the browser with no server needed.

Tech stack

JavaScriptNode.js

Getting it running

Difficulty · easy Time to first run · 30min

In plain English

Ohm is a JavaScript toolkit for building parsers, interpreters, and compilers. A parser is a program that reads text written in some structured format, such as a programming language, a configuration file, or a custom data format, and converts it into a form that code can work with. Ohm gives you a way to describe the rules of that format in its own grammar language, then generate a working parser from those rules. The grammar language is based on a formalism called parsing expression grammars (PEGs), which are a precise way to describe what valid text looks like. You write the grammar as a set of rules, and Ohm handles all the parsing machinery. Separating the grammar from the code that processes matches is a design goal of the library: you define the structure in the grammar, and separately define what to do with each matched piece. This keeps both parts readable and maintainable as your language grows. Notable features include support for left-recursive rules, which makes it natural to describe left-associative arithmetic like 1+2+3, and an object-oriented extension system that lets you build a new grammar by extending an existing one rather than copying it. Ohm comes with an interactive online editor and visualizer at ohmjs.org/editor, where you can type a grammar and test inputs against it with instant feedback. This makes it much easier to debug why a grammar is or is not matching something. Installation is via npm for Node.js projects, or a single script tag for use in the browser. The README includes code examples for defining a simple grammar and checking whether input matches it. Real-world projects built with Ohm include a live programming environment for classrooms, a particle simulation language, and an audio composition tool.

Copy-paste prompts

Prompt 1
Show me how to install Ohm with npm and write a grammar that parses simple arithmetic expressions like 1 + 2 * 3.
Prompt 2
How do I define a left-recursive rule in an Ohm grammar for left-associative addition, and then write the semantic action to evaluate it?
Prompt 3
I want to build a custom config file format with Ohm. Walk me through writing the grammar, parsing a sample file, and extracting the values.
Prompt 4
How does Ohm's grammar extension work? Show me how to take a base arithmetic grammar and add support for variables.
Open on GitHub → Explain another repo

← ohmjs on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.