Skip to content

Commit

Permalink
Enhance support for floating point numbers
Browse files Browse the repository at this point in the history
Floating points numbers (eg. `2.34`, `.54`) were not being matched
correctly. This patch splits the number class regex in different variants to
make things clearer and also adds variants to better match floating points,
including the `_` character, and also the special case of version numbers
(eg. v5.38). Markup test for the new number class was also added.
  • Loading branch information
bmeneg committed Sep 4, 2023
1 parent 7a522e9 commit 186aa61
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/languages/perl.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,22 @@ export default function(hljs) {
relevance: 0
}
] };
const NUMBER = {
className: 'number',
variants: [
// in Perl numbers can have '_' in the middle, but not at the end,
// so we lookbehind the '\b' for a non-trailing '_'.
// decimal numbers
{ match: /0?\.[0-9][0-9_]+(?<!_)\b/ },
// include the special versioned number (eg. v5.38)
{ match: /\bv?(0|[1-9][0-9_]*(\.[0-9_]+)?|[1-9][0-9_]*)(?<!_)\b/ },
// non-decimal numbers
{ match: /\b0[0-7][0-7_]*(?<!_)\b/ },
{ match: /\b0x[0-9a-fA-F][0-9a-fA-F_]*(?<!_)\b/ },
{ match: /\b0b[0-1][0-1_]*(?<!_)\b/ },
],
relevance: 0
}
const STRING_CONTAINS = [
hljs.BACKSLASH_ESCAPE,
SUBST,
Expand Down Expand Up @@ -385,11 +401,7 @@ export default function(hljs) {
}
]
},
{
className: 'number',
begin: '(\\b0[0-7_]+)|(\\b0x[0-9a-fA-F_]+)|(\\b[1-9][0-9_]*(\\.[0-9_]+)?)|[0_]\\b',
relevance: 0
},
NUMBER,
{ // regexp container
begin: '(\\/\\/|' + hljs.RE_STARTERS_RE + '|\\b(split|return|print|reverse|grep)\\b)\\s*',
keywords: 'split return print reverse grep',
Expand Down
26 changes: 26 additions & 0 deletions test/markup/perl/number.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<span class="hljs-keyword">use</span> <span class="hljs-number">v5.38</span>;
<span class="hljs-keyword">use</span> <span class="hljs-number">5.0020</span>;

<span class="hljs-keyword">my</span> $number = <span class="hljs-number">9</span>;
$number = <span class="hljs-number">0</span>;
$number = 0_;
$number = <span class="hljs-number">0.99</span>;
$number = <span class="hljs-number">99</span>;
$number = <span class="hljs-number">00</span>;
$number = 99__;
$number = <span class="hljs-number">99.9</span>;
$number = <span class="hljs-number">99.99</span>;
$number = .99_;
$number = ._99;
$number = <span class="hljs-number">0x99FF</span>;
$number = <span class="hljs-number">0x99_EE_FF</span>;
$number = 0x99_EE_FF_;
$number = 0x_99_FF;
$number = <span class="hljs-number">0b00101011</span>;
$number = 0b00101011_;
$number = <span class="hljs-number">0b0010_1011</span>;
$number = 0b_0010_1011;
$number = <span class="hljs-number">0777</span>;
$number = <span class="hljs-number">07_7_7</span>;
$number = 0_777;
$number = 07_7_7_;
26 changes: 26 additions & 0 deletions test/markup/perl/number.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use v5.38;
use 5.0020;

my $number = 9;
$number = 0;
$number = 0_;
$number = 0.99;
$number = 99;
$number = 00;
$number = 99__;
$number = 99.9;
$number = 99.99;
$number = .99_;
$number = ._99;
$number = 0x99FF;
$number = 0x99_EE_FF;
$number = 0x99_EE_FF_;
$number = 0x_99_FF;
$number = 0b00101011;
$number = 0b00101011_;
$number = 0b0010_1011;
$number = 0b_0010_1011;
$number = 0777;
$number = 07_7_7;
$number = 0_777;
$number = 07_7_7_;

0 comments on commit 186aa61

Please sign in to comment.