feat(errors): improve error messages with example values
Add token type examples to make error messages more helpful. Created an ExpectedToken enum to replace string literals for better type safety, added example values for each token type, and enhanced error display to show concrete examples of valid syntax.
This commit is contained in:
parent
e78511f692
commit
567aa31c07
8 changed files with 105 additions and 25 deletions
|
|
@ -47,10 +47,47 @@ impl Command {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ExpectedToken {
|
||||
Integer,
|
||||
String,
|
||||
Semicolon,
|
||||
Statement,
|
||||
MetaCommand,
|
||||
EndOfFile,
|
||||
}
|
||||
|
||||
impl ExpectedToken {
|
||||
/// Returns an example value for this token type
|
||||
pub fn example(&self) -> &'static str {
|
||||
match self {
|
||||
ExpectedToken::Integer => "42",
|
||||
ExpectedToken::String => "\"example\"",
|
||||
ExpectedToken::Semicolon => ";",
|
||||
ExpectedToken::Statement => "select",
|
||||
ExpectedToken::MetaCommand => ".exit",
|
||||
ExpectedToken::EndOfFile => "<end of input>",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ExpectedToken {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
ExpectedToken::Integer => write!(f, "integer"),
|
||||
ExpectedToken::String => write!(f, "string"),
|
||||
ExpectedToken::Semicolon => write!(f, "semicolon"),
|
||||
ExpectedToken::Statement => write!(f, "statement"),
|
||||
ExpectedToken::MetaCommand => write!(f, "meta command"),
|
||||
ExpectedToken::EndOfFile => write!(f, "end of file"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum CommandParseError {
|
||||
Scan(ScanError),
|
||||
UnexpectedToken(Token, &'static [&'static str]),
|
||||
UnexpectedToken(Token, &'static [ExpectedToken]),
|
||||
}
|
||||
|
||||
impl From<MetaCommand> for Command {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue