aboutsummaryrefslogtreecommitdiff
path: root/cordova/node_modules/pegjs/lib/compiler/index.js
blob: 1248598ad594de3b3805fd4b7747a78a0e87d44a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"use strict";

var arrays  = require("../utils/arrays"),
    objects = require("../utils/objects");

var compiler = {
  /*
   * AST node visitor builder. Useful mainly for plugins which manipulate the
   * AST.
   */
  visitor: require("./visitor"),

  /*
   * Compiler passes.
   *
   * Each pass is a function that is passed the AST. It can perform checks on it
   * or modify it as needed. If the pass encounters a semantic error, it throws
   * |peg.GrammarError|.
   */
  passes: {
    check: {
      reportUndefinedRules:     require("./passes/report-undefined-rules"),
      reportDuplicateRules:     require("./passes/report-duplicate-rules"),
      reportDuplicateLabels:    require("./passes/report-duplicate-labels"),
      reportInfiniteRecursion:  require("./passes/report-infinite-recursion"),
      reportInfiniteRepetition: require("./passes/report-infinite-repetition")
    },
    transform: {
      removeProxyRules:         require("./passes/remove-proxy-rules")
    },
    generate: {
      generateBytecode:         require("./passes/generate-bytecode"),
      generateJS:               require("./passes/generate-js")
    }
  },

  /*
   * Generates a parser from a specified grammar AST. Throws |peg.GrammarError|
   * if the AST contains a semantic error. Note that not all errors are detected
   * during the generation and some may protrude to the generated parser and
   * cause its malfunction.
   */
  compile: function(ast, passes, options) {
    options = options !== void 0 ? options : {};

    var stage;

    options = objects.clone(options);
    objects.defaults(options, {
      allowedStartRules: [ast.rules[0].name],
      cache:             false,
      dependencies:      {},
      exportVar:         null,
      format:            "bare",
      optimize:          "speed",
      output:            "parser",
      trace:             false
    });

    for (stage in passes) {
      if (passes.hasOwnProperty(stage)) {
        arrays.each(passes[stage], function(p) { p(ast, options); });
      }
    }

    switch (options.output) {
      case "parser": return eval(ast.code);
      case "source": return ast.code;
    }
  }
};

module.exports = compiler;