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
15 changes: 8 additions & 7 deletions book/src/attributes/logos.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ For more details about extras, read the [eponym section](../extras.md).

By default, **Logos** uses `()` as the error type, which means that it
doesn't store any information about the error.
This can be changed by using `#[logos(error = ErrorType)]` attribute on the enum.
The type `ErrorType` can be any type that implements `Clone`, `PartialEq`,
`Default` and `From<E>` for each callback's error type.
This can be changed by using the `#[logos(error = ErrorType)]` attribute on the enum.
The type `ErrorType` can be any type that implements `From<E>` for each callback's error type.
If no error callback is provided, `ErrorType` must implement `Default` as well.

Here is an example using a custom error type:

Expand All @@ -46,10 +46,7 @@ You can add error variants to `LexingError`,
and implement `From<E>` for each error type `E` that could
be returned by a callback. See [callbacks](../callbacks.md).

`ErrorType` must implement the `Default` trait because invalid tokens, i.e.,
literals that do not match any variant, will produce `Err(ErrorType::default())`.

Alternatively, you can provide a callback with the alternate syntax
You can provide a callback with the syntax
`#[logos(error(ErrorType, callback = ...))]`, which allows you to include information
from the lexer such as the span where the error occurred:

Expand All @@ -64,6 +61,10 @@ enum Token {
}
```

If you do not provide a callback, then invalid tokens, i.e.,
literals that do not match any variant, will produce `Err(ErrorType::default())`.
This requires `ErrorType` to implement the `Default` trait.

## Specifying path to logos

You can force the derive macro to use a different path to `Logos`' crate
Expand Down
11 changes: 2 additions & 9 deletions examples/array_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,10 @@ use std::path::Path;

/* ANCHOR: error_type */
/// Token error type, tied to the lifetime of the source.
#[derive(Default, Debug, Clone, PartialEq)]
enum LexingError<'s> {
UnknownSymbol(&'s str),
InvalidInteger {
err: ParseIntError,
source: &'s str,
},
InvalidInteger { err: ParseIntError, source: &'s str },
UnknownVariable(&'s str),
#[default]
Other,
}
/* ANCHOR_END: error_type */

Expand All @@ -41,7 +35,6 @@ impl Display for LexingError<'_> {
write!(f, "int error in source `{source}`: {err}")
}
Self::UnknownVariable(s) => write!(f, "unknown variable `{s}`"),
Self::Other => write!(f, "unknown error"),
}
}
}
Expand All @@ -51,7 +44,7 @@ type Environment = HashMap<String, Vec<i128>>;
/* ANCHOR_END: environment */

/* ANCHOR: callbacks */
/// Parse lexer slice as an i128
/// Parse lexer slice as an `i128`
fn number_callback<'s>(lex: &mut Lexer<'s, Token>) -> Result<i128, LexingError<'s>> {
let source = lex.slice();
let res = source.parse();
Expand Down
4 changes: 1 addition & 3 deletions examples/custom_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ use logos::Logos;

use std::num::ParseIntError;

#[derive(Default, Debug, Clone, PartialEq)]
#[derive(Debug, PartialEq)]
enum LexingError {
InvalidInteger(String),
NonAsciiCharacter(char),
#[default]
Other,
}

/// Error type returned by calling `lex.slice().parse()` to u8.
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

extern crate core;

use core::fmt::Debug;
#[cfg(feature = "export_derive")]
pub use logos_derive::Logos;

Expand All @@ -53,7 +52,7 @@ pub trait Logos<'source>: Sized {

/// Error type returned by the lexer. This can be set using
/// `#[logos(error = MyError)]`. Defaults to `()` if not set.
type Error: Default + Clone + PartialEq + Debug + 'source;
type Error: 'source;

/// The heart of Logos. Called by the `Lexer`. The implementation for this function
/// is generated by the `logos-derive` crate.
Expand Down
1 change: 1 addition & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn assert_lex<'a, Token>(
) where
Token: Logos<'a> + fmt::Debug + PartialEq,
Token::Extras: Default,
Token::Error: fmt::Debug + PartialEq,
{
let mut lex = Token::lexer(source);

Expand Down
Loading