You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.0 KiB
JavaScript
72 lines
2.0 KiB
JavaScript
9 years ago
|
"use strict";
|
||
|
|
||
8 years ago
|
var arrays = require("../utils/arrays"),
|
||
|
objects = require("../utils/objects");
|
||
12 years ago
|
|
||
10 years ago
|
var compiler = {
|
||
9 years ago
|
/*
|
||
|
* AST node visitor builder. Useful mainly for plugins which manipulate the
|
||
|
* AST.
|
||
|
*/
|
||
8 years ago
|
visitor: require("./visitor"),
|
||
9 years ago
|
|
||
12 years ago
|
/*
|
||
|
* 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
|
||
8 years ago
|
* |peg.GrammarError|.
|
||
12 years ago
|
*/
|
||
|
passes: {
|
||
11 years ago
|
check: {
|
||
8 years ago
|
reportMissingRules: require("./passes/report-missing-rules"),
|
||
|
reportLeftRecursion: require("./passes/report-left-recursion"),
|
||
|
reportInfiniteLoops: require("./passes/report-infinite-loops")
|
||
11 years ago
|
},
|
||
|
transform: {
|
||
8 years ago
|
removeProxyRules: require("./passes/remove-proxy-rules")
|
||
11 years ago
|
},
|
||
|
generate: {
|
||
8 years ago
|
generateBytecode: require("./passes/generate-bytecode"),
|
||
|
generateJS: require("./passes/generate-js")
|
||
11 years ago
|
}
|
||
12 years ago
|
},
|
||
12 years ago
|
|
||
14 years ago
|
/*
|
||
8 years ago
|
* Generates a parser from a specified grammar AST. Throws |peg.GrammarError|
|
||
14 years ago
|
* 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.
|
||
|
*/
|
||
8 years ago
|
compile: function(ast, passes, options) {
|
||
|
options = options !== void 0 ? options : {};
|
||
11 years ago
|
|
||
8 years ago
|
var stage;
|
||
|
|
||
|
options = objects.clone(options);
|
||
10 years ago
|
objects.defaults(options, {
|
||
8 years ago
|
allowedStartRules: [ast.rules[0].name],
|
||
|
cache: false,
|
||
|
trace: false,
|
||
|
optimize: "speed",
|
||
8 years ago
|
output: "parser",
|
||
|
format: "bare",
|
||
8 years ago
|
dependencies: {},
|
||
8 years ago
|
exportVar: null
|
||
12 years ago
|
});
|
||
13 years ago
|
|
||
11 years ago
|
for (stage in passes) {
|
||
11 years ago
|
if (passes.hasOwnProperty(stage)) {
|
||
10 years ago
|
arrays.each(passes[stage], function(p) { p(ast, options); });
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|
||
14 years ago
|
|
||
12 years ago
|
switch (options.output) {
|
||
12 years ago
|
case "parser": return eval(ast.code);
|
||
|
case "source": return ast.code;
|
||
|
}
|
||
14 years ago
|
}
|
||
|
};
|
||
10 years ago
|
|
||
|
module.exports = compiler;
|