Files
rust/crates/libsyntax2/src/lexer/strings.rs

136 lines
2.8 KiB
Rust
Raw Normal View History

2018-07-29 15:16:07 +03:00
use SyntaxKind::{self, *};
use lexer::ptr::Ptr;
2017-12-31 15:43:12 +03:00
pub(crate) fn is_string_literal_start(c: char, c1: Option<char>, c2: Option<char>) -> bool {
match (c, c1, c2) {
2018-01-27 18:31:23 -05:00
('r', Some('"'), _)
| ('r', Some('#'), _)
| ('b', Some('"'), _)
| ('b', Some('\''), _)
| ('b', Some('r'), Some('"'))
| ('b', Some('r'), Some('#')) => true,
_ => false,
}
}
pub(crate) fn scan_char(ptr: &mut Ptr) {
2018-08-22 13:22:06 +03:00
while let Some(c) = ptr.next() {
match c {
'\\' => {
ptr.bump();
if ptr.next_is('\\') || ptr.next_is('\'') {
ptr.bump();
}
}
'\'' => {
ptr.bump();
return;
}
'\n' => return,
_ => {
2018-08-18 12:13:34 +03:00
ptr.bump();
}
}
}
}
pub(crate) fn scan_byte_char_or_string(ptr: &mut Ptr) -> SyntaxKind {
// unwrapping and not-exhaustive match are ok
// because of string_literal_start
let c = ptr.bump().unwrap();
match c {
'\'' => {
scan_byte(ptr);
2017-12-31 14:02:55 +03:00
BYTE
}
'"' => {
scan_byte_string(ptr);
2017-12-31 14:02:55 +03:00
BYTE_STRING
}
'r' => {
scan_raw_byte_string(ptr);
2017-12-31 14:02:55 +03:00
RAW_BYTE_STRING
}
_ => unreachable!(),
}
}
2017-12-31 15:14:47 +03:00
pub(crate) fn scan_string(ptr: &mut Ptr) {
2018-08-22 13:22:06 +03:00
while let Some(c) = ptr.next() {
match c {
'\\' => {
ptr.bump();
if ptr.next_is('\\') || ptr.next_is('"') {
ptr.bump();
}
}
'"' => {
ptr.bump();
return;
}
_ => {
ptr.bump();
},
2017-12-31 15:14:47 +03:00
}
}
}
pub(crate) fn scan_raw_string(ptr: &mut Ptr) {
2018-08-14 15:03:27 +03:00
let mut hashes = 0;
while ptr.next_is('#') {
hashes += 1;
ptr.bump();
}
2017-12-31 15:14:47 +03:00
if !ptr.next_is('"') {
2018-01-27 18:31:23 -05:00
return;
2017-12-31 15:14:47 +03:00
}
ptr.bump();
while let Some(c) = ptr.bump() {
if c == '"' {
2018-08-14 15:03:27 +03:00
let mut hashes_left = hashes;
while ptr.next_is('#') && hashes_left > 0{
hashes_left -= 1;
ptr.bump();
}
if hashes_left == 0 {
return;
}
2017-12-31 15:14:47 +03:00
}
}
}
fn scan_byte(ptr: &mut Ptr) {
2017-12-31 14:02:55 +03:00
if ptr.next_is('\'') {
ptr.bump();
2018-01-27 18:31:23 -05:00
return;
2017-12-31 14:02:55 +03:00
}
ptr.bump();
if ptr.next_is('\'') {
ptr.bump();
2018-01-27 18:31:23 -05:00
return;
2017-12-31 14:02:55 +03:00
}
}
fn scan_byte_string(ptr: &mut Ptr) {
2017-12-31 14:02:55 +03:00
while let Some(c) = ptr.bump() {
if c == '"' {
2018-01-27 18:31:23 -05:00
return;
2017-12-31 14:02:55 +03:00
}
}
}
fn scan_raw_byte_string(ptr: &mut Ptr) {
2017-12-31 14:02:55 +03:00
if !ptr.next_is('"') {
2018-01-27 18:31:23 -05:00
return;
2017-12-31 14:02:55 +03:00
}
ptr.bump();
2017-12-31 14:02:55 +03:00
while let Some(c) = ptr.bump() {
if c == '"' {
2018-01-27 18:31:23 -05:00
return;
2017-12-31 14:02:55 +03:00
}
}
}