highlightjs / highlight.js

JavaScript syntax highlighter with language auto-detection and zero dependencies.
https://highlightjs.org/
BSD 3-Clause "New" or "Revised" License
23.31k stars 3.52k forks source link

fix(rust): add support for r# raw identifiers #3947

Closed Jaebaek-Lee closed 6 months ago

Jaebaek-Lee commented 6 months ago

https://github.com/highlightjs/highlight.js/issues/3924

Changes

In order to support raw identifiers in Rust, the regular expressions for UNDERSCORE_IDENT_RE and IDENT_RE need to be modified to include the r# keyword. However, since modes.js is shared by other languages, we need to ensure that only rust supports the r# keyword.

Original (modes.js)

const UNDERSCORE_IDENT_RE = '[a-zA-Z_]\\w*';
const IDENT_RE = '[a-zA-Z]\\w*';

Change (rust.js)

So, add the following code to rust.js to support the r# keyword only in rust.

const UNDERSCORE_IDENT_RE = '(r#)?[a-zA-Z_]\\w*';
const IDENT_RE = '(r#)?[a-zA-Z]\\w*';

Checklist

github-actions[bot] commented 6 months ago

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

3 files changed

Total change +35 B

View Changes | file | base | pr | diff | | --- | --- | --- | --- | | es/languages/rust.min.js | 1.46 KB | 1.48 KB | +18 B | | highlight.min.js | 8.21 KB | 8.21 KB | -1 B | | languages/rust.min.js | 1.47 KB | 1.49 KB | +18 B |
joshgoebel commented 6 months ago

When the identifier is referenced again, try appears as a reserved keyword again in r#try.

Ok, now we're talking about false positives, which is a different situation entirely. I'm not sure we should use the keyword engine to consume these (but not match, and hence not apply scoping)... can we add a few failing tests for this case and I'll take another look?

Jaebaek-Lee commented 6 months ago

When the identifier is referenced again, try appears as a reserved keyword again in r#try.

Ok, now we're talking about false positives, which is a different situation entirely. I'm not sure we should use the keyword engine to consume these (but not match, and hence not apply scoping)... can we add a few failing tests for this case and I'll take another look?

Good! I'll create a new issue for the new problem and figure out how to fix it.

Please feel free to provide any additional feedback or suggestions for improvement in this PR. I'm open to making further corrections if needed.

Thanks so much!