From 71a9d82d963481c040331274de8ee132d88e9e82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Sat, 10 May 2025 10:55:44 +0200 Subject: [PATCH] feat(tokenizer): add Token::Int & UnexpectedToken error type --- notes.org | 6 +++++- src/command.rs | 3 ++- src/error_display.rs | 8 ++++++-- src/parser.rs | 12 ++++++++++-- src/tokens.rs | 1 + 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/notes.org b/notes.org index 9e6c400..1d0c993 100644 --- a/notes.org +++ b/notes.org @@ -212,7 +212,11 @@ i will use rustyline, since it seems like the most feature-complete * DONE remove uneeded error variants -* TODO cli tests using insta-cmd +* STRT parse integers + +* TODO parse strings + +* WAIT cli tests using insta-cmd https://insta.rs/docs/cmd/ * WAIT autocompletion diff --git a/src/command.rs b/src/command.rs index c325db6..75c2792 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,6 +1,6 @@ use crate::meta_commands::{MetaCommand, MetaCommandExecuteResult}; use crate::statements::{Statement, StatementExecuteResult}; -use crate::tokens::ScanError; +use crate::tokens::{ScanError, Token}; #[derive(Debug)] pub enum Command { @@ -50,6 +50,7 @@ impl Command { #[derive(Debug)] pub enum CommandParseError { Scan(ScanError), + UnexpectedToken(Token, &'static [&'static str]), } impl From for Command { diff --git a/src/error_display.rs b/src/error_display.rs index fcb6468..5297abb 100644 --- a/src/error_display.rs +++ b/src/error_display.rs @@ -7,8 +7,12 @@ pub trait OSDBError { impl OSDBError for CommandParseError { fn display(&self, file: &str, input: &str) { - let CommandParseError::Scan(x) = self; - x.display(file, input); + match self { + CommandParseError::Scan(x) => { + x.display(file, input); + } + _ => todo!(), + } } } diff --git a/src/parser.rs b/src/parser.rs index 98325d8..e97d474 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -11,7 +11,7 @@ pub fn parse(file: String, input: String) -> Result, Vec>())? .into(); let mut cmds = Vec::new(); - let errs = Vec::new(); + let mut errs = Vec::new(); while let Some(token) = tokens.pop_front() { match token.data { crate::tokens::TokenData::Insert => cmds.push(Command::Statement(Statement::Insert)), @@ -19,10 +19,18 @@ pub fn parse(file: String, input: String) -> Result, Vec { cmds.push(Command::MetaCommand(meta_command)) } + crate::tokens::TokenData::Int(_) => errs.push(CommandParseError::UnexpectedToken( + token, + &["statement", "meta command", "eof"], + )), crate::tokens::TokenData::EndOfFile => (), } } - if errs.is_empty() { Ok(cmds) } else { Err(errs) } + if errs.is_empty() { + Ok(cmds) + } else { + Err(errs) + } } #[cfg(test)] diff --git a/src/tokens.rs b/src/tokens.rs index 0c5c1f7..958a254 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -6,6 +6,7 @@ pub enum TokenData { Select, MetaCommand(MetaCommand), EndOfFile, + Int(i64), } #[derive(Debug, Eq, PartialEq)]