From a0a994bc82925c0be0b427a3647fc08fd411acc3 Mon Sep 17 00:00:00 2001 From: styrix560 Date: Sun, 21 Apr 2024 14:33:13 +0200 Subject: [PATCH 1/2] Add test cases --- compiler/frontend/src/string_to_rcst/text.rs | 37 ++++++++----------- .../frontend/src/string_to_rcst/whitespace.rs | 12 +++++- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/compiler/frontend/src/string_to_rcst/text.rs b/compiler/frontend/src/string_to_rcst/text.rs index 9359d06dc..04c812baf 100644 --- a/compiler/frontend/src/string_to_rcst/text.rs +++ b/compiler/frontend/src/string_to_rcst/text.rs @@ -20,12 +20,11 @@ pub fn text(input: &str, indentation: usize) -> Option<(&str, Rcst)> { let (new_input, mut opening_whitespace) = whitespaces_and_newlines(input, indentation + 1, false); + dbg!(&opening_whitespace); + // If the string does not contain any newlines, parse the whitespace in // front of the string as part of the string and not as trailing whitespace. // This fixes https://github.com/candy-lang/candy/issues/896. - // if the string does not contain any newlines, parse the whitespace in - // front of the string as part of the string and not as trailing whitespace - // this fixes issue #896 if opening_whitespace.iter().any(|it| it.is_newline()) { input = new_input; } else { @@ -64,6 +63,8 @@ pub fn text(input: &str, indentation: usize) -> Option<(&str, Rcst)> { whitespaces_and_newlines(input, indentation + 1, false); input = input_after_whitespace; + dbg!(input); + let mut whitespace_before_closing_quote = if let Some(last_newline_index) = whitespace .iter() .enumerate() @@ -79,12 +80,15 @@ pub fn text(input: &str, indentation: usize) -> Option<(&str, Rcst)> { whitespace }; + dbg!(&whitespace_before_closing_quote); + // Allow closing quotes to have the same indentation level as the opening quotes let (input_after_whitespace, whitespace) = if newline(input).is_some() { whitespaces_and_newlines(input, indentation, false) } else { (input, Vec::new()) }; + dbg!(input_after_whitespace); let closing_quote = if let Some((input_after_double_quote, closing_double_quote)) = double_quote(input_after_whitespace) && let Some((input_after_single_quotes, closing_single_quotes)) = parse_multiple( @@ -297,8 +301,8 @@ mod test { closing_double_quote: DoubleQuote closing_single_quotes: "###); - // issue: https://github.com/candy-lang/candy/issues/1016 - assert_rich_ir_snapshot!(text("\"\n text\n\"", 0), @r###" + // https://github.com/candy-lang/candy/issues/1016 + assert_rich_ir_snapshot!(text("\"\n foo\n bar\n\"", 0), @r###" Remaining input: "" Parsed: Text: opening: TrailingWhitespace: @@ -308,18 +312,19 @@ mod test { whitespace: Newline "\n" Whitespace " " - Whitespace " " parts: + TextPart "foo" TrailingWhitespace: - child: TextPart "text" + child: TextNewline "\n" whitespace: - Newline "\n" + Whitespace " " + TextPart " bar" closing: ClosingText: closing_double_quote: DoubleQuote closing_single_quotes: "###); - // https://github.com/candy-lang/candy/issues/1016 - assert_rich_ir_snapshot!(text("\"\n foo\n bar\n\"", 0), @r###" + // issue: https://github.com/candy-lang/candy/issues/1016 + assert_rich_ir_snapshot!(text("\"\n text\n\"", 0), @r###" Remaining input: "" Parsed: Text: opening: TrailingWhitespace: @@ -330,21 +335,11 @@ mod test { Newline "\n" Whitespace " " parts: - TextPart "foo" - TrailingWhitespace: - child: TextNewline "\n" - whitespace: - Whitespace " " - Whitespace " " - TrailingWhitespace: - child: TextPart "bar" - whitespace: - Newline "\n" + TextPart " text" closing: ClosingText: closing_double_quote: DoubleQuote closing_single_quotes: "###); - assert_rich_ir_snapshot!(text("\" foobar \"", 0), @r###" Remaining input: "" Parsed: Text: diff --git a/compiler/frontend/src/string_to_rcst/whitespace.rs b/compiler/frontend/src/string_to_rcst/whitespace.rs index 31814c095..51ce40846 100644 --- a/compiler/frontend/src/string_to_rcst/whitespace.rs +++ b/compiler/frontend/src/string_to_rcst/whitespace.rs @@ -122,7 +122,10 @@ pub fn whitespaces_and_newlines( let mut new_input = input; let mut new_parts = vec![]; let mut is_sufficiently_indented = true; + let mut current_indendation_level = 0; + println!("\ncalled with {input:?}, {indentation}\n"); loop { + println!("indent: {current_indendation_level}/{indentation}"); let new_input_from_iteration_start = new_input; if also_comments @@ -137,12 +140,15 @@ pub fn whitespaces_and_newlines( } if let Some((new_new_input, newline)) = newline(new_input) { + current_indendation_level = 0; new_input = new_new_input; new_parts.push(newline); is_sufficiently_indented = false; } - - if let Some((new_new_input, whitespace)) = leading_indentation(new_input, indentation) { + if current_indendation_level < indentation + && let Some((new_new_input, whitespace)) = leading_indentation(new_input, indentation) + { + current_indendation_level += 1; new_input = new_new_input; new_parts.push(whitespace); @@ -154,6 +160,8 @@ pub fn whitespaces_and_newlines( new_parts.push(whitespace); } + println!("new input after iter: {new_input:?}\n"); + if new_input == new_input_from_iteration_start { break; } From 84909b0b0a1e769dc997dd840ebb193514ee5269 Mon Sep 17 00:00:00 2001 From: styrix560 <60841004+styrix560@users.noreply.github.com> Date: Sun, 21 Apr 2024 14:54:51 +0200 Subject: [PATCH 2/2] Fix parsing of whitespace after indentation --- compiler/frontend/src/string_to_rcst/text.rs | 17 ++++++++-------- .../frontend/src/string_to_rcst/whitespace.rs | 20 +++++++------------ 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/compiler/frontend/src/string_to_rcst/text.rs b/compiler/frontend/src/string_to_rcst/text.rs index 04c812baf..befeda729 100644 --- a/compiler/frontend/src/string_to_rcst/text.rs +++ b/compiler/frontend/src/string_to_rcst/text.rs @@ -20,8 +20,6 @@ pub fn text(input: &str, indentation: usize) -> Option<(&str, Rcst)> { let (new_input, mut opening_whitespace) = whitespaces_and_newlines(input, indentation + 1, false); - dbg!(&opening_whitespace); - // If the string does not contain any newlines, parse the whitespace in // front of the string as part of the string and not as trailing whitespace. // This fixes https://github.com/candy-lang/candy/issues/896. @@ -63,8 +61,6 @@ pub fn text(input: &str, indentation: usize) -> Option<(&str, Rcst)> { whitespaces_and_newlines(input, indentation + 1, false); input = input_after_whitespace; - dbg!(input); - let mut whitespace_before_closing_quote = if let Some(last_newline_index) = whitespace .iter() .enumerate() @@ -80,15 +76,12 @@ pub fn text(input: &str, indentation: usize) -> Option<(&str, Rcst)> { whitespace }; - dbg!(&whitespace_before_closing_quote); - // Allow closing quotes to have the same indentation level as the opening quotes let (input_after_whitespace, whitespace) = if newline(input).is_some() { whitespaces_and_newlines(input, indentation, false) } else { (input, Vec::new()) }; - dbg!(input_after_whitespace); let closing_quote = if let Some((input_after_double_quote, closing_double_quote)) = double_quote(input_after_whitespace) && let Some((input_after_single_quotes, closing_single_quotes)) = parse_multiple( @@ -318,7 +311,10 @@ mod test { child: TextNewline "\n" whitespace: Whitespace " " - TextPart " bar" + TrailingWhitespace: + child: TextPart " bar" + whitespace: + Newline "\n" closing: ClosingText: closing_double_quote: DoubleQuote closing_single_quotes: @@ -335,7 +331,10 @@ mod test { Newline "\n" Whitespace " " parts: - TextPart " text" + TrailingWhitespace: + child: TextPart " text" + whitespace: + Newline "\n" closing: ClosingText: closing_double_quote: DoubleQuote closing_single_quotes: diff --git a/compiler/frontend/src/string_to_rcst/whitespace.rs b/compiler/frontend/src/string_to_rcst/whitespace.rs index 51ce40846..ee2dc2619 100644 --- a/compiler/frontend/src/string_to_rcst/whitespace.rs +++ b/compiler/frontend/src/string_to_rcst/whitespace.rs @@ -122,10 +122,7 @@ pub fn whitespaces_and_newlines( let mut new_input = input; let mut new_parts = vec![]; let mut is_sufficiently_indented = true; - let mut current_indendation_level = 0; - println!("\ncalled with {input:?}, {indentation}\n"); loop { - println!("indent: {current_indendation_level}/{indentation}"); let new_input_from_iteration_start = new_input; if also_comments @@ -140,28 +137,23 @@ pub fn whitespaces_and_newlines( } if let Some((new_new_input, newline)) = newline(new_input) { - current_indendation_level = 0; new_input = new_new_input; new_parts.push(newline); is_sufficiently_indented = false; } - if current_indendation_level < indentation - && let Some((new_new_input, whitespace)) = leading_indentation(new_input, indentation) - { - current_indendation_level += 1; + if let Some((new_new_input, whitespace)) = leading_indentation(new_input, indentation) { new_input = new_new_input; new_parts.push(whitespace); input = new_input; parts.append(&mut new_parts); is_sufficiently_indented = true; - } else if let Some((new_new_input, whitespace)) = single_line_whitespace(new_input) { + } + if let Some((new_new_input, whitespace)) = single_line_whitespace(new_input) { new_input = new_new_input; new_parts.push(whitespace); } - println!("new input after iter: {new_input:?}\n"); - if new_input == new_input_from_iteration_start { break; } @@ -239,16 +231,18 @@ mod test { assert_rich_ir_snapshot!( whitespaces_and_newlines("\n foo", 0, true), @r###" - Remaining input: " foo" + Remaining input: "foo" Parsed: Newline "\n" + Whitespace " " "### ); assert_rich_ir_snapshot!( whitespaces_and_newlines(" \n foo", 0, true), @r###" - Remaining input: " foo" + Remaining input: "foo" Parsed: Whitespace " " Newline "\n" + Whitespace " " "### ); assert_rich_ir_snapshot!(