diff options
author | Pavan Kumar Sunkara <[email protected]> | 2020-08-13 01:57:26 +0100 |
---|---|---|
committer | Pavan Kumar Sunkara <[email protected]> | 2020-08-13 02:18:19 +0100 |
commit | 349e6c62ada1fa45a8b80edb877b5e7c9d0c306d (patch) | |
tree | c08cb7cd48089c1c7316ff2570de02df76e9d9ef /crates/ra_proc_macro_srv/src/proc_macro/mod.rs | |
parent | f277ec27ac024992e8b8834c68e16b31c983be1b (diff) |
Rename ra_proc_macro_srv -> proc_macro_srv
Diffstat (limited to 'crates/ra_proc_macro_srv/src/proc_macro/mod.rs')
-rw-r--r-- | crates/ra_proc_macro_srv/src/proc_macro/mod.rs | 926 |
1 files changed, 0 insertions, 926 deletions
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/mod.rs b/crates/ra_proc_macro_srv/src/proc_macro/mod.rs deleted file mode 100644 index ee0dc9722..000000000 --- a/crates/ra_proc_macro_srv/src/proc_macro/mod.rs +++ /dev/null | |||
@@ -1,926 +0,0 @@ | |||
1 | //! lib-proc-macro main module | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/lib.rs | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | // NOTE(@edwin0cheng): | ||
7 | // Because we just copy the bridge module from rustc for ABI compatible | ||
8 | // There are some unused stuffs inside it. | ||
9 | // We suppress these warning here. | ||
10 | #[doc(hidden)] | ||
11 | #[allow(unused_macros)] | ||
12 | #[allow(unused_variables)] | ||
13 | pub mod bridge; | ||
14 | |||
15 | mod diagnostic; | ||
16 | |||
17 | pub use diagnostic::{Diagnostic, Level, MultiSpan}; | ||
18 | |||
19 | use std::ops::{Bound, RangeBounds}; | ||
20 | use std::path::PathBuf; | ||
21 | use std::str::FromStr; | ||
22 | use std::{fmt, iter, mem}; | ||
23 | |||
24 | /// The main type provided by this crate, representing an abstract stream of | ||
25 | /// tokens, or, more specifically, a sequence of token trees. | ||
26 | /// The type provide interfaces for iterating over those token trees and, conversely, | ||
27 | /// collecting a number of token trees into one stream. | ||
28 | /// | ||
29 | /// This is both the input and output of `#[proc_macro]`, `#[proc_macro_attribute]` | ||
30 | /// and `#[proc_macro_derive]` definitions. | ||
31 | #[derive(Clone)] | ||
32 | pub struct TokenStream(bridge::client::TokenStream); | ||
33 | |||
34 | /// Error returned from `TokenStream::from_str` | ||
35 | #[derive(Debug)] | ||
36 | pub struct LexError { | ||
37 | _inner: (), | ||
38 | } | ||
39 | |||
40 | impl TokenStream { | ||
41 | /// Returns an empty `TokenStream` containing no token trees. | ||
42 | pub fn new() -> TokenStream { | ||
43 | TokenStream(bridge::client::TokenStream::new()) | ||
44 | } | ||
45 | |||
46 | /// Checks if this `TokenStream` is empty. | ||
47 | pub fn is_empty(&self) -> bool { | ||
48 | self.0.is_empty() | ||
49 | } | ||
50 | } | ||
51 | |||
52 | /// Attempts to break the string into tokens and parse those tokens into a token stream. | ||
53 | /// May fail for a number of reasons, for example, if the string contains unbalanced delimiters | ||
54 | /// or characters not existing in the language. | ||
55 | /// All tokens in the parsed stream get `Span::call_site()` spans. | ||
56 | /// | ||
57 | /// NOTE: some errors may cause panics instead of returning `LexError`. We reserve the right to | ||
58 | /// change these errors into `LexError`s later. | ||
59 | impl FromStr for TokenStream { | ||
60 | type Err = LexError; | ||
61 | |||
62 | fn from_str(src: &str) -> Result<TokenStream, LexError> { | ||
63 | Ok(TokenStream(bridge::client::TokenStream::from_str(src))) | ||
64 | } | ||
65 | } | ||
66 | |||
67 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
68 | // based on it (the reverse of the usual relationship between the two). | ||
69 | // impl ToString for TokenStream { | ||
70 | // fn to_string(&self) -> String { | ||
71 | // self.0.to_string() | ||
72 | // } | ||
73 | // } | ||
74 | |||
75 | /// Prints the token stream as a string that is supposed to be losslessly convertible back | ||
76 | /// into the same token stream (modulo spans), except for possibly `TokenTree::Group`s | ||
77 | /// with `Delimiter::None` delimiters and negative numeric literals. | ||
78 | impl fmt::Display for TokenStream { | ||
79 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
80 | f.write_str(&self.to_string()) | ||
81 | } | ||
82 | } | ||
83 | |||
84 | /// Prints token in a form convenient for debugging. | ||
85 | impl fmt::Debug for TokenStream { | ||
86 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
87 | f.write_str("TokenStream ")?; | ||
88 | f.debug_list().entries(self.clone()).finish() | ||
89 | } | ||
90 | } | ||
91 | |||
92 | /// Creates a token stream containing a single token tree. | ||
93 | impl From<TokenTree> for TokenStream { | ||
94 | fn from(tree: TokenTree) -> TokenStream { | ||
95 | TokenStream(bridge::client::TokenStream::from_token_tree(match tree { | ||
96 | TokenTree::Group(tt) => bridge::TokenTree::Group(tt.0), | ||
97 | TokenTree::Punct(tt) => bridge::TokenTree::Punct(tt.0), | ||
98 | TokenTree::Ident(tt) => bridge::TokenTree::Ident(tt.0), | ||
99 | TokenTree::Literal(tt) => bridge::TokenTree::Literal(tt.0), | ||
100 | })) | ||
101 | } | ||
102 | } | ||
103 | |||
104 | /// Collects a number of token trees into a single stream. | ||
105 | impl iter::FromIterator<TokenTree> for TokenStream { | ||
106 | fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self { | ||
107 | trees.into_iter().map(TokenStream::from).collect() | ||
108 | } | ||
109 | } | ||
110 | |||
111 | /// A "flattening" operation on token streams, collects token trees | ||
112 | /// from multiple token streams into a single stream. | ||
113 | impl iter::FromIterator<TokenStream> for TokenStream { | ||
114 | fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self { | ||
115 | let mut builder = bridge::client::TokenStreamBuilder::new(); | ||
116 | streams.into_iter().for_each(|stream| builder.push(stream.0)); | ||
117 | TokenStream(builder.build()) | ||
118 | } | ||
119 | } | ||
120 | |||
121 | impl Extend<TokenTree> for TokenStream { | ||
122 | fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, trees: I) { | ||
123 | self.extend(trees.into_iter().map(TokenStream::from)); | ||
124 | } | ||
125 | } | ||
126 | |||
127 | impl Extend<TokenStream> for TokenStream { | ||
128 | fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) { | ||
129 | // FIXME(eddyb) Use an optimized implementation if/when possible. | ||
130 | *self = iter::once(mem::replace(self, Self::new())).chain(streams).collect(); | ||
131 | } | ||
132 | } | ||
133 | |||
134 | /// Public implementation details for the `TokenStream` type, such as iterators. | ||
135 | pub mod token_stream { | ||
136 | use crate::proc_macro::{bridge, Group, Ident, Literal, Punct, TokenStream, TokenTree}; | ||
137 | |||
138 | /// An iterator over `TokenStream`'s `TokenTree`s. | ||
139 | /// The iteration is "shallow", e.g., the iterator doesn't recurse into delimited groups, | ||
140 | /// and returns whole groups as token trees. | ||
141 | #[derive(Clone)] | ||
142 | pub struct IntoIter(bridge::client::TokenStreamIter); | ||
143 | |||
144 | impl Iterator for IntoIter { | ||
145 | type Item = TokenTree; | ||
146 | |||
147 | fn next(&mut self) -> Option<TokenTree> { | ||
148 | self.0.next().map(|tree| match tree { | ||
149 | bridge::TokenTree::Group(tt) => TokenTree::Group(Group(tt)), | ||
150 | bridge::TokenTree::Punct(tt) => TokenTree::Punct(Punct(tt)), | ||
151 | bridge::TokenTree::Ident(tt) => TokenTree::Ident(Ident(tt)), | ||
152 | bridge::TokenTree::Literal(tt) => TokenTree::Literal(Literal(tt)), | ||
153 | }) | ||
154 | } | ||
155 | } | ||
156 | |||
157 | impl IntoIterator for TokenStream { | ||
158 | type Item = TokenTree; | ||
159 | type IntoIter = IntoIter; | ||
160 | |||
161 | fn into_iter(self) -> IntoIter { | ||
162 | IntoIter(self.0.into_iter()) | ||
163 | } | ||
164 | } | ||
165 | } | ||
166 | |||
167 | /// A region of source code, along with macro expansion information. | ||
168 | #[derive(Copy, Clone)] | ||
169 | pub struct Span(bridge::client::Span); | ||
170 | |||
171 | macro_rules! diagnostic_method { | ||
172 | ($name:ident, $level:expr) => { | ||
173 | /// Creates a new `Diagnostic` with the given `message` at the span | ||
174 | /// `self`. | ||
175 | pub fn $name<T: Into<String>>(self, message: T) -> Diagnostic { | ||
176 | Diagnostic::spanned(self, $level, message) | ||
177 | } | ||
178 | }; | ||
179 | } | ||
180 | |||
181 | impl Span { | ||
182 | /// A span that resolves at the macro definition site. | ||
183 | pub fn def_site() -> Span { | ||
184 | Span(bridge::client::Span::def_site()) | ||
185 | } | ||
186 | |||
187 | /// The span of the invocation of the current procedural macro. | ||
188 | /// Identifiers created with this span will be resolved as if they were written | ||
189 | /// directly at the macro call location (call-site hygiene) and other code | ||
190 | /// at the macro call site will be able to refer to them as well. | ||
191 | pub fn call_site() -> Span { | ||
192 | Span(bridge::client::Span::call_site()) | ||
193 | } | ||
194 | |||
195 | /// A span that represents `macro_rules` hygiene, and sometimes resolves at the macro | ||
196 | /// definition site (local variables, labels, `$crate`) and sometimes at the macro | ||
197 | /// call site (everything else). | ||
198 | /// The span location is taken from the call-site. | ||
199 | pub fn mixed_site() -> Span { | ||
200 | Span(bridge::client::Span::mixed_site()) | ||
201 | } | ||
202 | |||
203 | /// The original source file into which this span points. | ||
204 | pub fn source_file(&self) -> SourceFile { | ||
205 | SourceFile(self.0.source_file()) | ||
206 | } | ||
207 | |||
208 | /// The `Span` for the tokens in the previous macro expansion from which | ||
209 | /// `self` was generated from, if any. | ||
210 | pub fn parent(&self) -> Option<Span> { | ||
211 | self.0.parent().map(Span) | ||
212 | } | ||
213 | |||
214 | /// The span for the origin source code that `self` was generated from. If | ||
215 | /// this `Span` wasn't generated from other macro expansions then the return | ||
216 | /// value is the same as `*self`. | ||
217 | pub fn source(&self) -> Span { | ||
218 | Span(self.0.source()) | ||
219 | } | ||
220 | |||
221 | /// Gets the starting line/column in the source file for this span. | ||
222 | pub fn start(&self) -> LineColumn { | ||
223 | self.0.start() | ||
224 | } | ||
225 | |||
226 | /// Gets the ending line/column in the source file for this span. | ||
227 | pub fn end(&self) -> LineColumn { | ||
228 | self.0.end() | ||
229 | } | ||
230 | |||
231 | /// Creates a new span encompassing `self` and `other`. | ||
232 | /// | ||
233 | /// Returns `None` if `self` and `other` are from different files. | ||
234 | pub fn join(&self, other: Span) -> Option<Span> { | ||
235 | self.0.join(other.0).map(Span) | ||
236 | } | ||
237 | |||
238 | /// Creates a new span with the same line/column information as `self` but | ||
239 | /// that resolves symbols as though it were at `other`. | ||
240 | pub fn resolved_at(&self, other: Span) -> Span { | ||
241 | Span(self.0.resolved_at(other.0)) | ||
242 | } | ||
243 | |||
244 | /// Creates a new span with the same name resolution behavior as `self` but | ||
245 | /// with the line/column information of `other`. | ||
246 | pub fn located_at(&self, other: Span) -> Span { | ||
247 | other.resolved_at(*self) | ||
248 | } | ||
249 | |||
250 | /// Compares to spans to see if they're equal. | ||
251 | pub fn eq(&self, other: &Span) -> bool { | ||
252 | self.0 == other.0 | ||
253 | } | ||
254 | |||
255 | /// Returns the source text behind a span. This preserves the original source | ||
256 | /// code, including spaces and comments. It only returns a result if the span | ||
257 | /// corresponds to real source code. | ||
258 | /// | ||
259 | /// Note: The observable result of a macro should only rely on the tokens and | ||
260 | /// not on this source text. The result of this function is a best effort to | ||
261 | /// be used for diagnostics only. | ||
262 | pub fn source_text(&self) -> Option<String> { | ||
263 | self.0.source_text() | ||
264 | } | ||
265 | |||
266 | diagnostic_method!(error, Level::Error); | ||
267 | diagnostic_method!(warning, Level::Warning); | ||
268 | diagnostic_method!(note, Level::Note); | ||
269 | diagnostic_method!(help, Level::Help); | ||
270 | } | ||
271 | |||
272 | /// Prints a span in a form convenient for debugging. | ||
273 | impl fmt::Debug for Span { | ||
274 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
275 | self.0.fmt(f) | ||
276 | } | ||
277 | } | ||
278 | |||
279 | /// A line-column pair representing the start or end of a `Span`. | ||
280 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
281 | pub struct LineColumn { | ||
282 | /// The 1-indexed line in the source file on which the span starts or ends (inclusive). | ||
283 | pub line: usize, | ||
284 | /// The 0-indexed column (in UTF-8 characters) in the source file on which | ||
285 | /// the span starts or ends (inclusive). | ||
286 | pub column: usize, | ||
287 | } | ||
288 | |||
289 | /// The source file of a given `Span`. | ||
290 | #[derive(Clone)] | ||
291 | pub struct SourceFile(bridge::client::SourceFile); | ||
292 | |||
293 | impl SourceFile { | ||
294 | /// Gets the path to this source file. | ||
295 | /// | ||
296 | /// ### Note | ||
297 | /// If the code span associated with this `SourceFile` was generated by an external macro, this | ||
298 | /// macro, this may not be an actual path on the filesystem. Use [`is_real`] to check. | ||
299 | /// | ||
300 | /// Also note that even if `is_real` returns `true`, if `--remap-path-prefix` was passed on | ||
301 | /// the command line, the path as given may not actually be valid. | ||
302 | /// | ||
303 | /// [`is_real`]: #method.is_real | ||
304 | pub fn path(&self) -> PathBuf { | ||
305 | PathBuf::from(self.0.path()) | ||
306 | } | ||
307 | |||
308 | /// Returns `true` if this source file is a real source file, and not generated by an external | ||
309 | /// macro's expansion. | ||
310 | pub fn is_real(&self) -> bool { | ||
311 | // This is a hack until intercrate spans are implemented and we can have real source files | ||
312 | // for spans generated in external macros. | ||
313 | // https://github.com/rust-lang/rust/pull/43604#issuecomment-333334368 | ||
314 | self.0.is_real() | ||
315 | } | ||
316 | } | ||
317 | |||
318 | impl fmt::Debug for SourceFile { | ||
319 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
320 | f.debug_struct("SourceFile") | ||
321 | .field("path", &self.path()) | ||
322 | .field("is_real", &self.is_real()) | ||
323 | .finish() | ||
324 | } | ||
325 | } | ||
326 | |||
327 | impl PartialEq for SourceFile { | ||
328 | fn eq(&self, other: &Self) -> bool { | ||
329 | self.0.eq(&other.0) | ||
330 | } | ||
331 | } | ||
332 | |||
333 | impl Eq for SourceFile {} | ||
334 | |||
335 | /// A single token or a delimited sequence of token trees (e.g., `[1, (), ..]`). | ||
336 | #[derive(Clone)] | ||
337 | pub enum TokenTree { | ||
338 | /// A token stream surrounded by bracket delimiters. | ||
339 | Group(Group), | ||
340 | /// An identifier. | ||
341 | Ident(Ident), | ||
342 | /// A single punctuation character (`+`, `,`, `$`, etc.). | ||
343 | Punct(Punct), | ||
344 | /// A literal character (`'a'`), string (`"hello"`), number (`2.3`), etc. | ||
345 | Literal(Literal), | ||
346 | } | ||
347 | |||
348 | impl TokenTree { | ||
349 | /// Returns the span of this tree, delegating to the `span` method of | ||
350 | /// the contained token or a delimited stream. | ||
351 | pub fn span(&self) -> Span { | ||
352 | match *self { | ||
353 | TokenTree::Group(ref t) => t.span(), | ||
354 | TokenTree::Ident(ref t) => t.span(), | ||
355 | TokenTree::Punct(ref t) => t.span(), | ||
356 | TokenTree::Literal(ref t) => t.span(), | ||
357 | } | ||
358 | } | ||
359 | |||
360 | /// Configures the span for *only this token*. | ||
361 | /// | ||
362 | /// Note that if this token is a `Group` then this method will not configure | ||
363 | /// the span of each of the internal tokens, this will simply delegate to | ||
364 | /// the `set_span` method of each variant. | ||
365 | pub fn set_span(&mut self, span: Span) { | ||
366 | match *self { | ||
367 | TokenTree::Group(ref mut t) => t.set_span(span), | ||
368 | TokenTree::Ident(ref mut t) => t.set_span(span), | ||
369 | TokenTree::Punct(ref mut t) => t.set_span(span), | ||
370 | TokenTree::Literal(ref mut t) => t.set_span(span), | ||
371 | } | ||
372 | } | ||
373 | } | ||
374 | |||
375 | /// Prints token tree in a form convenient for debugging. | ||
376 | impl fmt::Debug for TokenTree { | ||
377 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
378 | // Each of these has the name in the struct type in the derived debug, | ||
379 | // so don't bother with an extra layer of indirection | ||
380 | match *self { | ||
381 | TokenTree::Group(ref tt) => tt.fmt(f), | ||
382 | TokenTree::Ident(ref tt) => tt.fmt(f), | ||
383 | TokenTree::Punct(ref tt) => tt.fmt(f), | ||
384 | TokenTree::Literal(ref tt) => tt.fmt(f), | ||
385 | } | ||
386 | } | ||
387 | } | ||
388 | |||
389 | impl From<Group> for TokenTree { | ||
390 | fn from(g: Group) -> TokenTree { | ||
391 | TokenTree::Group(g) | ||
392 | } | ||
393 | } | ||
394 | |||
395 | impl From<Ident> for TokenTree { | ||
396 | fn from(g: Ident) -> TokenTree { | ||
397 | TokenTree::Ident(g) | ||
398 | } | ||
399 | } | ||
400 | |||
401 | impl From<Punct> for TokenTree { | ||
402 | fn from(g: Punct) -> TokenTree { | ||
403 | TokenTree::Punct(g) | ||
404 | } | ||
405 | } | ||
406 | |||
407 | impl From<Literal> for TokenTree { | ||
408 | fn from(g: Literal) -> TokenTree { | ||
409 | TokenTree::Literal(g) | ||
410 | } | ||
411 | } | ||
412 | |||
413 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
414 | // based on it (the reverse of the usual relationship between the two). | ||
415 | // impl ToString for TokenTree { | ||
416 | // fn to_string(&self) -> String { | ||
417 | // match *self { | ||
418 | // TokenTree::Group(ref t) => t.to_string(), | ||
419 | // TokenTree::Ident(ref t) => t.to_string(), | ||
420 | // TokenTree::Punct(ref t) => t.to_string(), | ||
421 | // TokenTree::Literal(ref t) => t.to_string(), | ||
422 | // } | ||
423 | // } | ||
424 | // } | ||
425 | |||
426 | /// Prints the token tree as a string that is supposed to be losslessly convertible back | ||
427 | /// into the same token tree (modulo spans), except for possibly `TokenTree::Group`s | ||
428 | /// with `Delimiter::None` delimiters and negative numeric literals. | ||
429 | impl fmt::Display for TokenTree { | ||
430 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
431 | f.write_str(&self.to_string()) | ||
432 | } | ||
433 | } | ||
434 | |||
435 | /// A delimited token stream. | ||
436 | /// | ||
437 | /// A `Group` internally contains a `TokenStream` which is surrounded by `Delimiter`s. | ||
438 | #[derive(Clone)] | ||
439 | pub struct Group(bridge::client::Group); | ||
440 | |||
441 | /// Describes how a sequence of token trees is delimited. | ||
442 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
443 | pub enum Delimiter { | ||
444 | /// `( ... )` | ||
445 | Parenthesis, | ||
446 | /// `{ ... }` | ||
447 | Brace, | ||
448 | /// `[ ... ]` | ||
449 | Bracket, | ||
450 | /// `Ø ... Ø` | ||
451 | /// An implicit delimiter, that may, for example, appear around tokens coming from a | ||
452 | /// "macro variable" `$var`. It is important to preserve operator priorities in cases like | ||
453 | /// `$var * 3` where `$var` is `1 + 2`. | ||
454 | /// Implicit delimiters may not survive roundtrip of a token stream through a string. | ||
455 | None, | ||
456 | } | ||
457 | |||
458 | impl Group { | ||
459 | /// Creates a new `Group` with the given delimiter and token stream. | ||
460 | /// | ||
461 | /// This constructor will set the span for this group to | ||
462 | /// `Span::call_site()`. To change the span you can use the `set_span` | ||
463 | /// method below. | ||
464 | pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group { | ||
465 | Group(bridge::client::Group::new(delimiter, stream.0)) | ||
466 | } | ||
467 | |||
468 | /// Returns the delimiter of this `Group` | ||
469 | pub fn delimiter(&self) -> Delimiter { | ||
470 | self.0.delimiter() | ||
471 | } | ||
472 | |||
473 | /// Returns the `TokenStream` of tokens that are delimited in this `Group`. | ||
474 | /// | ||
475 | /// Note that the returned token stream does not include the delimiter | ||
476 | /// returned above. | ||
477 | pub fn stream(&self) -> TokenStream { | ||
478 | TokenStream(self.0.stream()) | ||
479 | } | ||
480 | |||
481 | /// Returns the span for the delimiters of this token stream, spanning the | ||
482 | /// entire `Group`. | ||
483 | /// | ||
484 | /// ```text | ||
485 | /// pub fn span(&self) -> Span { | ||
486 | /// ^^^^^^^ | ||
487 | /// ``` | ||
488 | pub fn span(&self) -> Span { | ||
489 | Span(self.0.span()) | ||
490 | } | ||
491 | |||
492 | /// Returns the span pointing to the opening delimiter of this group. | ||
493 | /// | ||
494 | /// ```text | ||
495 | /// pub fn span_open(&self) -> Span { | ||
496 | /// ^ | ||
497 | /// ``` | ||
498 | pub fn span_open(&self) -> Span { | ||
499 | Span(self.0.span_open()) | ||
500 | } | ||
501 | |||
502 | /// Returns the span pointing to the closing delimiter of this group. | ||
503 | /// | ||
504 | /// ```text | ||
505 | /// pub fn span_close(&self) -> Span { | ||
506 | /// ^ | ||
507 | /// ``` | ||
508 | pub fn span_close(&self) -> Span { | ||
509 | Span(self.0.span_close()) | ||
510 | } | ||
511 | |||
512 | /// Configures the span for this `Group`'s delimiters, but not its internal | ||
513 | /// tokens. | ||
514 | /// | ||
515 | /// This method will **not** set the span of all the internal tokens spanned | ||
516 | /// by this group, but rather it will only set the span of the delimiter | ||
517 | /// tokens at the level of the `Group`. | ||
518 | pub fn set_span(&mut self, span: Span) { | ||
519 | self.0.set_span(span.0); | ||
520 | } | ||
521 | } | ||
522 | |||
523 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
524 | // based on it (the reverse of the usual relationship between the two). | ||
525 | // impl ToString for Group { | ||
526 | // fn to_string(&self) -> String { | ||
527 | // TokenStream::from(TokenTree::from(self.clone())).to_string() | ||
528 | // } | ||
529 | // } | ||
530 | |||
531 | /// Prints the group as a string that should be losslessly convertible back | ||
532 | /// into the same group (modulo spans), except for possibly `TokenTree::Group`s | ||
533 | /// with `Delimiter::None` delimiters. | ||
534 | impl fmt::Display for Group { | ||
535 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
536 | f.write_str(&self.to_string()) | ||
537 | } | ||
538 | } | ||
539 | |||
540 | impl fmt::Debug for Group { | ||
541 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
542 | f.debug_struct("Group") | ||
543 | .field("delimiter", &self.delimiter()) | ||
544 | .field("stream", &self.stream()) | ||
545 | .field("span", &self.span()) | ||
546 | .finish() | ||
547 | } | ||
548 | } | ||
549 | |||
550 | /// An `Punct` is an single punctuation character like `+`, `-` or `#`. | ||
551 | /// | ||
552 | /// Multi-character operators like `+=` are represented as two instances of `Punct` with different | ||
553 | /// forms of `Spacing` returned. | ||
554 | #[derive(Clone)] | ||
555 | pub struct Punct(bridge::client::Punct); | ||
556 | |||
557 | /// Whether an `Punct` is followed immediately by another `Punct` or | ||
558 | /// followed by another token or whitespace. | ||
559 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
560 | pub enum Spacing { | ||
561 | /// e.g., `+` is `Alone` in `+ =`, `+ident` or `+()`. | ||
562 | Alone, | ||
563 | /// e.g., `+` is `Joint` in `+=` or `'#`. | ||
564 | /// Additionally, single quote `'` can join with identifiers to form lifetimes `'ident`. | ||
565 | Joint, | ||
566 | } | ||
567 | |||
568 | impl Punct { | ||
569 | /// Creates a new `Punct` from the given character and spacing. | ||
570 | /// The `ch` argument must be a valid punctuation character permitted by the language, | ||
571 | /// otherwise the function will panic. | ||
572 | /// | ||
573 | /// The returned `Punct` will have the default span of `Span::call_site()` | ||
574 | /// which can be further configured with the `set_span` method below. | ||
575 | pub fn new(ch: char, spacing: Spacing) -> Punct { | ||
576 | Punct(bridge::client::Punct::new(ch, spacing)) | ||
577 | } | ||
578 | |||
579 | /// Returns the value of this punctuation character as `char`. | ||
580 | pub fn as_char(&self) -> char { | ||
581 | self.0.as_char() | ||
582 | } | ||
583 | |||
584 | /// Returns the spacing of this punctuation character, indicating whether it's immediately | ||
585 | /// followed by another `Punct` in the token stream, so they can potentially be combined into | ||
586 | /// a multi-character operator (`Joint`), or it's followed by some other token or whitespace | ||
587 | /// (`Alone`) so the operator has certainly ended. | ||
588 | pub fn spacing(&self) -> Spacing { | ||
589 | self.0.spacing() | ||
590 | } | ||
591 | |||
592 | /// Returns the span for this punctuation character. | ||
593 | pub fn span(&self) -> Span { | ||
594 | Span(self.0.span()) | ||
595 | } | ||
596 | |||
597 | /// Configure the span for this punctuation character. | ||
598 | pub fn set_span(&mut self, span: Span) { | ||
599 | self.0 = self.0.with_span(span.0); | ||
600 | } | ||
601 | } | ||
602 | |||
603 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
604 | // based on it (the reverse of the usual relationship between the two). | ||
605 | // impl ToString for Punct { | ||
606 | // fn to_string(&self) -> String { | ||
607 | // TokenStream::from(TokenTree::from(self.clone())).to_string() | ||
608 | // } | ||
609 | // } | ||
610 | |||
611 | /// Prints the punctuation character as a string that should be losslessly convertible | ||
612 | /// back into the same character. | ||
613 | impl fmt::Display for Punct { | ||
614 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
615 | f.write_str(&self.to_string()) | ||
616 | } | ||
617 | } | ||
618 | |||
619 | impl fmt::Debug for Punct { | ||
620 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
621 | f.debug_struct("Punct") | ||
622 | .field("ch", &self.as_char()) | ||
623 | .field("spacing", &self.spacing()) | ||
624 | .field("span", &self.span()) | ||
625 | .finish() | ||
626 | } | ||
627 | } | ||
628 | |||
629 | /// An identifier (`ident`). | ||
630 | #[derive(Clone, PartialEq, Eq, Hash)] | ||
631 | pub struct Ident(bridge::client::Ident); | ||
632 | |||
633 | impl Ident { | ||
634 | /// Creates a new `Ident` with the given `string` as well as the specified | ||
635 | /// `span`. | ||
636 | /// The `string` argument must be a valid identifier permitted by the | ||
637 | /// language, otherwise the function will panic. | ||
638 | /// | ||
639 | /// Note that `span`, currently in rustc, configures the hygiene information | ||
640 | /// for this identifier. | ||
641 | /// | ||
642 | /// As of this time `Span::call_site()` explicitly opts-in to "call-site" hygiene | ||
643 | /// meaning that identifiers created with this span will be resolved as if they were written | ||
644 | /// directly at the location of the macro call, and other code at the macro call site will be | ||
645 | /// able to refer to them as well. | ||
646 | /// | ||
647 | /// Later spans like `Span::def_site()` will allow to opt-in to "definition-site" hygiene | ||
648 | /// meaning that identifiers created with this span will be resolved at the location of the | ||
649 | /// macro definition and other code at the macro call site will not be able to refer to them. | ||
650 | /// | ||
651 | /// Due to the current importance of hygiene this constructor, unlike other | ||
652 | /// tokens, requires a `Span` to be specified at construction. | ||
653 | pub fn new(string: &str, span: Span) -> Ident { | ||
654 | Ident(bridge::client::Ident::new(string, span.0, false)) | ||
655 | } | ||
656 | |||
657 | /// Same as `Ident::new`, but creates a raw identifier (`r#ident`). | ||
658 | pub fn new_raw(string: &str, span: Span) -> Ident { | ||
659 | Ident(bridge::client::Ident::new(string, span.0, true)) | ||
660 | } | ||
661 | |||
662 | /// Returns the span of this `Ident`, encompassing the entire string returned | ||
663 | /// by `as_str`. | ||
664 | pub fn span(&self) -> Span { | ||
665 | Span(self.0.span()) | ||
666 | } | ||
667 | |||
668 | /// Configures the span of this `Ident`, possibly changing its hygiene context. | ||
669 | pub fn set_span(&mut self, span: Span) { | ||
670 | self.0 = self.0.with_span(span.0); | ||
671 | } | ||
672 | } | ||
673 | |||
674 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
675 | // based on it (the reverse of the usual relationship between the two). | ||
676 | // impl ToString for Ident { | ||
677 | // fn to_string(&self) -> String { | ||
678 | // TokenStream::from(TokenTree::from(self.clone())).to_string() | ||
679 | // } | ||
680 | // } | ||
681 | |||
682 | /// Prints the identifier as a string that should be losslessly convertible | ||
683 | /// back into the same identifier. | ||
684 | impl fmt::Display for Ident { | ||
685 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
686 | f.write_str(&self.to_string()) | ||
687 | } | ||
688 | } | ||
689 | |||
690 | impl fmt::Debug for Ident { | ||
691 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
692 | f.debug_struct("Ident") | ||
693 | .field("ident", &self.to_string()) | ||
694 | .field("span", &self.span()) | ||
695 | .finish() | ||
696 | } | ||
697 | } | ||
698 | |||
699 | /// A literal string (`"hello"`), byte string (`b"hello"`), | ||
700 | /// character (`'a'`), byte character (`b'a'`), an integer or floating point number | ||
701 | /// with or without a suffix (`1`, `1u8`, `2.3`, `2.3f32`). | ||
702 | /// Boolean literals like `true` and `false` do not belong here, they are `Ident`s. | ||
703 | #[derive(Clone)] | ||
704 | pub struct Literal(bridge::client::Literal); | ||
705 | |||
706 | macro_rules! suffixed_int_literals { | ||
707 | ($($name:ident => $kind:ident,)*) => ($( | ||
708 | /// Creates a new suffixed integer literal with the specified value. | ||
709 | /// | ||
710 | /// This function will create an integer like `1u32` where the integer | ||
711 | /// value specified is the first part of the token and the integral is | ||
712 | /// also suffixed at the end. | ||
713 | /// Literals created from negative numbers may not survive round-trips through | ||
714 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
715 | /// | ||
716 | /// Literals created through this method have the `Span::call_site()` | ||
717 | /// span by default, which can be configured with the `set_span` method | ||
718 | /// below. | ||
719 | pub fn $name(n: $kind) -> Literal { | ||
720 | Literal(bridge::client::Literal::typed_integer(&n.to_string(), stringify!($kind))) | ||
721 | } | ||
722 | )*) | ||
723 | } | ||
724 | |||
725 | macro_rules! unsuffixed_int_literals { | ||
726 | ($($name:ident => $kind:ident,)*) => ($( | ||
727 | /// Creates a new unsuffixed integer literal with the specified value. | ||
728 | /// | ||
729 | /// This function will create an integer like `1` where the integer | ||
730 | /// value specified is the first part of the token. No suffix is | ||
731 | /// specified on this token, meaning that invocations like | ||
732 | /// `Literal::i8_unsuffixed(1)` are equivalent to | ||
733 | /// `Literal::u32_unsuffixed(1)`. | ||
734 | /// Literals created from negative numbers may not survive rountrips through | ||
735 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
736 | /// | ||
737 | /// Literals created through this method have the `Span::call_site()` | ||
738 | /// span by default, which can be configured with the `set_span` method | ||
739 | /// below. | ||
740 | pub fn $name(n: $kind) -> Literal { | ||
741 | Literal(bridge::client::Literal::integer(&n.to_string())) | ||
742 | } | ||
743 | )*) | ||
744 | } | ||
745 | |||
746 | impl Literal { | ||
747 | suffixed_int_literals! { | ||
748 | u8_suffixed => u8, | ||
749 | u16_suffixed => u16, | ||
750 | u32_suffixed => u32, | ||
751 | u64_suffixed => u64, | ||
752 | u128_suffixed => u128, | ||
753 | usize_suffixed => usize, | ||
754 | i8_suffixed => i8, | ||
755 | i16_suffixed => i16, | ||
756 | i32_suffixed => i32, | ||
757 | i64_suffixed => i64, | ||
758 | i128_suffixed => i128, | ||
759 | isize_suffixed => isize, | ||
760 | } | ||
761 | |||
762 | unsuffixed_int_literals! { | ||
763 | u8_unsuffixed => u8, | ||
764 | u16_unsuffixed => u16, | ||
765 | u32_unsuffixed => u32, | ||
766 | u64_unsuffixed => u64, | ||
767 | u128_unsuffixed => u128, | ||
768 | usize_unsuffixed => usize, | ||
769 | i8_unsuffixed => i8, | ||
770 | i16_unsuffixed => i16, | ||
771 | i32_unsuffixed => i32, | ||
772 | i64_unsuffixed => i64, | ||
773 | i128_unsuffixed => i128, | ||
774 | isize_unsuffixed => isize, | ||
775 | } | ||
776 | |||
777 | /// Creates a new unsuffixed floating-point literal. | ||
778 | /// | ||
779 | /// This constructor is similar to those like `Literal::i8_unsuffixed` where | ||
780 | /// the float's value is emitted directly into the token but no suffix is | ||
781 | /// used, so it may be inferred to be a `f64` later in the compiler. | ||
782 | /// Literals created from negative numbers may not survive rountrips through | ||
783 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
784 | /// | ||
785 | /// # Panics | ||
786 | /// | ||
787 | /// This function requires that the specified float is finite, for | ||
788 | /// example if it is infinity or NaN this function will panic. | ||
789 | pub fn f32_unsuffixed(n: f32) -> Literal { | ||
790 | if !n.is_finite() { | ||
791 | panic!("Invalid float literal {}", n); | ||
792 | } | ||
793 | Literal(bridge::client::Literal::float(&n.to_string())) | ||
794 | } | ||
795 | |||
796 | /// Creates a new suffixed floating-point literal. | ||
797 | /// | ||
798 | /// This constructor will create a literal like `1.0f32` where the value | ||
799 | /// specified is the preceding part of the token and `f32` is the suffix of | ||
800 | /// the token. This token will always be inferred to be an `f32` in the | ||
801 | /// compiler. | ||
802 | /// Literals created from negative numbers may not survive rountrips through | ||
803 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
804 | /// | ||
805 | /// # Panics | ||
806 | /// | ||
807 | /// This function requires that the specified float is finite, for | ||
808 | /// example if it is infinity or NaN this function will panic. | ||
809 | pub fn f32_suffixed(n: f32) -> Literal { | ||
810 | if !n.is_finite() { | ||
811 | panic!("Invalid float literal {}", n); | ||
812 | } | ||
813 | Literal(bridge::client::Literal::f32(&n.to_string())) | ||
814 | } | ||
815 | |||
816 | /// Creates a new unsuffixed floating-point literal. | ||
817 | /// | ||
818 | /// This constructor is similar to those like `Literal::i8_unsuffixed` where | ||
819 | /// the float's value is emitted directly into the token but no suffix is | ||
820 | /// used, so it may be inferred to be a `f64` later in the compiler. | ||
821 | /// Literals created from negative numbers may not survive rountrips through | ||
822 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
823 | /// | ||
824 | /// # Panics | ||
825 | /// | ||
826 | /// This function requires that the specified float is finite, for | ||
827 | /// example if it is infinity or NaN this function will panic. | ||
828 | pub fn f64_unsuffixed(n: f64) -> Literal { | ||
829 | if !n.is_finite() { | ||
830 | panic!("Invalid float literal {}", n); | ||
831 | } | ||
832 | Literal(bridge::client::Literal::float(&n.to_string())) | ||
833 | } | ||
834 | |||
835 | /// Creates a new suffixed floating-point literal. | ||
836 | /// | ||
837 | /// This constructor will create a literal like `1.0f64` where the value | ||
838 | /// specified is the preceding part of the token and `f64` is the suffix of | ||
839 | /// the token. This token will always be inferred to be an `f64` in the | ||
840 | /// compiler. | ||
841 | /// Literals created from negative numbers may not survive rountrips through | ||
842 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
843 | /// | ||
844 | /// # Panics | ||
845 | /// | ||
846 | /// This function requires that the specified float is finite, for | ||
847 | /// example if it is infinity or NaN this function will panic. | ||
848 | pub fn f64_suffixed(n: f64) -> Literal { | ||
849 | if !n.is_finite() { | ||
850 | panic!("Invalid float literal {}", n); | ||
851 | } | ||
852 | Literal(bridge::client::Literal::f64(&n.to_string())) | ||
853 | } | ||
854 | |||
855 | /// String literal. | ||
856 | pub fn string(string: &str) -> Literal { | ||
857 | Literal(bridge::client::Literal::string(string)) | ||
858 | } | ||
859 | |||
860 | /// Character literal. | ||
861 | pub fn character(ch: char) -> Literal { | ||
862 | Literal(bridge::client::Literal::character(ch)) | ||
863 | } | ||
864 | |||
865 | /// Byte string literal. | ||
866 | pub fn byte_string(bytes: &[u8]) -> Literal { | ||
867 | Literal(bridge::client::Literal::byte_string(bytes)) | ||
868 | } | ||
869 | |||
870 | /// Returns the span encompassing this literal. | ||
871 | pub fn span(&self) -> Span { | ||
872 | Span(self.0.span()) | ||
873 | } | ||
874 | |||
875 | /// Configures the span associated for this literal. | ||
876 | pub fn set_span(&mut self, span: Span) { | ||
877 | self.0.set_span(span.0); | ||
878 | } | ||
879 | |||
880 | /// Returns a `Span` that is a subset of `self.span()` containing only the | ||
881 | /// source bytes in range `range`. Returns `None` if the would-be trimmed | ||
882 | /// span is outside the bounds of `self`. | ||
883 | // FIXME(SergioBenitez): check that the byte range starts and ends at a | ||
884 | // UTF-8 boundary of the source. otherwise, it's likely that a panic will | ||
885 | // occur elsewhere when the source text is printed. | ||
886 | // FIXME(SergioBenitez): there is no way for the user to know what | ||
887 | // `self.span()` actually maps to, so this method can currently only be | ||
888 | // called blindly. For example, `to_string()` for the character 'c' returns | ||
889 | // "'\u{63}'"; there is no way for the user to know whether the source text | ||
890 | // was 'c' or whether it was '\u{63}'. | ||
891 | pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> { | ||
892 | // HACK(eddyb) something akin to `Option::cloned`, but for `Bound<&T>`. | ||
893 | fn cloned_bound<T: Clone>(bound: Bound<&T>) -> Bound<T> { | ||
894 | match bound { | ||
895 | Bound::Included(x) => Bound::Included(x.clone()), | ||
896 | Bound::Excluded(x) => Bound::Excluded(x.clone()), | ||
897 | Bound::Unbounded => Bound::Unbounded, | ||
898 | } | ||
899 | } | ||
900 | |||
901 | self.0.subspan(cloned_bound(range.start_bound()), cloned_bound(range.end_bound())).map(Span) | ||
902 | } | ||
903 | } | ||
904 | |||
905 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
906 | // based on it (the reverse of the usual relationship between the two). | ||
907 | // impl ToString for Literal { | ||
908 | // fn to_string(&self) -> String { | ||
909 | // TokenStream::from(TokenTree::from(self.clone())).to_string() | ||
910 | // } | ||
911 | // } | ||
912 | |||
913 | /// Prints the literal as a string that should be losslessly convertible | ||
914 | /// back into the same literal (except for possible rounding for floating point literals). | ||
915 | impl fmt::Display for Literal { | ||
916 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
917 | f.write_str(&self.to_string()) | ||
918 | } | ||
919 | } | ||
920 | |||
921 | impl fmt::Debug for Literal { | ||
922 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
923 | // FIXME(eddyb) `Literal` should not expose internal `Debug` impls. | ||
924 | self.0.fmt(f) | ||
925 | } | ||
926 | } | ||