|
|
|
// Ruler is helper class to build responsibility chains from parse rules.
|
|
|
|
// It allows:
|
|
|
|
//
|
|
|
|
// - easy stack rules chains
|
|
|
|
// - getting main chain and named chains content (as arrays of functions)
|
|
|
|
//
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function Ruler() {
|
|
|
|
// List of added rules. Each element is:
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// name: XXX,
|
|
|
|
// enabled: Boolean,
|
|
|
|
// fn: Function(),
|
|
|
|
// alt: [ name2, name3 ]
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
this.__rules__ = [];
|
|
|
|
|
|
|
|
// Cached rule chains.
|
|
|
|
//
|
|
|
|
// First level - chain name, '' for default.
|
|
|
|
// Second level - diginal anchor for fast filtering by charcodes.
|
|
|
|
//
|
|
|
|
this.__cache__ = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Helper methods, should not be used directly
|
|
|
|
|
|
|
|
|
|
|
|
// Find rule index by name
|
|
|
|
//
|
|
|
|
Ruler.prototype.__find__ = function (name) {
|
|
|
|
for (var i = 0; i < this.__rules__.length; i++) {
|
|
|
|
if (this.__rules__[i].name === name) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Build rules lookup cache
|
|
|
|
//
|
|
|
|
Ruler.prototype.__compile__ = function () {
|
|
|
|
var self = this;
|
|
|
|
var chains = [ '' ];
|
|
|
|
|
|
|
|
// collect unique names
|
|
|
|
self.__rules__.forEach(function (rule) {
|
|
|
|
if (!rule.enabled) { return; }
|
|
|
|
|
|
|
|
rule.alt.forEach(function (altName) {
|
|
|
|
if (chains.indexOf(altName) < 0) {
|
|
|
|
chains.push(altName);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
self.__cache__ = {};
|
|
|
|
|
|
|
|
chains.forEach(function (chain) {
|
|
|
|
self.__cache__[chain] = [];
|
|
|
|
self.__rules__.forEach(function (rule) {
|
|
|
|
if (!rule.enabled) { return; }
|
|
|
|
|
|
|
|
if (chain && rule.alt.indexOf(chain) < 0) { return; }
|
|
|
|
|
|
|
|
self.__cache__[chain].push(rule.fn);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Public methods
|
|
|
|
|
|
|
|
|
|
|
|
// Replace rule function
|
|
|
|
//
|
|
|
|
Ruler.prototype.at = function (name, fn, options) {
|
|
|
|
var index = this.__find__(name);
|
|
|
|
var opt = options || {};
|
|
|
|
|
|
|
|
if (index === -1) { throw new Error('Parser rule not found: ' + name); }
|
|
|
|
|
|
|
|
this.__rules__[index].fn = fn;
|
|
|
|
this.__rules__[index].alt = opt.alt || [];
|
|
|
|
this.__cache__ = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Add rule to chain before one with given name.
|
|
|
|
//
|
|
|
|
Ruler.prototype.before = function (beforeName, ruleName, fn, options) {
|
|
|
|
var index = this.__find__(beforeName);
|
|
|
|
var opt = options || {};
|
|
|
|
|
|
|
|
if (index === -1) { throw new Error('Parser rule not found: ' + beforeName); }
|
|
|
|
|
|
|
|
this.__rules__.splice(index, 0, {
|
|
|
|
name: ruleName,
|
|
|
|
enabled: true,
|
|
|
|
fn: fn,
|
|
|
|
alt: opt.alt || []
|
|
|
|
});
|
|
|
|
|
|
|
|
this.__cache__ = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Add rule to chain after one with given name.
|
|
|
|
//
|
|
|
|
Ruler.prototype.after = function (afterName, ruleName, fn, options) {
|
|
|
|
var index = this.__find__(afterName);
|
|
|
|
var opt = options || {};
|
|
|
|
|
|
|
|
if (index === -1) { throw new Error('Parser rule not found: ' + afterName); }
|
|
|
|
|
|
|
|
this.__rules__.splice(index + 1, 0, {
|
|
|
|
name: ruleName,
|
|
|
|
enabled: true,
|
|
|
|
fn: fn,
|
|
|
|
alt: opt.alt || []
|
|
|
|
});
|
|
|
|
|
|
|
|
this.__cache__ = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add rule to the end of chain.
|
|
|
|
//
|
|
|
|
Ruler.prototype.push = function (ruleName, fn, options) {
|
|
|
|
var opt = options || {};
|
|
|
|
|
|
|
|
this.__rules__.push({
|
|
|
|
name: ruleName,
|
|
|
|
enabled: true,
|
|
|
|
fn: fn,
|
|
|
|
alt: opt.alt || []
|
|
|
|
});
|
|
|
|
|
|
|
|
this.__cache__ = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Enable list of rules by names. If `strict` is true, then all non listed
|
|
|
|
// rules will be disabled.
|
|
|
|
//
|
|
|
|
Ruler.prototype.enable = function (list, strict) {
|
|
|
|
if (!Array.isArray(list)) {
|
|
|
|
list = [ list ];
|
|
|
|
}
|
|
|
|
|
|
|
|
// In strict mode disable all existing rules first
|
|
|
|
if (strict) {
|
|
|
|
this.__rules__.forEach(function (rule) {
|
|
|
|
rule.enabled = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search by name and enable
|
|
|
|
list.forEach(function (name) {
|
|
|
|
var idx = this.__find__(name);
|
|
|
|
|
|
|
|
if (idx < 0) { throw new Error('Rules manager: invalid rule name ' + name); }
|
|
|
|
this.__rules__[idx].enabled = true;
|
|
|
|
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
this.__cache__ = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Disable list of rules by names.
|
|
|
|
//
|
|
|
|
Ruler.prototype.disable = function (list) {
|
|
|
|
if (!Array.isArray(list)) {
|
|
|
|
list = [ list ];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search by name and disable
|
|
|
|
list.forEach(function (name) {
|
|
|
|
var idx = this.__find__(name);
|
|
|
|
|
|
|
|
if (idx < 0) { throw new Error('Rules manager: invalid rule name ' + name); }
|
|
|
|
this.__rules__[idx].enabled = false;
|
|
|
|
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
this.__cache__ = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Get rules list as array of functions.
|
|
|
|
//
|
|
|
|
Ruler.prototype.getRules = function (chainName) {
|
|
|
|
if (this.__cache__ === null) {
|
|
|
|
this.__compile__();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.__cache__[chainName];
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Ruler;
|