| 1 | <img src="https://raw.githubusercontent.com/maciejhirsz/logos/master/logos.svg?sanitize=true" alt="Logos logo" width="250" align="right"> |
| 2 | |
| 3 | # Logos |
| 4 | |
| 5 | [](https://logos.maciej.codes/) |
| 6 | [](https://crates.io/crates/logos) |
| 7 | [](https://docs.rs/logos) |
| 8 | [](https://crates.io/crates/logos) |
| 9 | [](https://codecov.io/gh/maciejhirsz/logos) |
| 10 | |
| 11 | _Create ridiculously fast Lexers._ |
| 12 | |
| 13 | **Logos** has two goals: |
| 14 | |
| 15 | + To make it easy to create a Lexer, so you can focus on more complex problems. |
| 16 | + To make the generated Lexer faster than anything you'd write by hand. |
| 17 | |
| 18 | To achieve those, **Logos**: |
| 19 | |
| 20 | + Combines all token definitions into a single [deterministic state machine](https://en.wikipedia.org/wiki/Deterministic_finite_automaton). |
| 21 | + Optimizes branches into [lookup tables](https://en.wikipedia.org/wiki/Lookup_table) or [jump tables](https://en.wikipedia.org/wiki/Branch_table). |
| 22 | + Prevents [backtracking](https://en.wikipedia.org/wiki/ReDoS) inside token definitions. |
| 23 | + [Unwinds loops](https://en.wikipedia.org/wiki/Loop_unrolling), and batches reads to minimize bounds checking. |
| 24 | + Does all of that heavy lifting at compile time. |
| 25 | |
| 26 | ## Example |
| 27 | |
| 28 | ```rust |
| 29 | use logos::Logos; |
| 30 | |
| 31 | #[derive(Logos, Debug, PartialEq)] |
| 32 | #[logos(skip r"[ \t\n\f]+")] // Ignore this regex pattern between tokens |
| 33 | enum Token { |
| 34 | // Tokens can be literal strings, of any length. |
| 35 | #[token("fast")] |
| 36 | Fast, |
| 37 | |
| 38 | #[token(".")] |
| 39 | Period, |
| 40 | |
| 41 | // Or regular expressions. |
| 42 | #[regex("[a-zA-Z]+")] |
| 43 | Text, |
| 44 | } |
| 45 | |
| 46 | fn main() { |
| 47 | let mut lex = Token::lexer("Create ridiculously fast Lexers."); |
| 48 | |
| 49 | assert_eq!(lex.next(), Some(Ok(Token::Text))); |
| 50 | assert_eq!(lex.span(), 0..6); |
| 51 | assert_eq!(lex.slice(), "Create"); |
| 52 | |
| 53 | assert_eq!(lex.next(), Some(Ok(Token::Text))); |
| 54 | assert_eq!(lex.span(), 7..19); |
| 55 | assert_eq!(lex.slice(), "ridiculously"); |
| 56 | |
| 57 | assert_eq!(lex.next(), Some(Ok(Token::Fast))); |
| 58 | assert_eq!(lex.span(), 20..24); |
| 59 | assert_eq!(lex.slice(), "fast"); |
| 60 | |
| 61 | assert_eq!(lex.next(), Some(Ok(Token::Text))); |
| 62 | assert_eq!(lex.slice(), "Lexers"); |
| 63 | assert_eq!(lex.span(), 25..31); |
| 64 | |
| 65 | assert_eq!(lex.next(), Some(Ok(Token::Period))); |
| 66 | assert_eq!(lex.span(), 31..32); |
| 67 | assert_eq!(lex.slice(), "."); |
| 68 | |
| 69 | assert_eq!(lex.next(), None); |
| 70 | } |
| 71 | ``` |
| 72 | |
| 73 | For more examples and documentation, please refer to the |
| 74 | [Logos handbook](https://maciejhirsz.github.io/logos/) or the |
| 75 | [crate documentation](https://docs.rs/logos/latest/logos/). |
| 76 | |
| 77 | ## How fast? |
| 78 | |
| 79 | Ridiculously fast! |
| 80 | |
| 81 | ```norust |
| 82 | test identifiers ... bench: 647 ns/iter (+/- 27) = 1204 MB/s |
| 83 | test keywords_operators_and_punctators ... bench: 2,054 ns/iter (+/- 78) = 1037 MB/s |
| 84 | test strings ... bench: 553 ns/iter (+/- 34) = 1575 MB/s |
| 85 | ``` |
| 86 | |
| 87 | ## Acknowledgements |
| 88 | |
| 89 | + [Pedrors](https://pedrors.pt/) for the **Logos** logo. |
| 90 | |
| 91 | ## Thank you |
| 92 | |
| 93 | **Logos** is very much a labor of love. If you find it useful, consider |
| 94 | [getting me some coffee](https://github.com/sponsors/maciejhirsz). ☕ |
| 95 | |
| 96 | If you'd like to contribute to Logos, then consider reading the |
| 97 | [Contributing guide](https://maciejhirsz.github.io/logos/contributing). |
| 98 | |
| 99 | ## Contributing |
| 100 | |
| 101 | **Logos** welcome any kind of contribution: bug reports, suggestions, |
| 102 | or new features! |
| 103 | |
| 104 | Please use the |
| 105 | [issues](https://github.com/maciejhirsz/logos/issues) or |
| 106 | [pull requests](https://github.com/maciejhirsz/logos/pulls) tabs, |
| 107 | when appropriate. |
| 108 | |
| 109 | To release a new version, follow the [RELEASE-PROCESS](RELEASE-PROCESS.md) |
| 110 | |
| 111 | ## License |
| 112 | |
| 113 | This code is distributed under the terms of both the MIT license |
| 114 | and the Apache License (Version 2.0), choose whatever works for you. |
| 115 | |
| 116 | See [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT) for details. |