feat(tokenizer): add Token::Int & UnexpectedToken error type
This commit is contained in:
parent
faedfadeab
commit
71a9d82d96
5 changed files with 24 additions and 6 deletions
|
|
@ -212,7 +212,11 @@ i will use rustyline, since it seems like the most feature-complete
|
||||||
|
|
||||||
* DONE remove uneeded error variants
|
* 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/
|
https://insta.rs/docs/cmd/
|
||||||
|
|
||||||
* WAIT autocompletion
|
* WAIT autocompletion
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::meta_commands::{MetaCommand, MetaCommandExecuteResult};
|
use crate::meta_commands::{MetaCommand, MetaCommandExecuteResult};
|
||||||
use crate::statements::{Statement, StatementExecuteResult};
|
use crate::statements::{Statement, StatementExecuteResult};
|
||||||
use crate::tokens::ScanError;
|
use crate::tokens::{ScanError, Token};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
|
|
@ -50,6 +50,7 @@ impl Command {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum CommandParseError {
|
pub enum CommandParseError {
|
||||||
Scan(ScanError),
|
Scan(ScanError),
|
||||||
|
UnexpectedToken(Token, &'static [&'static str]),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<MetaCommand> for Command {
|
impl From<MetaCommand> for Command {
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,12 @@ pub trait OSDBError {
|
||||||
|
|
||||||
impl OSDBError for CommandParseError {
|
impl OSDBError for CommandParseError {
|
||||||
fn display(&self, file: &str, input: &str) {
|
fn display(&self, file: &str, input: &str) {
|
||||||
let CommandParseError::Scan(x) = self;
|
match self {
|
||||||
x.display(file, input);
|
CommandParseError::Scan(x) => {
|
||||||
|
x.display(file, input);
|
||||||
|
}
|
||||||
|
_ => todo!(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ pub fn parse(file: String, input: String) -> Result<Vec<Command>, Vec<CommandPar
|
||||||
.map_err(|x| x.into_iter().map(|x| x.into()).collect::<Vec<_>>())?
|
.map_err(|x| x.into_iter().map(|x| x.into()).collect::<Vec<_>>())?
|
||||||
.into();
|
.into();
|
||||||
let mut cmds = Vec::new();
|
let mut cmds = Vec::new();
|
||||||
let errs = Vec::new();
|
let mut errs = Vec::new();
|
||||||
while let Some(token) = tokens.pop_front() {
|
while let Some(token) = tokens.pop_front() {
|
||||||
match token.data {
|
match token.data {
|
||||||
crate::tokens::TokenData::Insert => cmds.push(Command::Statement(Statement::Insert)),
|
crate::tokens::TokenData::Insert => cmds.push(Command::Statement(Statement::Insert)),
|
||||||
|
|
@ -19,10 +19,18 @@ pub fn parse(file: String, input: String) -> Result<Vec<Command>, Vec<CommandPar
|
||||||
crate::tokens::TokenData::MetaCommand(meta_command) => {
|
crate::tokens::TokenData::MetaCommand(meta_command) => {
|
||||||
cmds.push(Command::MetaCommand(meta_command))
|
cmds.push(Command::MetaCommand(meta_command))
|
||||||
}
|
}
|
||||||
|
crate::tokens::TokenData::Int(_) => errs.push(CommandParseError::UnexpectedToken(
|
||||||
|
token,
|
||||||
|
&["statement", "meta command", "eof"],
|
||||||
|
)),
|
||||||
crate::tokens::TokenData::EndOfFile => (),
|
crate::tokens::TokenData::EndOfFile => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if errs.is_empty() { Ok(cmds) } else { Err(errs) }
|
if errs.is_empty() {
|
||||||
|
Ok(cmds)
|
||||||
|
} else {
|
||||||
|
Err(errs)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ pub enum TokenData {
|
||||||
Select,
|
Select,
|
||||||
MetaCommand(MetaCommand),
|
MetaCommand(MetaCommand),
|
||||||
EndOfFile,
|
EndOfFile,
|
||||||
|
Int(i64),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue