perl.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. module.exports = function(hljs) {
  2. var PERL_KEYWORDS = 'getpwent getservent quotemeta msgrcv scalar kill dbmclose undef lc ' +
  3. 'ma syswrite tr send umask sysopen shmwrite vec qx utime local oct semctl localtime ' +
  4. 'readpipe do return format read sprintf dbmopen pop getpgrp not getpwnam rewinddir qq' +
  5. 'fileno qw endprotoent wait sethostent bless s|0 opendir continue each sleep endgrent ' +
  6. 'shutdown dump chomp connect getsockname die socketpair close flock exists index shmget' +
  7. 'sub for endpwent redo lstat msgctl setpgrp abs exit select print ref gethostbyaddr ' +
  8. 'unshift fcntl syscall goto getnetbyaddr join gmtime symlink semget splice x|0 ' +
  9. 'getpeername recv log setsockopt cos last reverse gethostbyname getgrnam study formline ' +
  10. 'endhostent times chop length gethostent getnetent pack getprotoent getservbyname rand ' +
  11. 'mkdir pos chmod y|0 substr endnetent printf next open msgsnd readdir use unlink ' +
  12. 'getsockopt getpriority rindex wantarray hex system getservbyport endservent int chr ' +
  13. 'untie rmdir prototype tell listen fork shmread ucfirst setprotoent else sysseek link ' +
  14. 'getgrgid shmctl waitpid unpack getnetbyname reset chdir grep split require caller ' +
  15. 'lcfirst until warn while values shift telldir getpwuid my getprotobynumber delete and ' +
  16. 'sort uc defined srand accept package seekdir getprotobyname semop our rename seek if q|0 ' +
  17. 'chroot sysread setpwent no crypt getc chown sqrt write setnetent setpriority foreach ' +
  18. 'tie sin msgget map stat getlogin unless elsif truncate exec keys glob tied closedir' +
  19. 'ioctl socket readlink eval xor readline binmode setservent eof ord bind alarm pipe ' +
  20. 'atan2 getgrent exp time push setgrent gt lt or ne m|0 break given say state when';
  21. var SUBST = {
  22. className: 'subst',
  23. begin: '[$@]\\{', end: '\\}',
  24. keywords: PERL_KEYWORDS
  25. };
  26. var METHOD = {
  27. begin: '->{', end: '}'
  28. // contains defined later
  29. };
  30. var VAR = {
  31. variants: [
  32. {begin: /\$\d/},
  33. {begin: /[\$%@](\^\w\b|#\w+(::\w+)*|{\w+}|\w+(::\w*)*)/},
  34. {begin: /[\$%@][^\s\w{]/, relevance: 0}
  35. ]
  36. };
  37. var STRING_CONTAINS = [hljs.BACKSLASH_ESCAPE, SUBST, VAR];
  38. var PERL_DEFAULT_CONTAINS = [
  39. VAR,
  40. hljs.HASH_COMMENT_MODE,
  41. hljs.COMMENT(
  42. '^\\=\\w',
  43. '\\=cut',
  44. {
  45. endsWithParent: true
  46. }
  47. ),
  48. METHOD,
  49. {
  50. className: 'string',
  51. contains: STRING_CONTAINS,
  52. variants: [
  53. {
  54. begin: 'q[qwxr]?\\s*\\(', end: '\\)',
  55. relevance: 5
  56. },
  57. {
  58. begin: 'q[qwxr]?\\s*\\[', end: '\\]',
  59. relevance: 5
  60. },
  61. {
  62. begin: 'q[qwxr]?\\s*\\{', end: '\\}',
  63. relevance: 5
  64. },
  65. {
  66. begin: 'q[qwxr]?\\s*\\|', end: '\\|',
  67. relevance: 5
  68. },
  69. {
  70. begin: 'q[qwxr]?\\s*\\<', end: '\\>',
  71. relevance: 5
  72. },
  73. {
  74. begin: 'qw\\s+q', end: 'q',
  75. relevance: 5
  76. },
  77. {
  78. begin: '\'', end: '\'',
  79. contains: [hljs.BACKSLASH_ESCAPE]
  80. },
  81. {
  82. begin: '"', end: '"'
  83. },
  84. {
  85. begin: '`', end: '`',
  86. contains: [hljs.BACKSLASH_ESCAPE]
  87. },
  88. {
  89. begin: '{\\w+}',
  90. contains: [],
  91. relevance: 0
  92. },
  93. {
  94. begin: '\-?\\w+\\s*\\=\\>',
  95. contains: [],
  96. relevance: 0
  97. }
  98. ]
  99. },
  100. {
  101. className: 'number',
  102. begin: '(\\b0[0-7_]+)|(\\b0x[0-9a-fA-F_]+)|(\\b[1-9][0-9_]*(\\.[0-9_]+)?)|[0_]\\b',
  103. relevance: 0
  104. },
  105. { // regexp container
  106. begin: '(\\/\\/|' + hljs.RE_STARTERS_RE + '|\\b(split|return|print|reverse|grep)\\b)\\s*',
  107. keywords: 'split return print reverse grep',
  108. relevance: 0,
  109. contains: [
  110. hljs.HASH_COMMENT_MODE,
  111. {
  112. className: 'regexp',
  113. begin: '(s|tr|y)/(\\\\.|[^/])*/(\\\\.|[^/])*/[a-z]*',
  114. relevance: 10
  115. },
  116. {
  117. className: 'regexp',
  118. begin: '(m|qr)?/', end: '/[a-z]*',
  119. contains: [hljs.BACKSLASH_ESCAPE],
  120. relevance: 0 // allows empty "//" which is a common comment delimiter in other languages
  121. }
  122. ]
  123. },
  124. {
  125. className: 'function',
  126. beginKeywords: 'sub', end: '(\\s*\\(.*?\\))?[;{]', excludeEnd: true,
  127. relevance: 5,
  128. contains: [hljs.TITLE_MODE]
  129. },
  130. {
  131. begin: '-\\w\\b',
  132. relevance: 0
  133. },
  134. {
  135. begin: "^__DATA__$",
  136. end: "^__END__$",
  137. subLanguage: 'mojolicious',
  138. contains: [
  139. {
  140. begin: "^@@.*",
  141. end: "$",
  142. className: "comment"
  143. }
  144. ]
  145. }
  146. ];
  147. SUBST.contains = PERL_DEFAULT_CONTAINS;
  148. METHOD.contains = PERL_DEFAULT_CONTAINS;
  149. return {
  150. aliases: ['pl', 'pm'],
  151. lexemes: /[\w\.]+/,
  152. keywords: PERL_KEYWORDS,
  153. contains: PERL_DEFAULT_CONTAINS
  154. };
  155. };