Home

Awesome

Ohm ยท NPM Node.js CI Chat on Discord

Ohm is a parsing toolkit consisting of a library and a domain-specific language. You can use it to parse custom file formats or quickly build parsers, interpreters, and compilers for programming languages.

The Ohm language is based on parsing expression grammars (PEGs), which are a formal way of describing syntax, similar to regular expressions and context-free grammars. The Ohm library provides a JavaScript interface for creating parsers, interpreters, and more from the grammars you write.

Some awesome things people have built using Ohm:

Getting Started

The easiest way to get started with Ohm is to use the interactive editor. Alternatively, you can play with one of the following examples on JSFiddle:

Resources

Installation

On a web page

To use Ohm in the browser, just add a single <script> tag to your page:

<!-- Development version of Ohm from unpkg.com -->
<script src="https://unpkg.com/ohm-js@17/dist/ohm.js"></script>

or

<!-- Minified version, for faster page loads -->
<script src="https://unpkg.com/ohm-js@17/dist/ohm.min.js"></script>

This creates a global variable named ohm.

Node.js

First, install the ohm-js package with your package manager:

Then, you can use require to use Ohm in a script:

<!-- @markscript markscript.transformNextBlock(s => s.replace('const ', 'var ')); -->
const ohm = require('ohm-js');

Ohm can also be imported as an ES module:

import * as ohm from 'ohm-js';

Deno

To use Ohm from Deno:

import * as ohm from 'https://unpkg.com/ohm-js@17';

Basics

Defining Grammars

Instantiating a grammar

To use Ohm, you need a grammar that is written in the Ohm language. The grammar provides a formal definition of the language or data format that you want to parse. There are a few different ways you can define an Ohm grammar:

For more information, see Instantiating Grammars in the API reference.

Using Grammars

Matching input

<!-- @markscript // The duplication here is required because Markscript only executes top-level code blocks. // TODO: Consider fixing this in Markscript. const myGrammar = ohm.grammar('MyGrammar { greeting = "Hello" | "Hola" }'); -->

Once you've instantiated a grammar object, use the grammar's match() method to recognize input:

const userInput = 'Hello';
const m = myGrammar.match(userInput);
if (m.succeeded()) {
  console.log('Greetings, human.');
} else {
  console.log("That's not a greeting!");
}

The result is a MatchResult object. You can use the succeeded() and failed() methods to see whether the input was recognized or not.

For more information, see the main documentation.

Debugging

Ohm has two tools to help you debug grammars: a text trace, and a graphical visualizer.

Ohm Visualizer

You can try the visualizer online.

To see the text trace for a grammar g, just use the g.trace() method instead of g.match. It takes the same arguments, but instead of returning a MatchResult object, it returns a Trace object โ€” calling its toString method returns a string describing all of the decisions the parser made when trying to match the input. For example, here is the result of g.trace('ab').toString() for the grammar G { start = letter+ }:

<!-- @markscript markscript.transformNextBlock(function(code) { const trace = ohm.grammar('G { start = letter+ }').trace('ab'); assert.equal(trace.toString().trim(), code.trim()); }); -->
ab         โœ“ start โ‡’  "ab"
ab           โœ“ letter+ โ‡’  "ab"
ab             โœ“ letter โ‡’  "a"
ab                 โœ“ lower โ‡’  "a"
ab                   โœ“ Unicode [Ll] character โ‡’  "a"
b              โœ“ letter โ‡’  "b"
b                  โœ“ lower โ‡’  "b"
b                    โœ“ Unicode [Ll] character โ‡’  "b"
               โœ— letter
                   โœ— lower
                     โœ— Unicode [Ll] character
                   โœ— upper
                     โœ— Unicode [Lu] character
                   โœ— unicodeLtmo
                     โœ— Unicode [Ltmo] character
           โœ“ end โ‡’  ""

Publishing Grammars

If you've written an Ohm grammar that you'd like to share with others, see our suggestions for publishing grammars.

Contributing to Ohm

Interested in contributing to Ohm? Please read CONTRIBUTING.md and the Ohm Contributor Guide.