1
0
Fork 0
mirror of https://github.com/actions/setup-python.git synced 2024-11-26 18:22:14 +00:00
setup-python/node_modules/micromatch/lib/parsers.js
Danny McCormick 39c08a0eaa Initial pass
2019-06-26 21:12:00 -04:00

83 lines
2 KiB
JavaScript

'use strict';
var extglob = require('extglob');
var nanomatch = require('nanomatch');
var regexNot = require('regex-not');
var toRegex = require('to-regex');
var not;
/**
* Characters to use in negation regex (we want to "not" match
* characters that are matched by other parsers)
*/
var TEXT = '([!@*?+]?\\(|\\)|\\[:?(?=.*?:?\\])|:?\\]|[*+?!^$.\\\\/])+';
var createNotRegex = function(opts) {
return not || (not = textRegex(TEXT));
};
/**
* Parsers
*/
module.exports = function(snapdragon) {
var parsers = snapdragon.parser.parsers;
// register nanomatch parsers
snapdragon.use(nanomatch.parsers);
// get references to some specific nanomatch parsers before they
// are overridden by the extglob and/or parsers
var escape = parsers.escape;
var slash = parsers.slash;
var qmark = parsers.qmark;
var plus = parsers.plus;
var star = parsers.star;
var dot = parsers.dot;
// register extglob parsers
snapdragon.use(extglob.parsers);
// custom micromatch parsers
snapdragon.parser
.use(function() {
// override "notRegex" created in nanomatch parser
this.notRegex = /^\!+(?!\()/;
})
// reset the referenced parsers
.capture('escape', escape)
.capture('slash', slash)
.capture('qmark', qmark)
.capture('star', star)
.capture('plus', plus)
.capture('dot', dot)
/**
* Override `text` parser
*/
.capture('text', function() {
if (this.isInside('bracket')) return;
var pos = this.position();
var m = this.match(createNotRegex(this.options));
if (!m || !m[0]) return;
// escape regex boundary characters and simple brackets
var val = m[0].replace(/([[\]^$])/g, '\\$1');
return pos({
type: 'text',
val: val
});
});
};
/**
* Create text regex
*/
function textRegex(pattern) {
var notStr = regexNot.create(pattern, {contains: true, strictClose: false});
var prefix = '(?:[\\^]|\\\\|';
return toRegex(prefix + notStr + ')', {strictClose: false});
}