Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ohm-js

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ohm-js

An object-oriented language for parsing and pattern matching

  • 0.8.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
149K
increased by4.04%
Maintainers
1
Weekly downloads
 
Created

What is ohm-js?

Ohm is a parser generator for building languages and interpreters. It provides a way to define grammars and parse text according to those grammars. Ohm is particularly useful for creating domain-specific languages, interpreters, and compilers.

What are ohm-js's main functionalities?

Defining Grammars

This feature allows you to define a grammar using Ohm's syntax. The example defines a simple arithmetic grammar that can parse expressions involving addition, subtraction, multiplication, and division.

const ohm = require('ohm-js');
const grammar = ohm.grammar(`
  Arithmetic {
    Exp = AddExp
    AddExp = AddExp "+" MulExp  -- plus
           | AddExp "-" MulExp  -- minus
           | MulExp
    MulExp = MulExp "*" PriExp  -- times
           | MulExp "/" PriExp  -- divide
           | PriExp
    PriExp = "(" Exp ")"        -- paren
           | number
    number = digit+
  }
`);

Parsing Input

Once a grammar is defined, you can use it to parse input strings. This example parses an arithmetic expression and checks if the parsing succeeded.

const input = '3 + 5 * (10 - 4)';
const matchResult = grammar.match(input);
if (matchResult.succeeded()) {
  console.log('Parsing succeeded!');
} else {
  console.log('Parsing failed.');
}

Semantic Actions

Ohm allows you to define semantic actions that can be performed on the parse tree. This example defines an 'eval' operation to evaluate arithmetic expressions parsed by the grammar.

const semantics = grammar.createSemantics().addOperation('eval', {
  Exp: function(e) { return e.eval(); },
  AddExp_plus: function(a, _, b) { return a.eval() + b.eval(); },
  AddExp_minus: function(a, _, b) { return a.eval() - b.eval(); },
  MulExp_times: function(a, _, b) { return a.eval() * b.eval(); },
  MulExp_divide: function(a, _, b) { return a.eval() / b.eval(); },
  PriExp_paren: function(_1, e, _2) { return e.eval(); },
  number: function(digits) { return parseInt(this.sourceString, 10); }
});
const result = semantics(matchResult).eval();
console.log(result);

Other packages similar to ohm-js

Keywords

FAQs

Package last updated on 27 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc