1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! Lexer and parser collections.
//!
//! With `laps`, you can build lexers/parsers by just defining tokens/ASTs
//! and deriving [`Tokenize`](lexer::Tokenize)/[`Parse`](parse::Parse)
//! trait for them.
//!
//! # Example
//!
//! Implement a lexer for
//! [S-expression](https://en.wikipedia.org/wiki/S-expression):
//!
//! # fn main() {}
//! use laps::prelude::*;
//!
//! #[token_kind]
//! #[derive(Debug, Tokenize)]
//! enum TokenKind {
//! // This token will be skipped.
//! #[skip(r"\s+")]
//! _Skip,
//! /// Parentheses.
//! #[regex(r"[()]")]
//! Paren(char),
//! /// Atom.
//! #[regex(r"[^\s()]+")]
//! Atom(String),
//! /// End-of-file.
//! #[eof]
//! Eof,
//! }
//! ```
//!
//! And the parser and [ASTs](https://en.wikipedia.org/wiki/Abstract_syntax_tree)
//! (or actually [CSTs](https://en.wikipedia.org/wiki/Parse_tree)):
//!
//! # fn main() {}
//! # use laps::prelude::*;
//! # #[token_kind]
//! # #[derive(Debug, Tokenize)]
//! # enum TokenKind {
//! # // This token will be skipped.
//! # #[skip(r"\s+")]
//! # _Skip,
//! # /// Parentheses.
//! # #[regex(r"[()]")]
//! # Paren(char),
//! # /// Atom.
//! # #[regex(r"[^\s()]+")]
//! # Atom(String),
//! # /// End-of-file.
//! # #[eof]
//! # Eof,
//! # }
//! type Token = laps::token::Token<TokenKind>;
//!
//! token_ast! {
//! macro Token<TokenKind> {
//! [atom] => { kind: TokenKind::Atom(_), prompt: "atom" },
//! [lpr] => { kind: TokenKind::Paren('(') },
//! [rpr] => { kind: TokenKind::Paren(')') },
//! [eof] => { kind: TokenKind::Eof },
//! }
//! }
//!
//! #[derive(Parse)]
//! #[token(Token)]
//! enum Statement {
//! Elem(Elem),
//! End(Token![eof]),
//! }
//!
//! #[derive(Parse)]
//! #[token(Token)]
//! struct SExp(Token![lpr], Vec<Elem>, Token![rpr]);
//!
//! #[derive(Parse)]
//! #[token(Token)]
//! enum Elem {
//! Atom(Token![atom]),
//! SExp(SExp),
//! }
//! ```
//!
//! The above implementation is very close in form to the corresponding
//! EBNF representation of the S-expression:
//!
//! ```text
//! Statement ::= Elem | EOF;
//! SExp ::= "(" {Elem} ")";
//! Elem ::= ATOM | SExp;
//! ```
//!
//! # More Examples
//!
//! See the
//! [`examples` directory](https://github.com/MaxXSoft/laps/tree/master/examples),
//! which contains the following examples:
//!
//! * [`sexp`](https://github.com/MaxXSoft/laps/tree/master/examples/sexp):
//! a [S-expression](https://en.wikipedia.org/wiki/S-expression) parser.
//! * [`calc`](https://github.com/MaxXSoft/laps/tree/master/examples/calc):
//! a simple expression calculator.
//! * [`json`](https://github.com/MaxXSoft/laps/tree/master/examples/json):
//! a simple JSON parser.
//! * [`clike`](https://github.com/MaxXSoft/laps/tree/master/examples/clike):
//! interpreter for a C-like programming language.
//!
//! # Accelerating Code Completion for IDEs
//!
//! By default, Cargo does not enable optimizations for procedural macros,
//! which may result in slower code completion if you are using `laps` to
//! generate lexers. To avoid this, you can add the following configuration
//! to `Cargo.toml`:
//!
//! ```toml
//! [profile.dev.build-override]
//! opt-level = 3
//! ```
//!
//! You can also try to manually enable/disable parallelization for lexer
//! generation by adding:
//!
//! # fn main() {}
//! # use laps::prelude::*;
//! #[derive(Tokenize)]
//! #[enable_par(true)] // or #[enable_par(false)]
//! enum TokenKind {
//! // ...
//! # #[regex(r"[^\s()]+")]
//! # Atom(String),
//! # #[eof]
//! # Eof,
//! }
//! ```
/// A prelude of some common traits and macros (if enabled feature `macros`)
/// in [`laps`](crate).
///
/// ```
/// use laps::prelude::*;
/// ```