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

(python) Keywords directly following numbers are not detected #2985

Closed
Hirse opened this issue Feb 2, 2021 · 6 comments · Fixed by #3367
Closed

(python) Keywords directly following numbers are not detected #2985

Hirse opened this issue Feb 2, 2021 · 6 comments · Fixed by #3367
Labels
autoclose Flag things to future autoclose. bug help welcome Could use help from community language

Comments

@Hirse
Copy link
Contributor

Hirse commented Feb 2, 2021

Describe the issue
As keywords and variables cannot start with a number in Python, the space between a number and a keyword can be skipped.
The highlighting should separate the number and the keyword part.

Actual output

Which language seems to have the issue?
python

Are you using highlight or highlightAuto?
highlight

Sample Code to Reproduce

print(1if 0==0else"b")

Expected behavior
Expeted output

(Also see GitHub's highlighting above)

(Also, also should print be a built-in here?)

Additional context
Found on this StackExchange answer:
https://codegolf.stackexchange.com/a/218451/25026
StackExchange Code

@Hirse Hirse added bug help welcome Could use help from community language labels Feb 2, 2021
@joshgoebel
Copy link
Member

joshgoebel commented Feb 5, 2021

print(1if 0==0else"b")

First facts: This if of course broken since our number regex in Python requires word boundaries (which do not exist between 0 and e... making 0 not a number... and the keyword regex is \w+ (the default), which matches 0else which is NOT a keyword.

As keywords and variables cannot start with a number in Python, the space between a number and a keyword can be skipped.

This is terrible style though and it doesn't bother me much if we don't support it (and code golf in general). I also imagine it could result in potential false positives if we remove the boundary checks for numbers. If someone wants to play around with a fix happy to review a PR. But if the fix has ill effects for regular (non golf code) code or detrimental effects on auto-detection then that's an issue.

(Also, also should print be a built-in here?)

See comments in python.js regarding this.

@joshgoebel
Copy link
Member

It's also possible one half of this might be more easily fixed than the other... perhaps the keyword can be fixed just by declaring that keywords in Python can never start with a numeral? Is this true?

@Hirse
Copy link
Contributor Author

Hirse commented Feb 5, 2021

This is terrible style though and it doesn't bother me much if we don't support it (and code golf in general).

I absolutely agree, but wanted to create the issue anyway just in case it's easier to fix than I thought.

It's also possible one half of this might be more easily fixed than the other... perhaps the keyword can be fixed just by declaring that keywords in Python can never start with a numeral? Is this true?

I don't think there are keywords that start with a number, but not entirely sure.

@joshgoebel
Copy link
Member

joshgoebel commented Feb 5, 2021

https://docs.python.org/3.8/reference/lexical_analysis.html#keywords

So I think just altering $pattern would fix half the issues here.

@joshgoebel joshgoebel added the good first issue Should be easier for first time contributors label Feb 8, 2021
@joshgoebel
Copy link
Member

@joshgoebel
Copy link
Member

joshgoebel commented Mar 17, 2021

Reaching out to the author of the PR for our Python numeric support so see if there is a reason for all those \b at the end of number matches. But if this boils down to "better support average python" vs "better support code golf" (with no easy/clear solution) then I'll probably close this a #wontfix and just say that code golf is not our specialty. We'll see.

@joshgoebel joshgoebel removed the good first issue Should be easier for first time contributors label Mar 17, 2021
@joshgoebel joshgoebel added the autoclose Flag things to future autoclose. label Oct 7, 2021
gibson042 added a commit to gibson042/highlight.js that referenced this issue Oct 19, 2021
gibson042 added a commit to gibson042/highlight.js that referenced this issue Oct 19, 2021
gibson042 added a commit to gibson042/highlight.js that referenced this issue Oct 19, 2021
gibson042 added a commit to gibson042/highlight.js that referenced this issue Oct 19, 2021
gibson042 added a commit to gibson042/highlight.js that referenced this issue Oct 19, 2021
joshgoebel pushed a commit that referenced this issue Oct 22, 2021
…without whitespace (#3367)

Fixes #2985.

* enh(python) Add tests for keyword after numeric literal
* fix(python) Fix recognition of numeric literals followed by keywords without whitespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autoclose Flag things to future autoclose. bug help welcome Could use help from community language
Projects
None yet
2 participants