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
enh(haskell) BinaryLiterals, NumericUnderscores, HexFloatLiterals #3150
Conversation
f7bb34a
to
4d1afc7
Compare
cb8d5fe
to
b3f881e
Compare
b3f881e
to
c55ad76
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty good, just needs a few changes!
src/languages/haskell.js
Outdated
}), | ||
|
||
hljs.inherit(hljs.C_NUMBER_MODE, { | ||
begin: '(-?)(\\b0[xX][a-fA-F0-9_]+|(\\b(\\d|_)+(\\.(\\d|_)*)?|\\.(\\d|_)+)([eE][-+]?(\\d|_)+)?)' |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :-)
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
249ba7f
to
c2b2c2b
Compare
@joshgoebel I've rewritten Haskell's number parsing taking inspiration from Swift's implementation. I've included a whole bunch of test cases taken from the GHC documentation. As a happy coincidence, the highlighter now also supports the |
Awesome. This looks MUCH much closer to mergeable. :-) Not sure what all the valid/invalid is trying to accomplish since we randomly highlight some invalids? Is this sample code just copied and pasted from GHC? If so, ok, but we should add a comment saying so with a link to the source to explain what's going on and that the invalid can mostly be ignored - and that all the valid should be highlighted. If it's not then I'm not a direct copy/paste I'm sure what valid having any |
c2b2c2b
to
9afd66d
Compare
Yeah, you're right, leaving the invalids in is a bit silly. Removed! |
9afd66d
to
133b0aa
Compare
133b0aa
to
afa912b
Compare
Please remove the IE:
The |
afa912b
to
1a2a838
Compare
Good point!
That's absolutely correct. I've updated the code to reflec this. I've also added a comment to the negative number tests, in case someone else stumbles upon it in the future. Could you check whether the comment is accurate? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great other than my last question!
Restructured Haskell's number parsing, taking a cue from Swift's implementation. As a happy coincidence, this patch adds support for Haskell's `HexFloatLiterals` language extension.
1a2a838
to
29479c1
Compare
@martijnbastiaan Thanks for all the effort! |
@joshgoebel Thanks for all the patience! This is what makes me love OSS ❤️ |
Changes
Added support for the following Haskell language extensions:
It slightly over-approximates. For example, it would highlight
10_
. If that's an issue, I'll put more effort in the PR. IMO this isn't really a problem though, as it would be a syntax error anyway.Edit: sorry for all the force pushes, should be done now :)
Checklist
CHANGES.md