fn read_number(&mut self, first: char) -> LexResult<Token> {
        let mut seen_dot = false;

        let mut s = String::new();

        // Add the first digit
        s.push(first);

        // Keep adding until next character is not a digit
        while let Some(&ch) = self.peek_char() {
            if is_digit(ch) {
                s.push(self.read_char().unwrap());
            }
            // Dot for decimals
            else if ch == '.' {
                if seen_dot == false {
                    seen_dot = true;
                    s.push(self.read_char().unwrap());
                } else {
                    break;
                }
            }
            // Underscore for separation
            else if ch == '_' {
                // Ignore it
                self.read_char();
            } else {
                break;
            }
        }

        if seen_dot {
            match s.parse() {
                Ok(value) => Ok(Token::Float(value)),
                Err(error) => Err(LexError::InvalidFloat(error)),
            }
        } else {
            match s.parse() {
                Ok(value) => Ok(Token::Integer(value)),
                Err(error) => Err(LexError::InvalidInt(error)),
            }
        }
    }
/// Read the current and following characters as a number token.
/// Source: https://michael-f-bryan.github.io/static-analyser-in-rust/book/lex.html
Read the current and following characters as a number token.
Read the current and following characters as a number token .
fn read_number(&mut self, first: char) -> LexResult<Token> {
        let mut seen_dot = false;
        let mut s = String::new();
        // Add the first digit
        s.push(first);
        // Keep adding until next character is not a digit
        while let Some(&ch) = self.peek_char() {
            if is_digit(ch) {
                s.push(self.read_char().unwrap());
            }
            // Dot for decimals
            else if ch == '.' {
                if seen_dot == false {
                    seen_dot = true;
                    s.push(self.read_char().unwrap());
                } else {
                    break;
                }
            }
            // Underscore for separation
            else if ch == '_' {
                // Ignore it
                self.read_char();
            } else {
                break;
            }
        }
        if seen_dot {
            match s.parse() {
                Ok(value) => Ok(Token::Float(value)),
                Err(error) => Err(LexError::InvalidFloat(error)),
            }
        } else {
            match s.parse() {
                Ok(value) => Ok(Token::Integer(value)),
                Err(error) => Err(LexError::InvalidInt(error)),
            }
        }
    }
fn read_number ( & mut self , first : char ) -> LexResult < Token > { let mut seen_dot = false ; let mut s = String :: new ( ) ; // Add the first digit s . push ( first ) ; // Keep adding until next character is not a digit while let Some ( & ch ) = self . peek_char ( ) { if is_digit ( ch ) { s . push ( self . read_char ( ) . unwrap ( ) ) ; } // Dot for decimals else if ch == '.' { if seen_dot == false { seen_dot = true ; s . push ( self . read_char ( ) . unwrap ( ) ) ; } else { break ; } } // Underscore for separation else if ch == '_' { // Ignore it self . read_char ( ) ; } else { break ; } } if seen_dot { match s . parse ( ) { Ok ( value ) => Ok ( Token :: Float ( value ) ) , Err ( error ) => Err ( LexError :: InvalidFloat ( error ) ) , } } else { match s . parse ( ) { Ok ( value ) => Ok ( Token :: Integer ( value ) ) , Err ( error ) => Err ( LexError :: InvalidInt ( error ) ) , } } }
Read the current and following characters as a number token.
Read the current and following characters as a number token .
&mut self
first
char