@@ -4,23 +4,22 @@ use crate::token::Token;
44use crate :: token:: TokenKind ;
55use crate :: token:: GITQL_RESERVED_KEYWORDS ;
66
7- pub struct Tokenizer {
8- pub ( crate ) content : Vec < char > ,
9- pub ( crate ) content_len : usize ,
10- pub ( crate ) index : usize ,
11-
12- pub ( crate ) line_start : u32 ,
13- pub ( crate ) line_end : u32 ,
14- pub ( crate ) column_start : u32 ,
15- pub ( crate ) column_end : u32 ,
7+ pub struct Tokenizer < ' a > {
8+ content : & ' a [ char ] ,
9+ content_len : usize ,
10+ index : usize ,
11+
12+ line_start : u32 ,
13+ line_end : u32 ,
14+ column_start : u32 ,
15+ column_end : u32 ,
1616}
1717
18- impl Tokenizer {
19- pub ( crate ) fn new ( chars : Vec < char > ) -> Tokenizer {
20- let content_len = chars. len ( ) ;
18+ impl < ' a > Tokenizer < ' a > {
19+ pub ( crate ) fn new ( chars : & ' a [ char ] ) -> Tokenizer < ' a > {
2120 Tokenizer {
2221 content : chars,
23- content_len,
22+ content_len : chars . len ( ) ,
2423 index : 0 ,
2524
2625 line_start : 1 ,
@@ -30,9 +29,9 @@ impl Tokenizer {
3029 }
3130 }
3231
33- pub fn tokenize ( content : String ) -> Result < Vec < Token > , Box < Diagnostic > > {
34- let mut tokenizer = Tokenizer :: new ( content . chars ( ) . collect ( ) ) ;
35- tokenizer . tokenize_characters ( )
32+ pub fn tokenize ( chars : & ' a str ) -> Result < Vec < Token > , Box < Diagnostic > > {
33+ let chars : Vec < char > = chars . chars ( ) . collect ( ) ;
34+ Tokenizer :: new ( & chars ) . tokenize_characters ( )
3635 }
3736
3837 fn current_source_location ( & self ) -> SourceLocation {
@@ -340,7 +339,7 @@ impl Tokenizer {
340339
341340 // Consume `!`
342341 self . advance ( ) ;
343- let kind = if self . index < len && self . content [ self . index ] == '=' {
342+ let kind = if self . is_current_char ( '=' ) {
344343 // Consume `=`
345344 self . advance ( ) ;
346345 TokenKind :: BangEqual
@@ -420,7 +419,7 @@ impl Tokenizer {
420419 self . advance ( ) ;
421420
422421 // Make sure first character is alphabetic
423- if self . has_next ( ) && ! self . content [ self . index ] . is_alphabetic ( ) {
422+ if ! self . is_current_char_func ( |c| c . is_alphanumeric ( ) ) {
424423 return Err ( Diagnostic :: error (
425424 "Global variable name must start with alphabetic character" ,
426425 )
@@ -429,7 +428,7 @@ impl Tokenizer {
429428 . as_boxed ( ) ) ;
430429 }
431430
432- while self . has_next ( ) && self . is_current_char_func ( |c| c == '_' || c. is_alphanumeric ( ) ) {
431+ while self . is_current_char_func ( |c| c == '_' || c. is_alphanumeric ( ) ) {
433432 self . advance ( ) ;
434433 }
435434
@@ -444,8 +443,7 @@ impl Tokenizer {
444443
445444 fn consume_identifier ( & mut self ) -> Token {
446445 let start_index = self . index ;
447-
448- while self . has_next ( ) && self . is_current_char_func ( |c| c == '_' || c. is_alphanumeric ( ) ) {
446+ while self . is_current_char_func ( |c| c == '_' || c. is_alphanumeric ( ) ) {
449447 self . advance ( ) ;
450448 }
451449
@@ -467,7 +465,7 @@ impl Tokenizer {
467465 // Advance '`'
468466 self . advance ( ) ;
469467
470- while self . has_next ( ) && !self . is_current_char ( '`' ) {
468+ while !self . is_current_char ( '`' ) {
471469 self . advance ( ) ;
472470 }
473471
@@ -490,16 +488,16 @@ impl Tokenizer {
490488 fn consume_number ( & mut self ) -> Result < Token , Box < Diagnostic > > {
491489 let start_index = self . index ;
492490
493- while self . has_next ( ) && self . is_current_char_func ( |c| c == '_' || c. is_numeric ( ) ) {
491+ while self . is_current_char_func ( |c| c == '_' || c. is_numeric ( ) ) {
494492 self . advance ( ) ;
495493 }
496494
497495 let mut is_float_value = false ;
498- if self . has_next ( ) && self . is_current_char ( '.' ) {
496+ if self . is_current_char ( '.' ) {
499497 self . advance ( ) ;
500498
501499 is_float_value = true ;
502- while self . has_next ( ) && self . is_current_char_func ( |c| c == '_' || c. is_numeric ( ) ) {
500+ while self . is_current_char_func ( |c| c == '_' || c. is_numeric ( ) ) {
503501 self . advance ( ) ;
504502 }
505503 }
@@ -538,7 +536,7 @@ impl Tokenizer {
538536
539537 fn consume_binary_number ( & mut self ) -> Result < Token , Box < Diagnostic > > {
540538 let start_index = self . index ;
541- while self . has_next ( ) && self . is_current_char_func ( |c| c == '_' || c == '0' || c >= '1' ) {
539+ while self . is_current_char_func ( |c| c == '_' || c == '0' || c >= '1' ) {
542540 self . advance ( ) ;
543541 }
544542
@@ -575,8 +573,7 @@ impl Tokenizer {
575573
576574 fn consume_octal_number ( & mut self ) -> Result < Token , Box < Diagnostic > > {
577575 let start_index = self . index ;
578- while self . has_next ( ) && self . is_current_char_func ( |c| c == '_' || ( '0' ..='8' ) . contains ( & c) )
579- {
576+ while self . is_current_char_func ( |c| c == '_' || ( '0' ..='8' ) . contains ( & c) ) {
580577 self . advance ( ) ;
581578 }
582579
@@ -613,7 +610,7 @@ impl Tokenizer {
613610
614611 fn consume_hex_number ( & mut self ) -> Result < Token , Box < Diagnostic > > {
615612 let start_index = self . index ;
616- while self . has_next ( ) && self . is_current_char_func ( |c| c == '_' || c. is_ascii_hexdigit ( ) ) {
613+ while self . is_current_char_func ( |c| c == '_' || c. is_ascii_hexdigit ( ) ) {
617614 self . advance ( ) ;
618615 }
619616
@@ -685,7 +682,7 @@ impl Tokenizer {
685682 self . advance ( ) ;
686683
687684 let mut buffer = String :: new ( ) ;
688- while self . has_next ( ) && !self . is_current_char ( around) {
685+ while !self . is_current_char ( around) {
689686 if !self . is_current_char ( '\\' ) {
690687 buffer. push ( self . content [ self . index ] ) ;
691688 self . advance ( ) ;
@@ -748,7 +745,7 @@ impl Tokenizer {
748745 // Advance `--`
749746 self . advance_n ( 2 ) ;
750747
751- while self . has_next ( ) && !self . is_current_char ( '\n' ) {
748+ while !self . is_current_char ( '\n' ) {
752749 self . advance ( ) ;
753750 }
754751
0 commit comments