From d76d7d2a7426e34b0fb358029cbaa2d71f0118e8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 31 Dec 2017 15:14:47 +0300 Subject: [PATCH] Lexer: strings --- src/lexer/mod.rs | 12 +++++++++- src/lexer/strings.rs | 24 ++++++++++++++++++- .../{0008_strings.rs => 0008_byte_strings.rs} | 0 ...0008_strings.txt => 0008_byte_strings.txt} | 0 tests/data/lexer/0009_strings.rs | 1 + tests/data/lexer/0009_strings.txt | 4 ++++ 6 files changed, 39 insertions(+), 2 deletions(-) rename tests/data/lexer/{0008_strings.rs => 0008_byte_strings.rs} (100%) rename tests/data/lexer/{0008_strings.txt => 0008_byte_strings.txt} (100%) create mode 100644 tests/data/lexer/0009_strings.rs create mode 100644 tests/data/lexer/0009_strings.txt diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index d81ea03395..d66046ca1d 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -11,7 +11,7 @@ mod numbers; use self::numbers::scan_number; mod strings; -use self::strings::{string_literal_start, scan_char, scan_byte_char_or_string}; +use self::strings::{string_literal_start, scan_char, scan_byte_char_or_string, scan_string, scan_raw_string}; pub fn next_token(text: &str) -> Token { assert!(!text.is_empty()); @@ -128,6 +128,16 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind { scan_literal_suffix(ptr); return kind }, + '"' => { + scan_string(ptr); + scan_literal_suffix(ptr); + return STRING; + } + 'r' => { + scan_raw_string(ptr); + scan_literal_suffix(ptr); + return RAW_STRING; + } _ => (), } ERROR diff --git a/src/lexer/strings.rs b/src/lexer/strings.rs index 283ce8feb3..2c1d863746 100644 --- a/src/lexer/strings.rs +++ b/src/lexer/strings.rs @@ -47,6 +47,27 @@ 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 + } + } +} + +pub(crate) fn scan_raw_string(ptr: &mut Ptr) { + if !ptr.next_is('"') { + return + } + ptr.bump(); + + while let Some(c) = ptr.bump() { + if c == '"' { + return + } + } +} + fn scan_byte(ptr: &mut Ptr) { if ptr.next_is('\'') { ptr.bump(); @@ -83,4 +104,5 @@ fn scan_raw_byte_string(ptr: &mut Ptr) { fn scan_char_or_byte(ptr: &mut Ptr) { //FIXME: deal with escape sequencies ptr.bump(); -} \ No newline at end of file +} + diff --git a/tests/data/lexer/0008_strings.rs b/tests/data/lexer/0008_byte_strings.rs similarity index 100% rename from tests/data/lexer/0008_strings.rs rename to tests/data/lexer/0008_byte_strings.rs diff --git a/tests/data/lexer/0008_strings.txt b/tests/data/lexer/0008_byte_strings.txt similarity index 100% rename from tests/data/lexer/0008_strings.txt rename to tests/data/lexer/0008_byte_strings.txt diff --git a/tests/data/lexer/0009_strings.rs b/tests/data/lexer/0009_strings.rs new file mode 100644 index 0000000000..7b7faa5d8f --- /dev/null +++ b/tests/data/lexer/0009_strings.rs @@ -0,0 +1 @@ +"hello" r"world" diff --git a/tests/data/lexer/0009_strings.txt b/tests/data/lexer/0009_strings.txt new file mode 100644 index 0000000000..7fb6b7b368 --- /dev/null +++ b/tests/data/lexer/0009_strings.txt @@ -0,0 +1,4 @@ +STRING 7 "\"hello\"" +WHITESPACE 1 " " +RAW_STRING 8 "r\"world\"" +WHITESPACE 1 "\n"