trishume / syntect

Rust library for syntax highlighting using Sublime Text syntax definitions.
https://docs.rs/syntect
MIT License
1.89k stars 132 forks source link

C++ string bug when using a '%' character #509

Open P-E-Meunier opened 9 months ago

P-E-Meunier commented 9 months ago

When using the C or C++ parser, the '%' character in a string is misinterpreted as a "placeholder".

This can be observed by trying to format "% + 10px". Syntect outputs <span class="constant other placeholder c">% + 10p</span>x instead of the expected % + 10px.

Complete example:

extern crate syntect;
use syntect::util::{LinesWithEndings};
use syntect::html::{ClassedHTMLGenerator, ClassStyle};
fn main() {
    let syntax_set = syntect::parsing::SyntaxSet::load_defaults_newlines();
    let syntax = syntax_set.find_syntax_by_extension("c++").unwrap();
    let mut h = ClassedHTMLGenerator::new_with_class_style(syntax, &syntax_set, ClassStyle::Spaced);
    for line in LinesWithEndings::from(r#""% + 10px""#) {
        h.parse_html_for_line_which_includes_newline(line).unwrap();
    }
    assert_eq!(h.finalize(), r#"<span class="source c++"><span class="string quoted double c"><span class="punctuation definition string begin c">&quot;</span>% + 10px<span class="punctuation definition string end c">&quot;</span></span></span>"#);
}