Skip to content

Commit

Permalink
Add support for Perl new classes system
Browse files Browse the repository at this point in the history
Perl got a new builtin class support which brought new keywords like
`class`, `method` and `field`, being that some support versioning and
attributes. This commit adds the new keywords with the additional features
they support (eg. attributes and version), alongside their markup test.
  • Loading branch information
bmeneg committed Sep 4, 2023
1 parent 186aa61 commit 57f5461
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
50 changes: 35 additions & 15 deletions src/languages/perl.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default function(hljs) {
'chown',
'chr',
'chroot',
'class',
'close',
'closedir',
'connect',
Expand Down Expand Up @@ -55,6 +56,7 @@ export default function(hljs) {
'exit',
'exp',
'fcntl',
'field',
'fileno',
'flock',
'for',
Expand Down Expand Up @@ -114,6 +116,7 @@ export default function(hljs) {
'lt',
'ma',
'map',
'method',
'mkdir',
'msgctl',
'msgget',
Expand Down Expand Up @@ -258,19 +261,28 @@ export default function(hljs) {
end: /\}/
// contains defined later
};
const VAR = { variants: [
{ begin: /\$\d/ },
{ begin: regex.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
}
] };
const ATTRS = {
className: 'attribute',
begin: /\s+:\s*\w+/,
end: /(\\s*\\(.*?\\))?/,
};
const VAR = {
variants: [
{ begin: /\$\d/ },
{ begin: regex.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
}
],
contains: [ ATTRS ],
};
const NUMBER = {
className: 'number',
variants: [
Expand Down Expand Up @@ -443,11 +455,19 @@ export default function(hljs) {
},
{
className: 'function',
beginKeywords: 'sub',
beginKeywords: 'sub method',
end: '(\\s*\\(.*?\\))?[;{]',
excludeEnd: true,
relevance: 5,
contains: [ hljs.TITLE_MODE ]
contains: [ hljs.TITLE_MODE, ATTRS ]
},
{
className: 'class',
beginKeywords: 'class',
end: '[;{]',
excludeEnd: true,
relevance: 5,
contains: [ hljs.TITLE_MODE, ATTRS, NUMBER ]
},
{
begin: '-\\w\\b',
Expand Down
12 changes: 12 additions & 0 deletions test/markup/perl/class.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<span class="hljs-keyword">use</span> <span class="hljs-number">v5.38</span>;

<span class="hljs-keyword">use</span> Object::Pad;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Example</span>::<span class="hljs-title">Subclass</span><span class="hljs-attribute"> :isa</span>(<span class="hljs-title">Example</span>::<span class="hljs-title">Base</span> <span class="hljs-number">2.345</span>) </span>{
<span class="hljs-keyword">field</span> $_config<span class="hljs-attribute"> :param</span><span class="hljs-attribute"> :reader</span><span class="hljs-attribute"> :writer</span>;
<span class="hljs-keyword">field</span> $_name<span class="hljs-attribute"> : param</span> = <span class="hljs-string">&#x27;Test&#x27;</span>;

<span class="hljs-function"><span class="hljs-keyword">method</span> <span class="hljs-title">test</span>() </span>{
$_name == <span class="hljs-string">&#x27;Test&#x27;</span> ? <span class="hljs-string">&#x27;y&#x27;</span> : <span class="hljs-string">&#x27;n&#x27;</span>;
}
}
12 changes: 12 additions & 0 deletions test/markup/perl/class.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use v5.38;

use Object::Pad;

class Example::Subclass :isa(Example::Base 2.345) {
field $_config :param :reader :writer;
field $_name : param = 'Test';

method test() {
$_name == 'Test' ? 'y' : 'n';
}
}

0 comments on commit 57f5461

Please sign in to comment.