Skip to content

Commit 71b00cc

Browse files
committed
Perl:
- Simplified regexps - Made most string and regexp patterns multi-line - Added support for regexp's n flag - Added missing operators - Fix -s operator - Fix %= operator
1 parent a831a2b commit 71b00cc

File tree

2 files changed

+62
-34
lines changed

2 files changed

+62
-34
lines changed

components/prism-perl.js

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,96 +2,124 @@ Prism.languages.perl = {
22
'comment': [
33
{
44
// POD
5-
pattern: /((?:^|\n)\s*)=\w+[\s\S]*?=cut.*/,
5+
pattern: /(^\s*)=\w+[\s\S]*?=cut.*/m,
66
lookbehind: true
77
},
88
{
9-
pattern: /(^|[^\\$])#.*?(\r?\n|$)/,
9+
pattern: /(^|[^\\$])#.*/,
1010
lookbehind: true
1111
}
1212
],
1313
// TODO Could be nice to handle Heredoc too.
1414
'string': [
1515
// q/.../
16-
/\b(?:q|qq|qx|qw)\s*([^a-zA-Z0-9\s\{\(\[<])(\\?.)*?\s*\1/,
16+
/\b(?:q|qq|qx|qw)\s*([^a-zA-Z0-9\s\{\(\[<])(?:[^\\]|\\[\s\S])*?\1/,
1717

1818
// q a...a
19-
/\b(?:q|qq|qx|qw)\s+([a-zA-Z0-9])(\\?.)*?\s*\1/,
19+
/\b(?:q|qq|qx|qw)\s+([a-zA-Z0-9])(?:[^\\]|\\[\s\S])*?\1/,
2020

2121
// q(...)
22-
/\b(?:q|qq|qx|qw)\s*\(([^()]|\\.)*\s*\)/,
22+
/\b(?:q|qq|qx|qw)\s*\((?:[^()\\]|\\[\s\S])*\)/,
2323

2424
// q{...}
25-
/\b(?:q|qq|qx|qw)\s*\{([^{}]|\\.)*\s*\}/,
25+
/\b(?:q|qq|qx|qw)\s*\{(?:[^{}\\]|\\[\s\S])*\}/,
2626

2727
// q[...]
28-
/\b(?:q|qq|qx|qw)\s*\[([^[\]]|\\.)*\s*\]/,
28+
/\b(?:q|qq|qx|qw)\s*\[(?:[^[\]\\]|\\[\s\S])*\]/,
2929

3030
// q<...>
31-
/\b(?:q|qq|qx|qw)\s*<([^<>]|\\.)*\s*>/,
31+
/\b(?:q|qq|qx|qw)\s*<(?:[^<>\\]|\\[\s\S])*>/,
3232

33-
// "...", '...', `...`
34-
/("|'|`)(\\?.)*?\1/
33+
// "...", `...`
34+
/("|`)(?:[^\\]|\\[\s\S])*?\1/,
35+
36+
// '...'
37+
// FIXME Multi-line single-quoted strings are not supported as they would break variables containing '
38+
/'(?:[^'\\\r\n]|\\.)*'/
3539
],
3640
'regex': [
3741
// m/.../
38-
/\b(?:m|qr)\s*([^a-zA-Z0-9\s\{\(\[<])(\\?.)*?\s*\1[msixpodualgc]*/,
42+
/\b(?:m|qr)\s*([^a-zA-Z0-9\s\{\(\[<])(?:[^\\]|\\[\s\S])*?\1[msixpodualngc]*/,
3943

4044
// m a...a
41-
/\b(?:m|qr)\s+([a-zA-Z0-9])(\\?.)*?\s*\1[msixpodualgc]*/,
45+
/\b(?:m|qr)\s+([a-zA-Z0-9])(?:[^\\]|\\.)*?\1[msixpodualngc]*/,
4246

4347
// m(...)
44-
/\b(?:m|qr)\s*\(([^()]|\\.)*\s*\)[msixpodualgc]*/,
48+
/\b(?:m|qr)\s*\((?:[^()\\]|\\[\s\S])*\)[msixpodualngc]*/,
4549

4650
// m{...}
47-
/\b(?:m|qr)\s*\{([^{}]|\\.)*\s*\}[msixpodualgc]*/,
51+
/\b(?:m|qr)\s*\{(?:[^{}\\]|\\[\s\S])*\}[msixpodualngc]*/,
4852

4953
// m[...]
50-
/\b(?:m|qr)\s*\[([^[\]]|\\.)*\s*\][msixpodualgc]*/,
54+
/\b(?:m|qr)\s*\[(?:[^[\]\\]|\\[\s\S])*\][msixpodualngc]*/,
5155

5256
// m<...>
53-
/\b(?:m|qr)\s*<([^<>]|\\.)*\s*>[msixpodualgc]*/,
54-
57+
/\b(?:m|qr)\s*<(?:[^<>\\]|\\[\s\S])*>[msixpodualngc]*/,
58+
59+
// The lookbehinds prevent -s from breaking
60+
// FIXME We don't handle change of separator like s(...)[...]
5561
// s/.../.../
56-
/\b(?:s|tr|y)\s*([^a-zA-Z0-9\s\{\(\[<])(\\?.)*?\s*\1\s*((?!\1).|\\.)*\s*\1[msixpodualgcer]*/,
62+
{
63+
pattern: /(^|[^-]\b)(?:s|tr|y)\s*([^a-zA-Z0-9\s\{\(\[<])(?:[^\\]|\\[\s\S])*?\2(?:[^\\]|\\[\s\S])*?\2[msixpodualngcer]*/,
64+
lookbehind: true
65+
},
5766

5867
// s a...a...a
59-
/\b(?:s|tr|y)\s+([a-zA-Z0-9])(\\?.)*?\s*\1\s*((?!\1).|\\.)*\s*\1[msixpodualgcer]*/,
68+
{
69+
pattern: /(^|[^-]\b)(?:s|tr|y)\s+([a-zA-Z0-9])(?:[^\\]|\\[\s\S])*?\2(?:[^\\]|\\[\s\S])*?\2[msixpodualngcer]*/,
70+
lookbehind: true
71+
},
6072

6173
// s(...)(...)
62-
/\b(?:s|tr|y)\s*\(([^()]|\\.)*\s*\)\s*\(\s*([^()]|\\.)*\s*\)[msixpodualgcer]*/,
74+
{
75+
pattern: /(^|[^-]\b)(?:s|tr|y)\s*\((?:[^()\\]|\\[\s\S])*\)\s*\((?:[^()\\]|\\[\s\S])*\)[msixpodualngcer]*/,
76+
lookbehind: true
77+
},
6378

6479
// s{...}{...}
65-
/\b(?:s|tr|y)\s*\{([^{}]|\\.)*\s*\}\s*\{\s*([^{}]|\\.)*\s*\}[msixpodualgcer]*/,
80+
{
81+
pattern: /(^|[^-]\b)(?:s|tr|y)\s*\{(?:[^{}\\]|\\[\s\S])*\}\s*\{(?:[^{}\\]|\\[\s\S])*\}[msixpodualngcer]*/,
82+
lookbehind: true
83+
},
6684

6785
// s[...][...]
68-
/\b(?:s|tr|y)\s*\[([^[\]]|\\.)*\s*\]\s*\[\s*([^[\]]|\\.)*\s*\][msixpodualgcer]*/,
86+
{
87+
pattern: /(^|[^-]\b)(?:s|tr|y)\s*\[(?:[^[\]\\]|\\[\s\S])*\]\s*\[(?:[^[\]\\]|\\[\s\S])*\][msixpodualngcer]*/,
88+
lookbehind: true
89+
},
6990

7091
// s<...><...>
71-
/\b(?:s|tr|y)\s*<([^<>]|\\.)*\s*>\s*<\s*([^<>]|\\.)*\s*>[msixpodualgcer]*/,
92+
{
93+
pattern: /(^|[^-]\b)(?:s|tr|y)\s*<(?:[^<>\\]|\\[\s\S])*>\s*<(?:[^<>\\]|\\[\s\S])*>[msixpodualngcer]*/,
94+
lookbehind: true
95+
},
7296

7397
// /.../
74-
/\/(\[.+?]|\\.|[^\/\r\n])*\/[msixpodualgc]*(?=\s*($|[\r\n,.;})&|\-+*=~<>!?^]|(lt|gt|le|ge|eq|ne|cmp|not|and|or|xor|x)\b))/
98+
// The look-ahead tries to prevent two divisions on
99+
// the same line from being highlighted as regex.
100+
// This does not support multi-line regex.
101+
/\/(?:[^\/\\\r\n]|\\.)*\/[msixpodualngc]*(?=\s*(?:$|[\r\n,.;})&|\-+*~<>!?^]|(lt|gt|le|ge|eq|ne|cmp|not|and|or|xor|x)\b))/
75102
],
76103

77104
// FIXME Not sure about the handling of ::, ', and #
78105
'variable': [
79106
// ${^POSTMATCH}
80-
/[&*\$@%]\{\^[A-Z]+\}/,
107+
/[&*$@%]\{\^[A-Z]+\}/,
81108
// $^V
82-
/[&*\$@%]\^[A-Z_]/,
109+
/[&*$@%]\^[A-Z_]/,
83110
// ${...}
84-
/[&*\$@%]#?(?=\{)/,
111+
/[&*$@%]#?(?=\{)/,
85112
// $foo
86-
/[&*\$@%]#?((::)*'?(?!\d)[\w$]+)+(::)*/i,
113+
/[&*$@%]#?((::)*'?(?!\d)[\w$]+)+(::)*/i,
87114
// $1
88-
/[&*\$@%]\d+/,
115+
/[&*$@%]\d+/,
89116
// $_, @_, %!
90-
/[\$@%][!"#\$%&'()*+,\-.\/:;<=>?@[\\\]^_`{|}~]/
117+
// The negative lookahead prevents from breaking the %= operator
118+
/(?!%=)[$@%][!"#$%&'()*+,\-.\/:;<=>?@[\\\]^_`{|}~]/
91119
],
92120
'filehandle': {
93121
// <>, <FOO>, _
94-
pattern: /<(?!=).*>|\b_\b/,
122+
pattern: /<(?![<=])\S*>|\b_\b/,
95123
alias: 'symbol'
96124
},
97125
'vstring': {
@@ -106,7 +134,7 @@ Prism.languages.perl = {
106134
}
107135
},
108136
'keyword': /\b(any|break|continue|default|delete|die|do|else|elsif|eval|for|foreach|given|goto|if|last|local|my|next|our|package|print|redo|require|say|state|sub|switch|undef|unless|until|use|when|while)\b/,
109-
'number': /(\n|\b)-?(0x[\dA-Fa-f](_?[\dA-Fa-f])*|0b[01](_?[01])*|(\d(_?\d)*)?\.?\d(_?\d)*([Ee]-?\d+)?)\b/,
110-
'operator': /-[rwxoRWXOezsfdlpSbctugkTBMAC]\b|[-+*=~\/|&]{1,2}|<=?|>=?|\.{1,3}|[!?\\^]|\b(lt|gt|le|ge|eq|ne|cmp|not|and|or|xor|x)\b/,
137+
'number': /\b-?(0x[\dA-Fa-f](_?[\dA-Fa-f])*|0b[01](_?[01])*|(\d(_?\d)*)?\.?\d(_?\d)*([Ee][+-]?\d+)?)\b/,
138+
'operator': /-[rwxoRWXOezsfdlpSbctugkTBMAC]\b|\+[+=]?|-[-=>]?|\*\*?=?|\/\/?=?|=[=~>]?|~[~=]?|\|\|?=?|&&?=?|<(?:=>?|<=?)?|>>?=?|![~=]?|[%^]=?|\.(?:=|\.\.?)?|[\\?]|\bx(?:=|\b)|\b(lt|gt|le|ge|eq|ne|cmp|not|and|or|xor)\b/,
111139
'punctuation': /[{}[\];(),:]/
112140
};

components/prism-perl.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)