2011-08-22 19:48:43 +02:00
|
|
|
/* Like Python's |range|, but without |step|. */
|
|
|
|
function range(start, stop) {
|
|
|
|
if (stop === undefined) {
|
|
|
|
stop = start;
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = new Array(Math.max(0, stop - start));
|
|
|
|
for (var i = 0, j = start; j < stop; i++, j++) {
|
|
|
|
result[i] = j;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-03-04 17:29:32 +01:00
|
|
|
function find(array, callback) {
|
|
|
|
var length = array.length;
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
if (callback(array[i])) {
|
|
|
|
return array[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-16 21:20:37 +02:00
|
|
|
function contains(array, value) {
|
2010-07-25 17:54:09 +02:00
|
|
|
/*
|
2010-08-16 21:20:37 +02:00
|
|
|
* Stupid IE does not have Array.prototype.indexOf, otherwise this function
|
|
|
|
* would be a one-liner.
|
2010-07-25 17:54:09 +02:00
|
|
|
*/
|
2010-08-16 21:20:37 +02:00
|
|
|
var length = array.length;
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
if (array[i] === value) {
|
|
|
|
return true;
|
2010-07-25 17:54:09 +02:00
|
|
|
}
|
|
|
|
}
|
2010-08-16 21:20:37 +02:00
|
|
|
return false;
|
|
|
|
}
|
2010-07-25 17:54:09 +02:00
|
|
|
|
2010-08-16 21:20:37 +02:00
|
|
|
function each(array, callback) {
|
|
|
|
var length = array.length;
|
|
|
|
for (var i = 0; i < length; i++) {
|
2011-08-22 19:48:43 +02:00
|
|
|
callback(array[i], i);
|
2010-07-25 17:54:09 +02:00
|
|
|
}
|
2010-08-16 21:20:37 +02:00
|
|
|
}
|
2010-07-25 17:54:09 +02:00
|
|
|
|
2010-08-16 21:20:37 +02:00
|
|
|
function map(array, callback) {
|
|
|
|
var result = [];
|
|
|
|
var length = array.length;
|
|
|
|
for (var i = 0; i < length; i++) {
|
2011-08-22 19:48:43 +02:00
|
|
|
result[i] = callback(array[i], i);
|
2010-08-16 21:20:37 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2010-07-25 17:54:09 +02:00
|
|
|
|
2012-02-27 20:10:02 +01:00
|
|
|
function pluck(array, key) {
|
|
|
|
return map(array, function (e) { return e[key]; });
|
|
|
|
}
|
|
|
|
|
2012-02-27 20:18:03 +01:00
|
|
|
function keys(object) {
|
|
|
|
var result = [];
|
|
|
|
for (var key in object) {
|
|
|
|
result.push(key);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function values(object) {
|
|
|
|
var result = [];
|
|
|
|
for (var key in object) {
|
|
|
|
result.push(object[key]);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-10-21 12:29:38 +02:00
|
|
|
function clone(object) {
|
|
|
|
var result = {};
|
|
|
|
for (var key in object) {
|
|
|
|
result[key] = object[key];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function defaults(object, defaults) {
|
|
|
|
for (var key in defaults) {
|
|
|
|
if (object[key] === undefined) {
|
|
|
|
object[key] = defaults[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-28 19:17:37 +01:00
|
|
|
/*
|
|
|
|
* The code needs to be in sync with the code template in the compilation
|
|
|
|
* function for "action" nodes.
|
|
|
|
*/
|
2012-10-28 19:06:47 +01:00
|
|
|
function subclass(child, parent) {
|
|
|
|
function ctor() { this.constructor = child; }
|
|
|
|
ctor.prototype = parent.prototype;
|
|
|
|
child.prototype = new ctor();
|
|
|
|
}
|
|
|
|
|
2010-11-20 16:58:47 +01:00
|
|
|
/*
|
|
|
|
* Returns a string padded on the left to a desired length with a character.
|
|
|
|
*
|
2011-09-18 20:06:52 +02:00
|
|
|
* The code needs to be in sync with the code template in the compilation
|
2010-11-20 16:58:47 +01:00
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns an escape sequence for given character. Uses \x for characters <=
|
|
|
|
* 0xFF to save space, \u for the rest.
|
|
|
|
*
|
2011-09-18 20:06:52 +02:00
|
|
|
* The code needs to be in sync with the code template in the compilation
|
2010-11-20 16:58:47 +01:00
|
|
|
* function for "action" nodes.
|
|
|
|
*/
|
|
|
|
function escape(ch) {
|
|
|
|
var charCode = ch.charCodeAt(0);
|
2011-09-13 18:53:27 +02:00
|
|
|
var escapeChar;
|
|
|
|
var length;
|
2010-11-20 16:58:47 +01:00
|
|
|
|
2011-01-26 14:10:00 +01:00
|
|
|
if (charCode <= 0xFF) {
|
2011-09-13 18:53:27 +02:00
|
|
|
escapeChar = 'x';
|
|
|
|
length = 2;
|
2010-11-20 16:58:47 +01:00
|
|
|
} else {
|
2011-09-13 18:53:27 +02:00
|
|
|
escapeChar = 'u';
|
|
|
|
length = 4;
|
2010-11-20 16:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return '\\' + escapeChar + padLeft(charCode.toString(16).toUpperCase(), '0', length);
|
|
|
|
}
|
|
|
|
|
2010-08-16 21:20:37 +02:00
|
|
|
/*
|
|
|
|
* Surrounds the string with quotes and escapes characters inside so that the
|
|
|
|
* result is a valid JavaScript string.
|
|
|
|
*
|
2011-09-18 20:06:52 +02:00
|
|
|
* The code needs to be in sync with the code template in the compilation
|
2010-08-16 21:20:37 +02:00
|
|
|
* function for "action" nodes.
|
|
|
|
*/
|
|
|
|
function quote(s) {
|
2010-07-25 17:54:09 +02:00
|
|
|
/*
|
2010-08-16 21:20:37 +02:00
|
|
|
* ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a string
|
|
|
|
* literal except for the closing quote character, backslash, carriage return,
|
|
|
|
* line separator, paragraph separator, and line feed. Any character may
|
|
|
|
* appear in the form of an escape sequence.
|
2010-11-20 16:58:47 +01:00
|
|
|
*
|
2011-09-14 11:13:55 +02:00
|
|
|
* For portability, we also escape escape all control and non-ASCII
|
|
|
|
* characters. Note that "\0" and "\v" escape sequences are not used because
|
|
|
|
* JSHint does not like the first and IE the second.
|
2010-07-25 17:54:09 +02:00
|
|
|
*/
|
2010-08-16 21:20:37 +02:00
|
|
|
return '"' + s
|
2011-09-14 11:13:55 +02:00
|
|
|
.replace(/\\/g, '\\\\') // backslash
|
|
|
|
.replace(/"/g, '\\"') // closing quote character
|
|
|
|
.replace(/\x08/g, '\\b') // backspace
|
|
|
|
.replace(/\t/g, '\\t') // horizontal tab
|
|
|
|
.replace(/\n/g, '\\n') // line feed
|
|
|
|
.replace(/\f/g, '\\f') // form feed
|
|
|
|
.replace(/\r/g, '\\r') // carriage return
|
|
|
|
.replace(/[\x00-\x07\x0B\x0E-\x1F\x80-\uFFFF]/g, escape)
|
2010-08-16 21:20:37 +02:00
|
|
|
+ '"';
|
2011-09-13 18:59:12 +02:00
|
|
|
}
|
2010-08-16 21:20:37 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 quoteForRegexpClass(s) {
|
2010-11-20 16:58:47 +01:00
|
|
|
/*
|
|
|
|
* Based on ECMA-262, 5th ed., 7.8.5 & 15.10.1.
|
|
|
|
*
|
2011-09-14 11:35:40 +02:00
|
|
|
* For portability, we also escape escape all control and non-ASCII
|
|
|
|
* characters.
|
2010-11-20 16:58:47 +01:00
|
|
|
*/
|
2010-08-16 21:20:37 +02:00
|
|
|
return s
|
2011-09-14 11:35:40 +02:00
|
|
|
.replace(/\\/g, '\\\\') // backslash
|
|
|
|
.replace(/\//g, '\\/') // closing slash
|
|
|
|
.replace(/\]/g, '\\]') // closing bracket
|
|
|
|
.replace(/-/g, '\\-') // dash
|
2012-02-13 08:13:01 +01:00
|
|
|
.replace(/\0/g, '\\0') // null
|
2011-09-14 11:35:40 +02:00
|
|
|
.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, escape);
|
2010-08-16 21:20:37 +02:00
|
|
|
}
|
2010-08-17 20:34:14 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
function buildNodeVisitor(functions) {
|
|
|
|
return function(node) {
|
2010-08-21 11:54:35 +02:00
|
|
|
return functions[node.type].apply(null, arguments);
|
2011-09-13 18:59:12 +02:00
|
|
|
};
|
2010-08-17 20:34:14 +02:00
|
|
|
}
|
2012-03-04 17:29:32 +01:00
|
|
|
|
|
|
|
function findRuleByName(ast, name) {
|
|
|
|
return find(ast.rules, function(r) { return r.name === name; });
|
|
|
|
}
|