jscc-parser
Version:
A LALR(1) Parser Generator for JavaScript written in JavaScript.
274 lines (238 loc) • 15.8 kB
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: main.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: main.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*
* Universal module definition for main entry point of JS/CC.
*/
(function(root, factory) {
/* istanbul ignore next */
if (typeof define === 'function' && define.amd) {
define(['require', './global', './io/io', './first',
'./printtab', './tabgen', './util',
'./integrity', './lexdbg',
'./parse', './log/log', './enums/LOG_LEVEL', './enums/EXEC', './lexdfa',
'./enums/MODE_GEN'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(require);
} else {
root.jscc = factory(function(mod) {
return root["jscc" + mod.split("/").pop()];
});
}
}(this,
/**
* @param {reqParameter} require
* @param {...*} others
* @returns {function(?mainOptions=)}
*/
function(require, others) {
var io, log, global = /** @type {jscc.global} */ (require("./global")),
first = /** @type {jscc.first} */ (require("./first")),
printtab = /** @type {jscc.printtab} */ (require("./printtab")),
tabgen = /** @type {jscc.tabgen} */ (require("./tabgen")),
util = /** @type {jscc.util} */ (require("./util")),
integrity = /** @type {jscc.integrity} */ (require("./integrity")),
lexdbg = /** @type {jscc.lexdbg} */ (require("./lexdbg")),
parse = /** @type {function(string, string=):number} */ (require("./parse")),
LOG_LEVEL = require("./enums/LOG_LEVEL"),
EXEC = require("./enums/EXEC"),
lexdfa = /** @type {jscc.lexdfa} */ (require("./lexdfa")),
MODE_GEN = require("./enums/MODE_GEN");
//>>excludeStart("closure", pragmas.closure);
var has = /** @type {hasObject} */ (require("./localHas"));
//>>excludeEnd("closure");
/**
* @suppress {uselessCode}
*/
(function() {
if (has("node")) {
io = /** @type {jscc.io} */ (require("./io/ioNode"));
log = /** @type {jscc.log} */ (require("./log/logNode"));
} else {
io = /** @type {jscc.io} */ (require("./io/io"));
log = /** @type {jscc.log} */ (require("./log/log"));
}
})();
/**
* The main entry point of JS/CC. Call this module as a function to
* process a grammar specification.
* @module jscc
* @requires module:jscc/global
* @requires module:jscc/io/io
* @requires module:jscc/first
* @requires module:jscc/printtab
* @requires module:jscc/tabgen
* @requires module:jscc/util
* @requires module:jscc/integrity
* @requires module:jscc/lexdbg
* @requires module:jscc/parse
* @requires module:jscc/log/log
* @param {?mainOptions=} options - Configuration options for the jscc module.
*/
var main =
function(options) {
var opt = options || {};
var logLevel = /** jscc.enums.LOG_LEVEL */ (LOG_LEVEL.WARN);
if (typeof opt['logLevel'] === 'string') {
logLevel = util.log_level_value(opt['logLevel']);
} else if (opt['logLevel']) {
logLevel = /** jscc.enums.LOG_LEVEL */ (opt['logLevel']);
}
log.setLevel(logLevel);
log.trace("jscc main: processing options");
var out_file = (typeof opt['out_file'] === 'string') ? opt['out_file'] : "";
var src_file = (typeof opt['src_file'] === 'string') ? opt['src_file'] : "";
var tpl_file = (typeof opt['tpl_file'] === 'string') ? opt['tpl_file'] : "";
var dump_nfa = (typeof opt['dump_nfa'] === 'boolean') ? opt['dump_nfa'] : false;
var dump_dfa = (typeof opt['dump_dfa'] === 'boolean') ? opt['dump_dfa'] : false;
var verbose = (typeof opt['verbose'] === 'boolean') ? opt['verbose'] : false;
var inputString = /** @type {string} */ ((typeof opt['input'] === 'string') ? opt['input'] : "");
var inputFunction = (typeof opt['input'] === 'function') ? opt['input'] : null;
var templateString = (typeof opt['template'] === 'string') ? opt['template'] : global.DEFAULT_DRIVER;
var templateFunction = (typeof opt['template'] === 'function') ? opt['template'] : null;
var outputCallback = (typeof opt['outputCallback'] === 'function') ? opt['outputCallback'] : null;
var throwIfErrors = (typeof opt['throwIfErrors'] === 'boolean') ? opt['throwIfErrors'] : false;
var exitIfErrors = (typeof opt['exitIfErrors'] === 'boolean') ? opt['exitIfErrors'] : false;
// Only relevant to browsers, but include anyway
if (inputString !== "") {
global.read_all_input_function = function() {
return inputString;
}
} else if (inputFunction) {
global.read_all_input_function = inputFunction;
}
if (templateString !== "") {
global.read_template_function = function() {
return templateString;
}
} else if (templateFunction) {
global.read_template_function = templateFunction;
}
if (outputCallback) {
global.write_output_function = outputCallback;
}
global.file = (src_file || "") === "" ? "[input]" : src_file;
global.dump_nfa = dump_nfa;
global.dump_dfa = dump_dfa;
log.trace("jscc main: reading source");
var src = inputString;
if (src === "") {
if (inputFunction) {
src = inputFunction();
} else if (src_file !== "") {
src = /** @type {string} */ (io.read_all_input(src_file));
} else {
// TODO: read standard input
log.error("No input. Specify input or src_file in the options parameter.");
}
}
if (src !== "") {
log.trace("jscc main: parse");
parse(src, global.file);
if (global.errors == 0) {
log.trace("jscc main: integrity.undef()");
integrity.undef();
log.trace("jscc main: integrity.unreachable()");
integrity.unreachable();
if (global.errors == 0) {
log.trace("jscc main: first.first()");
first.first();
log.trace("jscc main: tabgen.lalr1_parse_table(false)");
tabgen.lalr1_parse_table(false);
log.trace("jscc main: integrity.check_empty_states()");
integrity.check_empty_states();
if (global.errors == 0) {
if (global.dump_dfa) {
lexdbg.print_dfa(global.dfa_states);
}
log.trace("jscc main: lexdfa.create_subset(global.nfa_states.value)");
global.dfa_states = lexdfa.create_subset(global.nfa_states.value);
log.trace("jscc main: lexdfa.minimize_dfa(global.dfa_states)");
global.dfa_states = lexdfa.minimize_dfa(global.dfa_states);
log.trace("jscc main: read template");
/**
* @type {string}
*/
var driver = templateString;
if (templateFunction) {
driver = templateFunction();
} else if (tpl_file !== "") {
driver = /** @type {string} */ (io.read_template(tpl_file));
}
log.trace("jscc main: replace template strings");
driver = driver.replace(/##HEADER##/gi, global.code_head);
driver = driver.replace(/##TABLES##/gi, printtab.print_parse_tables(MODE_GEN.JS));
driver = driver.replace(/##DFA##/gi, printtab.print_dfa_table(global.dfa_states));
driver = driver.replace(/##TERMINAL_ACTIONS##/gi, printtab.print_term_actions());
driver = driver.replace(/##LABELS##/gi, printtab.print_symbol_labels());
driver = driver.replace(/##ACTIONS##/gi, printtab.print_actions());
driver = driver.replace(/##FOOTER##/gi, global.code_foot);
driver = driver.replace(/##ERROR_TOKEN##/gi, printtab.get_error_symbol_id().toString());
driver = driver.replace(/##EOF##/gi, printtab.get_eof_symbol_id().toString());
driver = /** @type {string} */
(driver.replace(/##WHITESPACE##/gi, printtab.get_whitespace_symbol_id().toString()));
log.trace("jscc main: output");
if (global.errors == 0) {
if (outputCallback) {
outputCallback(driver);
} else if (out_file != "") {
io.write_output({
text: driver,
destination: out_file
});
} else {
io.write_output(driver);
}
}
if (verbose) {
log.info("\"" + src_file + "\" produced " + global.states.length + " states (" +
global.shifts + " shifts," +
global.reduces + " reductions, " + global.gotos + " gotos)");
}
}
}
}
if (verbose) {
log.info(global.warnings + " warning" + (global.warnings > 1 ? "s" : "") + ", " +
global.errors + " error" + (global.errors > 1 ? "s" : ""));
}
}
if (exitIfErrors && global.errors > 0) {
io.exit(1);
}
if (throwIfErrors && global.errors > 0) {
throw new Error("There were one or more compilation errors. See the log output for more information.");
}
};
return main;
}));
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-jscc.html">jscc</a></li><li><a href="module-jscc_bitset_BitSet32.html">jscc/bitset/BitSet32</a></li><li><a href="module-jscc_bitset_BitSetBool.html">jscc/bitset/BitSetBool</a></li><li><a href="module-jscc_bitset_BitSetJava.html">jscc/bitset/BitSetJava</a></li><li><a href="module-jscc_classes_Dfa.html">jscc/classes/Dfa</a></li><li><a href="module-jscc_classes_Item.html">jscc/classes/Item</a></li><li><a href="module-jscc_classes_Nfa.html">jscc/classes/Nfa</a></li><li><a href="module-jscc_classes_Param.html">jscc/classes/Param</a></li><li><a href="module-jscc_classes_Production.html">jscc/classes/Production</a></li><li><a href="module-jscc_classes_State.html">jscc/classes/State</a></li><li><a href="module-jscc_classes_Symbol.html">jscc/classes/Symbol</a></li><li><a href="module-jscc_classes_TableEntry.html">jscc/classes/TableEntry</a></li><li><a href="module-jscc_enums_ASSOC.html">jscc/enums/ASSOC</a></li><li><a href="module-jscc_enums_EDGE.html">jscc/enums/EDGE</a></li><li><a href="module-jscc_enums_EXEC.html">jscc/enums/EXEC</a></li><li><a href="module-jscc_enums_LOG_LEVEL.html">jscc/enums/LOG_LEVEL</a></li><li><a href="module-jscc_enums_MODE_GEN.html">jscc/enums/MODE_GEN</a></li><li><a href="module-jscc_enums_SPECIAL.html">jscc/enums/SPECIAL</a></li><li><a href="module-jscc_enums_SYM.html">jscc/enums/SYM</a></li><li><a href="module-jscc_first.html">jscc/first</a></li><li><a href="module-jscc_global.html">jscc/global</a></li><li><a href="module-jscc_integrity.html">jscc/integrity</a></li><li><a href="module-jscc_io_io.html">jscc/io/io</a></li><li><a href="module-jscc_lexdbg.html">jscc/lexdbg</a></li><li><a href="module-jscc_lexdfa.html">jscc/lexdfa</a></li><li><a href="module-jscc_log_log.html">jscc/log/log</a></li><li><a href="module-jscc_parse.html">jscc/parse</a></li><li><a href="module-jscc_printtab.html">jscc/printtab</a></li><li><a href="module-jscc_regex.html">jscc/regex</a></li><li><a href="module-jscc_util.html">jscc/util</a></li></ul><h3>Classes</h3><ul><li><a href="-_anonymous_-jscc.BitSet32.html">BitSet32</a></li><li><a href="-_anonymous_-jscc.BitSetBool.html">BitSetBool</a></li><li><a href="-_anonymous_-jscc.BitSetJava.html">BitSetJava</a></li><li><a href="-_anonymous_-jscc.classes.Dfa.html">classes.Dfa</a></li><li><a href="-_anonymous_-jscc.first.html">first</a></li><li><a href="-_anonymous_-jscc.global.html">global</a></li><li><a href="-_anonymous_-jscc.integrity.html">integrity</a></li><li><a href="-_anonymous_-jscc.ioBrowser.html">ioBrowser</a></li><li><a href="-_anonymous_-jscc.ioNashorn.html">ioNashorn</a></li><li><a href="-_anonymous_-jscc.ioNode.html">ioNode</a></li><li><a href="-_anonymous_-jscc.ioRhino.html">ioRhino</a></li><li><a href="-_anonymous_-jscc.lexdbg.html">lexdbg</a></li><li><a href="-_anonymous_-jscc.lexdfa.html">lexdfa</a></li><li><a href="-_anonymous_-jscc.logBrowser.html">logBrowser</a></li><li><a href="-_anonymous_-jscc.logJava.html">logJava</a></li><li><a href="-_anonymous_-jscc.logNode.html">logNode</a></li><li><a href="-_anonymous_-jscc.printtab.html">printtab</a></li><li><a href="-_anonymous_-jscc.util.html">util</a></li><li><a href="jscc_debug.html">jscc/debug</a></li><li><a href="jscc_nfaStates.html">jscc/nfaStates</a></li><li><a href="jscc_tabgen.html">jscc/tabgen</a></li><li><a href="%257Bjscc.classes%257D.jscc.classes.State.html">jscc.classes.State</a></li></ul><h3>Namespaces</h3><ul><li><a href="jscc.debug.html">debug</a></li></ul><h3>Interfaces</h3><ul><li><a href="jscc.bitset.html">bitset</a></li><li><a href="jscc.io.html">io</a></li><li><a href="jscc.log.html">log</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Aug 01 2016 13:35:55 GMT-0500 (Central Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>