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

(cpp) function declaration claims variable declarations incorrectly #1114

Closed
unaiur opened this issue Mar 16, 2016 · 8 comments
Closed

(cpp) function declaration claims variable declarations incorrectly #1114

unaiur opened this issue Mar 16, 2016 · 8 comments
Labels
cantfix / wontfix Impossible to fix help welcome Could use help from community language

Comments

@unaiur
Copy link

unaiur commented Mar 16, 2016

In highlight.js 9.2, the following code isn't recognized as C++:

int main()
{
    CUrlQuery q(request.GetQuery() ? request.GetQuery() : "");
}

I detected the following throw while detecting the language:
Illegal lexeme "?" for mode "function"

@unaiur
Copy link
Author

unaiur commented Mar 16, 2016

I've managed to reduce the example to its minimal expression:

This is detected as perl and it throws Illegal lexeme "?" for mode "function" while detecting C++
hljs.highlightAuto("int q(f ? f : 0);");

This is correctly detected as C++
hljs.highlightAuto("int q = f ? f : 0;");

Two options are perfectly valid expressions in C++, and the first one is the only way to call object constructors in C++ 2003

@Sannis
Copy link
Collaborator

Sannis commented Mar 16, 2016

Highlight.js relevance system works bad for such short pieces of code. On first piece:

[perl: 7, vbscript: 4, cpp: 3]

@Sannis Sannis added the on hold label Mar 16, 2016
@isagalaev
Copy link
Member

@Sannis the actual problem is not mis-detection. We have a rather simplistic rule for highlighting function definitions, so anything that starts with a b( is considered one. In this example it's not a function definition at all, it's a variable initialization. This is very closely related to #1104.

I'm not sure how to fix it yet. One simple answer would be to simply drop highlighting function definitions altogether but that would remove a lot of relevance from C/C++ and degrade highlighting.

@isagalaev
Copy link
Member

Status update: I believe an oncoming backtracking parser (#1140) will allow us to constrain a function definition to only have something that looks like arguments within the parentheses, so it won't touch constructs like type ident(a + b);

@joshgoebel joshgoebel changed the title Error highlighting C++ code (cpp) function declaration mode claims variable declarations incorrectly Oct 22, 2019
@joshgoebel joshgoebel added help welcome Could use help from community and removed on hold labels Oct 22, 2019
@joshgoebel
Copy link
Member

Related to #1222

@joshgoebel
Copy link
Member

joshgoebel commented Dec 24, 2019

If anyone gets around to this we should add tests for some of the great examples that can be found here:

https://stackoverflow.com/a/4324780/12430243

Regarding lambdas, function within function, static type functions inside functions, etc.

@joshgoebel
Copy link
Member

This is much improved since 2016 though... the code isn't flagged as illegal anymore... but it's still mistakenly assumed to be a function definition.

@joshgoebel joshgoebel changed the title (cpp) function declaration mode claims variable declarations incorrectly (cpp) function declaration claims variable declarations incorrectly Sep 11, 2020
@joshgoebel
Copy link
Member

Closing because:

  • Original issue is actually resolved: "the following code isn't recognized as C++"
  • Secondary issue "function declaration claims variable declarations incorrectly" is impossible to resolve with current parser AFAIK, so this issue is effectively "dead".
  • If there is some smart way to do this with the current parser no one has found it in 4 years, so I'm guessing no.

Rather than leave issues like this potentially open forever I am closing. One day if we switch to a backtracking parser we can naturally re-review edge cases like this (among other things).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cantfix / wontfix Impossible to fix help welcome Could use help from community language
Projects
None yet
Development

No branches or pull requests

4 participants