From 415c891d641fa305e7ddbbbcc78db990dd5d3564 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 29 Jul 2018 15:16:07 +0300 Subject: Reorganize --- src/lexer/comments.rs | 3 +-- src/lexer/mod.rs | 50 +++++++++++++++++++++++++++++++------------------- src/lexer/numbers.rs | 3 +-- src/lexer/strings.rs | 3 +-- 4 files changed, 34 insertions(+), 25 deletions(-) (limited to 'src/lexer') diff --git a/src/lexer/comments.rs b/src/lexer/comments.rs index d1e958817..01acb6515 100644 --- a/src/lexer/comments.rs +++ b/src/lexer/comments.rs @@ -1,7 +1,6 @@ use lexer::ptr::Ptr; -use SyntaxKind; -use syntax_kinds::*; +use SyntaxKind::{self, *}; pub(crate) fn scan_shebang(ptr: &mut Ptr) -> bool { if ptr.next_is('!') && ptr.nnext_is('/') { diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index 65a994327..69cab5b57 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -1,21 +1,32 @@ -use {SyntaxKind, Token}; -use syntax_kinds::*; - mod ptr; -use self::ptr::Ptr; - +mod comments; +mod strings; +mod numbers; mod classes; -use self::classes::*; -mod numbers; -use self::numbers::scan_number; +use { + TextUnit, + SyntaxKind::{self, *}, +}; -mod strings; -use self::strings::{is_string_literal_start, scan_byte_char_or_string, scan_char, scan_raw_string, - scan_string}; +use self::{ + ptr::Ptr, + classes::*, + numbers::scan_number, + strings::{ + is_string_literal_start, scan_byte_char_or_string, scan_char, + scan_raw_string, scan_string}, + comments::{scan_comment, scan_shebang}, +}; -mod comments; -use self::comments::{scan_comment, scan_shebang}; +/// A token of Rust source. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Token { + /// The kind of token. + pub kind: SyntaxKind, + /// The length of the token. + pub len: TextUnit, +} /// Break a string up into its component tokens pub fn tokenize(text: &str) -> Vec { @@ -29,6 +40,7 @@ pub fn tokenize(text: &str) -> Vec { } acc } + /// Get the next token from a string pub fn next_token(text: &str) -> Token { assert!(!text.is_empty()); @@ -109,7 +121,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind { DOTDOT } _ => DOT, - } + }; } ':' => { return match ptr.next() { @@ -118,7 +130,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind { COLONCOLON } _ => COLON, - } + }; } '=' => { return match ptr.next() { @@ -131,7 +143,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind { FAT_ARROW } _ => EQ, - } + }; } '!' => { return match ptr.next() { @@ -140,7 +152,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind { NEQ } _ => EXCL, - } + }; } '-' => { return if ptr.next_is('>') { @@ -148,7 +160,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind { THIN_ARROW } else { MINUS - } + }; } // If the character is an ident start not followed by another single @@ -202,7 +214,7 @@ fn scan_ident(c: char, ptr: &mut Ptr) -> SyntaxKind { return if c == '_' { UNDERSCORE } else { IDENT }; } ptr.bump_while(is_ident_continue); - if let Some(kind) = ident_to_keyword(ptr.current_token_text()) { + if let Some(kind) = SyntaxKind::from_keyword(ptr.current_token_text()) { return kind; } IDENT diff --git a/src/lexer/numbers.rs b/src/lexer/numbers.rs index 95e42246f..38eac9212 100644 --- a/src/lexer/numbers.rs +++ b/src/lexer/numbers.rs @@ -1,8 +1,7 @@ use lexer::ptr::Ptr; use lexer::classes::*; -use SyntaxKind; -use syntax_kinds::*; +use SyntaxKind::{self, *}; pub(crate) fn scan_number(c: char, ptr: &mut Ptr) -> SyntaxKind { if c == '0' { diff --git a/src/lexer/strings.rs b/src/lexer/strings.rs index 00a84ec85..e3704fbb3 100644 --- a/src/lexer/strings.rs +++ b/src/lexer/strings.rs @@ -1,5 +1,4 @@ -use SyntaxKind; -use syntax_kinds::*; +use SyntaxKind::{self, *}; use lexer::ptr::Ptr; -- cgit v1.2.3