MCPcopy
hub / github.com/ohmjs/ohm

github.com/ohmjs/ohm @v17.5.0 sqlite

repository ↗ · DeepWiki ↗ · release v17.5.0 ↗
2,532 symbols 6,437 edges 691 files 73 documented · 3%
README

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.

  • 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.
  • 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.

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:


<script src="https://unpkg.com/ohm-js@17/dist/ohm.js"></script>

or


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

  • npm: npm install ohm-js
  • Yarn: yarn add ohm-js
  • pnpm: pnpm add ohm-js

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

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:

  • The simplest option is to define the grammar directly in a JavaScript string and instantiate it using ohm.grammar(). In most cases, you should use a template literal with String.raw:

js const myGrammar = ohm.grammar(String.raw` MyGrammar { greeting = "Hello" | "Hola" } `);

  • In Node.js, you can define the grammar in a separate file, and read the file's contents and instantiate it using ohm.grammar(contents):

In myGrammar.ohm:

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

In JavaScript:

js const fs = require('fs'); const ohm = require('ohm-js'); const contents = fs.readFileSync('myGrammar.ohm', 'utf-8'); const 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:

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

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.

Extension points exported contracts — how you extend this code

LineAndColumnInfo (Interface)
(no doc) [28 implementers]
packages/ohm-js/index.d.ts
PExpr (Interface)
(no doc) [23 implementers]
packages/labrat/index.ts
LineAndColumnInfo (Interface)
(no doc) [28 implementers]
packages/runtime/src/extras.ts
Grammar (Interface)
(no doc) [2 implementers]
packages/compiler/src/compat.ts
GreetingGrammar (Interface)
(no doc) [1 implementers]
packages/packaging-tests/src/greeting-esm.ohm-bundle.d.ts
VisitorCtx (Interface)
(no doc)
packages/semantics/src/types.ts
NonterminalNode (Interface)
(no doc) [4 implementers]
packages/ohm-js/index.d.ts
MemoRec (Interface)
(no doc)
packages/labrat/index.ts

Core symbols most depended-on inside this repo

match
called by 555
packages/ohm-js/index.d.ts
grammar
called by 345
packages/ohm-js/src/Builder.js
succeeded
called by 146
packages/ohm-js/index.d.ts
matchWithInput
called by 127
packages/compiler/test/_helpers.js
toWasmGrammar
called by 122
packages/compiler/test/_helpers.js
createSemantics
called by 103
packages/ohm-js/index.d.ts
addOperation
called by 91
packages/ohm-js/index.d.ts
Pos
called by 88
examples/incremental/third_party/codemirror.js

Shape

Function 1,450
Method 706
Class 318
Interface 56
Struct 2

Languages

TypeScript99%
Go1%
Python1%

Modules by API surface

examples/incremental/third_party/codemirror.js423 symbols
packages/compiler/src/Compiler.js166 symbols
packages/runtime/src/miniohm.ts126 symbols
packages/ohm-js/index.d.ts88 symbols
examples/incremental/third_party/jquery-3.1.1.min.js85 symbols
examples/simple-lisp/src/simple-lisp.mjs63 symbols
packages/es-grammars/scripts/generate-ecmascript-grammar.mjs60 symbols
packages/ohm-js/src/pexprs-main.js51 symbols
packages/compiler/src/ir.ts48 symbols
packages/ohm-js/src/Semantics.js46 symbols
packages/compiler/runtime/ohmRuntime.ts44 symbols
packages/ohm-js/src/v18.js43 symbols

Dependencies from manifests, versioned

github.com/tetratelabs/wazerov1.6.0 · 1×
@biomejs/biome2.2.4 · 1×
@clack/prompts1.0.0 · 1×
@eslint/compat1.3.2 · 1×
@eslint/eslintrc3.3.1 · 1×
@eslint/js9.35.0 · 1×
@humanwhocodes/crosspost1.0.3 · 1×
@ohm-js/cli2.0.0 · 1×
@ohm-js/compilerworkspace:^ · 1×
@rollup/plugin-commonjs21.0.1 · 1×
@rollup/plugin-json4.1.0 · 1×
@rollup/plugin-node-resolve16.0.1 · 1×

For agents

$ claude mcp add ohm \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact