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

Fix text with leading whitespace parsing #1015

Merged
merged 6 commits into from
Apr 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 101 additions & 2 deletions compiler/frontend/src/string_to_rcst/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ use tracing::instrument;
pub fn text(input: &str, indentation: usize) -> Option<(&str, Rcst)> {
let (input, opening_single_quotes) = parse_multiple(input, single_quote, None)?;
let (mut input, opening_double_quote) = double_quote(input)?;
let (new_input, opening_whitespace) = whitespaces_and_newlines(input, indentation + 1, false);
input = new_input;

let (new_input, mut opening_whitespace) =
whitespaces_and_newlines(input, indentation + 1, false);

styrix560 marked this conversation as resolved.
Show resolved Hide resolved
// 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 opening_whitespace.iter().any(|it| it.is_newline()) {
input = new_input;
} else {
opening_whitespace = vec![];
}

let (mut opening_whitespace, mut parts) = if let Some(second_newline_index) = opening_whitespace
.iter()
Expand Down Expand Up @@ -272,6 +282,95 @@ mod test {
#[test]
fn test_text() {
assert_rich_ir_snapshot!(text("foo", 0), @"Nothing was parsed");
assert_rich_ir_snapshot!(text("\" foobar \"", 0), @r###"
Remaining input: ""
Parsed: Text:
opening: OpeningText:
opening_single_quotes:
opening_double_quote: DoubleQuote
parts:
TextPart " foobar "
closing: ClosingText:
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###"
Remaining input: ""
Parsed: Text:
opening: TrailingWhitespace:
child: OpeningText:
opening_single_quotes:
opening_double_quote: DoubleQuote
whitespace:
Newline "\n"
Whitespace " "
Whitespace " "
parts:
TrailingWhitespace:
child: TextPart "text"
whitespace:
Newline "\n"
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###"
Remaining input: ""
Parsed: Text:
opening: TrailingWhitespace:
child: OpeningText:
opening_single_quotes:
opening_double_quote: DoubleQuote
whitespace:
Newline "\n"
Whitespace " "
parts:
TextPart "foo"
TrailingWhitespace:
child: TextNewline "\n"
whitespace:
Whitespace " "
Whitespace " "
TrailingWhitespace:
child: TextPart "bar"
whitespace:
Newline "\n"
closing: ClosingText:
closing_double_quote: DoubleQuote
closing_single_quotes:
"###);

assert_rich_ir_snapshot!(text("\" foobar \"", 0), @r###"
Remaining input: ""
Parsed: Text:
opening: OpeningText:
opening_single_quotes:
opening_double_quote: DoubleQuote
parts:
TextPart " foobar "
closing: ClosingText:
closing_double_quote: DoubleQuote
closing_single_quotes:
"###);
assert_rich_ir_snapshot!(text("\" \n foobar \"", 0), @r###"
Remaining input: ""
Parsed: Text:
opening: TrailingWhitespace:
child: OpeningText:
opening_single_quotes:
opening_double_quote: DoubleQuote
whitespace:
Whitespace " "
Newline "\n"
Whitespace " "
parts:
TextPart "foobar "
closing: ClosingText:
closing_double_quote: DoubleQuote
closing_single_quotes:
"###);
assert_rich_ir_snapshot!(text(r#""foo" bar"#, 0), @r###"
Remaining input: " bar"
Parsed: Text:
Expand Down
Loading