Skip to content

Latest commit

 

History

History
106 lines (87 loc) · 2.96 KB

README.md

File metadata and controls

106 lines (87 loc) · 2.96 KB

Ohm Documentation

Examples

Here are some quick samples of what it's like to work with Ohm. For more in-depth examples, see the examples directory, especially the math example which is extensively commented.

Matching Strings

Instantiate a grammar from a string using ohm.grammar(), and check inputs using the grammar's match() method:

const ohm = require('ohm-js');
const g = ohm.grammar(`
  Laugh {
    laugh = lol | "lmao"
    lol = "l" "o"+ "l"
  }`);
assert(g.match('lol').succeeded());
assert(!g.match('lmao').failed());
assert(g.match('loooooool').succeeded());

Implementing Semantics

You can use operations and attributes to analyze and extract values from parsed data. For example, take the following grammar in arithmetic.ohm:

Arithmetic {
  Exp
    = AddExp

  AddExp
    = AddExp "+" PriExp  -- plus
    | AddExp "-" PriExp  -- minus
    | PriExp

  PriExp
    = "(" Exp ")"  -- paren
    | number

  number
    = digit+
}

We can create an operation named 'eval' to evaluate arithmetic expressions that match the grammar:

// Instantiate the grammar.
const fs = require('fs');
const g = ohm.grammar(fs.readFileSync('arithmetic.ohm'));

// Create an operation that evaluates the expression. An operation always belongs to a Semantics,
// which is a family of related operations and attributes for a particular grammar.
const semantics = g.createSemantics().addOperation('eval', {
  Exp(e) {
    return e.eval();
  },
  AddExp(e) {
    return e.eval();
  },
  AddExp_plus(left, op, right) {
    return left.eval() + right.eval();
  },
  AddExp_minus(left, op, right) {
    return left.eval() - right.eval();
  },
  PriExp(e) {
    return e.eval();
  },
  PriExp_paren(open, exp, close) {
    return exp.eval();
  },
  number(chars) {
    return parseInt(this.sourceString, 10);
  }
});
const match = g.match('1 + (2 - 3) + 4');
assert.equal(semantics(match).eval(), 4);

You can learn more about semantics in the API reference.