From a4f140b0f3fa0e1fcedb2c8472e5bb6cbf051684 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 22 Aug 2018 13:22:06 +0300 Subject: no escape --- crates/libsyntax2/src/lexer/strings.rs | 45 ++++++++++++++-------- crates/libsyntax2/tests/data/lexer/0009_strings.rs | 3 +- .../libsyntax2/tests/data/lexer/0009_strings.txt | 4 ++ .../tests/data/lexer/0014_unclosed_char.rs | 1 + .../tests/data/lexer/0014_unclosed_char.txt | 1 + .../tests/data/lexer/0015_unclosed_string.rs | 1 + .../tests/data/lexer/0015_unclosed_string.txt | 1 + 7 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 crates/libsyntax2/tests/data/lexer/0014_unclosed_char.rs create mode 100644 crates/libsyntax2/tests/data/lexer/0014_unclosed_char.txt create mode 100644 crates/libsyntax2/tests/data/lexer/0015_unclosed_string.rs create mode 100644 crates/libsyntax2/tests/data/lexer/0015_unclosed_string.txt (limited to 'crates') diff --git a/crates/libsyntax2/src/lexer/strings.rs b/crates/libsyntax2/src/lexer/strings.rs index 795ea97b7..a6da97d47 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 7b7faa5d8..4ddb5bffc 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 7fb6b7b36..4cb4d711d 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 000000000..9c0007077 --- /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 000000000..812dfbc18 --- /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 000000000..d771a26d4 --- /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 000000000..728c40b66 --- /dev/null +++ b/crates/libsyntax2/tests/data/lexer/0015_unclosed_string.txt @@ -0,0 +1 @@ +STRING 7 "\"hello\n" -- cgit v1.2.3