logos icon indicating copy to clipboard operation
logos copied to clipboard

No way to propagate error from callback

Open stepancheg opened this issue 2 years ago • 2 comments

Consider this example:

fn parse_int(lex: &Lexer<Token>) -> ??? {
  lex.slice().parse::<i32>()
}

enum Token {
  #[error]
  Error,

  #[regex("[0-9]+"), parse_int]
  Int(i32),

  #[regex(...), parse_string_literal]
  StringLiteral,
}

Parse errors are converted to Token::Error which contains no information. There's no way (to my knowledge) to propagate parse_int or parse_string_literal error to the caller.

stepancheg avatar Apr 16 '22 06:04 stepancheg

Wanted to second this. I am trying to propagate an error out from one of my token parsers, but it appears there is no way to map the Error token to something else?

daprahamian avatar Jan 07 '23 01:01 daprahamian

As a workaround, you can store extra error information inside the extras field of the lexer

fn parse_int(lex: &mut Lexer<Token>) -> Result<i32, ()> {
  match lex.slice().parse::<i32>() {
    Ok(value) => Ok(value),
    Err(err) => {
      lex.extras.errors.push(err.into());
      Err(())
    }
}

#[derive(Default)]
struct TokenExtras {
    errors: Vec<MyErrorType>,
}

#[derive(Logos)]
#[logos(extras = TokenExtras)]
enum Token {
  #[error]
  Error,
  #[regex("[0-9]+"), parse_int]
  Int(i32),
}

andrewhickman avatar Jan 07 '23 12:01 andrewhickman