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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Needed for the roto macros
extern crate self as roto;
pub
pub use cli;
pub use ;
pub use crateList;
pub use ;
pub use ;
pub use RotoError;
pub use ;
pub use ;
pub use ;
pub use ;
/// Create a list of items to be registered.
///
/// This macro evaluates to a [`Library`], which can be passed to
/// [`Runtime::from_lib`] and [`Runtime::add`] to be registered.
///
/// This macro automates some of the boilerplate around registering items, such
/// as adding documentation comments and extracting the names and parameter
/// names of functions. Within functions, all regular Rust syntax is accepted.
///
/// Documentation comments can be added to any of the items. These will be stored and
/// added to the documentation generated by Roto.
///
/// # Usage
///
/// ```
/// use roto::{library, Runtime};
///
/// let lib = library! {
/// /// Double the argument
/// fn double(x: i32) -> i32 {
/// 2 * x
/// }
/// };
///
/// let rt = Runtime::from_lib(lib).unwrap();
/// // or
/// # let lib = library! {
/// # /// Double the argument
/// # fn double(x: i32) -> i32 {
/// # 2 * x
/// # }
/// # };
/// let mut rt = Runtime::new();
/// rt.add(lib).unwrap();
/// ```
///
/// # Syntax
///
/// ## Types
///
/// Types are declared with a `type` alias style syntax. The declaration must
/// be annotated with either `#[clone]` or `#[copy]`. Only types implementing
/// `Copy` can be marked with `#[copy]`. Since the Rust type (the right-hand
/// side) must implement [`Value`], you should wrap any custom type in `Val`.
///
/// ```rust
/// # #[derive(Clone, Copy)]
/// # struct Foo;
/// # use roto::Val;
/// # roto::library! {
/// /// A `Clone` type `Val<Foo>` registered as `Foo`
/// #[clone] type Foo = Val<Foo>;
///
/// /// A `Copy` type `Val<Foo>` registered as `Foo`
/// #[copy] type Foo = Val<Foo>;
/// # };
/// ```
///
/// ## Functions
///
/// Both `fn`-style and closure-style syntax is supported for functions. As expected,
/// closures can use variables from their environment.
///
/// ```rust
/// # #[derive(Clone)]
/// # struct Foo;
/// # #[derive(Clone)]
/// # struct Bar;
/// # use roto::Val;
/// # roto::library! {
/// /// A function
/// fn foo(a: i32, b: Val<Foo>) -> Val<Bar> {
/// todo!()
/// }
///
/// /// A closure
/// let foo = |a: i32, b: Val<Foo>| -> Val<Bar> {
/// todo!()
/// };
///
/// /// A closure with `move`
/// let foo = move |a: i32, b: Val<Foo>| -> Val<Bar> {
/// todo!()
/// };
/// # };
/// ```
///
/// ## Constants
///
/// ```rust
/// # #[derive(Clone)]
/// # struct Foo(i32);
/// # use roto::Val;
/// # roto::library! {
/// /// A constant `BAR` of type `u32`
/// const BAR: u32 = 42;
///
/// /// A constant `BAZ` of a custom type `Val<Foo>`
/// const BAZ: Val<Foo> = Val(Foo(42));
/// # };
/// ```
///
/// ## Modules
///
/// ```rust
/// # roto::library! {
/// /// Make a new module with some items
/// mod foo {
/// // Add items here (with the same syntax)
/// }
/// # };
/// ```
///
/// ## Impl blocks
///
/// <div class="warning">
///
/// Using `self` is not supported yet, but all functions declared
/// in an `impl` block with the "self" type as first parameter will be
/// registered as method.
///
/// </div>
///
/// ```rust
/// # #[derive(Clone)]
/// # struct Foo;
/// # use roto::Val;
/// # roto::library! {
/// /// Add (static) methods to the `Val<Foo>`
/// ///
/// /// Note that you have to use the Rust type, not the Roto name.
/// impl Val<Foo> {
/// // Add items here (with the same syntax)
/// }
/// # };
/// ```
///
/// ## Use
///
/// Imports can be added to the runtime as well, this will make the item
/// available to Roto in 2 locations: the original location and the location
/// where this `use` is located. The path must consist of modules _Roto_ names
/// of types.
///
/// <div class="warning">
///
/// Renaming items (e.g. `as`) and wildcard imports (e.g. `*`) are
/// not supported yet.
///
/// </div>
///
/// ```rust
/// # roto::library! {
/// /// Add imports, note that this uses Roto names for types
/// use Option::{Some, None};
/// # };
/// ```
///
/// ## Including other library items
///
/// ```rust
/// # let some_registerable = roto::library!{};
/// # roto::library! {
/// /// Include some registerable, including libraries
/// include!(some_registerable);
/// # };
///
/// # let some_registerable = roto::library!{};
/// # roto::library! {
/// // This is especially useful in combination with `impl` and `mod`:
/// mod foo { include!(some_registerable); }
/// # };
/// ```
///
/// # Tips
///
/// * It's possible to use `#[doc = some_expression]` to add documentation.
/// * Note that throughout this macro, you should use the _Rust_ name of a
/// type, instead of the _Roto_ name. There are only 2 exceptions: while
/// defining the type and while specifying imports.
/// * The order in which the items are specified within this macro is not
/// significant. You can even register functions that use types that you
/// declare later in the macro.
/// * It's not necessary to use this macro if you prefer not to or if you
/// want to define a library in a more dynamic way. This macro expands to
/// relatively simple function calls, though the API is a bit more verbose.
/// For the types involved, see the [`Library`] type.
pub use library;
/// Items exported only for use in macros
pub const FIND_HELP: &str = "\n\
If you are seeing this error you have found a bug in the Roto compiler.\n\
Please open an issue at https://github.com/NLnetLabs/roto.";
/// Panic with an internal compiler error
///
/// Calling this macro instead of [`panic!`] signals a bug in the compiler
pub use ice;
/// A source file location
///
/// Generally constructed with the [`location!`] macro.
/// Rust source code location