Skip to content

0.13

Compare
Choose a tag to compare
@maciejhirsz maciejhirsz released this 10 Apr 11:12
· 163 commits to master since this release
cd6d40a

This is a long overdue release that includes a bunch of community provided PRs over the last months:

  • ⚠️ BREAKING: @agluszak did the heroic feat of making the Lexer produce a Result<Token, Token::Error> which removes, the need for the #[error] variant. More on that below. (#273)
  • @jannik4 implemented support for #[logos(crate = path::to::logos)]. (#268)
  • @Plaba added the support for {n,m} regex ranges. (#278)
  • SpannedIter now derefs into Lexer, so all Lexer methods are available on it, based on work started by @simvux. (#283, #231)
  • Added new #[logos(skip ...)] attribute to help declare whitespace now that #[error] variant is gone. (#284)
  • Updated syn dependency to 2.0. (#289)

Migrating from 0.12

In 0.12 interacting with Logos would look like:

use logos::Logos;

#[derive(Logos, Debug, PartialEq)]
enum Token {
    #[token("Hello")]
    Hello,

    #[token("Logos")]
    Logos,

    // This variant will be emitted if an error is encountered
    #[error]
    // By convention we also add an attribute to skip whitespace here
    #[regex(r"[ \t\n\f]+", logos::skip)]
    Error,
}

fn main() {
    let mut lex = Token::lexer("Hello Logos");

    // `Lexer` is an iterator of `Token`:
    assert_eq!(lex.next(), Some(Token::Hello));
    assert_eq!(lex.next(), Some(Token::Logos));
    assert_eq!(lex.next(), None);
}

In 0.13 the #[error] variant is gone, this is the updated code:

use logos::Logos;

#[derive(Logos, Debug, PartialEq)]
#[logos(skip r"[ \t\n\f]+")] // new way to annotate whitespace
enum Token {
    #[token("Hello")]
    Hello,

    #[token("Logos")]
    Logos,
}

fn main() {
    let mut lex = Token::lexer("Hello Logos");

    // `Lexer` is an iterator of `Result<Token, Token::Error>`:
    assert_eq!(lex.next(), Some(Ok(Token::Hello)));
    assert_eq!(lex.next(), Some(Ok(Token::Logos)));
    assert_eq!(lex.next(), None);
}

By default the associated Token::Error type is just () and holds no data. Originally I've put the #[error] variant on the token because having one flat enum to make a match on seemed like a performance win. Upon some scrutiny however there is no performance cost to matching on Result<Token, ()> vs flat Token due to Rust's ability to optimize enums: if your Token is a simple enumeration with only unit variants (holds no data), and the number of variants doesn't exceed 254, then Result<Token, ()> and Option<Result<Token, ()>> will be represented by a single byte at runtime, and matching for pattern Ok(Token::Hello) in 0.13 should compile to the same code as plain Token::Hello would be in 0.12. See the full discussion in #104.

Future of Logos

If you've been using Logos for a while now and you've been fighting with some compile-time bugs, you might have been expecting a bunch of fixes here. Alas, as much as it pains me, there are none. There are two main reasons for this, one being me taking a really long coding break last year well into this year:

image

This is including public and private repos, I simply wrote no code at all in any capacity, not personal projects, not professional, nothing at all. I really needed that time.

While I'm back and active now, a lot of my time currently is spent on Kobold.

The derive macro hell

The elephant in the room is that the derive macro is simply unfixable in the current state and requires a complete rewrite. Last time this happened was 3 years ago in 0.10. That rewrite improved things considerably and allowed me to fix numerous outstanding bugs at the time. It took me about two weeks of pretty intense work, and I reckon this time it would be no smaller task.

The main issue is that I've chosen to implement different states of the state machine as separate functions. This made it impossible for the these functions to share common variables on stack, but being really clever I got it to work for the existing test suite. A fatal mistake. Douglas Crockford once said, paraphrasing from memory:

Debugging is harder than writing code. If you write code as cleverly as you possibly can, you are by definition not smart enough to debug it.

The good news is I know how to fix things in a way that basically avoids nearly all pitfalls of current implementation. Instead of functions all states of the state machine should be just unit variants of a single enum with some code in their corresponding match expression arms stuck in a loop.

There are pros and cons to that. When it comes to performance lookup tables that currently use fn pointers would become much smaller (by a factor of 8 for most projects compiled on 64bit architecture), making it much less demanding on CPU cache. On the flip side the generated code would lose automatic inlining of nested function calls, and while that can be done manually as an optimization rolled into the macro itself, it might be a new source of bugs if not done right, and it will almost definitely bloat out the amount of code produced (though it might be sensible to have some feature flags to disable this, particularly for targets like wasm32).

The rewrite in principle should also make it possible to make Logos use ropes such as ropey.

Phasing out #[regex]

Not as much of a nightmare, but something I feel has been a problem for users is relying on a subset of regex syntax, particularly limiting backtracking and look-ahead features for the sake of both simplicity and performance. While it is possible to express pretty much any common programming language syntax necessary with that subset, often enough people find it confusing that you can't just do things like x*?.

What I'd like to do eventually is expand on the #[token] attribute with more DSL-y features, using string literals as means of escaping characters, backwards compatible with current use of the attribute to denote a simple byte sequence. Consider a simple block comment /* ... */, I would like that to be declared as:

#[derive(Logos)]
enum Token {
    #[token("/*" ... "*/")]
    BlockComment,

    // ...
}

The equivalent regex rules for that today would be #[regex(r"\/\*([^*]|\*[^/])*\*\/")]... I think. The fact that I'm not able to write this out of the top of my head without being sure it works should be a testament enough of how clunky it can be. Not to mention this one example kind of breaks one of the main promises of Logos, and that is of making the generated code faster than anything you could write by hand. You do not want to see the codegen for that particular regex.

When?

I've no idea.

In the next few weeks I'd like to get Kobold to 1.0. I also want to get Ramhorns to 1.0 sometime soon, that carte honestly should have been marked 1.0 since last year. I've been guilty of not pushing my crates to 1.0 fast enough, and by not fast enough I mean I've been on noughty versions for most of my projects for years, even though most of them really are production ready and I'm just too much of a chicken to stabilize the API.

With the changes done in 0.13 now I believe the last two issues standing ahead of Logos going 1.0 are the two mentioned above: derive rewrite to fix bugs, and #[token] syntax expansion / phasing out of #[regex] to make the API nicer to use. The former is something I'm best suited to do alone, although the latter could be done by community (though realistically only after the rewrite is done).

If there is a living soul willing to undertake the big rewrite and help push this along faster, do reach out. I can at very least help explain what the hell current codegen is doing. I'd be also happy to give a person or two the ability to review and merge community PRs.