2024-01-16 21:26:16 +08:00
|
|
|
/**
|
|
|
|
* @param {string} value
|
|
|
|
* @returns {RegExp}
|
|
|
|
* */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {RegExp | string } re
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function source(re) {
|
|
|
|
if (!re) return null;
|
|
|
|
if (typeof re === "string") return re;
|
|
|
|
|
|
|
|
return re.source;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {...(RegExp | string) } args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function concat(...args) {
|
|
|
|
const joined = args.map((x) => source(x)).join("");
|
|
|
|
return joined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Any of the passed expresssions may match
|
|
|
|
*
|
|
|
|
* Creates a huge this | this | that | that match
|
|
|
|
* @param {(RegExp | string)[] } args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function either(...args) {
|
|
|
|
const joined = '(' + args.map((x) => source(x)).join("|") + ")";
|
|
|
|
return joined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Language: Perl
|
|
|
|
Author: Peter Leonov <gojpeg@yandex.ru>
|
|
|
|
Website: https://www.perl.org
|
|
|
|
Category: common
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @type LanguageFn */
|
|
|
|
function perl(hljs) {
|
|
|
|
const KEYWORDS = [
|
|
|
|
'abs',
|
|
|
|
'accept',
|
|
|
|
'alarm',
|
|
|
|
'and',
|
|
|
|
'atan2',
|
|
|
|
'bind',
|
|
|
|
'binmode',
|
|
|
|
'bless',
|
|
|
|
'break',
|
|
|
|
'caller',
|
|
|
|
'chdir',
|
|
|
|
'chmod',
|
|
|
|
'chomp',
|
|
|
|
'chop',
|
|
|
|
'chown',
|
|
|
|
'chr',
|
|
|
|
'chroot',
|
|
|
|
'close',
|
|
|
|
'closedir',
|
|
|
|
'connect',
|
|
|
|
'continue',
|
|
|
|
'cos',
|
|
|
|
'crypt',
|
|
|
|
'dbmclose',
|
|
|
|
'dbmopen',
|
|
|
|
'defined',
|
|
|
|
'delete',
|
|
|
|
'die',
|
|
|
|
'do',
|
|
|
|
'dump',
|
|
|
|
'each',
|
|
|
|
'else',
|
|
|
|
'elsif',
|
|
|
|
'endgrent',
|
|
|
|
'endhostent',
|
|
|
|
'endnetent',
|
|
|
|
'endprotoent',
|
|
|
|
'endpwent',
|
|
|
|
'endservent',
|
|
|
|
'eof',
|
|
|
|
'eval',
|
|
|
|
'exec',
|
|
|
|
'exists',
|
|
|
|
'exit',
|
|
|
|
'exp',
|
|
|
|
'fcntl',
|
|
|
|
'fileno',
|
|
|
|
'flock',
|
|
|
|
'for',
|
|
|
|
'foreach',
|
|
|
|
'fork',
|
|
|
|
'format',
|
|
|
|
'formline',
|
|
|
|
'getc',
|
|
|
|
'getgrent',
|
|
|
|
'getgrgid',
|
|
|
|
'getgrnam',
|
|
|
|
'gethostbyaddr',
|
|
|
|
'gethostbyname',
|
|
|
|
'gethostent',
|
|
|
|
'getlogin',
|
|
|
|
'getnetbyaddr',
|
|
|
|
'getnetbyname',
|
|
|
|
'getnetent',
|
|
|
|
'getpeername',
|
|
|
|
'getpgrp',
|
|
|
|
'getpriority',
|
|
|
|
'getprotobyname',
|
|
|
|
'getprotobynumber',
|
|
|
|
'getprotoent',
|
|
|
|
'getpwent',
|
|
|
|
'getpwnam',
|
|
|
|
'getpwuid',
|
|
|
|
'getservbyname',
|
|
|
|
'getservbyport',
|
|
|
|
'getservent',
|
|
|
|
'getsockname',
|
|
|
|
'getsockopt',
|
|
|
|
'given',
|
|
|
|
'glob',
|
|
|
|
'gmtime',
|
|
|
|
'goto',
|
|
|
|
'grep',
|
|
|
|
'gt',
|
|
|
|
'hex',
|
|
|
|
'if',
|
|
|
|
'index',
|
|
|
|
'int',
|
|
|
|
'ioctl',
|
|
|
|
'join',
|
|
|
|
'keys',
|
|
|
|
'kill',
|
|
|
|
'last',
|
|
|
|
'lc',
|
|
|
|
'lcfirst',
|
|
|
|
'length',
|
|
|
|
'link',
|
|
|
|
'listen',
|
|
|
|
'local',
|
|
|
|
'localtime',
|
|
|
|
'log',
|
|
|
|
'lstat',
|
|
|
|
'lt',
|
|
|
|
'ma',
|
|
|
|
'map',
|
|
|
|
'mkdir',
|
|
|
|
'msgctl',
|
|
|
|
'msgget',
|
|
|
|
'msgrcv',
|
|
|
|
'msgsnd',
|
|
|
|
'my',
|
|
|
|
'ne',
|
|
|
|
'next',
|
|
|
|
'no',
|
|
|
|
'not',
|
|
|
|
'oct',
|
|
|
|
'open',
|
|
|
|
'opendir',
|
|
|
|
'or',
|
|
|
|
'ord',
|
|
|
|
'our',
|
|
|
|
'pack',
|
|
|
|
'package',
|
|
|
|
'pipe',
|
|
|
|
'pop',
|
|
|
|
'pos',
|
|
|
|
'print',
|
|
|
|
'printf',
|
|
|
|
'prototype',
|
|
|
|
'push',
|
|
|
|
'q|0',
|
|
|
|
'qq',
|
|
|
|
'quotemeta',
|
|
|
|
'qw',
|
|
|
|
'qx',
|
|
|
|
'rand',
|
|
|
|
'read',
|
|
|
|
'readdir',
|
|
|
|
'readline',
|
|
|
|
'readlink',
|
|
|
|
'readpipe',
|
|
|
|
'recv',
|
|
|
|
'redo',
|
|
|
|
'ref',
|
|
|
|
'rename',
|
|
|
|
'require',
|
|
|
|
'reset',
|
|
|
|
'return',
|
|
|
|
'reverse',
|
|
|
|
'rewinddir',
|
|
|
|
'rindex',
|
|
|
|
'rmdir',
|
|
|
|
'say',
|
|
|
|
'scalar',
|
|
|
|
'seek',
|
|
|
|
'seekdir',
|
|
|
|
'select',
|
|
|
|
'semctl',
|
|
|
|
'semget',
|
|
|
|
'semop',
|
|
|
|
'send',
|
|
|
|
'setgrent',
|
|
|
|
'sethostent',
|
|
|
|
'setnetent',
|
|
|
|
'setpgrp',
|
|
|
|
'setpriority',
|
|
|
|
'setprotoent',
|
|
|
|
'setpwent',
|
|
|
|
'setservent',
|
|
|
|
'setsockopt',
|
|
|
|
'shift',
|
|
|
|
'shmctl',
|
|
|
|
'shmget',
|
|
|
|
'shmread',
|
|
|
|
'shmwrite',
|
|
|
|
'shutdown',
|
|
|
|
'sin',
|
|
|
|
'sleep',
|
|
|
|
'socket',
|
|
|
|
'socketpair',
|
|
|
|
'sort',
|
|
|
|
'splice',
|
|
|
|
'split',
|
|
|
|
'sprintf',
|
|
|
|
'sqrt',
|
|
|
|
'srand',
|
|
|
|
'stat',
|
|
|
|
'state',
|
|
|
|
'study',
|
|
|
|
'sub',
|
|
|
|
'substr',
|
|
|
|
'symlink',
|
|
|
|
'syscall',
|
|
|
|
'sysopen',
|
|
|
|
'sysread',
|
|
|
|
'sysseek',
|
|
|
|
'system',
|
|
|
|
'syswrite',
|
|
|
|
'tell',
|
|
|
|
'telldir',
|
|
|
|
'tie',
|
|
|
|
'tied',
|
|
|
|
'time',
|
|
|
|
'times',
|
|
|
|
'tr',
|
|
|
|
'truncate',
|
|
|
|
'uc',
|
|
|
|
'ucfirst',
|
|
|
|
'umask',
|
|
|
|
'undef',
|
|
|
|
'unless',
|
|
|
|
'unlink',
|
|
|
|
'unpack',
|
|
|
|
'unshift',
|
|
|
|
'untie',
|
|
|
|
'until',
|
|
|
|
'use',
|
|
|
|
'utime',
|
|
|
|
'values',
|
|
|
|
'vec',
|
|
|
|
'wait',
|
|
|
|
'waitpid',
|
|
|
|
'wantarray',
|
|
|
|
'warn',
|
|
|
|
'when',
|
|
|
|
'while',
|
|
|
|
'write',
|
|
|
|
'x|0',
|
|
|
|
'xor',
|
|
|
|
'y|0'
|
|
|
|
];
|
|
|
|
|
|
|
|
// https://perldoc.perl.org/perlre#Modifiers
|
|
|
|
const REGEX_MODIFIERS = /[dualxmsipngr]{0,12}/; // aa and xx are valid, making max length 12
|
|
|
|
const PERL_KEYWORDS = {
|
|
|
|
$pattern: /[\w.]+/,
|
|
|
|
keyword: KEYWORDS.join(" ")
|
|
|
|
};
|
|
|
|
const SUBST = {
|
2023-12-18 13:12:25 +08:00
|
|
|
className: 'subst',
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: '[$@]\\{',
|
|
|
|
end: '\\}',
|
2023-12-18 13:12:25 +08:00
|
|
|
keywords: PERL_KEYWORDS
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const METHOD = {
|
|
|
|
begin: /->\{/,
|
|
|
|
end: /\}/
|
2023-12-18 13:12:25 +08:00
|
|
|
// contains defined later
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const VAR = {
|
2023-12-18 13:12:25 +08:00
|
|
|
variants: [
|
2024-01-16 21:26:16 +08:00
|
|
|
{
|
|
|
|
begin: /\$\d/
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: concat(
|
|
|
|
/[$%@](\^\w\b|#\w+(::\w+)*|\{\w+\}|\w+(::\w*)*)/,
|
|
|
|
// negative look-ahead tries to avoid matching patterns that are not
|
|
|
|
// Perl at all like $ident$, @ident@, etc.
|
|
|
|
`(?![A-Za-z])(?![@$%])`
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: /[$%@][^\s\w{]/,
|
|
|
|
relevance: 0
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
]
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const STRING_CONTAINS = [
|
|
|
|
hljs.BACKSLASH_ESCAPE,
|
|
|
|
SUBST,
|
|
|
|
VAR
|
|
|
|
];
|
|
|
|
const REGEX_DELIMS = [
|
|
|
|
/!/,
|
|
|
|
/\//,
|
|
|
|
/\|/,
|
|
|
|
/\?/,
|
|
|
|
/'/,
|
|
|
|
/"/, // valid but infrequent and weird
|
|
|
|
/#/ // valid but infrequent and weird
|
|
|
|
];
|
|
|
|
/**
|
|
|
|
* @param {string|RegExp} prefix
|
|
|
|
* @param {string|RegExp} open
|
|
|
|
* @param {string|RegExp} close
|
|
|
|
*/
|
|
|
|
const PAIRED_DOUBLE_RE = (prefix, open, close = '\\1') => {
|
|
|
|
const middle = (close === '\\1')
|
|
|
|
? close
|
|
|
|
: concat(close, open);
|
|
|
|
return concat(
|
|
|
|
concat("(?:", prefix, ")"),
|
|
|
|
open,
|
|
|
|
/(?:\\.|[^\\\/])*?/,
|
|
|
|
middle,
|
|
|
|
/(?:\\.|[^\\\/])*?/,
|
|
|
|
close,
|
|
|
|
REGEX_MODIFIERS
|
|
|
|
);
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* @param {string|RegExp} prefix
|
|
|
|
* @param {string|RegExp} open
|
|
|
|
* @param {string|RegExp} close
|
|
|
|
*/
|
|
|
|
const PAIRED_RE = (prefix, open, close) => {
|
|
|
|
return concat(
|
|
|
|
concat("(?:", prefix, ")"),
|
|
|
|
open,
|
|
|
|
/(?:\\.|[^\\\/])*?/,
|
|
|
|
close,
|
|
|
|
REGEX_MODIFIERS
|
|
|
|
);
|
|
|
|
};
|
|
|
|
const PERL_DEFAULT_CONTAINS = [
|
2023-12-18 13:12:25 +08:00
|
|
|
VAR,
|
|
|
|
hljs.HASH_COMMENT_MODE,
|
|
|
|
hljs.COMMENT(
|
2024-01-16 21:26:16 +08:00
|
|
|
/^=\w/,
|
|
|
|
/=cut/,
|
2023-12-18 13:12:25 +08:00
|
|
|
{
|
|
|
|
endsWithParent: true
|
|
|
|
}
|
|
|
|
),
|
|
|
|
METHOD,
|
|
|
|
{
|
|
|
|
className: 'string',
|
|
|
|
contains: STRING_CONTAINS,
|
|
|
|
variants: [
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: 'q[qwxr]?\\s*\\(',
|
|
|
|
end: '\\)',
|
2023-12-18 13:12:25 +08:00
|
|
|
relevance: 5
|
|
|
|
},
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: 'q[qwxr]?\\s*\\[',
|
|
|
|
end: '\\]',
|
2023-12-18 13:12:25 +08:00
|
|
|
relevance: 5
|
|
|
|
},
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: 'q[qwxr]?\\s*\\{',
|
|
|
|
end: '\\}',
|
2023-12-18 13:12:25 +08:00
|
|
|
relevance: 5
|
|
|
|
},
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: 'q[qwxr]?\\s*\\|',
|
|
|
|
end: '\\|',
|
2023-12-18 13:12:25 +08:00
|
|
|
relevance: 5
|
|
|
|
},
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: 'q[qwxr]?\\s*<',
|
|
|
|
end: '>',
|
2023-12-18 13:12:25 +08:00
|
|
|
relevance: 5
|
|
|
|
},
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: 'qw\\s+q',
|
|
|
|
end: 'q',
|
2023-12-18 13:12:25 +08:00
|
|
|
relevance: 5
|
|
|
|
},
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: '\'',
|
|
|
|
end: '\'',
|
|
|
|
contains: [ hljs.BACKSLASH_ESCAPE ]
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: '"',
|
|
|
|
end: '"'
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: '`',
|
|
|
|
end: '`',
|
|
|
|
contains: [ hljs.BACKSLASH_ESCAPE ]
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: /\{\w+\}/,
|
2023-12-18 13:12:25 +08:00
|
|
|
relevance: 0
|
|
|
|
},
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: '-?\\w+\\s*=>',
|
2023-12-18 13:12:25 +08:00
|
|
|
relevance: 0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'number',
|
|
|
|
begin: '(\\b0[0-7_]+)|(\\b0x[0-9a-fA-F_]+)|(\\b[1-9][0-9_]*(\\.[0-9_]+)?)|[0_]\\b',
|
|
|
|
relevance: 0
|
|
|
|
},
|
|
|
|
{ // regexp container
|
|
|
|
begin: '(\\/\\/|' + hljs.RE_STARTERS_RE + '|\\b(split|return|print|reverse|grep)\\b)\\s*',
|
|
|
|
keywords: 'split return print reverse grep',
|
|
|
|
relevance: 0,
|
|
|
|
contains: [
|
|
|
|
hljs.HASH_COMMENT_MODE,
|
|
|
|
{
|
|
|
|
className: 'regexp',
|
2024-01-16 21:26:16 +08:00
|
|
|
variants: [
|
|
|
|
// allow matching common delimiters
|
|
|
|
{ begin: PAIRED_DOUBLE_RE("s|tr|y", either(...REGEX_DELIMS)) },
|
|
|
|
// and then paired delmis
|
|
|
|
{ begin: PAIRED_DOUBLE_RE("s|tr|y", "\\(", "\\)") },
|
|
|
|
{ begin: PAIRED_DOUBLE_RE("s|tr|y", "\\[", "\\]") },
|
|
|
|
{ begin: PAIRED_DOUBLE_RE("s|tr|y", "\\{", "\\}") }
|
|
|
|
],
|
|
|
|
relevance: 2
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'regexp',
|
2024-01-16 21:26:16 +08:00
|
|
|
variants: [
|
|
|
|
{
|
|
|
|
// could be a comment in many languages so do not count
|
|
|
|
// as relevant
|
|
|
|
begin: /(m|qr)\/\//,
|
|
|
|
relevance: 0
|
|
|
|
},
|
|
|
|
// prefix is optional with /regex/
|
|
|
|
{ begin: PAIRED_RE("(?:m|qr)?", /\//, /\//)},
|
|
|
|
// allow matching common delimiters
|
|
|
|
{ begin: PAIRED_RE("m|qr", either(...REGEX_DELIMS), /\1/)},
|
|
|
|
// allow common paired delmins
|
|
|
|
{ begin: PAIRED_RE("m|qr", /\(/, /\)/)},
|
|
|
|
{ begin: PAIRED_RE("m|qr", /\[/, /\]/)},
|
|
|
|
{ begin: PAIRED_RE("m|qr", /\{/, /\}/)}
|
|
|
|
]
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'function',
|
2024-01-16 21:26:16 +08:00
|
|
|
beginKeywords: 'sub',
|
|
|
|
end: '(\\s*\\(.*?\\))?[;{]',
|
|
|
|
excludeEnd: true,
|
2023-12-18 13:12:25 +08:00
|
|
|
relevance: 5,
|
2024-01-16 21:26:16 +08:00
|
|
|
contains: [ hljs.TITLE_MODE ]
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: '-\\w\\b',
|
|
|
|
relevance: 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: "^__DATA__$",
|
|
|
|
end: "^__END__$",
|
|
|
|
subLanguage: 'mojolicious',
|
|
|
|
contains: [
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: "^@@.*",
|
|
|
|
end: "$",
|
|
|
|
className: "comment"
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
];
|
|
|
|
SUBST.contains = PERL_DEFAULT_CONTAINS;
|
|
|
|
METHOD.contains = PERL_DEFAULT_CONTAINS;
|
|
|
|
|
|
|
|
return {
|
2024-01-16 21:26:16 +08:00
|
|
|
name: 'Perl',
|
|
|
|
aliases: [
|
|
|
|
'pl',
|
|
|
|
'pm'
|
|
|
|
],
|
2023-12-18 13:12:25 +08:00
|
|
|
keywords: PERL_KEYWORDS,
|
|
|
|
contains: PERL_DEFAULT_CONTAINS
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = perl;
|