Updated Ptr methods to better match Parser method names.

This commit is contained in:
Zach Lute 2018-09-04 22:56:16 -07:00
parent f87771092c
commit af0ae9ee04
5 changed files with 59 additions and 45 deletions

View file

@ -3,7 +3,7 @@ use lexer::ptr::Ptr;
use SyntaxKind::{self, *}; use SyntaxKind::{self, *};
pub(crate) fn scan_shebang(ptr: &mut Ptr) -> bool { pub(crate) fn scan_shebang(ptr: &mut Ptr) -> bool {
if ptr.next_is('!') && ptr.nnext_is('/') { if ptr.at_str("!/") {
ptr.bump(); ptr.bump();
ptr.bump(); ptr.bump();
bump_until_eol(ptr); bump_until_eol(ptr);
@ -14,15 +14,15 @@ pub(crate) fn scan_shebang(ptr: &mut Ptr) -> bool {
} }
fn scan_block_comment(ptr: &mut Ptr) -> Option<SyntaxKind> { fn scan_block_comment(ptr: &mut Ptr) -> Option<SyntaxKind> {
if ptr.next_is('*') { if ptr.at('*') {
ptr.bump(); ptr.bump();
let mut depth: u32 = 1; let mut depth: u32 = 1;
while depth > 0 { while depth > 0 {
if ptr.next_is('*') && ptr.nnext_is('/') { if ptr.at_str("*/") {
depth -= 1; depth -= 1;
ptr.bump(); ptr.bump();
ptr.bump(); ptr.bump();
} else if ptr.next_is('/') && ptr.nnext_is('*') { } else if ptr.at_str("/*") {
depth += 1; depth += 1;
ptr.bump(); ptr.bump();
ptr.bump(); ptr.bump();
@ -37,7 +37,7 @@ fn scan_block_comment(ptr: &mut Ptr) -> Option<SyntaxKind> {
} }
pub(crate) fn scan_comment(ptr: &mut Ptr) -> Option<SyntaxKind> { pub(crate) fn scan_comment(ptr: &mut Ptr) -> Option<SyntaxKind> {
if ptr.next_is('/') { if ptr.at('/') {
bump_until_eol(ptr); bump_until_eol(ptr);
Some(COMMENT) Some(COMMENT)
} else { } else {
@ -47,7 +47,7 @@ pub(crate) fn scan_comment(ptr: &mut Ptr) -> Option<SyntaxKind> {
fn bump_until_eol(ptr: &mut Ptr) { fn bump_until_eol(ptr: &mut Ptr) {
loop { loop {
if ptr.next_is('\n') || ptr.next_is('\r') && ptr.nnext_is('\n') { if ptr.at('\n') || ptr.at_str("\r\n") {
return; return;
} }
if ptr.bump().is_none() { if ptr.bump().is_none() {

View file

@ -67,7 +67,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind {
_ => (), _ => (),
} }
let ident_start = is_ident_start(c) && !is_string_literal_start(c, ptr.next(), ptr.nnext()); let ident_start = is_ident_start(c) && !is_string_literal_start(c, ptr.current(), ptr.nth(1));
if ident_start { if ident_start {
return scan_ident(c, ptr); return scan_ident(c, ptr);
} }
@ -86,7 +86,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind {
match c { match c {
// Multi-byte tokens. // Multi-byte tokens.
'.' => { '.' => {
return match (ptr.next(), ptr.nnext()) { return match (ptr.current(), ptr.nth(1)) {
(Some('.'), Some('.')) => { (Some('.'), Some('.')) => {
ptr.bump(); ptr.bump();
ptr.bump(); ptr.bump();
@ -105,7 +105,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind {
}; };
} }
':' => { ':' => {
return match ptr.next() { return match ptr.current() {
Some(':') => { Some(':') => {
ptr.bump(); ptr.bump();
COLONCOLON COLONCOLON
@ -114,7 +114,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind {
}; };
} }
'=' => { '=' => {
return match ptr.next() { return match ptr.current() {
Some('=') => { Some('=') => {
ptr.bump(); ptr.bump();
EQEQ EQEQ
@ -127,7 +127,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind {
}; };
} }
'!' => { '!' => {
return match ptr.next() { return match ptr.current() {
Some('=') => { Some('=') => {
ptr.bump(); ptr.bump();
NEQ NEQ
@ -136,7 +136,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind {
}; };
} }
'-' => { '-' => {
return if ptr.next_is('>') { return if ptr.at('>') {
ptr.bump(); ptr.bump();
THIN_ARROW THIN_ARROW
} else { } else {
@ -147,14 +147,14 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind {
// If the character is an ident start not followed by another single // If the character is an ident start not followed by another single
// quote, then this is a lifetime name: // quote, then this is a lifetime name:
'\'' => { '\'' => {
return if ptr.next_is_p(is_ident_start) && !ptr.nnext_is('\'') { return if ptr.at_p(is_ident_start) && !ptr.at_str("''") {
ptr.bump(); ptr.bump();
while ptr.next_is_p(is_ident_continue) { while ptr.at_p(is_ident_continue) {
ptr.bump(); ptr.bump();
} }
// lifetimes shouldn't end with a single quote // lifetimes shouldn't end with a single quote
// if we find one, then this is an invalid character literal // if we find one, then this is an invalid character literal
if ptr.next_is('\'') { if ptr.at('\'') {
ptr.bump(); ptr.bump();
return CHAR; // TODO: error reporting return CHAR; // TODO: error reporting
} }
@ -186,7 +186,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind {
} }
fn scan_ident(c: char, ptr: &mut Ptr) -> SyntaxKind { fn scan_ident(c: char, ptr: &mut Ptr) -> SyntaxKind {
let is_single_letter = match ptr.next() { let is_single_letter = match ptr.current() {
None => true, None => true,
Some(c) if !is_ident_continue(c) => true, Some(c) if !is_ident_continue(c) => true,
_ => false, _ => false,
@ -202,7 +202,7 @@ fn scan_ident(c: char, ptr: &mut Ptr) -> SyntaxKind {
} }
fn scan_literal_suffix(ptr: &mut Ptr) { fn scan_literal_suffix(ptr: &mut Ptr) {
if ptr.next_is_p(is_ident_start) { if ptr.at_p(is_ident_start) {
ptr.bump(); ptr.bump();
} }
ptr.bump_while(is_ident_continue); ptr.bump_while(is_ident_continue);

View file

@ -5,7 +5,7 @@ use SyntaxKind::{self, *};
pub(crate) fn scan_number(c: char, ptr: &mut Ptr) -> SyntaxKind { pub(crate) fn scan_number(c: char, ptr: &mut Ptr) -> SyntaxKind {
if c == '0' { if c == '0' {
match ptr.next().unwrap_or('\0') { match ptr.current().unwrap_or('\0') {
'b' | 'o' => { 'b' | 'o' => {
ptr.bump(); ptr.bump();
scan_digits(ptr, false); scan_digits(ptr, false);
@ -26,7 +26,7 @@ pub(crate) fn scan_number(c: char, ptr: &mut Ptr) -> SyntaxKind {
// might be a float, but don't be greedy if this is actually an // might be a float, but don't be greedy if this is actually an
// integer literal followed by field/method access or a range pattern // integer literal followed by field/method access or a range pattern
// (`0..2` and `12.foo()`) // (`0..2` and `12.foo()`)
if ptr.next_is('.') && !(ptr.nnext_is('.') || ptr.nnext_is_p(is_ident_start)) { if ptr.at('.') && !(ptr.at_str("..") || ptr.nth_is_p(1, is_ident_start)) {
// might have stuff after the ., and if it does, it needs to start // might have stuff after the ., and if it does, it needs to start
// with a number // with a number
ptr.bump(); ptr.bump();
@ -35,7 +35,7 @@ pub(crate) fn scan_number(c: char, ptr: &mut Ptr) -> SyntaxKind {
return FLOAT_NUMBER; return FLOAT_NUMBER;
} }
// it might be a float if it has an exponent // it might be a float if it has an exponent
if ptr.next_is('e') || ptr.next_is('E') { if ptr.at('e') || ptr.at('E') {
scan_float_exponent(ptr); scan_float_exponent(ptr);
return FLOAT_NUMBER; return FLOAT_NUMBER;
} }
@ -43,7 +43,7 @@ pub(crate) fn scan_number(c: char, ptr: &mut Ptr) -> SyntaxKind {
} }
fn scan_digits(ptr: &mut Ptr, allow_hex: bool) { fn scan_digits(ptr: &mut Ptr, allow_hex: bool) {
while let Some(c) = ptr.next() { while let Some(c) = ptr.current() {
match c { match c {
'_' | '0'...'9' => { '_' | '0'...'9' => {
ptr.bump(); ptr.bump();
@ -57,9 +57,9 @@ fn scan_digits(ptr: &mut Ptr, allow_hex: bool) {
} }
fn scan_float_exponent(ptr: &mut Ptr) { fn scan_float_exponent(ptr: &mut Ptr) {
if ptr.next_is('e') || ptr.next_is('E') { if ptr.at('e') || ptr.at('E') {
ptr.bump(); ptr.bump();
if ptr.next_is('-') || ptr.next_is('+') { if ptr.at('-') || ptr.at('+') {
ptr.bump(); ptr.bump();
} }
scan_digits(ptr, false); scan_digits(ptr, false);

View file

@ -2,12 +2,14 @@ use TextUnit;
use std::str::Chars; use std::str::Chars;
/// A simple view into the characters of a string.
pub(crate) struct Ptr<'s> { pub(crate) struct Ptr<'s> {
text: &'s str, text: &'s str,
len: TextUnit, len: TextUnit,
} }
impl<'s> Ptr<'s> { impl<'s> Ptr<'s> {
/// Creates a new `Ptr` from a string.
pub fn new(text: &'s str) -> Ptr<'s> { pub fn new(text: &'s str) -> Ptr<'s> {
Ptr { Ptr {
text, text,
@ -15,45 +17,55 @@ impl<'s> Ptr<'s> {
} }
} }
/// Gets the length of the remaining string.
pub fn into_len(self) -> TextUnit { pub fn into_len(self) -> TextUnit {
self.len self.len
} }
pub fn next(&self) -> Option<char> { /// Gets the current character, if one exists.
pub fn current(&self) -> Option<char> {
self.chars().next() self.chars().next()
} }
pub fn nnext(&self) -> Option<char> { /// Gets the nth character from the current.
let mut chars = self.chars(); /// For example, 0 will return the current token, 1 will return the next, etc.
chars.next()?; pub fn nth(&self, n: u32) -> Option<char> {
chars.next() let mut chars = self.chars().peekable();
chars.by_ref().skip(n as usize).next()
} }
pub fn next_is(&self, c: char) -> bool { /// Checks whether the current character is `c`.
self.next() == Some(c) pub fn at(&self, c: char) -> bool {
self.current() == Some(c)
} }
pub fn nnext_is(&self, c: char) -> bool { /// Checks whether the next characters match `s`.
self.nnext() == Some(c) pub fn at_str(&self, s: &str) -> bool {
let chars = self.chars();
chars.as_str().starts_with(s)
} }
pub fn next_is_p<P: Fn(char) -> bool>(&self, p: P) -> bool { /// Checks whether the current character satisfies the predicate `p`.
self.next().map(p) == Some(true) pub fn at_p<P: Fn(char) -> bool>(&self, p: P) -> bool {
self.current().map(p) == Some(true)
} }
pub fn nnext_is_p<P: Fn(char) -> bool>(&self, p: P) -> bool { /// Checks whether the nth character satisfies the predicate `p`.
self.nnext().map(p) == Some(true) pub fn nth_is_p<P: Fn(char) -> bool>(&self, n: u32, p: P) -> bool {
self.nth(n).map(p) == Some(true)
} }
/// Moves to the next character.
pub fn bump(&mut self) -> Option<char> { pub fn bump(&mut self) -> Option<char> {
let ch = self.chars().next()?; let ch = self.chars().next()?;
self.len += TextUnit::of_char(ch); self.len += TextUnit::of_char(ch);
Some(ch) Some(ch)
} }
/// Moves to the next character as long as `pred` is satisfied.
pub fn bump_while<F: Fn(char) -> bool>(&mut self, pred: F) { pub fn bump_while<F: Fn(char) -> bool>(&mut self, pred: F) {
loop { loop {
match self.next() { match self.current() {
Some(c) if pred(c) => { Some(c) if pred(c) => {
self.bump(); self.bump();
} }
@ -62,11 +74,13 @@ impl<'s> Ptr<'s> {
} }
} }
/// Returns the text up to the current point.
pub fn current_token_text(&self) -> &str { pub fn current_token_text(&self) -> &str {
let len: u32 = self.len.into(); let len: u32 = self.len.into();
&self.text[..len as usize] &self.text[..len as usize]
} }
/// Returns an iterator over the remaining characters.
fn chars(&self) -> Chars { fn chars(&self) -> Chars {
let len: u32 = self.len.into(); let len: u32 = self.len.into();
self.text[len as usize..].chars() self.text[len as usize..].chars()

View file

@ -15,11 +15,11 @@ pub(crate) fn is_string_literal_start(c: char, c1: Option<char>, c2: Option<char
} }
pub(crate) fn scan_char(ptr: &mut Ptr) { pub(crate) fn scan_char(ptr: &mut Ptr) {
while let Some(c) = ptr.next() { while let Some(c) = ptr.current() {
match c { match c {
'\\' => { '\\' => {
ptr.bump(); ptr.bump();
if ptr.next_is('\\') || ptr.next_is('\'') { if ptr.at('\\') || ptr.at('\'') {
ptr.bump(); ptr.bump();
} }
} }
@ -57,11 +57,11 @@ pub(crate) fn scan_byte_char_or_string(ptr: &mut Ptr) -> SyntaxKind {
} }
pub(crate) fn scan_string(ptr: &mut Ptr) { pub(crate) fn scan_string(ptr: &mut Ptr) {
while let Some(c) = ptr.next() { while let Some(c) = ptr.current() {
match c { match c {
'\\' => { '\\' => {
ptr.bump(); ptr.bump();
if ptr.next_is('\\') || ptr.next_is('"') { if ptr.at('\\') || ptr.at('"') {
ptr.bump(); ptr.bump();
} }
} }
@ -78,11 +78,11 @@ pub(crate) fn scan_string(ptr: &mut Ptr) {
pub(crate) fn scan_raw_string(ptr: &mut Ptr) { pub(crate) fn scan_raw_string(ptr: &mut Ptr) {
let mut hashes = 0; let mut hashes = 0;
while ptr.next_is('#') { while ptr.at('#') {
hashes += 1; hashes += 1;
ptr.bump(); ptr.bump();
} }
if !ptr.next_is('"') { if !ptr.at('"') {
return; return;
} }
ptr.bump(); ptr.bump();
@ -90,7 +90,7 @@ pub(crate) fn scan_raw_string(ptr: &mut Ptr) {
while let Some(c) = ptr.bump() { while let Some(c) = ptr.bump() {
if c == '"' { if c == '"' {
let mut hashes_left = hashes; let mut hashes_left = hashes;
while ptr.next_is('#') && hashes_left > 0{ while ptr.at('#') && hashes_left > 0{
hashes_left -= 1; hashes_left -= 1;
ptr.bump(); ptr.bump();
} }
@ -110,7 +110,7 @@ fn scan_byte_string(ptr: &mut Ptr) {
} }
fn scan_raw_byte_string(ptr: &mut Ptr) { fn scan_raw_byte_string(ptr: &mut Ptr) {
if !ptr.next_is('"') { if !ptr.at('"') {
return; return;
} }
ptr.bump(); ptr.bump();