perl.js 9.3 KB

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