Ohm ยท
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.
- Full support for left-recursive rules means that you can define left-associative operators in a natural way.
- Object-oriented grammar extension makes it easy to extend an existing language with new syntax.
- Modular semantic actions. Unlike many similar tools, Ohm completely separates grammars from semantic actions. This separation improves modularity and extensibility, and makes both grammars and semantic actions easier to read and understand.
- Online editor and visualizer. The Ohm Editor provides instant feedback and an interactive visualization that makes the entire execution of the parser visible and tangible. It'll make you feel like you have superpowers. ๐ช
Some awesome things people have built using Ohm:
- Seymour, a live programming environment for the classroom.
- Chorus, a project exploring the middle ground between spreadsheets and programming.
- Shadama, a particle simulation language designed for high-school science.
- turtle.audio, an audio environment where simple text commands generate lines that can play music.
- A browser-based tool that turns written Konnakkol (a South Indian vocal percussion art) into audio.
- Wildcard, a browser extension that empowers anyone to modify websites to meet their own specific needs, uses Ohm for its spreadsheet formulas.
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:
- Tutorial: Ohm: Parsing Made Easy
- The math example is extensively commented and is a good way to dive deeper.
- Examples
- Documentation
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:
const ohm = require('ohm-js');
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()
:const 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 attributetype="text/ohm-js"
, and instantiate it usingohm.grammarFromScriptElement()
:<script type="text/ohm-js"> MyGrammar { greeting = "Hello" | "Hola" } </script> <script> const 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:
const fs = require('fs'); const ohm = require('ohm-js'); const contents = fs.readFileSync('myGrammar.ohm'); const myGrammar = ohm.grammar(contents);
For more information, see Instantiating Grammars in the API reference.
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.
Ohm has two tools to help you debug grammars: a text trace, and a graphical 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 โ ""
If you've written an Ohm grammar that you'd like to share with others, see our suggestions for publishing grammars.
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.
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
), runnpm 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.