-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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) grammar confused by complex Template << >> patterns #2102
Comments
The actual issue: #1412 |
This is indeed the first issue, but the second piece of code does not feature any wchar_t character literals. |
If i could read that code I might try fixing this, but I've never fully understood the crazier C/C++ syntactic stuff... I'm guessing it gets stuck inside "struct" and doesn't find what it expects so it never escapes, and that breaks the rest of the parsing. |
It's possible that the mess of < and > on that line is what breaks it. |
It's highly likely. :-) But since I don't understand C++ enough (at this level) [templates] I don't even know what's possible and what isn't, and hence no way I could hope to alter the parser for this without potentially just making it even worse. I actually don't see any support for templates in the parser right now. |
Nevermidn it has it: {
className: 'class',
beginKeywords: 'class struct', end: /[{;:]/,
contains: [
{begin: /</, end: />/, contains: ['self']}, // skip generic stuff
hljs.TITLE_MODE
]
} It doesn't look like the < and > pairs match to me (in your code sample)... so that would be a problem. |
Is |
AFAIK, << is always an operator (if there's cases where it can not be, I have never encountered them) In this snippet, it is an operator, albeit it's very hidden (basically it evaluates if |
Note that >> can not be an operator, mostly in cases where templates are nested, eg |
Well, as I said if that is a given then this should be fixable. :) Assuming that's the core issue, which seems likely. |
Just checked, |
I opened a PR for the string issue: #2156 |
Fixed |
This hasn't resolved the second snippet |
Yeah, this is still open. Someone else might have to look into second issue. |
I came across this today, a function with a trailing return type appears to break highlighting too: auto foo() -> void; |
That is likely a separate issue. |
@klmr Lets step back and take a second look at this... I worry we've made it too complex... all the dealing with nested What about: diff --git a/src/languages/c-like.js b/src/languages/c-like.js
index ece09524..47afb47d 100644
--- a/src/languages/c-like.js
+++ b/src/languages/c-like.js
@@ -132,7 +132,7 @@ export default function(hljs) {
// definition and shouldn't highlight anything that looks like one:
// `return some()`, `else if()`, `(x*sum(1, 2))`
variants: [
- {begin: /=/, end: /;/},
+ {begin: /=/, end: /[;<>]/},
{begin: /\(/, end: /\)/},
{beginKeywords: 'new throw return else', end: /;/}
],
@@ -225,10 +225,10 @@ export default function(hljs) {
},
{
className: 'class',
- beginKeywords: 'class struct', end: /[{;:]/,
+ beginKeywords: 'class struct', end: /[{;:<>,]/,
contains: [
{ beginKeywords: "final" },
- {begin: /</, end: />/, contains: ['self']}, // skip generic stuff
hljs.TITLE_MODE
]
} IE, just terminating most rules when they see a I also see a tiny issue with something like What would be a few other cases this would not handle so well? |
@joshgoebel Despite my radio silence I’be been mulling this over and I’ve reached a similar (though not quite the same) conclusion. It’s definitely a good fix in the first instance, and we can add improved heuristics later. One thing I’d probably do is to collect test cases that currently fail in a separate PR or issue. If you want to apply your diff as a “quick fix” go ahead, I think it shouldn’t work too too badly. I would add Something else that I just thought of: how would this handle C++11’s enum class Boolean : char {
True, False, FileNotFound
} |
Does that require special handling? Is enum just a keyword there and it would just work with the existing rules? |
It currently doesn’t require special handling but once added to the <class><keyword>enum</keyword> <title>class</title> <title>Boolean</title> :</class> <keyword>unsigned</keyword>… … but adding |
This code results in everything after
L':'
to be highlighted as if it was a string: (Editor: This is now resolved, the second issue still standing).On this one, it just completely stops highlighting after the second
is_streamable
Both pieces above is completely correct code which compiles, assuming the right headers are included
The text was updated successfully, but these errors were encountered: