diff --git a/crates/libsyntax2/src/lexer/strings.rs b/crates/libsyntax2/src/lexer/strings.rs index 795ea97b7b7..a6da97d47e1 100644 --- a/crates/libsyntax2/src/lexer/strings.rs +++ b/crates/libsyntax2/src/lexer/strings.rs @@ -15,22 +15,23 @@ pub(crate) fn is_string_literal_start(c: char, c1: Option, c2: Option { + ptr.bump(); + if ptr.next_is('\\') || ptr.next_is('\'') { + ptr.bump(); + } + } + '\'' => { + ptr.bump(); + return; + } + '\n' => return, + _ => { ptr.bump(); } - continue; } - if ptr.next_is('\'') { - ptr.bump(); - return; - } - if ptr.next_is('\n') { - break; - } - ptr.bump(); } } @@ -56,9 +57,21 @@ pub(crate) fn scan_byte_char_or_string(ptr: &mut Ptr) -> SyntaxKind { } pub(crate) fn scan_string(ptr: &mut Ptr) { - while let Some(c) = ptr.bump() { - if c == '"' { - return; + while let Some(c) = ptr.next() { + match c { + '\\' => { + ptr.bump(); + if ptr.next_is('\\') || ptr.next_is('"') { + ptr.bump(); + } + } + '"' => { + ptr.bump(); + return; + } + _ => { + ptr.bump(); + }, } } } diff --git a/crates/libsyntax2/tests/data/lexer/0009_strings.rs b/crates/libsyntax2/tests/data/lexer/0009_strings.rs index 7b7faa5d8f8..4ddb5bffccc 100644 --- a/crates/libsyntax2/tests/data/lexer/0009_strings.rs +++ b/crates/libsyntax2/tests/data/lexer/0009_strings.rs @@ -1 +1,2 @@ -"hello" r"world" +"hello" r"world" "\n\"\\no escape" "multi +line" diff --git a/crates/libsyntax2/tests/data/lexer/0009_strings.txt b/crates/libsyntax2/tests/data/lexer/0009_strings.txt index 7fb6b7b368c..4cb4d711d67 100644 --- a/crates/libsyntax2/tests/data/lexer/0009_strings.txt +++ b/crates/libsyntax2/tests/data/lexer/0009_strings.txt @@ -1,4 +1,8 @@ STRING 7 "\"hello\"" WHITESPACE 1 " " RAW_STRING 8 "r\"world\"" +WHITESPACE 1 " " +STRING 17 "\"\\n\\\"\\\\no escape\"" +WHITESPACE 1 " " +STRING 12 "\"multi\nline\"" WHITESPACE 1 "\n" diff --git a/crates/libsyntax2/tests/data/lexer/0014_unclosed_char.rs b/crates/libsyntax2/tests/data/lexer/0014_unclosed_char.rs new file mode 100644 index 00000000000..9c000707726 --- /dev/null +++ b/crates/libsyntax2/tests/data/lexer/0014_unclosed_char.rs @@ -0,0 +1 @@ +'1 \ No newline at end of file diff --git a/crates/libsyntax2/tests/data/lexer/0014_unclosed_char.txt b/crates/libsyntax2/tests/data/lexer/0014_unclosed_char.txt new file mode 100644 index 00000000000..812dfbc18d0 --- /dev/null +++ b/crates/libsyntax2/tests/data/lexer/0014_unclosed_char.txt @@ -0,0 +1 @@ +CHAR 2 "\'1" diff --git a/crates/libsyntax2/tests/data/lexer/0015_unclosed_string.rs b/crates/libsyntax2/tests/data/lexer/0015_unclosed_string.rs new file mode 100644 index 00000000000..d771a26d499 --- /dev/null +++ b/crates/libsyntax2/tests/data/lexer/0015_unclosed_string.rs @@ -0,0 +1 @@ +"hello diff --git a/crates/libsyntax2/tests/data/lexer/0015_unclosed_string.txt b/crates/libsyntax2/tests/data/lexer/0015_unclosed_string.txt new file mode 100644 index 00000000000..728c40b6624 --- /dev/null +++ b/crates/libsyntax2/tests/data/lexer/0015_unclosed_string.txt @@ -0,0 +1 @@ +STRING 7 "\"hello\n"