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