Fix compilation of generated Rust code when token stream contains single-quoted semicolon

When using glyph embedding, we generate a character map where each code
point is a literal char.  When the font contains a semicolon and we
generate an entry for that, we write ';' and the CodeFormatter would
think the semicolon is the end of a statement and produce a newline.
That breaks the build of the generated code. Instead teach the formatter
also about single-quoted string literals.
This commit is contained in:
Simon Hausmann 2022-06-15 16:09:08 +02:00 committed by Simon Hausmann
parent 6a668fe83b
commit caa42d82c6

View file

@ -114,6 +114,7 @@ pub enum CompileError {
struct CodeFormatter<Sink> {
indentation: usize,
in_string: bool,
in_char: bool,
sink: Sink,
}
@ -121,15 +122,15 @@ impl<Sink: Write> Write for CodeFormatter<Sink> {
fn write(&mut self, mut s: &[u8]) -> std::io::Result<usize> {
let len = s.len();
while let Some(idx) = s.iter().position(|c| match c {
b'{' if !self.in_string => {
b'{' if !self.in_string && !self.in_char => {
self.indentation += 1;
true
}
b'}' if !self.in_string => {
b'}' if !self.in_string && !self.in_char => {
self.indentation -= 1;
true
}
b';' if !self.in_string => true,
b';' if !self.in_string && !self.in_char => true,
b'"' if !self.in_string => {
self.in_string = true;
false
@ -139,6 +140,15 @@ impl<Sink: Write> Write for CodeFormatter<Sink> {
self.in_string = false;
false
}
b'\'' if !self.in_char => {
self.in_char = true;
false
}
b'\'' if self.in_char => {
// FIXME! escape character
self.in_char = false;
false
}
_ => false,
}) {
let idx = idx + 1;
@ -249,7 +259,8 @@ pub fn compile_with_config(
);
let file = std::fs::File::create(&output_file_path).map_err(CompileError::SaveError)?;
let mut code_formatter = CodeFormatter { indentation: 0, in_string: false, sink: file };
let mut code_formatter =
CodeFormatter { indentation: 0, in_string: false, in_char: false, sink: file };
let generated = i_slint_compiler::generator::rust::generate(&doc);
for x in &diag.all_loaded_files {