draconic/src/lexer/token.rs

104 lines
1.6 KiB
Rust
Raw Normal View History

use std::convert::From;
use std::str::FromStr;
use ::lexer::token_types::TokenTypes;
///
pub struct Token
{
///
string: String,
///
variant: TokenTypes
}
impl Token
{
///
pub fn new() -> Token
{
Token::from("")
}
///
pub fn get_type(&self) -> TokenTypes
{
self.variant
}
///
pub fn set_type(&mut self, token_type: TokenTypes)
{
self.variant = token_type;
}
}
impl ::std::str::FromStr for Token
{
type Err = String;
fn from_str(s: &str) -> Result<Token, String>
{
let token: Token;
// Create the new Token from the given &str.
token =
Token
{
string: String::from(s),
variant: TokenTypes::Unknown
};
// Return the token.
Ok(token)
}
}
impl<'a> ::std::convert::From<&'a str> for Token
{
fn from(s: &'a str) -> Self
{
// Just call the FromStr and handle the error.
match Token::from_str(s)
{
Ok(token) =>
{
token
}
Err(error) =>
{
// Just warn and create a blank unknown Token.
warn!("{}", error);
Token
{
string: String::new(),
variant: TokenTypes::Unknown
}
}
}
}
}
impl From<String> for Token
{
fn from(s: String) -> Self
{
// Just call the From<&str>.
Token::from(s.as_str())
}
}
impl ::std::fmt::Display for Token
{
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result
{
write!(f, "{}", self.string)
}
}