Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions kframework/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#[derive(Debug)]
pub enum KError {
KoreLexerError(String),
KoreParseError(String),
}
Comment on lines +2 to +5
Copy link
Copy Markdown
Collaborator

@tothtamas28 tothtamas28 Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider defining pub struct KoreLexerError in lexer.rs and pub struct KoreParserError in parser.rs. This reduces coupling between the two modules and keeps error definitions close to their source.

There doesn’t seem to be a strong need to handle these two errors uniformly. But even if such a need exists, you can still define a wrapper type:

pub enum KError {
    ParserError(KoreParserError),
    LexerError(KoreLexerError),
}


impl std::fmt::Display for KError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
KError::KoreParseError(msg) => write!(f, "{msg}"),
KError::KoreLexerError(msg) => write!(f, "{msg}"),
}
}
}

impl std::error::Error for KError {}
27 changes: 15 additions & 12 deletions kframework/src/kore/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::error::KError::{self, KoreLexerError};
use std::str::Chars;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -86,8 +87,7 @@ impl<'a> Lexer<'a> {
}
}

pub fn next_token(&mut self) -> Result<Token<'a>, String> {
// TODO KoreLexerError
pub fn next_token(&mut self) -> Result<Token<'a>, KError> {
let la = loop {
let Some(la) = self.la else {
return Ok(Token {
Expand Down Expand Up @@ -119,7 +119,7 @@ impl<'a> Lexer<'a> {
'[' => self.lbrack(),
']' => self.rbrack(),
// Error
_ => return Err(format!("Unexpected character: {:?}", la)),
_ => return Err(KoreLexerError(format!("Unexpected character: {:?}", la))),
};
debug_assert!(token.text == &self.text[token.offset..token.offset + token.text.len()]);
Ok(token)
Expand All @@ -138,7 +138,7 @@ impl<'a> Lexer<'a> {
Token { ty, text, offset }
}

fn string(&mut self) -> Result<Token<'a>, String> {
fn string(&mut self) -> Result<Token<'a>, KError> {
debug_assert!(self.la == Some('"'));
let offset = self.offset;
let mut len: usize = 1;
Expand Down Expand Up @@ -199,7 +199,7 @@ impl<'a> Lexer<'a> {
Token { ty, text, offset }
}

fn symbol_or_ml_conn(&mut self) -> Result<Token<'a>, String> {
fn symbol_or_ml_conn(&mut self) -> Result<Token<'a>, KError> {
debug_assert!(self.la == Some('\\'));

let offset = self.offset;
Expand All @@ -208,7 +208,7 @@ impl<'a> Lexer<'a> {
self.consume();
let la = self.la_or_err()?;
if !la.is_ascii_alphabetic() {
return Err(format!("Expected letter, got: {:?}", la));
return Err(KoreLexerError(format!("Expected letter, got: {:?}", la)));
}

self.consume();
Expand Down Expand Up @@ -248,7 +248,7 @@ impl<'a> Lexer<'a> {
Ok(Token { ty, text, offset })
}

fn set_var_id(&mut self) -> Result<Token<'a>, String> {
fn set_var_id(&mut self) -> Result<Token<'a>, KError> {
debug_assert!(self.la == Some('@'));

let offset = self.offset;
Expand All @@ -258,7 +258,7 @@ impl<'a> Lexer<'a> {
self.consume();
let la = self.la_or_err()?;
if !la.is_ascii_alphabetic() {
return Err(format!("Expected letter, got: {:?}", la));
return Err(KoreLexerError(format!("Expected letter, got: {:?}", la)));
}

// TODO consume_while or consume_until
Expand Down Expand Up @@ -383,7 +383,7 @@ impl<'a> Lexer<'a> {
}
}

fn consume_comment(&mut self) -> Result<(), String> {
fn consume_comment(&mut self) -> Result<(), KError> {
debug_assert!(self.la == Some('/'));

self.consume();
Expand Down Expand Up @@ -420,7 +420,10 @@ impl<'a> Lexer<'a> {
}
}

_ => Err(format!("Expected '/' or '*', got: {:?}", la)),
_ => Err(KoreLexerError(format!(
"Expected '/' or '*', got: {:?}",
la
))),
}
}

Expand All @@ -433,9 +436,9 @@ impl<'a> Lexer<'a> {
res
}

fn la_or_err(&self) -> Result<char, String> {
fn la_or_err(&self) -> Result<char, KError> {
self.la
.ok_or_else(|| String::from("Unexpected end of file"))
.ok_or_else(|| KoreLexerError(String::from("Unexpected end of file")))
}
}

Expand Down
Loading