globToRegExp.js
3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
function globToRegExp(glob) {
// * [^\\\/]*
// /**/ /.+/
// ^* \./.+ (concord special)
// ? [^\\\/]
// [!...] [^...]
// [^...] [^...]
// / [\\\/]
// {...,...} (...|...)
// ?(...|...) (...|...)?
// +(...|...) (...|...)+
// *(...|...) (...|...)*
// @(...|...) (...|...)
if(/^\(.+\)$/.test(glob)) {
// allow to pass an RegExp in brackets
return new RegExp(glob.substr(1, glob.length - 2));
}
var tokens = tokenize(glob);
var process = createRoot();
var regExpStr = tokens.map(process).join("");
return new RegExp("^" + regExpStr + "$");
}
var SIMPLE_TOKENS = {
"@(": "one",
"?(": "zero-one",
"+(": "one-many",
"*(": "zero-many",
"|": "segment-sep",
"/**/": "any-path-segments",
"**": "any-path",
"*": "any-path-segment",
"?": "any-char",
"{": "or",
"/": "path-sep",
",": "comma",
")": "closing-segment",
"}": "closing-or"
};
function tokenize(glob) {
return glob.split(/([@?+*]\(|\/\*\*\/|\*\*|[?*]|\[[\!\^]?(?:[^\]\\]|\\.)+\]|\{|,|\/|[|)}])/g).map(function(item) {
if(!item)
return null;
var t = SIMPLE_TOKENS[item];
if(t) {
return {
type: t
};
}
if(item[0] === "[") {
if(item[1] === "^" || item[1] === "!") {
return {
type: "inverted-char-set",
value: item.substr(2, item.length - 3)
};
} else {
return {
type: "char-set",
value: item.substr(1, item.length - 2)
};
}
}
return {
type: "string",
value: item
};
}).filter(Boolean).concat({
type: "end"
});
}
function createRoot() {
var inOr = [];
var process = createSeqment();
var initial = true;
return function(token) {
switch(token.type) {
case "or":
inOr.push(initial);
return "(";
case "comma":
if(inOr.length) {
initial = inOr[inOr.length - 1];
return "|";
} else {
return process({
type: "string",
value: ","
}, initial);
}
case "closing-or":
if(inOr.length === 0)
throw new Error("Unmatched '}'");
inOr.pop();
return ")";
case "end":
if(inOr.length)
throw new Error("Unmatched '{'");
return process(token, initial);
default:
var result = process(token, initial);
initial = false;
return result;
}
};
}
function createSeqment() {
var inSeqment = [];
var process = createSimple();
return function(token, initial) {
switch(token.type) {
case "one":
case "one-many":
case "zero-many":
case "zero-one":
inSeqment.push(token.type);
return "(";
case "segment-sep":
if(inSeqment.length) {
return "|";
} else {
return process({
type: "string",
value: "|"
}, initial);
}
case "closing-segment":
var segment = inSeqment.pop();
switch(segment) {
case "one":
return ")";
case "one-many":
return ")+";
case "zero-many":
return ")*";
case "zero-one":
return ")?";
}
throw new Error("Unexcepted segment " + segment);
case "end":
if(inSeqment.length > 0) {
throw new Error("Unmatched segment, missing ')'");
}
return process(token, initial);
default:
return process(token, initial);
}
};
}
function createSimple() {
return function(token, initial) {
switch(token.type) {
case "path-sep":
return "[\\\\/]+";
case "any-path-segments":
return "[\\\\/]+(?:(.+)[\\\\/]+)?";
case "any-path":
return "(.*)";
case "any-path-segment":
if(initial) {
return "\\.[\\\\/]+(?:.*[\\\\/]+)?([^\\\\/]+)";
} else {
return "([^\\\\/]*)";
}
case "any-char":
return "[^\\\\/]";
case "inverted-char-set":
return "[^" + token.value + "]";
case "char-set":
return "[" + token.value + "]";
case "string":
return token.value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
case "end":
return "";
default:
throw new Error("Unsupported token '" + token.type + "'");
}
};
}
exports.globToRegExp = globToRegExp;