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

C and C++: backslash-newline continues comments #1511

Closed
zackw opened this issue Aug 1, 2018 · 4 comments · Fixed by #2229
Closed

C and C++: backslash-newline continues comments #1511

zackw opened this issue Aug 1, 2018 · 4 comments · Fixed by #2229

Comments

@zackw
Copy link

zackw commented Aug 1, 2018

In C, C++, Objective-C, and Objective-C++, the official parser specification requires backslash-newline to be removed from the input stream before comments are handled, which means it can extend comments. The most important case of this is

// this line is a comment \
this line is also part of the comment
this line is outside the comment

where both the first and second lines, but not the third, should be highlighted as a comment. Other cases include

/\
/ this line is a comment
this line is outside the comment

and

/\
* this is a block comment *\
/ outside the comment

https://prismjs.com/test.html currently gets all of these wrong. I would forgive you for not bothering to handle the second and third cases.

And of course -newline can also be spelled ??/-newline, but again, I'd forgive you for not bothering with that one.

@mAAdhaTTah
Copy link
Member

I suspect this one is going to be tough to get right with a regex.

@zackw
Copy link
Author

zackw commented Aug 19, 2018

For the line comments,

/\/(?:|\\\n)\/.*?(?:\\\n.*)*?[^\\]$/m

WFM in casual testing (see https://regex101.com/r/Q1xuiv/2 ). I didn't bother with trigraphs, because I didn't see a good way to do it without negative lookbehind, which JS regexen don't have. But trigraphs come up far less often.

@mAAdhaTTah
Copy link
Member

Ok cool, would you be able to open a PR with that change + tests? Thanks!

@zackw
Copy link
Author

zackw commented Aug 19, 2018

I'm sorry, I will not have time to do that for at least two months. (Throwing together regexes is easy. Writing tests is hard.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants