perl.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. Language: Perl
  3. Author: Peter Leonov <gojpeg@yandex.ru>
  4. Website: https://www.perl.org
  5. Category: common
  6. */
  7. /** @type LanguageFn */
  8. function perl(hljs) {
  9. const regex = hljs.regex;
  10. const KEYWORDS = [
  11. 'abs',
  12. 'accept',
  13. 'alarm',
  14. 'and',
  15. 'atan2',
  16. 'bind',
  17. 'binmode',
  18. 'bless',
  19. 'break',
  20. 'caller',
  21. 'chdir',
  22. 'chmod',
  23. 'chomp',
  24. 'chop',
  25. 'chown',
  26. 'chr',
  27. 'chroot',
  28. 'class',
  29. 'close',
  30. 'closedir',
  31. 'connect',
  32. 'continue',
  33. 'cos',
  34. 'crypt',
  35. 'dbmclose',
  36. 'dbmopen',
  37. 'defined',
  38. 'delete',
  39. 'die',
  40. 'do',
  41. 'dump',
  42. 'each',
  43. 'else',
  44. 'elsif',
  45. 'endgrent',
  46. 'endhostent',
  47. 'endnetent',
  48. 'endprotoent',
  49. 'endpwent',
  50. 'endservent',
  51. 'eof',
  52. 'eval',
  53. 'exec',
  54. 'exists',
  55. 'exit',
  56. 'exp',
  57. 'fcntl',
  58. 'field',
  59. 'fileno',
  60. 'flock',
  61. 'for',
  62. 'foreach',
  63. 'fork',
  64. 'format',
  65. 'formline',
  66. 'getc',
  67. 'getgrent',
  68. 'getgrgid',
  69. 'getgrnam',
  70. 'gethostbyaddr',
  71. 'gethostbyname',
  72. 'gethostent',
  73. 'getlogin',
  74. 'getnetbyaddr',
  75. 'getnetbyname',
  76. 'getnetent',
  77. 'getpeername',
  78. 'getpgrp',
  79. 'getpriority',
  80. 'getprotobyname',
  81. 'getprotobynumber',
  82. 'getprotoent',
  83. 'getpwent',
  84. 'getpwnam',
  85. 'getpwuid',
  86. 'getservbyname',
  87. 'getservbyport',
  88. 'getservent',
  89. 'getsockname',
  90. 'getsockopt',
  91. 'given',
  92. 'glob',
  93. 'gmtime',
  94. 'goto',
  95. 'grep',
  96. 'gt',
  97. 'hex',
  98. 'if',
  99. 'index',
  100. 'int',
  101. 'ioctl',
  102. 'join',
  103. 'keys',
  104. 'kill',
  105. 'last',
  106. 'lc',
  107. 'lcfirst',
  108. 'length',
  109. 'link',
  110. 'listen',
  111. 'local',
  112. 'localtime',
  113. 'log',
  114. 'lstat',
  115. 'lt',
  116. 'ma',
  117. 'map',
  118. 'method',
  119. 'mkdir',
  120. 'msgctl',
  121. 'msgget',
  122. 'msgrcv',
  123. 'msgsnd',
  124. 'my',
  125. 'ne',
  126. 'next',
  127. 'no',
  128. 'not',
  129. 'oct',
  130. 'open',
  131. 'opendir',
  132. 'or',
  133. 'ord',
  134. 'our',
  135. 'pack',
  136. 'package',
  137. 'pipe',
  138. 'pop',
  139. 'pos',
  140. 'print',
  141. 'printf',
  142. 'prototype',
  143. 'push',
  144. 'q|0',
  145. 'qq',
  146. 'quotemeta',
  147. 'qw',
  148. 'qx',
  149. 'rand',
  150. 'read',
  151. 'readdir',
  152. 'readline',
  153. 'readlink',
  154. 'readpipe',
  155. 'recv',
  156. 'redo',
  157. 'ref',
  158. 'rename',
  159. 'require',
  160. 'reset',
  161. 'return',
  162. 'reverse',
  163. 'rewinddir',
  164. 'rindex',
  165. 'rmdir',
  166. 'say',
  167. 'scalar',
  168. 'seek',
  169. 'seekdir',
  170. 'select',
  171. 'semctl',
  172. 'semget',
  173. 'semop',
  174. 'send',
  175. 'setgrent',
  176. 'sethostent',
  177. 'setnetent',
  178. 'setpgrp',
  179. 'setpriority',
  180. 'setprotoent',
  181. 'setpwent',
  182. 'setservent',
  183. 'setsockopt',
  184. 'shift',
  185. 'shmctl',
  186. 'shmget',
  187. 'shmread',
  188. 'shmwrite',
  189. 'shutdown',
  190. 'sin',
  191. 'sleep',
  192. 'socket',
  193. 'socketpair',
  194. 'sort',
  195. 'splice',
  196. 'split',
  197. 'sprintf',
  198. 'sqrt',
  199. 'srand',
  200. 'stat',
  201. 'state',
  202. 'study',
  203. 'sub',
  204. 'substr',
  205. 'symlink',
  206. 'syscall',
  207. 'sysopen',
  208. 'sysread',
  209. 'sysseek',
  210. 'system',
  211. 'syswrite',
  212. 'tell',
  213. 'telldir',
  214. 'tie',
  215. 'tied',
  216. 'time',
  217. 'times',
  218. 'tr',
  219. 'truncate',
  220. 'uc',
  221. 'ucfirst',
  222. 'umask',
  223. 'undef',
  224. 'unless',
  225. 'unlink',
  226. 'unpack',
  227. 'unshift',
  228. 'untie',
  229. 'until',
  230. 'use',
  231. 'utime',
  232. 'values',
  233. 'vec',
  234. 'wait',
  235. 'waitpid',
  236. 'wantarray',
  237. 'warn',
  238. 'when',
  239. 'while',
  240. 'write',
  241. 'x|0',
  242. 'xor',
  243. 'y|0'
  244. ];
  245. // https://perldoc.perl.org/perlre#Modifiers
  246. const REGEX_MODIFIERS = /[dualxmsipngr]{0,12}/; // aa and xx are valid, making max length 12
  247. const PERL_KEYWORDS = {
  248. $pattern: /[\w.]+/,
  249. keyword: KEYWORDS.join(" ")
  250. };
  251. const SUBST = {
  252. className: 'subst',
  253. begin: '[$@]\\{',
  254. end: '\\}',
  255. keywords: PERL_KEYWORDS
  256. };
  257. const METHOD = {
  258. begin: /->\{/,
  259. end: /\}/
  260. // contains defined later
  261. };
  262. const ATTR = {
  263. scope: 'attr',
  264. match: /\s+:\s*\w+(\s*\(.*?\))?/,
  265. };
  266. const VAR = {
  267. scope: 'variable',
  268. variants: [
  269. { begin: /\$\d/ },
  270. { begin: regex.concat(
  271. /[$%@](?!")(\^\w\b|#\w+(::\w+)*|\{\w+\}|\w+(::\w*)*)/,
  272. // negative look-ahead tries to avoid matching patterns that are not
  273. // Perl at all like $ident$, @ident@, etc.
  274. `(?![A-Za-z])(?![@$%])`
  275. )
  276. },
  277. {
  278. // Only $= is a special Perl variable and one can't declare @= or %=.
  279. begin: /[$%@](?!")[^\s\w{=]|\$=/,
  280. relevance: 0
  281. }
  282. ],
  283. contains: [ ATTR ],
  284. };
  285. const NUMBER = {
  286. className: 'number',
  287. variants: [
  288. // decimal numbers:
  289. // include the case where a number starts with a dot (eg. .9), and
  290. // the leading 0? avoids mixing the first and second match on 0.x cases
  291. { match: /0?\.[0-9][0-9_]+\b/ },
  292. // include the special versioned number (eg. v5.38)
  293. { match: /\bv?(0|[1-9][0-9_]*(\.[0-9_]+)?|[1-9][0-9_]*)\b/ },
  294. // non-decimal numbers:
  295. { match: /\b0[0-7][0-7_]*\b/ },
  296. { match: /\b0x[0-9a-fA-F][0-9a-fA-F_]*\b/ },
  297. { match: /\b0b[0-1][0-1_]*\b/ },
  298. ],
  299. relevance: 0
  300. };
  301. const STRING_CONTAINS = [
  302. hljs.BACKSLASH_ESCAPE,
  303. SUBST,
  304. VAR
  305. ];
  306. const REGEX_DELIMS = [
  307. /!/,
  308. /\//,
  309. /\|/,
  310. /\?/,
  311. /'/,
  312. /"/, // valid but infrequent and weird
  313. /#/ // valid but infrequent and weird
  314. ];
  315. /**
  316. * @param {string|RegExp} prefix
  317. * @param {string|RegExp} open
  318. * @param {string|RegExp} close
  319. */
  320. const PAIRED_DOUBLE_RE = (prefix, open, close = '\\1') => {
  321. const middle = (close === '\\1')
  322. ? close
  323. : regex.concat(close, open);
  324. return regex.concat(
  325. regex.concat("(?:", prefix, ")"),
  326. open,
  327. /(?:\\.|[^\\\/])*?/,
  328. middle,
  329. /(?:\\.|[^\\\/])*?/,
  330. close,
  331. REGEX_MODIFIERS
  332. );
  333. };
  334. /**
  335. * @param {string|RegExp} prefix
  336. * @param {string|RegExp} open
  337. * @param {string|RegExp} close
  338. */
  339. const PAIRED_RE = (prefix, open, close) => {
  340. return regex.concat(
  341. regex.concat("(?:", prefix, ")"),
  342. open,
  343. /(?:\\.|[^\\\/])*?/,
  344. close,
  345. REGEX_MODIFIERS
  346. );
  347. };
  348. const PERL_DEFAULT_CONTAINS = [
  349. VAR,
  350. hljs.HASH_COMMENT_MODE,
  351. hljs.COMMENT(
  352. /^=\w/,
  353. /=cut/,
  354. { endsWithParent: true }
  355. ),
  356. METHOD,
  357. {
  358. className: 'string',
  359. contains: STRING_CONTAINS,
  360. variants: [
  361. {
  362. begin: 'q[qwxr]?\\s*\\(',
  363. end: '\\)',
  364. relevance: 5
  365. },
  366. {
  367. begin: 'q[qwxr]?\\s*\\[',
  368. end: '\\]',
  369. relevance: 5
  370. },
  371. {
  372. begin: 'q[qwxr]?\\s*\\{',
  373. end: '\\}',
  374. relevance: 5
  375. },
  376. {
  377. begin: 'q[qwxr]?\\s*\\|',
  378. end: '\\|',
  379. relevance: 5
  380. },
  381. {
  382. begin: 'q[qwxr]?\\s*<',
  383. end: '>',
  384. relevance: 5
  385. },
  386. {
  387. begin: 'qw\\s+q',
  388. end: 'q',
  389. relevance: 5
  390. },
  391. {
  392. begin: '\'',
  393. end: '\'',
  394. contains: [ hljs.BACKSLASH_ESCAPE ]
  395. },
  396. {
  397. begin: '"',
  398. end: '"'
  399. },
  400. {
  401. begin: '`',
  402. end: '`',
  403. contains: [ hljs.BACKSLASH_ESCAPE ]
  404. },
  405. {
  406. begin: /\{\w+\}/,
  407. relevance: 0
  408. },
  409. {
  410. begin: '-?\\w+\\s*=>',
  411. relevance: 0
  412. }
  413. ]
  414. },
  415. NUMBER,
  416. { // regexp container
  417. begin: '(\\/\\/|' + hljs.RE_STARTERS_RE + '|\\b(split|return|print|reverse|grep)\\b)\\s*',
  418. keywords: 'split return print reverse grep',
  419. relevance: 0,
  420. contains: [
  421. hljs.HASH_COMMENT_MODE,
  422. {
  423. className: 'regexp',
  424. variants: [
  425. // allow matching common delimiters
  426. { begin: PAIRED_DOUBLE_RE("s|tr|y", regex.either(...REGEX_DELIMS, { capture: true })) },
  427. // and then paired delmis
  428. { begin: PAIRED_DOUBLE_RE("s|tr|y", "\\(", "\\)") },
  429. { begin: PAIRED_DOUBLE_RE("s|tr|y", "\\[", "\\]") },
  430. { begin: PAIRED_DOUBLE_RE("s|tr|y", "\\{", "\\}") }
  431. ],
  432. relevance: 2
  433. },
  434. {
  435. className: 'regexp',
  436. variants: [
  437. {
  438. // could be a comment in many languages so do not count
  439. // as relevant
  440. begin: /(m|qr)\/\//,
  441. relevance: 0
  442. },
  443. // prefix is optional with /regex/
  444. { begin: PAIRED_RE("(?:m|qr)?", /\//, /\//) },
  445. // allow matching common delimiters
  446. { begin: PAIRED_RE("m|qr", regex.either(...REGEX_DELIMS, { capture: true }), /\1/) },
  447. // allow common paired delmins
  448. { begin: PAIRED_RE("m|qr", /\(/, /\)/) },
  449. { begin: PAIRED_RE("m|qr", /\[/, /\]/) },
  450. { begin: PAIRED_RE("m|qr", /\{/, /\}/) }
  451. ]
  452. }
  453. ]
  454. },
  455. {
  456. className: 'function',
  457. beginKeywords: 'sub method',
  458. end: '(\\s*\\(.*?\\))?[;{]',
  459. excludeEnd: true,
  460. relevance: 5,
  461. contains: [ hljs.TITLE_MODE, ATTR ]
  462. },
  463. {
  464. className: 'class',
  465. beginKeywords: 'class',
  466. end: '[;{]',
  467. excludeEnd: true,
  468. relevance: 5,
  469. contains: [ hljs.TITLE_MODE, ATTR, NUMBER ]
  470. },
  471. {
  472. begin: '-\\w\\b',
  473. relevance: 0
  474. },
  475. {
  476. begin: "^__DATA__$",
  477. end: "^__END__$",
  478. subLanguage: 'mojolicious',
  479. contains: [
  480. {
  481. begin: "^@@.*",
  482. end: "$",
  483. className: "comment"
  484. }
  485. ]
  486. }
  487. ];
  488. SUBST.contains = PERL_DEFAULT_CONTAINS;
  489. METHOD.contains = PERL_DEFAULT_CONTAINS;
  490. return {
  491. name: 'Perl',
  492. aliases: [
  493. 'pl',
  494. 'pm'
  495. ],
  496. keywords: PERL_KEYWORDS,
  497. contains: PERL_DEFAULT_CONTAINS
  498. };
  499. }
  500. export { perl as default };