Git repo npmization: Compose PEG.js from Node.js modules
PEG.js source code becomes a set of Node.js modules that include each other as needed. The distribution version is built by bundling these modules together, wrapping them inside a bit of boilerplate code that makes |module.exports| and |require| work. Part of a fix for GH-32.redux
parent
c6cf129635
commit
4cda79951a
@ -0,0 +1,9 @@
|
|||||||
|
var utils = require("./utils");
|
||||||
|
|
||||||
|
/* Thrown when the grammar contains an error. */
|
||||||
|
module.exports = function(message) {
|
||||||
|
this.name = "GrammarError";
|
||||||
|
this.message = message;
|
||||||
|
};
|
||||||
|
|
||||||
|
utils.subclass(module.exports, Error);
|
@ -1,209 +1,214 @@
|
|||||||
/* Like Python's |range|, but without |step|. */
|
var utils = {
|
||||||
function range(start, stop) {
|
/* Like Python's |range|, but without |step|. */
|
||||||
if (stop === undefined) {
|
range: function(start, stop) {
|
||||||
stop = start;
|
if (stop === undefined) {
|
||||||
start = 0;
|
stop = start;
|
||||||
}
|
start = 0;
|
||||||
|
}
|
||||||
|
|
||||||
var result = new Array(Math.max(0, stop - start));
|
var result = new Array(Math.max(0, stop - start));
|
||||||
for (var i = 0, j = start; j < stop; i++, j++) {
|
for (var i = 0, j = start; j < stop; i++, j++) {
|
||||||
result[i] = j;
|
result[i] = j;
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function find(array, callback) {
|
|
||||||
var length = array.length;
|
|
||||||
for (var i = 0; i < length; i++) {
|
|
||||||
if (callback(array[i])) {
|
|
||||||
return array[i];
|
|
||||||
}
|
}
|
||||||
}
|
return result;
|
||||||
}
|
},
|
||||||
|
|
||||||
|
find: function(array, callback) {
|
||||||
|
var length = array.length;
|
||||||
|
for (var i = 0; i < length; i++) {
|
||||||
|
if (callback(array[i])) {
|
||||||
|
return array[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
contains: function(array, value) {
|
||||||
|
/*
|
||||||
|
* Stupid IE does not have Array.prototype.indexOf, otherwise this function
|
||||||
|
* would be a one-liner.
|
||||||
|
*/
|
||||||
|
var length = array.length;
|
||||||
|
for (var i = 0; i < length; i++) {
|
||||||
|
if (array[i] === value) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
|
||||||
function contains(array, value) {
|
each: function(array, callback) {
|
||||||
/*
|
var length = array.length;
|
||||||
* Stupid IE does not have Array.prototype.indexOf, otherwise this function
|
for (var i = 0; i < length; i++) {
|
||||||
* would be a one-liner.
|
callback(array[i], i);
|
||||||
*/
|
|
||||||
var length = array.length;
|
|
||||||
for (var i = 0; i < length; i++) {
|
|
||||||
if (array[i] === value) {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function each(array, callback) {
|
map: function(array, callback) {
|
||||||
var length = array.length;
|
var result = [];
|
||||||
for (var i = 0; i < length; i++) {
|
var length = array.length;
|
||||||
callback(array[i], i);
|
for (var i = 0; i < length; i++) {
|
||||||
}
|
result[i] = callback(array[i], i);
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
|
||||||
function map(array, callback) {
|
pluck: function(array, key) {
|
||||||
var result = [];
|
return utils.map(array, function (e) { return e[key]; });
|
||||||
var length = array.length;
|
},
|
||||||
for (var i = 0; i < length; i++) {
|
|
||||||
result[i] = callback(array[i], i);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function pluck(array, key) {
|
keys: function(object) {
|
||||||
return map(array, function (e) { return e[key]; });
|
var result = [];
|
||||||
}
|
for (var key in object) {
|
||||||
|
result.push(key);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
|
||||||
function keys(object) {
|
values: function(object) {
|
||||||
var result = [];
|
var result = [];
|
||||||
for (var key in object) {
|
for (var key in object) {
|
||||||
result.push(key);
|
result.push(object[key]);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
},
|
||||||
|
|
||||||
function values(object) {
|
clone: function(object) {
|
||||||
var result = [];
|
var result = {};
|
||||||
for (var key in object) {
|
for (var key in object) {
|
||||||
result.push(object[key]);
|
result[key] = object[key];
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
},
|
||||||
|
|
||||||
|
defaults: function(object, defaults) {
|
||||||
|
for (var key in defaults) {
|
||||||
|
if (object[key] === undefined) {
|
||||||
|
object[key] = defaults[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
function clone(object) {
|
/*
|
||||||
var result = {};
|
* The code needs to be in sync with the code template in the compilation
|
||||||
for (var key in object) {
|
* function for "action" nodes.
|
||||||
result[key] = object[key];
|
*/
|
||||||
}
|
subclass: function(child, parent) {
|
||||||
return result;
|
function ctor() { this.constructor = child; }
|
||||||
}
|
ctor.prototype = parent.prototype;
|
||||||
|
child.prototype = new ctor();
|
||||||
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns a string padded on the left to a desired length with a character.
|
||||||
|
*
|
||||||
|
* The code needs to be in sync with the code template in the compilation
|
||||||
|
* function for "action" nodes.
|
||||||
|
*/
|
||||||
|
padLeft: function(input, padding, length) {
|
||||||
|
var result = input;
|
||||||
|
|
||||||
function defaults(object, defaults) {
|
var padLength = length - input.length;
|
||||||
for (var key in defaults) {
|
for (var i = 0; i < padLength; i++) {
|
||||||
if (object[key] === undefined) {
|
result = padding + result;
|
||||||
object[key] = defaults[key];
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* The code needs to be in sync with the code template in the compilation
|
|
||||||
* function for "action" nodes.
|
|
||||||
*/
|
|
||||||
function subclass(child, parent) {
|
|
||||||
function ctor() { this.constructor = child; }
|
|
||||||
ctor.prototype = parent.prototype;
|
|
||||||
child.prototype = new ctor();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Returns a string padded on the left to a desired length with a character.
|
|
||||||
*
|
|
||||||
* The code needs to be in sync with the code template in the compilation
|
|
||||||
* function for "action" nodes.
|
|
||||||
*/
|
|
||||||
function padLeft(input, padding, length) {
|
|
||||||
var result = input;
|
|
||||||
|
|
||||||
var padLength = length - input.length;
|
|
||||||
for (var i = 0; i < padLength; i++) {
|
|
||||||
result = padding + result;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
},
|
||||||
|
|
||||||
/*
|
|
||||||
* Returns an escape sequence for given character. Uses \x for characters <=
|
|
||||||
* 0xFF to save space, \u for the rest.
|
|
||||||
*
|
|
||||||
* The code needs to be in sync with the code template in the compilation
|
|
||||||
* function for "action" nodes.
|
|
||||||
*/
|
|
||||||
function escape(ch) {
|
|
||||||
var charCode = ch.charCodeAt(0);
|
|
||||||
var escapeChar;
|
|
||||||
var length;
|
|
||||||
|
|
||||||
if (charCode <= 0xFF) {
|
|
||||||
escapeChar = 'x';
|
|
||||||
length = 2;
|
|
||||||
} else {
|
|
||||||
escapeChar = 'u';
|
|
||||||
length = 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
return '\\' + escapeChar + padLeft(charCode.toString(16).toUpperCase(), '0', length);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Surrounds the string with quotes and escapes characters inside so that the
|
|
||||||
* result is a valid JavaScript string.
|
|
||||||
*
|
|
||||||
* The code needs to be in sync with the code template in the compilation
|
|
||||||
* function for "action" nodes.
|
|
||||||
*/
|
|
||||||
function quote(s) {
|
|
||||||
/*
|
/*
|
||||||
* ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a string
|
* Returns an escape sequence for given character. Uses \x for characters <=
|
||||||
* literal except for the closing quote character, backslash, carriage return,
|
* 0xFF to save space, \u for the rest.
|
||||||
* line separator, paragraph separator, and line feed. Any character may
|
|
||||||
* appear in the form of an escape sequence.
|
|
||||||
*
|
*
|
||||||
* For portability, we also escape escape all control and non-ASCII
|
* The code needs to be in sync with the code template in the compilation
|
||||||
* characters. Note that "\0" and "\v" escape sequences are not used because
|
* function for "action" nodes.
|
||||||
* JSHint does not like the first and IE the second.
|
|
||||||
*/
|
*/
|
||||||
return '"' + s
|
escape: function(ch) {
|
||||||
.replace(/\\/g, '\\\\') // backslash
|
var charCode = ch.charCodeAt(0);
|
||||||
.replace(/"/g, '\\"') // closing quote character
|
var escapeChar;
|
||||||
.replace(/\x08/g, '\\b') // backspace
|
var length;
|
||||||
.replace(/\t/g, '\\t') // horizontal tab
|
|
||||||
.replace(/\n/g, '\\n') // line feed
|
if (charCode <= 0xFF) {
|
||||||
.replace(/\f/g, '\\f') // form feed
|
escapeChar = 'x';
|
||||||
.replace(/\r/g, '\\r') // carriage return
|
length = 2;
|
||||||
.replace(/[\x00-\x07\x0B\x0E-\x1F\x80-\uFFFF]/g, escape)
|
} else {
|
||||||
+ '"';
|
escapeChar = 'u';
|
||||||
}
|
length = 4;
|
||||||
|
}
|
||||||
/*
|
|
||||||
* Escapes characters inside the string so that it can be used as a list of
|
return '\\' + escapeChar + utils.padLeft(charCode.toString(16).toUpperCase(), '0', length);
|
||||||
* characters in a character class of a regular expression.
|
},
|
||||||
*/
|
|
||||||
function quoteForRegexpClass(s) {
|
|
||||||
/*
|
/*
|
||||||
* Based on ECMA-262, 5th ed., 7.8.5 & 15.10.1.
|
* Surrounds the string with quotes and escapes characters inside so that the
|
||||||
|
* result is a valid JavaScript string.
|
||||||
*
|
*
|
||||||
* For portability, we also escape escape all control and non-ASCII
|
* The code needs to be in sync with the code template in the compilation
|
||||||
* characters.
|
* function for "action" nodes.
|
||||||
*/
|
*/
|
||||||
return s
|
quote: function(s) {
|
||||||
.replace(/\\/g, '\\\\') // backslash
|
/*
|
||||||
.replace(/\//g, '\\/') // closing slash
|
* ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a string
|
||||||
.replace(/\]/g, '\\]') // closing bracket
|
* literal except for the closing quote character, backslash, carriage
|
||||||
.replace(/-/g, '\\-') // dash
|
* return, line separator, paragraph separator, and line feed. Any character
|
||||||
.replace(/\0/g, '\\0') // null
|
* may appear in the form of an escape sequence.
|
||||||
.replace(/\t/g, '\\t') // horizontal tab
|
*
|
||||||
.replace(/\n/g, '\\n') // line feed
|
* For portability, we also escape escape all control and non-ASCII
|
||||||
.replace(/\v/g, '\\x0B') // vertical tab
|
* characters. Note that "\0" and "\v" escape sequences are not used because
|
||||||
.replace(/\f/g, '\\f') // form feed
|
* JSHint does not like the first and IE the second.
|
||||||
.replace(/\r/g, '\\r') // carriage return
|
*/
|
||||||
.replace(/[\x01-\x08\x0E-\x1F\x80-\uFFFF]/g, escape);
|
return '"' + s
|
||||||
}
|
.replace(/\\/g, '\\\\') // backslash
|
||||||
|
.replace(/"/g, '\\"') // closing quote character
|
||||||
/*
|
.replace(/\x08/g, '\\b') // backspace
|
||||||
* Builds a node visitor -- a function which takes a node and any number of
|
.replace(/\t/g, '\\t') // horizontal tab
|
||||||
* other parameters, calls an appropriate function according to the node type,
|
.replace(/\n/g, '\\n') // line feed
|
||||||
* passes it all its parameters and returns its value. The functions for various
|
.replace(/\f/g, '\\f') // form feed
|
||||||
* node types are passed in a parameter to |buildNodeVisitor| as a hash.
|
.replace(/\r/g, '\\r') // carriage return
|
||||||
*/
|
.replace(/[\x00-\x07\x0B\x0E-\x1F\x80-\uFFFF]/g, utils.escape)
|
||||||
function buildNodeVisitor(functions) {
|
+ '"';
|
||||||
return function(node) {
|
},
|
||||||
return functions[node.type].apply(null, arguments);
|
|
||||||
};
|
/*
|
||||||
}
|
* Escapes characters inside the string so that it can be used as a list of
|
||||||
|
* characters in a character class of a regular expression.
|
||||||
function findRuleByName(ast, name) {
|
*/
|
||||||
return find(ast.rules, function(r) { return r.name === name; });
|
quoteForRegexpClass: function(s) {
|
||||||
}
|
/*
|
||||||
|
* Based on ECMA-262, 5th ed., 7.8.5 & 15.10.1.
|
||||||
|
*
|
||||||
|
* For portability, we also escape escape all control and non-ASCII
|
||||||
|
* characters.
|
||||||
|
*/
|
||||||
|
return s
|
||||||
|
.replace(/\\/g, '\\\\') // backslash
|
||||||
|
.replace(/\//g, '\\/') // closing slash
|
||||||
|
.replace(/\]/g, '\\]') // closing bracket
|
||||||
|
.replace(/-/g, '\\-') // dash
|
||||||
|
.replace(/\0/g, '\\0') // null
|
||||||
|
.replace(/\t/g, '\\t') // horizontal tab
|
||||||
|
.replace(/\n/g, '\\n') // line feed
|
||||||
|
.replace(/\v/g, '\\x0B') // vertical tab
|
||||||
|
.replace(/\f/g, '\\f') // form feed
|
||||||
|
.replace(/\r/g, '\\r') // carriage return
|
||||||
|
.replace(/[\x01-\x08\x0E-\x1F\x80-\uFFFF]/g, utils.escape);
|
||||||
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Builds a node visitor -- a function which takes a node and any number of
|
||||||
|
* other parameters, calls an appropriate function according to the node type,
|
||||||
|
* passes it all its parameters and returns its value. The functions for
|
||||||
|
* various node types are passed in a parameter to |buildNodeVisitor| as a
|
||||||
|
* hash.
|
||||||
|
*/
|
||||||
|
buildNodeVisitor: function(functions) {
|
||||||
|
return function(node) {
|
||||||
|
return functions[node.type].apply(null, arguments);
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
findRuleByName: function(ast, name) {
|
||||||
|
return utils.find(ast.rules, function(r) { return r.name === name; });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = utils;
|
||||||
|
Loading…
Reference in New Issue