|
|
@ -7,20 +7,6 @@ |
|
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// helpers
|
|
|
|
|
|
|
|
function _class(obj) { return Object.prototype.toString.call(obj); } |
|
|
|
function isFunction(obj) { return _class(obj) === '[object Function]'; } |
|
|
|
|
|
|
|
function functionName(fn) { |
|
|
|
var ret = fn.toString(); |
|
|
|
ret = ret.substr('function '.length); |
|
|
|
ret = ret.substr(0, ret.indexOf('(')); |
|
|
|
return ret; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function Ruler(compileFn) { |
|
|
@ -51,89 +37,68 @@ Ruler.prototype.find = function (name) { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// Replace/delete parser function
|
|
|
|
// Replace rule function
|
|
|
|
//
|
|
|
|
Ruler.prototype.at = function (name, fn, altNames) { |
|
|
|
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); |
|
|
|
} |
|
|
|
if (index === -1) { throw new Error('Parser rule not found: ' + name); } |
|
|
|
|
|
|
|
if (isFunction(fn)) { |
|
|
|
this.rules[index].fn = fn; |
|
|
|
if (altNames) { |
|
|
|
this.rules[index].alt = altNames; |
|
|
|
} |
|
|
|
} else { |
|
|
|
this.rules = this.rules.slice(0, index).concat(this.rules.slice(index + 1)); |
|
|
|
} |
|
|
|
|
|
|
|
this.rules[index].alt = opt.alt || []; |
|
|
|
this.compile(); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// Add function to parser chain before one with given name.
|
|
|
|
// Or add to start, if name not defined
|
|
|
|
// Add rule to chain before one with given name.
|
|
|
|
//
|
|
|
|
Ruler.prototype.before = function (name, fn, altNames) { |
|
|
|
var index, rule; |
|
|
|
Ruler.prototype.before = function (beforeName, ruleName, fn, options) { |
|
|
|
var index = this.find(beforeName); |
|
|
|
var opt = options || {}; |
|
|
|
|
|
|
|
if (isFunction(name)) { |
|
|
|
altNames = fn; |
|
|
|
fn = name; |
|
|
|
name = ''; |
|
|
|
} |
|
|
|
if (index === -1) { throw new Error('Parser rule not found: ' + beforeName); } |
|
|
|
|
|
|
|
rule = { |
|
|
|
name: functionName(fn), |
|
|
|
this.rules.splice(index, 0, { |
|
|
|
name: ruleName, |
|
|
|
enabled: true, |
|
|
|
fn: fn, |
|
|
|
alt: altNames || [] |
|
|
|
}; |
|
|
|
|
|
|
|
if (!name) { |
|
|
|
this.rules.unshift(rule); |
|
|
|
} else { |
|
|
|
index = this.find(name); |
|
|
|
if (index === -1) { |
|
|
|
throw new Error('Parser rule not found: ' + name); |
|
|
|
} |
|
|
|
this.rules.splice(index, 0, rule); |
|
|
|
} |
|
|
|
alt: opt.alt || [] |
|
|
|
}); |
|
|
|
|
|
|
|
this.compile(); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// Add function to parser chain after one with given name.
|
|
|
|
// Or add to end, if name not defined
|
|
|
|
// Add rule to chain after one with given name.
|
|
|
|
//
|
|
|
|
Ruler.prototype.after = function (name, fn, altNames) { |
|
|
|
var index, rule; |
|
|
|
Ruler.prototype.after = function (afterName, ruleName, fn, options) { |
|
|
|
var index = this.find(afterName); |
|
|
|
var opt = options || {}; |
|
|
|
|
|
|
|
if (isFunction(name)) { |
|
|
|
altNames = fn; |
|
|
|
fn = name; |
|
|
|
name = ''; |
|
|
|
} |
|
|
|
if (index === -1) { throw new Error('Parser rule not found: ' + afterName); } |
|
|
|
|
|
|
|
rule = { |
|
|
|
name: functionName(fn), |
|
|
|
this.rules.splice(index + 1, 0, { |
|
|
|
name: ruleName, |
|
|
|
enabled: true, |
|
|
|
fn: fn, |
|
|
|
alt: altNames || [] |
|
|
|
alt: opt.alt || [] |
|
|
|
}); |
|
|
|
|
|
|
|
this.compile(); |
|
|
|
}; |
|
|
|
|
|
|
|
if (!name) { |
|
|
|
this.rules.push(rule); |
|
|
|
} else { |
|
|
|
index = this.find(name); |
|
|
|
if (index === -1) { |
|
|
|
throw new Error('Parser rule not found: ' + name); |
|
|
|
} |
|
|
|
this.rules.splice(index + 1, 0, rule); |
|
|
|
} |
|
|
|
// 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.compile(); |
|
|
|
}; |
|
|
|