您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

parsers.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. 'use strict';
  2. var regexNot = require('regex-not');
  3. var toRegex = require('to-regex');
  4. /**
  5. * Characters to use in negation regex (we want to "not" match
  6. * characters that are matched by other parsers)
  7. */
  8. var cached;
  9. var NOT_REGEX = '[\\[!*+?$^"\'.\\\\/]+';
  10. var not = createTextRegex(NOT_REGEX);
  11. /**
  12. * Nanomatch parsers
  13. */
  14. module.exports = function(nanomatch, options) {
  15. var parser = nanomatch.parser;
  16. var opts = parser.options;
  17. parser.state = {
  18. slashes: 0,
  19. paths: []
  20. };
  21. parser.ast.state = parser.state;
  22. parser
  23. /**
  24. * Beginning-of-string
  25. */
  26. .capture('prefix', function() {
  27. if (this.parsed) return;
  28. var m = this.match(/^\.[\\/]/);
  29. if (!m) return;
  30. this.state.strictOpen = !!this.options.strictOpen;
  31. this.state.addPrefix = true;
  32. })
  33. /**
  34. * Escape: "\\."
  35. */
  36. .capture('escape', function() {
  37. if (this.isInside('bracket')) return;
  38. var pos = this.position();
  39. var m = this.match(/^(?:\\(.)|([$^]))/);
  40. if (!m) return;
  41. return pos({
  42. type: 'escape',
  43. val: m[2] || m[1]
  44. });
  45. })
  46. /**
  47. * Quoted strings
  48. */
  49. .capture('quoted', function() {
  50. var pos = this.position();
  51. var m = this.match(/^["']/);
  52. if (!m) return;
  53. var quote = m[0];
  54. if (this.input.indexOf(quote) === -1) {
  55. return pos({
  56. type: 'escape',
  57. val: quote
  58. });
  59. }
  60. var tok = advanceTo(this.input, quote);
  61. this.consume(tok.len);
  62. return pos({
  63. type: 'quoted',
  64. val: tok.esc
  65. });
  66. })
  67. /**
  68. * Negations: "!"
  69. */
  70. .capture('not', function() {
  71. var parsed = this.parsed;
  72. var pos = this.position();
  73. var m = this.match(this.notRegex || /^!+/);
  74. if (!m) return;
  75. var val = m[0];
  76. var isNegated = (val.length % 2) === 1;
  77. if (parsed === '' && !isNegated) {
  78. val = '';
  79. }
  80. // if nothing has been parsed, we know `!` is at the start,
  81. // so we need to wrap the result in a negation regex
  82. if (parsed === '' && isNegated && this.options.nonegate !== true) {
  83. this.bos.val = '(?!^(?:';
  84. this.append = ')$).*';
  85. val = '';
  86. }
  87. return pos({
  88. type: 'not',
  89. val: val
  90. });
  91. })
  92. /**
  93. * Dot: "."
  94. */
  95. .capture('dot', function() {
  96. var parsed = this.parsed;
  97. var pos = this.position();
  98. var m = this.match(/^\.+/);
  99. if (!m) return;
  100. var val = m[0];
  101. this.state.dot = val === '.' && (parsed === '' || parsed.slice(-1) === '/');
  102. return pos({
  103. type: 'dot',
  104. dotfiles: this.state.dot,
  105. val: val
  106. });
  107. })
  108. /**
  109. * Plus: "+"
  110. */
  111. .capture('plus', /^\+(?!\()/)
  112. /**
  113. * Question mark: "?"
  114. */
  115. .capture('qmark', function() {
  116. var parsed = this.parsed;
  117. var pos = this.position();
  118. var m = this.match(/^\?+(?!\()/);
  119. if (!m) return;
  120. this.state.metachar = true;
  121. this.state.qmark = true;
  122. return pos({
  123. type: 'qmark',
  124. parsed: parsed,
  125. val: m[0]
  126. });
  127. })
  128. /**
  129. * Globstar: "**"
  130. */
  131. .capture('globstar', function() {
  132. var parsed = this.parsed;
  133. var pos = this.position();
  134. var m = this.match(/^\*{2}(?![*(])(?=[,)/]|$)/);
  135. if (!m) return;
  136. var type = opts.noglobstar !== true ? 'globstar' : 'star';
  137. var node = pos({type: type, parsed: parsed});
  138. this.state.metachar = true;
  139. while (this.input.slice(0, 4) === '/**/') {
  140. this.input = this.input.slice(3);
  141. }
  142. node.isInside = {
  143. brace: this.isInside('brace'),
  144. paren: this.isInside('paren')
  145. };
  146. if (type === 'globstar') {
  147. this.state.globstar = true;
  148. node.val = '**';
  149. } else {
  150. this.state.star = true;
  151. node.val = '*';
  152. }
  153. return node;
  154. })
  155. /**
  156. * Star: "*"
  157. */
  158. .capture('star', function() {
  159. var pos = this.position();
  160. var starRe = /^(?:\*(?![*(])|[*]{3,}(?!\()|[*]{2}(?![(/]|$)|\*(?=\*\())/;
  161. var m = this.match(starRe);
  162. if (!m) return;
  163. this.state.metachar = true;
  164. this.state.star = true;
  165. return pos({
  166. type: 'star',
  167. val: m[0]
  168. });
  169. })
  170. /**
  171. * Slash: "/"
  172. */
  173. .capture('slash', function() {
  174. var pos = this.position();
  175. var m = this.match(/^\//);
  176. if (!m) return;
  177. this.state.slashes++;
  178. return pos({
  179. type: 'slash',
  180. val: m[0]
  181. });
  182. })
  183. /**
  184. * Backslash: "\\"
  185. */
  186. .capture('backslash', function() {
  187. var pos = this.position();
  188. var m = this.match(/^\\(?![*+?(){}[\]'"])/);
  189. if (!m) return;
  190. var val = m[0];
  191. if (this.isInside('bracket')) {
  192. val = '\\';
  193. } else if (val.length > 1) {
  194. val = '\\\\';
  195. }
  196. return pos({
  197. type: 'backslash',
  198. val: val
  199. });
  200. })
  201. /**
  202. * Square: "[.]"
  203. */
  204. .capture('square', function() {
  205. if (this.isInside('bracket')) return;
  206. var pos = this.position();
  207. var m = this.match(/^\[([^!^\\])\]/);
  208. if (!m) return;
  209. return pos({
  210. type: 'square',
  211. val: m[1]
  212. });
  213. })
  214. /**
  215. * Brackets: "[...]" (basic, this can be overridden by other parsers)
  216. */
  217. .capture('bracket', function() {
  218. var pos = this.position();
  219. var m = this.match(/^(?:\[([!^]?)([^\]]+|\]-)(\]|[^*+?]+)|\[)/);
  220. if (!m) return;
  221. var val = m[0];
  222. var negated = m[1] ? '^' : '';
  223. var inner = (m[2] || '').replace(/\\\\+/, '\\\\');
  224. var close = m[3] || '';
  225. if (m[2] && inner.length < m[2].length) {
  226. val = val.replace(/\\\\+/, '\\\\');
  227. }
  228. var esc = this.input.slice(0, 2);
  229. if (inner === '' && esc === '\\]') {
  230. inner += esc;
  231. this.consume(2);
  232. var str = this.input;
  233. var idx = -1;
  234. var ch;
  235. while ((ch = str[++idx])) {
  236. this.consume(1);
  237. if (ch === ']') {
  238. close = ch;
  239. break;
  240. }
  241. inner += ch;
  242. }
  243. }
  244. return pos({
  245. type: 'bracket',
  246. val: val,
  247. escaped: close !== ']',
  248. negated: negated,
  249. inner: inner,
  250. close: close
  251. });
  252. })
  253. /**
  254. * Text
  255. */
  256. .capture('text', function() {
  257. if (this.isInside('bracket')) return;
  258. var pos = this.position();
  259. var m = this.match(not);
  260. if (!m || !m[0]) return;
  261. return pos({
  262. type: 'text',
  263. val: m[0]
  264. });
  265. });
  266. /**
  267. * Allow custom parsers to be passed on options
  268. */
  269. if (options && typeof options.parsers === 'function') {
  270. options.parsers(nanomatch.parser);
  271. }
  272. };
  273. /**
  274. * Advance to the next non-escaped character
  275. */
  276. function advanceTo(input, endChar) {
  277. var ch = input.charAt(0);
  278. var tok = { len: 1, val: '', esc: '' };
  279. var idx = 0;
  280. function advance() {
  281. if (ch !== '\\') {
  282. tok.esc += '\\' + ch;
  283. tok.val += ch;
  284. }
  285. ch = input.charAt(++idx);
  286. tok.len++;
  287. if (ch === '\\') {
  288. advance();
  289. advance();
  290. }
  291. }
  292. while (ch && ch !== endChar) {
  293. advance();
  294. }
  295. return tok;
  296. }
  297. /**
  298. * Create text regex
  299. */
  300. function createTextRegex(pattern) {
  301. if (cached) return cached;
  302. var opts = {contains: true, strictClose: false};
  303. var not = regexNot.create(pattern, opts);
  304. var re = toRegex('^(?:[*]\\((?=.)|' + not + ')', opts);
  305. return (cached = re);
  306. }
  307. /**
  308. * Expose negation string
  309. */
  310. module.exports.not = NOT_REGEX;