Skip to content

Commit

Permalink
enh(delphi) Improve handling of numeric literals (highlightjs#3932)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cirras authored Nov 24, 2023
1 parent a832789 commit 6317acd
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Core Grammars:
- fix(yaml) fix for yaml with keys having brackets highlighted incorrectly [Aneesh Kulkarni][]
- fix(bash) fix # within token being detected as the start of a comment [Felix Uhl][]
- fix(python) fix `or` conflicts with string highlighting [Mohamed Ali][]
- enh(delphi) allow digits to be omitted for hex and binary literals [Jonah Jeleniewski][]
- enh(delphi) add support for digit separators [Jonah Jeleniewski][]
- enh(delphi) add support for character strings with non-decimal numerics [Jonah Jeleniewski][]

New Grammars:

Expand All @@ -32,6 +35,7 @@ Developer Tool:
[nataliia-radina]: https://github.com/Nataliia-Radina
[Robloxian Demo]: https://github.com/RobloxianDemo
[Paul Tsnobiladzé]: https://github.com/tsnobip
[Jonah Jeleniewski]: https://github.com/cirras


## Version 11.9.0
Expand Down
25 changes: 20 additions & 5 deletions src/languages/delphi.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,36 @@ export default function(hljs) {
relevance: 0,
// Source: https://www.freepascal.org/docs-html/ref/refse6.html
variants: [
{
// Regular numbers, e.g., 123, 123.456.
match: /\b\d[\d_]*(\.\d[\d_]*)?/ },
{
// Hexadecimal notation, e.g., $7F.
begin: '\\$[0-9A-Fa-f]+' },
match: /\$[\dA-Fa-f_]+/ },
{
// Hexadecimal literal with no digits
match: /\$/,
relevance: 0 },
{
// Octal notation, e.g., &42.
begin: '&[0-7]+' },
match: /&[0-7][0-7_]*/ },
{
// Binary notation, e.g., %1010.
begin: '%[01]+' }
match: /%[01_]+/ },
{
// Binary literal with no digits
match: /%/,
relevance: 0 }
]
};
const CHAR_STRING = {
className: 'string',
begin: /(#\d+)+/
variants: [
{ match: /#\d[\d_]*/ },
{ match: /#\$[\dA-Fa-f][\dA-Fa-f_]*/ },
{ match: /#&[0-7][0-7_]*/ },
{ match: /#%[01][01_]*/ }
]
};
const CLASS = {
begin: hljs.IDENT_RE + '\\s*=\\s*class\\s*\\(',
Expand Down Expand Up @@ -219,7 +235,6 @@ export default function(hljs) {
contains: [
STRING,
CHAR_STRING,
hljs.NUMBER_MODE,
NUMBER,
CLASS,
FUNCTION,
Expand Down
12 changes: 12 additions & 0 deletions test/markup/delphi/character-string.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<span class="hljs-keyword">const</span>
CDecimalCharacterString = <span class="hljs-string">#123</span>;
CHexadecimalCharacterString = <span class="hljs-string">#$7B</span>;
COctalCharacterString = <span class="hljs-string">#&amp;123</span>;
CBinaryCharacterString = <span class="hljs-string">#%1111011</span>;

CDecimalCharacterStringWithSeparator = <span class="hljs-string">#1_23</span>;
CHexadecimalCharacterStringWithSeparator = <span class="hljs-string">#$7_B</span>;
COctalCharacterStringWithSeparator = <span class="hljs-string">#&amp;123</span>;
CBinaryCharacterStringWithSeparator = <span class="hljs-string">#%1_111011</span>;

CMultipleCharacterStrings = <span class="hljs-string">#123</span><span class="hljs-string">#$ABC</span><span class="hljs-string">#&amp;765</span><span class="hljs-string">#%0011001</span>;
12 changes: 12 additions & 0 deletions test/markup/delphi/character-string.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const
CDecimalCharacterString = #123;
CHexadecimalCharacterString = #$7B;
COctalCharacterString = #&123;
CBinaryCharacterString = #%1111011;

CDecimalCharacterStringWithSeparator = #1_23;
CHexadecimalCharacterStringWithSeparator = #$7_B;
COctalCharacterStringWithSeparator = #&123;
CBinaryCharacterStringWithSeparator = #%1_111011;

CMultipleCharacterStrings = #123#$ABC#&765#%0011001;
18 changes: 18 additions & 0 deletions test/markup/delphi/numeric-literal.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<span class="hljs-keyword">const</span>
CDecimal = <span class="hljs-number">123456</span>;
CFloat = <span class="hljs-number">123.456</span>;
CHexadecimal = <span class="hljs-number">$ABC123</span>;
COctal = <span class="hljs-number">&amp;123456</span>;
CBinary = <span class="hljs-number">%000111</span>;

CDecimalWithSeparator = <span class="hljs-number">123_456</span>;
CFloatWithSeparator = <span class="hljs-number">12_3.78_9</span>;
CHexadecimalWithSeparator = <span class="hljs-number">$ABC_123</span>;
COctalWithSeparator = <span class="hljs-number">&amp;123_456</span>;
CBinaryWithSeparator = <span class="hljs-number">%000_111</span>;

CHexadecimalWithOnlySeparator = <span class="hljs-number">$_</span>;
CBinaryWithOnlySeparator = <span class="hljs-number">%_</span>;

CHexadecimalWithNoDigits = <span class="hljs-number">$</span>;
CBinaryWithNoDigits = <span class="hljs-number">%</span>;
18 changes: 18 additions & 0 deletions test/markup/delphi/numeric-literal.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const
CDecimal = 123456;
CFloat = 123.456;
CHexadecimal = $ABC123;
COctal = &123456;
CBinary = %000111;

CDecimalWithSeparator = 123_456;
CFloatWithSeparator = 12_3.78_9;
CHexadecimalWithSeparator = $ABC_123;
COctalWithSeparator = &123_456;
CBinaryWithSeparator = %000_111;

CHexadecimalWithOnlySeparator = $_;
CBinaryWithOnlySeparator = %_;

CHexadecimalWithNoDigits = $;
CBinaryWithNoDigits = %;

0 comments on commit 6317acd

Please sign in to comment.