Enum tokenizer_py::Token

source ·
pub enum Token {
    EndMarker,
    Name(String),
    Number(String),
    String(String),
    NewLine,
    OP(String),
    Indent(String),
    Dedent,
    Comment(String),
    NL,
}
Expand description

An enumeration of Python tokens.

§Example

use tokenizer_py::{Tokenizer, Token};

struct BinaryExp{
    left: Token, center: Token,right: Token,
}
impl BinaryExp {
    fn new(left: Token, center: Token, right: Token) -> Self {
        BinaryExp { left, center, right}
    }
    fn execute(&self) -> Result<isize, <isize as std::str::FromStr>::Err> {
        use Token::{Number, OP};
        match (&self.left, &self.center, &self.right) {
            (Number(ref left), OP(ref op), Number(ref right)) => match op.as_str() {
                "+" => Ok(left.parse::<isize>()? + right.parse::<isize>()?),
                "-" => Ok(left.parse::<isize>()? - right.parse::<isize>()?),
                "*" => Ok(left.parse::<isize>()? * right.parse::<isize>()?),
                "/" => Ok(left.parse::<isize>()? / right.parse::<isize>()?),
                "%" => Ok(left.parse::<isize>()? % right.parse::<isize>()?),
                _ => panic!("Invalid operator"),
            }
            _ => panic!("Invalid tokens"),
        }
    }
}

let tokenizer = Tokenizer::new("10 + 10".to_owned());
let mut tokens = tokenizer.tokenize().unwrap();
let _ = tokens.pop(); // remove EndMarker

let binexp = BinaryExp::new(
    tokens.pop().unwrap(),
    tokens.pop().unwrap(),
    tokens.pop().unwrap()
);

assert_eq!(binexp.execute(), Ok(20));

Variants§

§

EndMarker

Indicates the end of the program.

§

Name(String)

A name token, such as a function or variable name.

§

Number(String)

A number token, such as a literal integer or floating-point number.

§

String(String)

A string token, such as a single or double-quoted string.

§

NewLine

A newline token, indicating a new line in the source code.

§

OP(String)

An operator token, such as an arithmetic or comparison operator.

§

Indent(String)

An indent token, indicating that a block of code is being indented.

§

Dedent

A dedent token, indicating that a block of code is being dedented.

§

Comment(String)

A comment token, such as a single-line or multi-line comment.

§

NL

A token indicating a new line, for compatibility with the original tokenizer.

Trait Implementations§

source§

impl Debug for Token

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq for Token

source§

fn eq(&self, other: &Token) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Token

source§

impl StructuralPartialEq for Token

Auto Trait Implementations§

§

impl RefUnwindSafe for Token

§

impl Send for Token

§

impl Sync for Token

§

impl Unpin for Token

§

impl UnwindSafe for Token

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.