gitmyhub

ohm

JavaScript ★ 5.5k updated 16d ago

A library and language for building parsers, interpreters, compilers, etc.

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.

JavaScriptNode.jssetup: easycomplexity 3/5

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.

Where it fits