rual-core 0.0.4

A slim, embeddable language
Documentation
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Keyword {
    /// `let`
    Let,
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Punct {
    /// `+`
    Add,

    /// `-`
    Sub,

    /// `*`
    Mul,

    /// `/`
    Div,

    /// `^`
    Pow,

    /// `:`
    Colon,

    /// `;`
    SemiColon,

    /// `=`
    Assign,

    /// `,`
    Comma,

    /// `(`
    OpenParen,

    /// `)`
    CloseParen,

    /// `[`
    OpenBracket,

    /// `]`
    CloseBracket,

    /// `{`
    OpenCurly,

    /// `}`
    CloseCurly,

    /// `...`
    Spread,

    /// `..`
    Range,

    /// `.`
    Dot,

    /// `|`
    Pipe,

    /// `->`
    Arrow,

    /// `\`
    Lambda,
}

impl Punct {
    // Lower is better
    pub fn precedence(self) -> u8 {
        use self::Punct::*;
        match self {
            Pow => 1,
            Mul | Div => 2,
            Add | Sub => 3,
            _ => 9,
        }
    }

    pub fn left_associative(self) -> bool {
        use self::Punct::*;
        match self {
            Add | Sub | Mul | Div => true,
            _ => false,
        }
    }

    pub fn is_arithmetic(self) -> bool {
        use self::Punct::*;
        match self {
            Add | Sub | Mul | Div | Pow => true,
            _ => false,
        }
    }
}

/// The various types of tokens.
///
/// Comments, strings and identifiers keep the range of their values within the source code.
/// The start of that range is the index of the first character of the token.
/// The end is the index after the last character of the token.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum TokenType {
    Keyword(Keyword),
    Ident { start: usize, end: usize },
    Num(i32),
    Str { start: usize, end: usize },
    Bool(bool),
    Comment { start: usize, end: usize },
    Discard,
    Punct(Punct),
    Term,
}

impl TokenType {
    // ew
    pub fn type_eq(self, other: TokenType) -> bool {
        use self::TokenType::*;
        match (self, other) {
            (Keyword(_), Keyword(_))
            | (Ident { .. }, Ident { .. })
            | (Num(_), Num(_))
            | (Str { .. }, Str { .. })
            | (Bool(_), Bool(_))
            | (Comment { .. }, Comment { .. })
            | (Punct(_), Punct(_))
            | (Term, Term) => true,
            _ => false,
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Token {
    pub col: usize,
    pub ln: usize,
    pub r#type: TokenType,
}

impl Token {
    pub fn new(ln: usize, col: usize, t: TokenType) -> Self {
        Token { col, ln, r#type: t }
    }
}