|
@ -7,20 +7,6 @@ |
|
|
'use strict'; |
|
|
'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) { |
|
|
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 index = this.find(name); |
|
|
|
|
|
var opt = options || {}; |
|
|
|
|
|
|
|
|
if (index === -1) { |
|
|
if (index === -1) { throw new Error('Parser rule not found: ' + name); } |
|
|
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].fn = fn; |
|
|
|
|
|
this.rules[index].alt = opt.alt || []; |
|
|
this.compile(); |
|
|
this.compile(); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add function to parser chain before one with given name.
|
|
|
// Add rule to chain before one with given name.
|
|
|
// Or add to start, if name not defined
|
|
|
|
|
|
//
|
|
|
//
|
|
|
Ruler.prototype.before = function (name, fn, altNames) { |
|
|
Ruler.prototype.before = function (beforeName, ruleName, fn, options) { |
|
|
var index, rule; |
|
|
var index = this.find(beforeName); |
|
|
|
|
|
var opt = options || {}; |
|
|
|
|
|
|
|
|
if (isFunction(name)) { |
|
|
if (index === -1) { throw new Error('Parser rule not found: ' + beforeName); } |
|
|
altNames = fn; |
|
|
|
|
|
fn = name; |
|
|
|
|
|
name = ''; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
rule = { |
|
|
this.rules.splice(index, 0, { |
|
|
name: functionName(fn), |
|
|
name: ruleName, |
|
|
enabled: true, |
|
|
enabled: true, |
|
|
fn: fn, |
|
|
fn: fn, |
|
|
alt: altNames || [] |
|
|
alt: opt.alt || [] |
|
|
}; |
|
|
}); |
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.compile(); |
|
|
this.compile(); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add function to parser chain after one with given name.
|
|
|
// Add rule to chain after one with given name.
|
|
|
// Or add to end, if name not defined
|
|
|
|
|
|
//
|
|
|
//
|
|
|
Ruler.prototype.after = function (name, fn, altNames) { |
|
|
Ruler.prototype.after = function (afterName, ruleName, fn, options) { |
|
|
var index, rule; |
|
|
var index = this.find(afterName); |
|
|
|
|
|
var opt = options || {}; |
|
|
|
|
|
|
|
|
if (isFunction(name)) { |
|
|
if (index === -1) { throw new Error('Parser rule not found: ' + afterName); } |
|
|
altNames = fn; |
|
|
|
|
|
fn = name; |
|
|
|
|
|
name = ''; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
rule = { |
|
|
this.rules.splice(index + 1, 0, { |
|
|
name: functionName(fn), |
|
|
name: ruleName, |
|
|
enabled: true, |
|
|
enabled: true, |
|
|
fn: fn, |
|
|
fn: fn, |
|
|
alt: altNames || [] |
|
|
alt: opt.alt || [] |
|
|
}; |
|
|
}); |
|
|
|
|
|
|
|
|
if (!name) { |
|
|
this.compile(); |
|
|
this.rules.push(rule); |
|
|
}; |
|
|
} else { |
|
|
|
|
|
index = this.find(name); |
|
|
// Add rule to the end of chain.
|
|
|
if (index === -1) { |
|
|
//
|
|
|
throw new Error('Parser rule not found: ' + name); |
|
|
Ruler.prototype.push = function (ruleName, fn, options) { |
|
|
} |
|
|
var opt = options || {}; |
|
|
this.rules.splice(index + 1, 0, rule); |
|
|
|
|
|
} |
|
|
this.rules.push({ |
|
|
|
|
|
name: ruleName, |
|
|
|
|
|
enabled: true, |
|
|
|
|
|
fn: fn, |
|
|
|
|
|
alt: opt.alt || [] |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
this.compile(); |
|
|
this.compile(); |
|
|
}; |
|
|
}; |
|
|