123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- 'use strict';
- function Ruler() {
-
-
-
-
-
-
-
-
-
- this.__rules__ = [];
-
-
-
-
-
- this.__cache__ = null;
- }
- Ruler.prototype.__find__ = function (name) {
- for (var i = 0; i < this.__rules__.length; i++) {
- if (this.__rules__[i].name === name) {
- return i;
- }
- }
- return -1;
- };
- Ruler.prototype.__compile__ = function () {
- var self = this;
- var chains = [ '' ];
-
- 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);
- });
- });
- };
- 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;
- };
- 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;
- };
- 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;
- };
- 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;
- };
- Ruler.prototype.enable = function (list, ignoreInvalid) {
- if (!Array.isArray(list)) { list = [ list ]; }
- var result = [];
-
- list.forEach(function (name) {
- var idx = this.__find__(name);
- if (idx < 0) {
- if (ignoreInvalid) { return; }
- throw new Error('Rules manager: invalid rule name ' + name);
- }
- this.__rules__[idx].enabled = true;
- result.push(name);
- }, this);
- this.__cache__ = null;
- return result;
- };
- Ruler.prototype.enableOnly = function (list, ignoreInvalid) {
- if (!Array.isArray(list)) { list = [ list ]; }
- this.__rules__.forEach(function (rule) { rule.enabled = false; });
- this.enable(list, ignoreInvalid);
- };
- Ruler.prototype.disable = function (list, ignoreInvalid) {
- if (!Array.isArray(list)) { list = [ list ]; }
- var result = [];
-
- list.forEach(function (name) {
- var idx = this.__find__(name);
- if (idx < 0) {
- if (ignoreInvalid) { return; }
- throw new Error('Rules manager: invalid rule name ' + name);
- }
- this.__rules__[idx].enabled = false;
- result.push(name);
- }, this);
- this.__cache__ = null;
- return result;
- };
- Ruler.prototype.getRules = function (chainName) {
- if (this.__cache__ === null) {
- this.__compile__();
- }
-
- return this.__cache__[chainName] || [];
- };
- module.exports = Ruler;
|