AkurAI Build
Menu

Public repository / master

AkurAI-Framework / crates/sql/src/token.rs

Latest change fa37e2544d31a1199954a395f9e5b3ebb237b17f - feat(sql): aggregates (COUNT/SUM/AVG/MIN/MAX) + GROUP BY (gpt-5.5) by Ólafur Búi Ólafsson

//! The SQL [`Token`] type, the keyword table, and position-tagged tokens.

/// A reserved SQL keyword. Recognised case-insensitively by the lexer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Keyword {
    Select,
    From,
    Where,
    Insert,
    Into,
    Values,
    Update,
    Set,
    Delete,
    Create,
    Table,
    Order,
    Group,
    By,
    Limit,
    And,
    Or,
    Not,
    Null,
    Asc,
    Desc,
    // Column types (used in CREATE TABLE).
    Integer,
    Text,
    Real,
}

impl Keyword {
    /// Map an already-uppercased word to its keyword, if it is reserved.
    pub(crate) fn from_upper(word: &str) -> Option<Keyword> {
        Some(match word {
            "SELECT" => Keyword::Select,
            "FROM" => Keyword::From,
            "WHERE" => Keyword::Where,
            "INSERT" => Keyword::Insert,
            "INTO" => Keyword::Into,
            "VALUES" => Keyword::Values,
            "UPDATE" => Keyword::Update,
            "SET" => Keyword::Set,
            "DELETE" => Keyword::Delete,
            "CREATE" => Keyword::Create,
            "TABLE" => Keyword::Table,
            "ORDER" => Keyword::Order,
            "GROUP" => Keyword::Group,
            "BY" => Keyword::By,
            "LIMIT" => Keyword::Limit,
            "AND" => Keyword::And,
            "OR" => Keyword::Or,
            "NOT" => Keyword::Not,
            "NULL" => Keyword::Null,
            "ASC" => Keyword::Asc,
            "DESC" => Keyword::Desc,
            "INTEGER" => Keyword::Integer,
            "TEXT" => Keyword::Text,
            "REAL" => Keyword::Real,
            _ => return None,
        })
    }
}

/// A single lexical token.
#[derive(Debug, Clone, PartialEq)]
pub enum Token {
    /// A reserved keyword (case-insensitive in the source).
    Keyword(Keyword),
    /// An identifier — table or column name.
    Ident(String),
    /// A single-quoted string literal (with `''` unescaped to `'`).
    Str(String),
    /// An integer literal (no `.`/`e`).
    Int(i64),
    /// A floating-point literal.
    Float(f64),

    // Operators.
    Eq,    // =
    NotEq, // != or <>
    Lt,    // <
    LtEq,  // <=
    Gt,    // >
    GtEq,  // >=

    // Punctuation.
    Star,      // *
    Comma,     // ,
    LParen,    // (
    RParen,    // )
    Semicolon, // ;
}

/// A token plus the character offset where it begins. Carried through to the
/// parser so error messages point at the right place.
#[derive(Debug, Clone, PartialEq)]
pub struct SpannedToken {
    pub token: Token,
    pub pos: usize,
}