1
|
var parse = require('ret');
|
2
|
var types = parse.types;
|
3
|
|
4
|
module.exports = function (re, opts) {
|
5
|
if (!opts) opts = {};
|
6
|
var replimit = opts.limit === undefined ? 25 : opts.limit;
|
7
|
|
8
|
if (isRegExp(re)) re = re.source;
|
9
|
else if (typeof re !== 'string') re = String(re);
|
10
|
|
11
|
try { re = parse(re) }
|
12
|
catch (err) { return false }
|
13
|
|
14
|
var reps = 0;
|
15
|
return (function walk (node, starHeight) {
|
16
|
if (node.type === types.REPETITION) {
|
17
|
starHeight ++;
|
18
|
reps ++;
|
19
|
if (starHeight > 1) return false;
|
20
|
if (reps > replimit) return false;
|
21
|
}
|
22
|
|
23
|
if (node.options) {
|
24
|
for (var i = 0, len = node.options.length; i < len; i++) {
|
25
|
var ok = walk({ stack: node.options[i] }, starHeight);
|
26
|
if (!ok) return false;
|
27
|
}
|
28
|
}
|
29
|
var stack = node.stack || (node.value && node.value.stack);
|
30
|
if (!stack) return true;
|
31
|
|
32
|
for (var i = 0; i < stack.length; i++) {
|
33
|
var ok = walk(stack[i], starHeight);
|
34
|
if (!ok) return false;
|
35
|
}
|
36
|
|
37
|
return true;
|
38
|
})(re, 0);
|
39
|
};
|
40
|
|
41
|
function isRegExp (x) {
|
42
|
return {}.toString.call(x) === '[object RegExp]';
|
43
|
}
|