Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(haskell) BinaryLiterals, NumericUnderscores, HexFloatLiterals #3150

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ Grammars:
- enh(c) Update keyword list for C11/C18 (#3010) [Josh Goebel][]
- enh(parser) highlight object properties (#3072) [Josh Goebel][]
- enh(javascript/typescript) highlight object properties (#3072) [Josh Goebel][]
- enh(haskell) add support for BinaryLiterals (#3150) [Martijn Bastiaan][]
- enh(haskell) add support for NumericUnderscores (#3150) [Martijn Bastiaan][]

New Languages:

Expand Down
8 changes: 7 additions & 1 deletion src/languages/haskell.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,16 @@ export default function(hljs) {
PREPROCESSOR,

// Literals and names.
hljs.inherit(hljs.BINARY_NUMBER_MODE, {
begin: '\\b(0b[01_]+)'
}),

hljs.inherit(hljs.C_NUMBER_MODE, {
begin: '(-?)(\\b0[xX][a-fA-F0-9_]+|(\\b(\\d|_)+(\\.(\\d|_)*)?|\\.(\\d|_)+)([eE][-+]?(\\d|_)+)?)'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally we do not match - since it could often by unary or binary and we do not try and figure out such distinctions. (very hard to do also without look-behind)

Also please add additional tests for some of the e variants, etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there two variants here? (hex and regular?) It seems so. Please split them out into variants for readability and maintenance rather than using a more complex single regex. See many other grammars for examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, I just copied it from C_NUMBER_MODE. I'll make a few tests though :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if it's exactly the same it of course shouldn't need to be copied at all... but if it's now specific to this language then we should clean it up into variants for future use - which is just the general policy on such things.

I'd suggest we could perhaps clean up C_NUMBER_MODE as well but too many grammars are likely dependent on it being exactly how it is now - but that doesn't mean we can't have nicer and easy to read matches in the individual grammars. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add such thoughts to the v12 list. v11 is already big enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah let me clarify a bit: I copied it and then added |_ to optionally match the underscores.

I'm a bit fuzzy on what you want me to do; should I just make a clean version for Haskell numbers?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://github.com/highlightjs/highlight.js/blob/main/src/languages/swift.js#L121 for an example.

When we start adding custom numeric rules to grammars then we always do it as nicely as possible (for future readability and maintenance). That means separate variants for hex, binary, etc... (as you see in Swift) not just one large regex with multiple rules "hidden" inside.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent. I'll get to it tonight. Thanks for the quick responses :)

}),

// TODO: characters.
hljs.QUOTE_STRING_MODE,
hljs.C_NUMBER_MODE,
CONSTRUCTOR,
hljs.inherit(hljs.TITLE_MODE, {
begin: '^[_a-z][\\w\']*'
Expand Down
3 changes: 3 additions & 0 deletions test/markup/haskell/numbers.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<span class="hljs-number">0b001_001</span>
<span class="hljs-number">0x000_0FF</span>
<span class="hljs-number">132_15</span>
3 changes: 3 additions & 0 deletions test/markup/haskell/numbers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0b001_001
0x000_0FF
132_15