You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parsers.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. var extglob = require('extglob');
  3. var nanomatch = require('nanomatch');
  4. var regexNot = require('regex-not');
  5. var toRegex = require('to-regex');
  6. var not;
  7. /**
  8. * Characters to use in negation regex (we want to "not" match
  9. * characters that are matched by other parsers)
  10. */
  11. var TEXT = '([!@*?+]?\\(|\\)|\\[:?(?=.*?:?\\])|:?\\]|[*+?!^$.\\\\/])+';
  12. var createNotRegex = function(opts) {
  13. return not || (not = textRegex(TEXT));
  14. };
  15. /**
  16. * Parsers
  17. */
  18. module.exports = function(snapdragon) {
  19. var parsers = snapdragon.parser.parsers;
  20. // register nanomatch parsers
  21. snapdragon.use(nanomatch.parsers);
  22. // get references to some specific nanomatch parsers before they
  23. // are overridden by the extglob and/or parsers
  24. var escape = parsers.escape;
  25. var slash = parsers.slash;
  26. var qmark = parsers.qmark;
  27. var plus = parsers.plus;
  28. var star = parsers.star;
  29. var dot = parsers.dot;
  30. // register extglob parsers
  31. snapdragon.use(extglob.parsers);
  32. // custom micromatch parsers
  33. snapdragon.parser
  34. .use(function() {
  35. // override "notRegex" created in nanomatch parser
  36. this.notRegex = /^\!+(?!\()/;
  37. })
  38. // reset the referenced parsers
  39. .capture('escape', escape)
  40. .capture('slash', slash)
  41. .capture('qmark', qmark)
  42. .capture('star', star)
  43. .capture('plus', plus)
  44. .capture('dot', dot)
  45. /**
  46. * Override `text` parser
  47. */
  48. .capture('text', function() {
  49. if (this.isInside('bracket')) return;
  50. var pos = this.position();
  51. var m = this.match(createNotRegex(this.options));
  52. if (!m || !m[0]) return;
  53. // escape regex boundary characters and simple brackets
  54. var val = m[0].replace(/([[\]^$])/g, '\\$1');
  55. return pos({
  56. type: 'text',
  57. val: val
  58. });
  59. });
  60. };
  61. /**
  62. * Create text regex
  63. */
  64. function textRegex(pattern) {
  65. var notStr = regexNot.create(pattern, {contains: true, strictClose: false});
  66. var prefix = '(?:[\\^]|\\\\|';
  67. return toRegex(prefix + notStr + ')', {strictClose: false});
  68. }