use #[derive(Debug)] on Token, Kind, State... SO MUCH EASIER
This commit is contained in:
parent
c84251b56f
commit
6b8ba6b6e8
3 changed files with 5 additions and 41 deletions
|
@ -7,14 +7,13 @@ mod char;
|
|||
mod charset;
|
||||
mod str;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use self::char::Lexable;
|
||||
use self::str::CharAt;
|
||||
use self::str::RelativeIndexable;
|
||||
use self::token::Token;
|
||||
use self::token::Kind;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum State {
|
||||
Initial,
|
||||
Identifier,
|
||||
|
@ -135,7 +134,7 @@ impl Iterator for Lexer {
|
|||
println!("Lexing '{}'", &self.input[self.begin ..]);
|
||||
while token.is_none() {
|
||||
if let Some(c) = self.input.char_at(self.forward) {
|
||||
println!("{}! c='{}'", self.state, c);
|
||||
println!("{:?}! c='{}'", self.state, c);
|
||||
match self.state {
|
||||
State::Initial => self.state_initial(c, &mut token),
|
||||
State::Identifier => self.state_identifier(c, &mut token),
|
||||
|
@ -151,15 +150,3 @@ impl Iterator for Lexer {
|
|||
token
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for State {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let s = match *self {
|
||||
State::Initial => "Initial",
|
||||
State::Identifier => "Identifier",
|
||||
State::Hash => "Hash",
|
||||
};
|
||||
write!(f, "{}", s)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
/// # Token
|
||||
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Kind {
|
||||
LeftParen,
|
||||
RightParen,
|
||||
|
@ -9,6 +8,7 @@ pub enum Kind {
|
|||
Boolean,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Token {
|
||||
kind: Kind,
|
||||
value: String,
|
||||
|
@ -19,26 +19,3 @@ impl Token {
|
|||
Token { kind: kind, value: value, }
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Display
|
||||
//
|
||||
|
||||
impl fmt::Display for Kind {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let s = match *self {
|
||||
Kind::LeftParen => "LeftParen",
|
||||
Kind::RightParen => "RightParen",
|
||||
Kind::Identifier => "Identifier",
|
||||
Kind::Boolean => "Boolean",
|
||||
};
|
||||
write!(f, "{}", s)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Token {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "({}, \"{}\")", self.kind, self.value)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,6 @@ mod lexer;
|
|||
fn main() {
|
||||
let lexer = lexer::Lexer::new(String::from("((abc def + ghi #f))"));
|
||||
for t in lexer {
|
||||
println!("token = {}", t);
|
||||
println!("token = {:?}", t);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue