How to use the ohm-js.grammars function in ohm-js

To help you get started, we’ve selected a few ohm-js examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github harc / ohm / examples / ecmascript / es5.js View on Github external
for (var i = 0; i < flatChildren.length; ++i) {
    var child = flatChildren[i];

    // Restore any discarded whitespace between this node and the previous one.
    if (child.source.startIdx > prevEndIdx) {
      code += node.source.sourceString.slice(prevEndIdx, child.source.startIdx);
    }
    code += child.toES5();
    prevEndIdx = child.source.endIdx;
  }
  return code;
}

// Instantiate the ES5 grammar.
var contents = fs.readFileSync(path.join(__dirname, 'es5.ohm'));
var g = ohm.grammars(contents).ES5;
var semantics = g.createSemantics();

semantics.addOperation('toES5()', {
  Program: function(_, sourceElements) {
    // Top-level leading and trailing whitespace is not handled by nodeToES5(), so do it here.
    var sourceString = this.source.sourceString;
    return sourceString.slice(0, this.source.startIdx) +
           nodeToES5(this, [sourceElements]) +
           sourceString.slice(this.source.endIdx);
  },
  _nonterminal: function(children) {
    return nodeToES5(this, children);
  },
  _terminal: function() {
    return this.sourceString;
  }
github harc / ohm-editor / test / testUtil.js View on Github external
function makeGrammars(source, optNamespace) {
  if (Array.isArray(source)) {
    source = source.join('\n');
  }
  return ohm.grammars(source, optNamespace);
}

ohm-js

An object-oriented language for parsing and pattern matching

MIT
Latest version published 2 years ago

Package Health Score

69 / 100
Full package analysis