1 //! A support library for macro authors when defining new macros.
2 //!
3 //! This library, provided by the standard distribution, provides the types
4 //! consumed in the interfaces of procedurally defined macro definitions such as
5 //! function-like macros `#[proc_macro]`, macro attributes `#[proc_macro_attribute]` and
6 //! custom derive attributes`#[proc_macro_derive]`.
7 //!
8 //! See [the book] for more.
9 //!
10 //! [the book]: ../book/ch19-06-macros.html#procedural-macros-for-generating-code-from-attributes
11
12 #[doc(hidden)]
13 pub mod bridge;
14
15 mod diagnostic;
16
17 pub use diagnostic::{Diagnostic, Level, MultiSpan};
18
19 use std::cmp::Ordering;
20 use std::ops::{Bound, RangeBounds};
21 use std::path::PathBuf;
22 use std::str::FromStr;
23 use std::{error, fmt, iter, mem};
24
25 /// Determines whether proc_macro has been made accessible to the currently
26 /// running program.
27 ///
28 /// The proc_macro crate is only intended for use inside the implementation of
29 /// procedural macros. All the functions in this crate panic if invoked from
30 /// outside of a procedural macro, such as from a build script or unit test or
31 /// ordinary Rust binary.
32 ///
33 /// With consideration for Rust libraries that are designed to support both
34 /// macro and non-macro use cases, `proc_macro::is_available()` provides a
35 /// non-panicking way to detect whether the infrastructure required to use the
36 /// API of proc_macro is presently available. Returns true if invoked from
37 /// inside of a procedural macro, false if invoked from any other binary.
is_available() -> bool38 pub fn is_available() -> bool {
39 bridge::Bridge::is_available()
40 }
41
42 /// The main type provided by this crate, representing an abstract stream of
43 /// tokens, or, more specifically, a sequence of token trees.
44 /// The type provide interfaces for iterating over those token trees and, conversely,
45 /// collecting a number of token trees into one stream.
46 ///
47 /// This is both the input and output of `#[proc_macro]`, `#[proc_macro_attribute]`
48 /// and `#[proc_macro_derive]` definitions.
49 #[derive(Clone)]
50 pub struct TokenStream(bridge::client::TokenStream);
51
52 /// Error returned from `TokenStream::from_str`.
53 #[non_exhaustive]
54 #[derive(Debug)]
55 pub struct LexError;
56
57 impl LexError {
new() -> Self58 fn new() -> Self {
59 LexError
60 }
61 }
62
63 impl fmt::Display for LexError {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 f.write_str("cannot parse string into token stream")
66 }
67 }
68
69 impl error::Error for LexError {}
70
71 impl TokenStream {
72 /// Returns an empty `TokenStream` containing no token trees.
new() -> TokenStream73 pub fn new() -> TokenStream {
74 TokenStream(bridge::client::TokenStream::new())
75 }
76
77 /// Checks if this `TokenStream` is empty.
is_empty(&self) -> bool78 pub fn is_empty(&self) -> bool {
79 self.0.is_empty()
80 }
81 }
82
83 /// Attempts to break the string into tokens and parse those tokens into a token stream.
84 /// May fail for a number of reasons, for example, if the string contains unbalanced delimiters
85 /// or characters not existing in the language.
86 /// All tokens in the parsed stream get `Span::call_site()` spans.
87 ///
88 /// NOTE: some errors may cause panics instead of returning `LexError`. We reserve the right to
89 /// change these errors into `LexError`s later.
90 impl FromStr for TokenStream {
91 type Err = LexError;
92
from_str(src: &str) -> Result<TokenStream, LexError>93 fn from_str(src: &str) -> Result<TokenStream, LexError> {
94 Ok(TokenStream(bridge::client::TokenStream::from_str(src)))
95 }
96 }
97
98 /// Prints the token stream as a string that is supposed to be losslessly convertible back
99 /// into the same token stream (modulo spans), except for possibly `TokenTree::Group`s
100 /// with `Delimiter::None` delimiters and negative numeric literals.
101 impl fmt::Display for TokenStream {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103 f.write_str(&self.to_string())
104 }
105 }
106
107 /// Prints token in a form convenient for debugging.
108 impl fmt::Debug for TokenStream {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110 f.write_str("TokenStream ")?;
111 f.debug_list().entries(self.clone()).finish()
112 }
113 }
114
115 impl Default for TokenStream {
default() -> Self116 fn default() -> Self {
117 TokenStream::new()
118 }
119 }
120
121 pub use quote::{quote, quote_span};
122
123 /// Creates a token stream containing a single token tree.
124 impl From<TokenTree> for TokenStream {
from(tree: TokenTree) -> TokenStream125 fn from(tree: TokenTree) -> TokenStream {
126 TokenStream(bridge::client::TokenStream::from_token_tree(match tree {
127 TokenTree::Group(tt) => bridge::TokenTree::Group(tt.0),
128 TokenTree::Punct(tt) => bridge::TokenTree::Punct(tt.0),
129 TokenTree::Ident(tt) => bridge::TokenTree::Ident(tt.0),
130 TokenTree::Literal(tt) => bridge::TokenTree::Literal(tt.0),
131 }))
132 }
133 }
134
135 /// Collects a number of token trees into a single stream.
136 impl iter::FromIterator<TokenTree> for TokenStream {
from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self137 fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
138 trees.into_iter().map(TokenStream::from).collect()
139 }
140 }
141
142 /// A "flattening" operation on token streams, collects token trees
143 /// from multiple token streams into a single stream.
144 impl iter::FromIterator<TokenStream> for TokenStream {
from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self145 fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
146 let mut builder = bridge::client::TokenStreamBuilder::new();
147 streams.into_iter().for_each(|stream| builder.push(stream.0));
148 TokenStream(builder.build())
149 }
150 }
151
152 impl Extend<TokenTree> for TokenStream {
extend<I: IntoIterator<Item = TokenTree>>(&mut self, trees: I)153 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, trees: I) {
154 self.extend(trees.into_iter().map(TokenStream::from));
155 }
156 }
157
158 impl Extend<TokenStream> for TokenStream {
extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I)159 fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
160 // FIXME(eddyb) Use an optimized implementation if/when possible.
161 *self = iter::once(mem::replace(self, Self::new())).chain(streams).collect();
162 }
163 }
164
165 /// Public implementation details for the `TokenStream` type, such as iterators.
166 pub mod token_stream {
167 use super::{bridge, Group, Ident, Literal, Punct, TokenStream, TokenTree};
168
169 /// An iterator over `TokenStream`'s `TokenTree`s.
170 /// The iteration is "shallow", e.g., the iterator doesn't recurse into delimited groups,
171 /// and returns whole groups as token trees.
172 #[derive(Clone)]
173 pub struct IntoIter(bridge::client::TokenStreamIter);
174
175 impl Iterator for IntoIter {
176 type Item = TokenTree;
177
next(&mut self) -> Option<TokenTree>178 fn next(&mut self) -> Option<TokenTree> {
179 self.0.next().map(|tree| match tree {
180 bridge::TokenTree::Group(tt) => TokenTree::Group(Group(tt)),
181 bridge::TokenTree::Punct(tt) => TokenTree::Punct(Punct(tt)),
182 bridge::TokenTree::Ident(tt) => TokenTree::Ident(Ident(tt)),
183 bridge::TokenTree::Literal(tt) => TokenTree::Literal(Literal(tt)),
184 })
185 }
186 }
187
188 impl IntoIterator for TokenStream {
189 type Item = TokenTree;
190 type IntoIter = IntoIter;
191
into_iter(self) -> IntoIter192 fn into_iter(self) -> IntoIter {
193 IntoIter(self.0.into_iter())
194 }
195 }
196 }
197
198 #[doc(hidden)]
199 mod quote;
200
201 /// A region of source code, along with macro expansion information.
202 #[derive(Copy, Clone)]
203 pub struct Span(bridge::client::Span);
204
205 macro_rules! diagnostic_method {
206 ($name:ident, $level:expr) => {
207 /// Creates a new `Diagnostic` with the given `message` at the span
208 /// `self`.
209 pub fn $name<T: Into<String>>(self, message: T) -> Diagnostic {
210 Diagnostic::spanned(self, $level, message)
211 }
212 };
213 }
214
215 impl Span {
216 /// A span that resolves at the macro definition site.
def_site() -> Span217 pub fn def_site() -> Span {
218 Span(bridge::client::Span::def_site())
219 }
220
221 /// The span of the invocation of the current procedural macro.
222 /// Identifiers created with this span will be resolved as if they were written
223 /// directly at the macro call location (call-site hygiene) and other code
224 /// at the macro call site will be able to refer to them as well.
call_site() -> Span225 pub fn call_site() -> Span {
226 Span(bridge::client::Span::call_site())
227 }
228
229 /// A span that represents `macro_rules` hygiene, and sometimes resolves at the macro
230 /// definition site (local variables, labels, `$crate`) and sometimes at the macro
231 /// call site (everything else).
232 /// The span location is taken from the call-site.
mixed_site() -> Span233 pub fn mixed_site() -> Span {
234 Span(bridge::client::Span::mixed_site())
235 }
236
237 /// The original source file into which this span points.
source_file(&self) -> SourceFile238 pub fn source_file(&self) -> SourceFile {
239 SourceFile(self.0.source_file())
240 }
241
242 /// The `Span` for the tokens in the previous macro expansion from which
243 /// `self` was generated from, if any.
parent(&self) -> Option<Span>244 pub fn parent(&self) -> Option<Span> {
245 self.0.parent().map(Span)
246 }
247
248 /// The span for the origin source code that `self` was generated from. If
249 /// this `Span` wasn't generated from other macro expansions then the return
250 /// value is the same as `*self`.
source(&self) -> Span251 pub fn source(&self) -> Span {
252 Span(self.0.source())
253 }
254
255 /// Gets the starting line/column in the source file for this span.
start(&self) -> LineColumn256 pub fn start(&self) -> LineColumn {
257 self.0.start().add_1_to_column()
258 }
259
260 /// Gets the ending line/column in the source file for this span.
end(&self) -> LineColumn261 pub fn end(&self) -> LineColumn {
262 self.0.end().add_1_to_column()
263 }
264
265 /// Creates a new span encompassing `self` and `other`.
266 ///
267 /// Returns `None` if `self` and `other` are from different files.
join(&self, other: Span) -> Option<Span>268 pub fn join(&self, other: Span) -> Option<Span> {
269 self.0.join(other.0).map(Span)
270 }
271
272 /// Creates a new span with the same line/column information as `self` but
273 /// that resolves symbols as though it were at `other`.
resolved_at(&self, other: Span) -> Span274 pub fn resolved_at(&self, other: Span) -> Span {
275 Span(self.0.resolved_at(other.0))
276 }
277
278 /// Creates a new span with the same name resolution behavior as `self` but
279 /// with the line/column information of `other`.
located_at(&self, other: Span) -> Span280 pub fn located_at(&self, other: Span) -> Span {
281 other.resolved_at(*self)
282 }
283
284 /// Compares to spans to see if they're equal.
eq(&self, other: &Span) -> bool285 pub fn eq(&self, other: &Span) -> bool {
286 self.0 == other.0
287 }
288
289 /// Returns the source text behind a span. This preserves the original source
290 /// code, including spaces and comments. It only returns a result if the span
291 /// corresponds to real source code.
292 ///
293 /// Note: The observable result of a macro should only rely on the tokens and
294 /// not on this source text. The result of this function is a best effort to
295 /// be used for diagnostics only.
source_text(&self) -> Option<String>296 pub fn source_text(&self) -> Option<String> {
297 self.0.source_text()
298 }
299
300 // Used by the implementation of `Span::quote`
301 #[doc(hidden)]
save_span(&self) -> usize302 pub fn save_span(&self) -> usize {
303 self.0.save_span()
304 }
305
306 // Used by the implementation of `Span::quote`
307 #[doc(hidden)]
recover_proc_macro_span(id: usize) -> Span308 pub fn recover_proc_macro_span(id: usize) -> Span {
309 Span(bridge::client::Span::recover_proc_macro_span(id))
310 }
311
312 diagnostic_method!(error, Level::Error);
313 diagnostic_method!(warning, Level::Warning);
314 diagnostic_method!(note, Level::Note);
315 diagnostic_method!(help, Level::Help);
316 }
317
318 /// Prints a span in a form convenient for debugging.
319 impl fmt::Debug for Span {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result320 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
321 self.0.fmt(f)
322 }
323 }
324
325 /// A line-column pair representing the start or end of a `Span`.
326 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
327 pub struct LineColumn {
328 /// The 1-indexed line in the source file on which the span starts or ends (inclusive).
329 pub line: usize,
330 /// The 1-indexed column (number of bytes in UTF-8 encoding) in the source
331 /// file on which the span starts or ends (inclusive).
332 pub column: usize,
333 }
334
335 impl LineColumn {
add_1_to_column(self) -> Self336 fn add_1_to_column(self) -> Self {
337 LineColumn { line: self.line, column: self.column + 1 }
338 }
339 }
340
341 impl Ord for LineColumn {
cmp(&self, other: &Self) -> Ordering342 fn cmp(&self, other: &Self) -> Ordering {
343 self.line.cmp(&other.line).then(self.column.cmp(&other.column))
344 }
345 }
346
347 impl PartialOrd for LineColumn {
partial_cmp(&self, other: &Self) -> Option<Ordering>348 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
349 Some(self.cmp(other))
350 }
351 }
352
353 /// The source file of a given `Span`.
354 #[derive(Clone)]
355 pub struct SourceFile(bridge::client::SourceFile);
356
357 impl SourceFile {
358 /// Gets the path to this source file.
359 ///
360 /// ### Note
361 /// If the code span associated with this `SourceFile` was generated by an external macro, this
362 /// macro, this might not be an actual path on the filesystem. Use [`is_real`] to check.
363 ///
364 /// Also note that even if `is_real` returns `true`, if `--remap-path-prefix` was passed on
365 /// the command line, the path as given might not actually be valid.
366 ///
367 /// [`is_real`]: Self::is_real
path(&self) -> PathBuf368 pub fn path(&self) -> PathBuf {
369 PathBuf::from(self.0.path())
370 }
371
372 /// Returns `true` if this source file is a real source file, and not generated by an external
373 /// macro's expansion.
is_real(&self) -> bool374 pub fn is_real(&self) -> bool {
375 // This is a hack until intercrate spans are implemented and we can have real source files
376 // for spans generated in external macros.
377 // https://github.com/rust-lang/rust/pull/43604#issuecomment-333334368
378 self.0.is_real()
379 }
380 }
381
382 impl fmt::Debug for SourceFile {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result383 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
384 f.debug_struct("SourceFile")
385 .field("path", &self.path())
386 .field("is_real", &self.is_real())
387 .finish()
388 }
389 }
390
391 impl PartialEq for SourceFile {
eq(&self, other: &Self) -> bool392 fn eq(&self, other: &Self) -> bool {
393 self.0.eq(&other.0)
394 }
395 }
396
397 impl Eq for SourceFile {}
398
399 /// A single token or a delimited sequence of token trees (e.g., `[1, (), ..]`).
400 #[derive(Clone)]
401 pub enum TokenTree {
402 /// A token stream surrounded by bracket delimiters.
403 Group(Group),
404 /// An identifier.
405 Ident(Ident),
406 /// A single punctuation character (`+`, `,`, `$`, etc.).
407 Punct(Punct),
408 /// A literal character (`'a'`), string (`"hello"`), number (`2.3`), etc.
409 Literal(Literal),
410 }
411
412 impl TokenTree {
413 /// Returns the span of this tree, delegating to the `span` method of
414 /// the contained token or a delimited stream.
span(&self) -> Span415 pub fn span(&self) -> Span {
416 match *self {
417 TokenTree::Group(ref t) => t.span(),
418 TokenTree::Ident(ref t) => t.span(),
419 TokenTree::Punct(ref t) => t.span(),
420 TokenTree::Literal(ref t) => t.span(),
421 }
422 }
423
424 /// Configures the span for *only this token*.
425 ///
426 /// Note that if this token is a `Group` then this method will not configure
427 /// the span of each of the internal tokens, this will simply delegate to
428 /// the `set_span` method of each variant.
set_span(&mut self, span: Span)429 pub fn set_span(&mut self, span: Span) {
430 match *self {
431 TokenTree::Group(ref mut t) => t.set_span(span),
432 TokenTree::Ident(ref mut t) => t.set_span(span),
433 TokenTree::Punct(ref mut t) => t.set_span(span),
434 TokenTree::Literal(ref mut t) => t.set_span(span),
435 }
436 }
437 }
438
439 /// Prints token tree in a form convenient for debugging.
440 impl fmt::Debug for TokenTree {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result441 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
442 // Each of these has the name in the struct type in the derived debug,
443 // so don't bother with an extra layer of indirection
444 match *self {
445 TokenTree::Group(ref tt) => tt.fmt(f),
446 TokenTree::Ident(ref tt) => tt.fmt(f),
447 TokenTree::Punct(ref tt) => tt.fmt(f),
448 TokenTree::Literal(ref tt) => tt.fmt(f),
449 }
450 }
451 }
452
453 impl From<Group> for TokenTree {
from(g: Group) -> TokenTree454 fn from(g: Group) -> TokenTree {
455 TokenTree::Group(g)
456 }
457 }
458
459 impl From<Ident> for TokenTree {
from(g: Ident) -> TokenTree460 fn from(g: Ident) -> TokenTree {
461 TokenTree::Ident(g)
462 }
463 }
464
465 impl From<Punct> for TokenTree {
from(g: Punct) -> TokenTree466 fn from(g: Punct) -> TokenTree {
467 TokenTree::Punct(g)
468 }
469 }
470
471 impl From<Literal> for TokenTree {
from(g: Literal) -> TokenTree472 fn from(g: Literal) -> TokenTree {
473 TokenTree::Literal(g)
474 }
475 }
476
477 /// Prints the token tree as a string that is supposed to be losslessly convertible back
478 /// into the same token tree (modulo spans), except for possibly `TokenTree::Group`s
479 /// with `Delimiter::None` delimiters and negative numeric literals.
480 impl fmt::Display for TokenTree {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result481 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
482 f.write_str(&self.to_string())
483 }
484 }
485
486 /// A delimited token stream.
487 ///
488 /// A `Group` internally contains a `TokenStream` which is surrounded by `Delimiter`s.
489 #[derive(Clone)]
490 pub struct Group(bridge::client::Group);
491
492 /// Describes how a sequence of token trees is delimited.
493 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
494 pub enum Delimiter {
495 /// `( ... )`
496 Parenthesis,
497 /// `{ ... }`
498 Brace,
499 /// `[ ... ]`
500 Bracket,
501 /// `Ø ... Ø`
502 /// An implicit delimiter, that may, for example, appear around tokens coming from a
503 /// "macro variable" `$var`. It is important to preserve operator priorities in cases like
504 /// `$var * 3` where `$var` is `1 + 2`.
505 /// Implicit delimiters might not survive roundtrip of a token stream through a string.
506 None,
507 }
508
509 impl Group {
510 /// Creates a new `Group` with the given delimiter and token stream.
511 ///
512 /// This constructor will set the span for this group to
513 /// `Span::call_site()`. To change the span you can use the `set_span`
514 /// method below.
new(delimiter: Delimiter, stream: TokenStream) -> Group515 pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
516 Group(bridge::client::Group::new(delimiter, stream.0))
517 }
518
519 /// Returns the delimiter of this `Group`
delimiter(&self) -> Delimiter520 pub fn delimiter(&self) -> Delimiter {
521 self.0.delimiter()
522 }
523
524 /// Returns the `TokenStream` of tokens that are delimited in this `Group`.
525 ///
526 /// Note that the returned token stream does not include the delimiter
527 /// returned above.
stream(&self) -> TokenStream528 pub fn stream(&self) -> TokenStream {
529 TokenStream(self.0.stream())
530 }
531
532 /// Returns the span for the delimiters of this token stream, spanning the
533 /// entire `Group`.
534 ///
535 /// ```text
536 /// pub fn span(&self) -> Span {
537 /// ^^^^^^^
538 /// ```
span(&self) -> Span539 pub fn span(&self) -> Span {
540 Span(self.0.span())
541 }
542
543 /// Returns the span pointing to the opening delimiter of this group.
544 ///
545 /// ```text
546 /// pub fn span_open(&self) -> Span {
547 /// ^
548 /// ```
span_open(&self) -> Span549 pub fn span_open(&self) -> Span {
550 Span(self.0.span_open())
551 }
552
553 /// Returns the span pointing to the closing delimiter of this group.
554 ///
555 /// ```text
556 /// pub fn span_close(&self) -> Span {
557 /// ^
558 /// ```
span_close(&self) -> Span559 pub fn span_close(&self) -> Span {
560 Span(self.0.span_close())
561 }
562
563 /// Configures the span for this `Group`'s delimiters, but not its internal
564 /// tokens.
565 ///
566 /// This method will **not** set the span of all the internal tokens spanned
567 /// by this group, but rather it will only set the span of the delimiter
568 /// tokens at the level of the `Group`.
set_span(&mut self, span: Span)569 pub fn set_span(&mut self, span: Span) {
570 self.0.set_span(span.0);
571 }
572 }
573
574 /// Prints the group as a string that should be losslessly convertible back
575 /// into the same group (modulo spans), except for possibly `TokenTree::Group`s
576 /// with `Delimiter::None` delimiters.
577 impl fmt::Display for Group {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result578 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
579 f.write_str(&self.to_string())
580 }
581 }
582
583 impl fmt::Debug for Group {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result584 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
585 f.debug_struct("Group")
586 .field("delimiter", &self.delimiter())
587 .field("stream", &self.stream())
588 .field("span", &self.span())
589 .finish()
590 }
591 }
592
593 /// A `Punct` is a single punctuation character such as `+`, `-` or `#`.
594 ///
595 /// Multi-character operators like `+=` are represented as two instances of `Punct` with different
596 /// forms of `Spacing` returned.
597 #[derive(Clone)]
598 pub struct Punct(bridge::client::Punct);
599
600 /// Describes whether a `Punct` is followed immediately by another `Punct` ([`Spacing::Joint`]) or
601 /// by a different token or whitespace ([`Spacing::Alone`]).
602 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
603 pub enum Spacing {
604 /// A `Punct` is not immediately followed by another `Punct`.
605 /// E.g. `+` is `Alone` in `+ =`, `+ident` and `+()`.
606 Alone,
607 /// A `Punct` is immediately followed by another `Punct`.
608 /// E.g. `+` is `Joint` in `+=` and `++`.
609 ///
610 /// Additionally, single quote `'` can join with identifiers to form lifetimes: `'ident`.
611 Joint,
612 }
613
614 impl Punct {
615 /// Creates a new `Punct` from the given character and spacing.
616 /// The `ch` argument must be a valid punctuation character permitted by the language,
617 /// otherwise the function will panic.
618 ///
619 /// The returned `Punct` will have the default span of `Span::call_site()`
620 /// which can be further configured with the `set_span` method below.
new(ch: char, spacing: Spacing) -> Punct621 pub fn new(ch: char, spacing: Spacing) -> Punct {
622 Punct(bridge::client::Punct::new(ch, spacing))
623 }
624
625 /// Returns the value of this punctuation character as `char`.
as_char(&self) -> char626 pub fn as_char(&self) -> char {
627 self.0.as_char()
628 }
629
630 /// Returns the spacing of this punctuation character, indicating whether it's immediately
631 /// followed by another `Punct` in the token stream, so they can potentially be combined into
632 /// a multi-character operator (`Joint`), or it's followed by some other token or whitespace
633 /// (`Alone`) so the operator has certainly ended.
spacing(&self) -> Spacing634 pub fn spacing(&self) -> Spacing {
635 self.0.spacing()
636 }
637
638 /// Returns the span for this punctuation character.
span(&self) -> Span639 pub fn span(&self) -> Span {
640 Span(self.0.span())
641 }
642
643 /// Configure the span for this punctuation character.
set_span(&mut self, span: Span)644 pub fn set_span(&mut self, span: Span) {
645 self.0 = self.0.with_span(span.0);
646 }
647 }
648
649 /// Prints the punctuation character as a string that should be losslessly convertible
650 /// back into the same character.
651 impl fmt::Display for Punct {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result652 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
653 f.write_str(&self.to_string())
654 }
655 }
656
657 impl fmt::Debug for Punct {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result658 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
659 f.debug_struct("Punct")
660 .field("ch", &self.as_char())
661 .field("spacing", &self.spacing())
662 .field("span", &self.span())
663 .finish()
664 }
665 }
666
667 impl PartialEq<char> for Punct {
eq(&self, rhs: &char) -> bool668 fn eq(&self, rhs: &char) -> bool {
669 self.as_char() == *rhs
670 }
671 }
672
673 impl PartialEq<Punct> for char {
eq(&self, rhs: &Punct) -> bool674 fn eq(&self, rhs: &Punct) -> bool {
675 *self == rhs.as_char()
676 }
677 }
678
679 /// An identifier (`ident`).
680 #[derive(Clone)]
681 pub struct Ident(bridge::client::Ident);
682
683 impl Ident {
684 /// Creates a new `Ident` with the given `string` as well as the specified
685 /// `span`.
686 /// The `string` argument must be a valid identifier permitted by the
687 /// language (including keywords, e.g. `self` or `fn`). Otherwise, the function will panic.
688 ///
689 /// Note that `span`, currently in rustc, configures the hygiene information
690 /// for this identifier.
691 ///
692 /// As of this time `Span::call_site()` explicitly opts-in to "call-site" hygiene
693 /// meaning that identifiers created with this span will be resolved as if they were written
694 /// directly at the location of the macro call, and other code at the macro call site will be
695 /// able to refer to them as well.
696 ///
697 /// Later spans like `Span::def_site()` will allow to opt-in to "definition-site" hygiene
698 /// meaning that identifiers created with this span will be resolved at the location of the
699 /// macro definition and other code at the macro call site will not be able to refer to them.
700 ///
701 /// Due to the current importance of hygiene this constructor, unlike other
702 /// tokens, requires a `Span` to be specified at construction.
new(string: &str, span: Span) -> Ident703 pub fn new(string: &str, span: Span) -> Ident {
704 Ident(bridge::client::Ident::new(string, span.0, false))
705 }
706
707 /// Same as `Ident::new`, but creates a raw identifier (`r#ident`).
708 /// The `string` argument be a valid identifier permitted by the language
709 /// (including keywords, e.g. `fn`). Keywords which are usable in path segments
710 /// (e.g. `self`, `super`) are not supported, and will cause a panic.
new_raw(string: &str, span: Span) -> Ident711 pub fn new_raw(string: &str, span: Span) -> Ident {
712 Ident(bridge::client::Ident::new(string, span.0, true))
713 }
714
715 /// Returns the span of this `Ident`, encompassing the entire string returned
716 /// by [`to_string`](Self::to_string).
span(&self) -> Span717 pub fn span(&self) -> Span {
718 Span(self.0.span())
719 }
720
721 /// Configures the span of this `Ident`, possibly changing its hygiene context.
set_span(&mut self, span: Span)722 pub fn set_span(&mut self, span: Span) {
723 self.0 = self.0.with_span(span.0);
724 }
725 }
726
727 /// Prints the identifier as a string that should be losslessly convertible
728 /// back into the same identifier.
729 impl fmt::Display for Ident {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result730 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
731 f.write_str(&self.to_string())
732 }
733 }
734
735 impl fmt::Debug for Ident {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result736 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
737 f.debug_struct("Ident")
738 .field("ident", &self.to_string())
739 .field("span", &self.span())
740 .finish()
741 }
742 }
743
744 /// A literal string (`"hello"`), byte string (`b"hello"`),
745 /// character (`'a'`), byte character (`b'a'`), an integer or floating point number
746 /// with or without a suffix (`1`, `1u8`, `2.3`, `2.3f32`).
747 /// Boolean literals like `true` and `false` do not belong here, they are `Ident`s.
748 #[derive(Clone)]
749 pub struct Literal(bridge::client::Literal);
750
751 macro_rules! suffixed_int_literals {
752 ($($name:ident => $kind:ident,)*) => ($(
753 /// Creates a new suffixed integer literal with the specified value.
754 ///
755 /// This function will create an integer like `1u32` where the integer
756 /// value specified is the first part of the token and the integral is
757 /// also suffixed at the end.
758 /// Literals created from negative numbers might not survive round-trips through
759 /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
760 ///
761 /// Literals created through this method have the `Span::call_site()`
762 /// span by default, which can be configured with the `set_span` method
763 /// below.
764 pub fn $name(n: $kind) -> Literal {
765 Literal(bridge::client::Literal::typed_integer(&n.to_string(), stringify!($kind)))
766 }
767 )*)
768 }
769
770 macro_rules! unsuffixed_int_literals {
771 ($($name:ident => $kind:ident,)*) => ($(
772 /// Creates a new unsuffixed integer literal with the specified value.
773 ///
774 /// This function will create an integer like `1` where the integer
775 /// value specified is the first part of the token. No suffix is
776 /// specified on this token, meaning that invocations like
777 /// `Literal::i8_unsuffixed(1)` are equivalent to
778 /// `Literal::u32_unsuffixed(1)`.
779 /// Literals created from negative numbers might not survive rountrips through
780 /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
781 ///
782 /// Literals created through this method have the `Span::call_site()`
783 /// span by default, which can be configured with the `set_span` method
784 /// below.
785 pub fn $name(n: $kind) -> Literal {
786 Literal(bridge::client::Literal::integer(&n.to_string()))
787 }
788 )*)
789 }
790
791 impl Literal {
792 suffixed_int_literals! {
793 u8_suffixed => u8,
794 u16_suffixed => u16,
795 u32_suffixed => u32,
796 u64_suffixed => u64,
797 u128_suffixed => u128,
798 usize_suffixed => usize,
799 i8_suffixed => i8,
800 i16_suffixed => i16,
801 i32_suffixed => i32,
802 i64_suffixed => i64,
803 i128_suffixed => i128,
804 isize_suffixed => isize,
805 }
806
807 unsuffixed_int_literals! {
808 u8_unsuffixed => u8,
809 u16_unsuffixed => u16,
810 u32_unsuffixed => u32,
811 u64_unsuffixed => u64,
812 u128_unsuffixed => u128,
813 usize_unsuffixed => usize,
814 i8_unsuffixed => i8,
815 i16_unsuffixed => i16,
816 i32_unsuffixed => i32,
817 i64_unsuffixed => i64,
818 i128_unsuffixed => i128,
819 isize_unsuffixed => isize,
820 }
821
822 /// Creates a new unsuffixed floating-point literal.
823 ///
824 /// This constructor is similar to those like `Literal::i8_unsuffixed` where
825 /// the float's value is emitted directly into the token but no suffix is
826 /// used, so it may be inferred to be a `f64` later in the compiler.
827 /// Literals created from negative numbers might not survive rountrips through
828 /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
829 ///
830 /// # Panics
831 ///
832 /// This function requires that the specified float is finite, for
833 /// example if it is infinity or NaN this function will panic.
f32_unsuffixed(n: f32) -> Literal834 pub fn f32_unsuffixed(n: f32) -> Literal {
835 if !n.is_finite() {
836 panic!("Invalid float literal {}", n);
837 }
838 Literal(bridge::client::Literal::float(&n.to_string()))
839 }
840
841 /// Creates a new suffixed floating-point literal.
842 ///
843 /// This constructor will create a literal like `1.0f32` where the value
844 /// specified is the preceding part of the token and `f32` is the suffix of
845 /// the token. This token will always be inferred to be an `f32` in the
846 /// compiler.
847 /// Literals created from negative numbers might not survive rountrips through
848 /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
849 ///
850 /// # Panics
851 ///
852 /// This function requires that the specified float is finite, for
853 /// example if it is infinity or NaN this function will panic.
f32_suffixed(n: f32) -> Literal854 pub fn f32_suffixed(n: f32) -> Literal {
855 if !n.is_finite() {
856 panic!("Invalid float literal {}", n);
857 }
858 Literal(bridge::client::Literal::f32(&n.to_string()))
859 }
860
861 /// Creates a new unsuffixed floating-point literal.
862 ///
863 /// This constructor is similar to those like `Literal::i8_unsuffixed` where
864 /// the float's value is emitted directly into the token but no suffix is
865 /// used, so it may be inferred to be a `f64` later in the compiler.
866 /// Literals created from negative numbers might not survive rountrips through
867 /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
868 ///
869 /// # Panics
870 ///
871 /// This function requires that the specified float is finite, for
872 /// example if it is infinity or NaN this function will panic.
f64_unsuffixed(n: f64) -> Literal873 pub fn f64_unsuffixed(n: f64) -> Literal {
874 if !n.is_finite() {
875 panic!("Invalid float literal {}", n);
876 }
877 Literal(bridge::client::Literal::float(&n.to_string()))
878 }
879
880 /// Creates a new suffixed floating-point literal.
881 ///
882 /// This constructor will create a literal like `1.0f64` where the value
883 /// specified is the preceding part of the token and `f64` is the suffix of
884 /// the token. This token will always be inferred to be an `f64` in the
885 /// compiler.
886 /// Literals created from negative numbers might not survive rountrips through
887 /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
888 ///
889 /// # Panics
890 ///
891 /// This function requires that the specified float is finite, for
892 /// example if it is infinity or NaN this function will panic.
f64_suffixed(n: f64) -> Literal893 pub fn f64_suffixed(n: f64) -> Literal {
894 if !n.is_finite() {
895 panic!("Invalid float literal {}", n);
896 }
897 Literal(bridge::client::Literal::f64(&n.to_string()))
898 }
899
900 /// String literal.
string(string: &str) -> Literal901 pub fn string(string: &str) -> Literal {
902 Literal(bridge::client::Literal::string(string))
903 }
904
905 /// Character literal.
character(ch: char) -> Literal906 pub fn character(ch: char) -> Literal {
907 Literal(bridge::client::Literal::character(ch))
908 }
909
910 /// Byte string literal.
byte_string(bytes: &[u8]) -> Literal911 pub fn byte_string(bytes: &[u8]) -> Literal {
912 Literal(bridge::client::Literal::byte_string(bytes))
913 }
914
915 /// Returns the span encompassing this literal.
span(&self) -> Span916 pub fn span(&self) -> Span {
917 Span(self.0.span())
918 }
919
920 /// Configures the span associated for this literal.
set_span(&mut self, span: Span)921 pub fn set_span(&mut self, span: Span) {
922 self.0.set_span(span.0);
923 }
924
925 /// Returns a `Span` that is a subset of `self.span()` containing only the
926 /// source bytes in range `range`. Returns `None` if the would-be trimmed
927 /// span is outside the bounds of `self`.
928 // FIXME(SergioBenitez): check that the byte range starts and ends at a
929 // UTF-8 boundary of the source. otherwise, it's likely that a panic will
930 // occur elsewhere when the source text is printed.
931 // FIXME(SergioBenitez): there is no way for the user to know what
932 // `self.span()` actually maps to, so this method can currently only be
933 // called blindly. For example, `to_string()` for the character 'c' returns
934 // "'\u{63}'"; there is no way for the user to know whether the source text
935 // was 'c' or whether it was '\u{63}'.
subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span>936 pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> {
937 // HACK(eddyb) something akin to `Option::cloned`, but for `Bound<&T>`.
938 fn cloned_bound<T: Clone>(bound: Bound<&T>) -> Bound<T> {
939 match bound {
940 Bound::Included(x) => Bound::Included(x.clone()),
941 Bound::Excluded(x) => Bound::Excluded(x.clone()),
942 Bound::Unbounded => Bound::Unbounded,
943 }
944 }
945
946 self.0.subspan(cloned_bound(range.start_bound()), cloned_bound(range.end_bound())).map(Span)
947 }
948 }
949
950 /// Parse a single literal from its stringified representation.
951 ///
952 /// In order to parse successfully, the input string must not contain anything
953 /// but the literal token. Specifically, it must not contain whitespace or
954 /// comments in addition to the literal.
955 ///
956 /// The resulting literal token will have a `Span::call_site()` span.
957 ///
958 /// NOTE: some errors may cause panics instead of returning `LexError`. We
959 /// reserve the right to change these errors into `LexError`s later.
960 impl FromStr for Literal {
961 type Err = LexError;
962
from_str(src: &str) -> Result<Self, LexError>963 fn from_str(src: &str) -> Result<Self, LexError> {
964 match bridge::client::Literal::from_str(src) {
965 Ok(literal) => Ok(Literal(literal)),
966 Err(()) => Err(LexError::new()),
967 }
968 }
969 }
970
971 /// Prints the literal as a string that should be losslessly convertible
972 /// back into the same literal (except for possible rounding for floating point literals).
973 impl fmt::Display for Literal {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result974 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
975 f.write_str(&self.to_string())
976 }
977 }
978
979 impl fmt::Debug for Literal {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result980 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
981 self.0.fmt(f)
982 }
983 }
984
985 /// Tracked access to environment variables.
986 pub mod tracked_env {
987 use std::env::{self, VarError};
988 use std::ffi::OsStr;
989
990 /// Retrieve an environment variable and add it to build dependency info.
991 /// Build system executing the compiler will know that the variable was accessed during
992 /// compilation, and will be able to rerun the build when the value of that variable changes.
993 /// Besides the dependency tracking this function should be equivalent to `env::var` from the
994 /// standard library, except that the argument must be UTF-8.
var<K: AsRef<OsStr> + AsRef<str>>(key: K) -> Result<String, VarError>995 pub fn var<K: AsRef<OsStr> + AsRef<str>>(key: K) -> Result<String, VarError> {
996 let key: &str = key.as_ref();
997 let value = env::var(key);
998 super::bridge::client::FreeFunctions::track_env_var(key, value.as_deref().ok());
999 value
1000 }
1001 }
1002
1003 /// Tracked access to additional files.
1004 pub mod tracked_path {
1005
1006 /// Track a file explicitly.
1007 ///
1008 /// Commonly used for tracking asset preprocessing.
path<P: AsRef<str>>(path: P)1009 pub fn path<P: AsRef<str>>(path: P) {
1010 let path: &str = path.as_ref();
1011 super::bridge::client::FreeFunctions::track_path(path);
1012 }
1013 }
1014