parsers.js 7.4 KB

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