ty-parser 0.1.0

C-syntax parser for the Ty programming language
Documentation
#[derive(Clone, Debug, PartialEq)]
pub enum Token {
    Identifier(String),
    Integer(u128),
    Float(f64),
    String(Vec<u8>),

    Fn,
    Extern,
    Type,
    Const,
    Impl,
    Let,
    Var,
    Ref,
    If,
    Else,
    Match,
    Label,
    Jump,
    Return,
    Print,
    As,
    Sizeof,
    Call,
    True,
    False,
    Import,
    Pub,
    Defer,
    While,

    LeftParen,
    RightParen,
    LeftBrace,
    RightBrace,
    LeftBracket,
    RightBracket,
    Semicolon,
    Comma,
    Colon,
    Arrow,
    FatArrow,
    Dot,
    At,
    Ellipsis,

    Plus,
    Minus,
    Star,
    Slash,
    Percent,
    EqualEqual,
    BangEqual,
    Less,
    LessEqual,
    Greater,
    GreaterEqual,
    AmpAmp,
    PipePipe,
    Amp,
    Pipe,
    Caret,
    Tilde,
    LessLess,
    GreaterGreater,
    Bang,
    Equal,
    ColonEqual,
    PlusEqual,
    MinusEqual,
    StarEqual,
    SlashEqual,

    End,
}

impl std::fmt::Display for Token {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Identifier(name) => write!(formatter, "'{name}'"),
            Self::Integer(value) => write!(formatter, "{value}"),
            Self::Float(value) => write!(formatter, "{value}"),
            Self::String(_) => write!(formatter, "string"),
            Self::Fn => write!(formatter, "'fn'"),
            Self::Extern => write!(formatter, "'extern'"),
            Self::Type => write!(formatter, "'type'"),
            Self::Const => write!(formatter, "'const'"),
            Self::Impl => write!(formatter, "'impl'"),
            Self::Let => write!(formatter, "'let'"),
            Self::Var => write!(formatter, "'var'"),
            Self::Ref => write!(formatter, "'ref'"),
            Self::If => write!(formatter, "'if'"),
            Self::Else => write!(formatter, "'else'"),
            Self::Match => write!(formatter, "'match'"),
            Self::Label => write!(formatter, "'label'"),
            Self::Jump => write!(formatter, "'jump'"),
            Self::Return => write!(formatter, "'return'"),
            Self::Print => write!(formatter, "'print'"),
            Self::As => write!(formatter, "'as'"),
            Self::Sizeof => write!(formatter, "'sizeof'"),
            Self::Call => write!(formatter, "'call'"),
            Self::True => write!(formatter, "'true'"),
            Self::False => write!(formatter, "'false'"),
            Self::Import => write!(formatter, "'import'"),
            Self::Pub => write!(formatter, "'pub'"),
            Self::Defer => write!(formatter, "'defer'"),
            Self::While => write!(formatter, "'while'"),
            Self::LeftParen => write!(formatter, "'('"),
            Self::RightParen => write!(formatter, "')'"),
            Self::LeftBrace => write!(formatter, "'{{'"),
            Self::RightBrace => write!(formatter, "'}}'"),
            Self::LeftBracket => write!(formatter, "'['"),
            Self::RightBracket => write!(formatter, "']'"),
            Self::Semicolon => write!(formatter, "';'"),
            Self::Comma => write!(formatter, "','"),
            Self::Colon => write!(formatter, "':'"),
            Self::Arrow => write!(formatter, "'->'"),
            Self::FatArrow => write!(formatter, "'=>'"),
            Self::Dot => write!(formatter, "'.'"),
            Self::At => write!(formatter, "'@'"),
            Self::Ellipsis => write!(formatter, "'...'"),
            Self::Plus => write!(formatter, "'+'"),
            Self::Minus => write!(formatter, "'-'"),
            Self::Star => write!(formatter, "'*'"),
            Self::Slash => write!(formatter, "'/'"),
            Self::Percent => write!(formatter, "'%'"),
            Self::EqualEqual => write!(formatter, "'=='"),
            Self::BangEqual => write!(formatter, "'!='"),
            Self::Less => write!(formatter, "'<'"),
            Self::LessEqual => write!(formatter, "'<='"),
            Self::Greater => write!(formatter, "'>'"),
            Self::GreaterEqual => write!(formatter, "'>='"),
            Self::AmpAmp => write!(formatter, "'&&'"),
            Self::PipePipe => write!(formatter, "'||'"),
            Self::Amp => write!(formatter, "'&'"),
            Self::Pipe => write!(formatter, "'|'"),
            Self::Caret => write!(formatter, "'^'"),
            Self::Tilde => write!(formatter, "'~'"),
            Self::LessLess => write!(formatter, "'<<'"),
            Self::GreaterGreater => write!(formatter, "'>>'"),
            Self::Bang => write!(formatter, "'!'"),
            Self::Equal => write!(formatter, "'='"),
            Self::ColonEqual => write!(formatter, "':='"),
            Self::PlusEqual => write!(formatter, "'+='"),
            Self::MinusEqual => write!(formatter, "'-='"),
            Self::StarEqual => write!(formatter, "'*='"),
            Self::SlashEqual => write!(formatter, "'/='"),
            Self::End => write!(formatter, "end of file"),
        }
    }
}

pub fn keyword(name: &str) -> Option<Token> {
    match name {
        "fn" => Some(Token::Fn),
        "extern" => Some(Token::Extern),
        "type" => Some(Token::Type),
        "const" => Some(Token::Const),
        "impl" => Some(Token::Impl),
        "let" => Some(Token::Let),
        "var" => Some(Token::Var),
        "ref" => Some(Token::Ref),
        "if" => Some(Token::If),
        "else" => Some(Token::Else),
        "match" => Some(Token::Match),
        "label" => Some(Token::Label),
        "jump" => Some(Token::Jump),
        "return" => Some(Token::Return),
        "print" => Some(Token::Print),
        "as" => Some(Token::As),
        "sizeof" => Some(Token::Sizeof),
        "call" => Some(Token::Call),
        "true" => Some(Token::True),
        "false" => Some(Token::False),
        "import" => Some(Token::Import),
        "pub" => Some(Token::Pub),
        "defer" => Some(Token::Defer),
        "while" => Some(Token::While),
        _ => None,
    }
}