2015-06-08 20:21:19 +02:00
|
|
|
"use strict";
|
|
|
|
|
2016-06-20 08:06:14 +02:00
|
|
|
var arrays = require("../utils/arrays"),
|
|
|
|
objects = require("../utils/objects");
|
2012-11-10 09:47:22 +01:00
|
|
|
|
2014-05-04 14:11:44 +02:00
|
|
|
var compiler = {
|
2015-12-11 14:07:44 +01:00
|
|
|
/*
|
|
|
|
* AST node visitor builder. Useful mainly for plugins which manipulate the
|
|
|
|
* AST.
|
|
|
|
*/
|
2016-06-20 08:06:14 +02:00
|
|
|
visitor: require("./visitor"),
|
2015-12-11 14:07:44 +01:00
|
|
|
|
2013-01-06 20:27:35 +01:00
|
|
|
/*
|
|
|
|
* 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
|
2016-05-04 12:37:13 +02:00
|
|
|
* |peg.GrammarError|.
|
2013-01-06 20:27:35 +01:00
|
|
|
*/
|
|
|
|
passes: {
|
2013-01-13 11:17:44 +01:00
|
|
|
check: {
|
2016-06-20 08:06:14 +02:00
|
|
|
reportMissingRules: require("./passes/report-missing-rules"),
|
|
|
|
reportLeftRecursion: require("./passes/report-left-recursion"),
|
|
|
|
reportInfiniteLoops: require("./passes/report-infinite-loops")
|
2013-01-13 11:17:44 +01:00
|
|
|
},
|
|
|
|
transform: {
|
2016-06-20 08:06:14 +02:00
|
|
|
removeProxyRules: require("./passes/remove-proxy-rules")
|
2013-01-13 11:17:44 +01:00
|
|
|
},
|
|
|
|
generate: {
|
2016-06-20 08:06:14 +02:00
|
|
|
generateBytecode: require("./passes/generate-bytecode"),
|
|
|
|
generateJS: require("./passes/generate-js")
|
2013-01-13 11:17:44 +01:00
|
|
|
}
|
2013-01-06 20:27:35 +01:00
|
|
|
},
|
2012-11-10 09:47:22 +01:00
|
|
|
|
2010-07-25 17:54:09 +02:00
|
|
|
/*
|
2016-05-04 12:37:13 +02:00
|
|
|
* Generates a parser from a specified grammar AST. Throws |peg.GrammarError|
|
2010-07-25 17:54:09 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
2016-03-18 16:39:19 +01:00
|
|
|
compile: function(ast, passes, options) {
|
|
|
|
options = options !== void 0 ? options : {};
|
2013-01-13 11:17:44 +01:00
|
|
|
|
2016-03-18 16:39:19 +01:00
|
|
|
var stage;
|
|
|
|
|
|
|
|
options = objects.clone(options);
|
2014-05-08 11:48:56 +02:00
|
|
|
objects.defaults(options, {
|
2016-04-28 10:10:48 +02:00
|
|
|
allowedStartRules: [ast.rules[0].name],
|
|
|
|
cache: false,
|
|
|
|
trace: false,
|
|
|
|
optimize: "speed",
|
2016-05-02 16:05:09 +02:00
|
|
|
output: "parser",
|
|
|
|
format: "bare",
|
2016-05-03 11:16:03 +02:00
|
|
|
dependencies: {},
|
2016-05-02 16:05:09 +02:00
|
|
|
exportVar: null
|
2013-01-06 10:32:52 +01:00
|
|
|
});
|
2011-10-03 14:14:14 +02:00
|
|
|
|
2013-01-13 11:17:44 +01:00
|
|
|
for (stage in passes) {
|
2013-12-14 21:46:47 +01:00
|
|
|
if (passes.hasOwnProperty(stage)) {
|
2014-05-08 11:48:56 +02:00
|
|
|
arrays.each(passes[stage], function(p) { p(ast, options); });
|
2013-12-14 21:46:47 +01:00
|
|
|
}
|
2013-01-13 11:17:44 +01:00
|
|
|
}
|
2010-07-25 17:54:09 +02:00
|
|
|
|
2013-01-06 10:32:52 +01:00
|
|
|
switch (options.output) {
|
2012-11-11 18:18:52 +01:00
|
|
|
case "parser": return eval(ast.code);
|
|
|
|
case "source": return ast.code;
|
|
|
|
}
|
2010-07-25 17:54:09 +02:00
|
|
|
}
|
|
|
|
};
|
2014-05-04 14:11:44 +02:00
|
|
|
|
|
|
|
module.exports = compiler;
|