Skip to content

Commit

Permalink
Added mini example to book
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteriouslyseeing committed Dec 7, 2024
1 parent 7834597 commit ac220e4
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions book/src/attributes/logos.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ 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.

`ErrorType` must implement the `Default` trait because invalid tokens, i.e.,
literals that do not match any variant, will produce `Err(ErrorType::default())`,
unless you provide a callback with the alternate syntax `#[logos(error(ErrorType, callback = ...))]`

For example, here is an example using a custom error type:

```rust,no_run,noplayground
Expand All @@ -47,6 +43,24 @@ 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
`#[logos(error(ErrorType, callback = ...))]`, which allows you to include information
from the lexer such as the span where the error occurred:

```rust,no_run,noplayground
#[derive(Logos)]
#[logos(error(Range<usize>, callback = |lex| lex.span()))]
enum Token {
#[token("a")]
A,
#[token("b")]
B,
}
```

## Specifying path to logos

You can force the derive macro to use a different path to `Logos`'s crate
Expand Down

0 comments on commit ac220e4

Please sign in to comment.