prism-perl.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. (function (Prism) {
  2. var brackets = /(?:\((?:[^()\\]|\\[\s\S])*\)|\{(?:[^{}\\]|\\[\s\S])*\}|\[(?:[^[\]\\]|\\[\s\S])*\]|<(?:[^<>\\]|\\[\s\S])*>)/.source;
  3. Prism.languages.perl = {
  4. 'comment': [
  5. {
  6. // POD
  7. pattern: /(^\s*)=\w[\s\S]*?=cut.*/m,
  8. lookbehind: true,
  9. greedy: true
  10. },
  11. {
  12. pattern: /(^|[^\\$])#.*/,
  13. lookbehind: true,
  14. greedy: true
  15. }
  16. ],
  17. // TODO Could be nice to handle Heredoc too.
  18. 'string': [
  19. {
  20. pattern: RegExp(
  21. /\b(?:q|qq|qw|qx)(?![a-zA-Z0-9])\s*/.source +
  22. '(?:' +
  23. [
  24. // q/.../
  25. /([^a-zA-Z0-9\s{(\[<])(?:(?!\1)[^\\]|\\[\s\S])*\1/.source,
  26. // q a...a
  27. // eslint-disable-next-line regexp/strict
  28. /([a-zA-Z0-9])(?:(?!\2)[^\\]|\\[\s\S])*\2/.source,
  29. // q(...)
  30. // q{...}
  31. // q[...]
  32. // q<...>
  33. brackets,
  34. ].join('|') +
  35. ')'
  36. ),
  37. greedy: true
  38. },
  39. // "...", `...`
  40. {
  41. pattern: /("|`)(?:(?!\1)[^\\]|\\[\s\S])*\1/,
  42. greedy: true
  43. },
  44. // '...'
  45. // FIXME Multi-line single-quoted strings are not supported as they would break variables containing '
  46. {
  47. pattern: /'(?:[^'\\\r\n]|\\.)*'/,
  48. greedy: true
  49. }
  50. ],
  51. 'regex': [
  52. {
  53. pattern: RegExp(
  54. /\b(?:m|qr)(?![a-zA-Z0-9])\s*/.source +
  55. '(?:' +
  56. [
  57. // m/.../
  58. /([^a-zA-Z0-9\s{(\[<])(?:(?!\1)[^\\]|\\[\s\S])*\1/.source,
  59. // m a...a
  60. // eslint-disable-next-line regexp/strict
  61. /([a-zA-Z0-9])(?:(?!\2)[^\\]|\\[\s\S])*\2/.source,
  62. // m(...)
  63. // m{...}
  64. // m[...]
  65. // m<...>
  66. brackets,
  67. ].join('|') +
  68. ')' +
  69. /[msixpodualngc]*/.source
  70. ),
  71. greedy: true
  72. },
  73. // The lookbehinds prevent -s from breaking
  74. {
  75. pattern: RegExp(
  76. /(^|[^-])\b(?:s|tr|y)(?![a-zA-Z0-9])\s*/.source +
  77. '(?:' +
  78. [
  79. // s/.../.../
  80. // eslint-disable-next-line regexp/strict
  81. /([^a-zA-Z0-9\s{(\[<])(?:(?!\2)[^\\]|\\[\s\S])*\2(?:(?!\2)[^\\]|\\[\s\S])*\2/.source,
  82. // s a...a...a
  83. // eslint-disable-next-line regexp/strict
  84. /([a-zA-Z0-9])(?:(?!\3)[^\\]|\\[\s\S])*\3(?:(?!\3)[^\\]|\\[\s\S])*\3/.source,
  85. // s(...)(...)
  86. // s{...}{...}
  87. // s[...][...]
  88. // s<...><...>
  89. // s(...)[...]
  90. brackets + /\s*/.source + brackets,
  91. ].join('|') +
  92. ')' +
  93. /[msixpodualngcer]*/.source
  94. ),
  95. lookbehind: true,
  96. greedy: true
  97. },
  98. // /.../
  99. // The look-ahead tries to prevent two divisions on
  100. // the same line from being highlighted as regex.
  101. // This does not support multi-line regex.
  102. {
  103. pattern: /\/(?:[^\/\\\r\n]|\\.)*\/[msixpodualngc]*(?=\s*(?:$|[\r\n,.;})&|\-+*~<>!?^]|(?:and|cmp|eq|ge|gt|le|lt|ne|not|or|x|xor)\b))/,
  104. greedy: true
  105. }
  106. ],
  107. // FIXME Not sure about the handling of ::, ', and #
  108. 'variable': [
  109. // ${^POSTMATCH}
  110. /[&*$@%]\{\^[A-Z]+\}/,
  111. // $^V
  112. /[&*$@%]\^[A-Z_]/,
  113. // ${...}
  114. /[&*$@%]#?(?=\{)/,
  115. // $foo
  116. /[&*$@%]#?(?:(?:::)*'?(?!\d)[\w$]+(?![\w$]))+(?:::)*/,
  117. // $1
  118. /[&*$@%]\d+/,
  119. // $_, @_, %!
  120. // The negative lookahead prevents from breaking the %= operator
  121. /(?!%=)[$@%][!"#$%&'()*+,\-.\/:;<=>?@[\\\]^_`{|}~]/
  122. ],
  123. 'filehandle': {
  124. // <>, <FOO>, _
  125. pattern: /<(?![<=])\S*?>|\b_\b/,
  126. alias: 'symbol'
  127. },
  128. 'v-string': {
  129. // v1.2, 1.2.3
  130. pattern: /v\d+(?:\.\d+)*|\d+(?:\.\d+){2,}/,
  131. alias: 'string'
  132. },
  133. 'function': {
  134. pattern: /(\bsub[ \t]+)\w+/,
  135. lookbehind: true
  136. },
  137. 'keyword': /\b(?:any|break|continue|default|delete|die|do|else|elsif|eval|for|foreach|given|goto|if|last|local|my|next|our|package|print|redo|require|return|say|state|sub|switch|undef|unless|until|use|when|while)\b/,
  138. 'number': /\b(?:0x[\dA-Fa-f](?:_?[\dA-Fa-f])*|0b[01](?:_?[01])*|(?:(?:\d(?:_?\d)*)?\.)?\d(?:_?\d)*(?:[Ee][+-]?\d+)?)\b/,
  139. 'operator': /-[rwxoRWXOezsfdlpSbctugkTBMAC]\b|\+[+=]?|-[-=>]?|\*\*?=?|\/\/?=?|=[=~>]?|~[~=]?|\|\|?=?|&&?=?|<(?:=>?|<=?)?|>>?=?|![~=]?|[%^]=?|\.(?:=|\.\.?)?|[\\?]|\bx(?:=|\b)|\b(?:and|cmp|eq|ge|gt|le|lt|ne|not|or|xor)\b/,
  140. 'punctuation': /[{}[\];(),:]/
  141. };
  142. }(Prism));