parsers.js 7.5 KB

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