A library and language for building parsers, interpreters, compilers, etc.
JavaScript CSS HTML Shell
Permalink
Failed to load latest commit information.
bin Replace visualizer/ with latest from ohm-editor repo. Nov 18, 2016
dist Make toDisplayString() impls delegate to toString() where possible Jan 24, 2017
doc Mention \x and \u escapes in the syntax reference (#179) Dec 12, 2016
examples make the linter stop requiring parens around the arg of a 1-arg arrow… Feb 13, 2017
extras Add VisitorFamily to extras (#156) Sep 14, 2016
src Make toDisplayString() impls delegate to toString() where possible Jan 24, 2017
test Make toDisplayString() impls delegate to toString() where possible Jan 24, 2017
third_party Update Unicode categories for Ohm rules Dec 13, 2016
visualizer Replace visualizer/ with latest from ohm-editor repo. Nov 18, 2016
.eslintignore Replace visualizer/ with latest from ohm-editor repo. Nov 18, 2016
.eslintrc make the linter stop requiring parens around the arg of a 1-arg arrow… Feb 13, 2017
.gitattributes Don't ignore diffs for ohm-grammar.js. Apr 9, 2015
.gitignore Enable incremental browserify builds to speed up the tests. Jul 22, 2015
.jscsrc Convert CSV example to be a Node script, and add it to the tests. May 22, 2015
.mailmap Update contributors. Aug 2, 2016
.npmignore Add a dev-setup script that is run on postinstall. Mar 19, 2015
.travis.yml Add a `ci-test` script which includes the lint step. Jan 10, 2017
CHANGELOG.md Update CHANGELOG for v0.12.0 release. Aug 3, 2016
CONTRIBUTING.md Add link to Google Group in CONTRIBUTING.md Jan 5, 2017
LICENSE Update LICENSE year, and remove my name. May 17, 2016
README.md Make toDisplayString() impls delegate to toString() where possible Jan 24, 2017
TODO.md Rename Grammar.prototype.semantics to `createSemantics`. Jul 25, 2016
package.json bump version number for new release Feb 15, 2017

README.md

Ohm

Build Status NPM

Ohm is a parser generator 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 (known as Ohm/JS) for creating parsers, interpreters, and more from the grammars you write.

Like its older sibling OMeta, Ohm supports object-oriented grammar extension. One thing that distinguishes Ohm from other parsing tools is that it completely separates grammars from semantic actions. In Ohm, a grammar defines a language, and semantic actions specify what to do with valid inputs in that language. Semantic actions are written in the host language -- e.g., for Ohm/JS, the host language is JavaScript. Ohm grammars, on the other hand, work without modification in any host language. This separation improves modularity, and makes both grammars and semantic actions easier to read and understand. Currently, JavaScript is the only host language, but as the API stabilizes, we hope to have implementations for other languages.

Learn more about the Ohm philosophy here.

Getting Started

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

Resources

Installation

For use in the browser:

  • Download ohm.js (development version, with full source and comments) or ohm.min.js (a minified version for faster page loads).
  • Add a new script tag to your page, and set the src attribute to the path of the file you just downloaded. E.g.:

    <script src="ohm.js"></script>

    This creates a global variable named ohm.

If you are using Node.js, you can just install the ohm-js package using npm:

npm install ohm-js

This will install Ohm in the local node_modules folder. Use require to access it from a Node script:

var ohm = require('ohm-js');

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:

  • Define the grammar directly in a JavaScript string and instantiate it using ohm.grammar():

    var myGrammar = ohm.grammar('MyGrammar { greeting = "Hello" | "Hola" }');

    This is the simplest option, but it can be awkward to define larger grammars this way.

  • Recommended when running in the browser: Embed the grammar source inside its own <script> tag with the attribute type="text/ohm-js", and instantiate it using ohm.grammarFromScriptElement():

    <script type="text/ohm-js">
      MyGrammar {
        greeting = "Hello" | "Hola"
      }
    </script>
    <script>
      var myGrammar = ohm.grammarFromScriptElement();
    </script>
  • Recommended with Node.js: Define the grammar in a separate file, read the file's contents and instantiate it using ohm.grammar(contents):

    In myGrammar.ohm:

    MyGrammar {
      greeting = "Hello" | "Hola"
    }
    

    In JavaScript:

    var fs = require('fs');
    var ohm = require('ohm-js');
    var contents = fs.readFileSync('myGrammar.ohm');
    var myGrammar = ohm.grammar(contents);

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

Using Grammars

Matching input

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

var userInput = 'Hello';
var 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. The visualizer is still under development (i.e., it might be buggy!) but it can still be useful.

Ohm Visualizer

You can try the visualizer online, or if you have an Ohm checkout, open visualizer/index.html in your web browser.

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+ }:

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

All you need to get started:

git clone https://github.com/harc/ohm.git
cd ohm
npm install

NOTE: We recommend using the latest Node.js stable release (>=0.12.1) for development. Some of the JSDOM-based tests are flaky on io.js, and other tests will reliably fail on older versions of Node.

Some useful scripts

  • npm test runs the unit tests.
  • npm run test-watch re-runs the unit tests every time a file changes.
  • npm run build builds dist/ohm.js and dist/ohm.min.js, which are stand-alone bundles that can be included in a webpage.
  • When editing Ohm's own grammar (in src/ohm-grammar.ohm), run npm run bootstrap to re-build Ohm and test your changes.

Before submitting a pull request, be sure to add tests, and ensure that npm run prepublish runs without errors.