All Projects → harc → Ohm

harc / Ohm

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

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects
typescript
32286 projects

Projects that are alternatives of or similar to Ohm

ohm-editor
An IDE for the Ohm language (JavaScript edition)
Stars: ✭ 78 (-98.02%)
Mutual labels:  grammars, parsing, peg
cppcombinator
parser combinator and AST generator in c++17
Stars: ✭ 20 (-99.49%)
Mutual labels:  parsing, peg
left-recursion
Quick explanation of eliminating left recursion in Haskell parsers
Stars: ✭ 36 (-99.09%)
Mutual labels:  grammars, parsing
Pom
PEG parser combinators using operator overloading without macros.
Stars: ✭ 310 (-92.13%)
Mutual labels:  peg, parsing
ParsecSharp
The faster monadic parser combinator library for C#
Stars: ✭ 23 (-99.42%)
Mutual labels:  parsing, peg
arborist
Arborist is a PEG parser that supports left-associative left recursion
Stars: ✭ 17 (-99.57%)
Mutual labels:  parsing, peg
latex2unicode
Convert LaTeX markup to Unicode (in Scala and Java)
Stars: ✭ 28 (-99.29%)
Mutual labels:  parsing, peg
3bmd
markdown processor in CL using esrap parser
Stars: ✭ 58 (-98.53%)
Mutual labels:  peg, parsing
Lug
Parsing expression grammar (PEG) embedded domain specific language and parsing machine for C++17
Stars: ✭ 44 (-98.88%)
Mutual labels:  peg, parsing
Rust Peg
Parsing Expression Grammar (PEG) parser generator for Rust
Stars: ✭ 836 (-78.77%)
Mutual labels:  peg, parsing
pyrser
A PEG Parsing Tool
Stars: ✭ 32 (-99.19%)
Mutual labels:  parsing, peg
Chevrotain
Parser Building Toolkit for JavaScript
Stars: ✭ 1,795 (-54.42%)
Mutual labels:  parsing, grammars
parson
Yet another PEG parser combinator library and DSL
Stars: ✭ 52 (-98.68%)
Mutual labels:  parsing, peg
pe
Fastest general-purpose parsing library for Python with a familiar API
Stars: ✭ 21 (-99.47%)
Mutual labels:  parsing, peg
autumn
A Java parser combinator library written with an unmatched feature set.
Stars: ✭ 112 (-97.16%)
Mutual labels:  grammars, parsing
Cpp Peglib
A single file C++ header-only PEG (Parsing Expression Grammars) library
Stars: ✭ 435 (-88.95%)
Mutual labels:  peg, parsing
Pegtl
Parsing Expression Grammar Template Library
Stars: ✭ 1,295 (-67.12%)
Mutual labels:  peg, parsing
Pest
The Elegant Parser
Stars: ✭ 2,783 (-29.33%)
Mutual labels:  peg, parsing
Goose Parser
Universal scrapping tool, which allows you to extract data using multiple environments
Stars: ✭ 211 (-94.64%)
Mutual labels:  parsing
Useragentparser
UserAgent parsing done right
Stars: ✭ 225 (-94.29%)
Mutual labels:  parsing

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

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');

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:

    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:

    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

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.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].