pegjs plugin helper: Run a function once for each code snippet in a grammar AST.


npm install pegjs-each-code

var eachCode = require("pegjs-each-code")
eachCode(ast, function(nodelabelsruleName) {
  try {
    node.code = compile(node.code, {locals: labels})
  } catch (error) {
    throw new Error([ruleName, error.message, "{" + node.code + "}"].join("\n\n"))
  • The function passed to eachCode will be run once for each node with a code property, such as “action”, “semantic and” and “semantic not” nodes, except the initializer.

  • labels is an array of all the labels that are available to the code snippet.

  • ruleName is the name of the rule that the code snippet occurs in.

eachCode is useful when making a plugin for a compile-to-js language (such as CoffeeScript and TypeScript) or a linter.


The X11 (“MIT”) License.