2017-12-31 13:32:00 +03:00
|
|
|
use {SyntaxKind};
|
|
|
|
|
use syntax_kinds::*;
|
|
|
|
|
|
|
|
|
|
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 {
|
2017-12-31 13:32:00 +03:00
|
|
|
match (c, c1, c2) {
|
|
|
|
|
('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) {
|
|
|
|
|
if ptr.bump().is_none() {
|
|
|
|
|
return; // TODO: error reporting is upper in the stack
|
|
|
|
|
}
|
|
|
|
|
scan_char_or_byte(ptr);
|
|
|
|
|
if !ptr.next_is('\'') {
|
|
|
|
|
return; // TODO: error reporting
|
|
|
|
|
}
|
|
|
|
|
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
|
2017-12-31 13:32:00 +03:00
|
|
|
}
|
|
|
|
|
'"' => {
|
|
|
|
|
scan_byte_string(ptr);
|
2017-12-31 14:02:55 +03:00
|
|
|
BYTE_STRING
|
2017-12-31 13:32:00 +03:00
|
|
|
}
|
|
|
|
|
'r' => {
|
|
|
|
|
scan_raw_byte_string(ptr);
|
2017-12-31 14:02:55 +03:00
|
|
|
RAW_BYTE_STRING
|
2017-12-31 13:32:00 +03:00
|
|
|
}
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-31 15:14:47 +03:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-31 13:32:00 +03:00
|
|
|
fn scan_byte(ptr: &mut Ptr) {
|
2017-12-31 14:02:55 +03:00
|
|
|
if ptr.next_is('\'') {
|
|
|
|
|
ptr.bump();
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ptr.bump();
|
|
|
|
|
if ptr.next_is('\'') {
|
|
|
|
|
ptr.bump();
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-12-31 13:32:00 +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 == '"' {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-31 13:32:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn scan_raw_byte_string(ptr: &mut Ptr) {
|
2017-12-31 14:02:55 +03:00
|
|
|
if !ptr.next_is('"') {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ptr.bump();
|
2017-12-31 13:32:00 +03:00
|
|
|
|
2017-12-31 14:02:55 +03:00
|
|
|
while let Some(c) = ptr.bump() {
|
|
|
|
|
if c == '"' {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-31 13:32:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn scan_char_or_byte(ptr: &mut Ptr) {
|
|
|
|
|
//FIXME: deal with escape sequencies
|
|
|
|
|
ptr.bump();
|
2017-12-31 15:14:47 +03:00
|
|
|
}
|
|
|
|
|
|