3 var regexNot = require('regex-not');
4 var toRegex = require('to-regex');
7 * Characters to use in negation regex (we want to "not" match
8 * characters that are matched by other parsers)
12 var NOT_REGEX = '[\\[!*+?$^"\'.\\\\/]+';
13 var not = createTextRegex(NOT_REGEX);
19 module.exports = function(nanomatch, options) {
20 var parser = nanomatch.parser;
21 var opts = parser.options;
28 parser.ast.state = parser.state;
35 .capture('prefix', function() {
36 if (this.parsed) return;
37 var m = this.match(/^\.[\\/]/);
39 this.state.strictOpen = !!this.options.strictOpen;
40 this.state.addPrefix = true;
47 .capture('escape', function() {
48 if (this.isInside('bracket')) return;
49 var pos = this.position();
50 var m = this.match(/^(?:\\(.)|([$^]))/);
63 .capture('quoted', function() {
64 var pos = this.position();
65 var m = this.match(/^["']/);
69 if (this.input.indexOf(quote) === -1) {
76 var tok = advanceTo(this.input, quote);
77 this.consume(tok.len);
89 .capture('not', function() {
90 var parsed = this.parsed;
91 var pos = this.position();
92 var m = this.match(this.notRegex || /^!+/);
96 var isNegated = (val.length % 2) === 1;
97 if (parsed === '' && !isNegated) {
101 // if nothing has been parsed, we know `!` is at the start,
102 // so we need to wrap the result in a negation regex
103 if (parsed === '' && isNegated && this.options.nonegate !== true) {
104 this.bos.val = '(?!^(?:';
105 this.append = ')$).*';
118 .capture('dot', function() {
119 var parsed = this.parsed;
120 var pos = this.position();
121 var m = this.match(/^\.+/);
125 this.state.dot = val === '.' && (parsed === '' || parsed.slice(-1) === '/');
129 dotfiles: this.state.dot,
138 .capture('plus', /^\+(?!\()/)
144 .capture('qmark', function() {
145 var parsed = this.parsed;
146 var pos = this.position();
147 var m = this.match(/^\?+(?!\()/);
150 this.state.metachar = true;
151 this.state.qmark = true;
164 .capture('globstar', function() {
165 var parsed = this.parsed;
166 var pos = this.position();
167 var m = this.match(/^\*{2}(?![*(])(?=[,)/]|$)/);
170 var type = opts.noglobstar !== true ? 'globstar' : 'star';
171 var node = pos({type: type, parsed: parsed});
172 this.state.metachar = true;
174 while (this.input.slice(0, 4) === '/**/') {
175 this.input = this.input.slice(3);
179 brace: this.isInside('brace'),
180 paren: this.isInside('paren')
183 if (type === 'globstar') {
184 this.state.globstar = true;
188 this.state.star = true;
199 .capture('star', function() {
200 var pos = this.position();
201 var starRe = /^(?:\*(?![*(])|[*]{3,}(?!\()|[*]{2}(?![(/]|$)|\*(?=\*\())/;
202 var m = this.match(starRe);
205 this.state.metachar = true;
206 this.state.star = true;
217 .capture('slash', function() {
218 var pos = this.position();
219 var m = this.match(/^\//);
222 this.state.slashes++;
233 .capture('backslash', function() {
234 var pos = this.position();
235 var m = this.match(/^\\(?![*+?(){}[\]'"])/);
240 if (this.isInside('bracket')) {
242 } else if (val.length > 1) {
256 .capture('square', function() {
257 if (this.isInside('bracket')) return;
258 var pos = this.position();
259 var m = this.match(/^\[([^!^\\])\]/);
269 * Brackets: "[...]" (basic, this can be overridden by other parsers)
272 .capture('bracket', function() {
273 var pos = this.position();
274 var m = this.match(/^(?:\[([!^]?)([^\]]+|\]-)(\]|[^*+?]+)|\[)/);
278 var negated = m[1] ? '^' : '';
279 var inner = (m[2] || '').replace(/\\\\+/, '\\\\');
280 var close = m[3] || '';
282 if (m[2] && inner.length < m[2].length) {
283 val = val.replace(/\\\\+/, '\\\\');
286 var esc = this.input.slice(0, 2);
287 if (inner === '' && esc === '\\]') {
291 var str = this.input;
295 while ((ch = str[++idx])) {
308 escaped: close !== ']',
319 .capture('text', function() {
320 if (this.isInside('bracket')) return;
321 var pos = this.position();
322 var m = this.match(not);
323 if (!m || !m[0]) return;
332 * Allow custom parsers to be passed on options
335 if (options && typeof options.parsers === 'function') {
336 options.parsers(nanomatch.parser);
341 * Advance to the next non-escaped character
344 function advanceTo(input, endChar) {
345 var ch = input.charAt(0);
346 var tok = { len: 1, val: '', esc: '' };
351 tok.esc += '\\' + ch;
355 ch = input.charAt(++idx);
364 while (ch && ch !== endChar) {
374 function createTextRegex(pattern) {
375 if (cached) return cached;
376 var opts = {contains: true, strictClose: false};
377 var not = regexNot.create(pattern, opts);
378 var re = toRegex('^(?:[*]\\((?=.)|' + not + ')', opts);
379 return (cached = re);
383 * Expose negation string
386 module.exports.not = NOT_REGEX;