| 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 | #![doc(html_root_url = "https://docs.rs/proc-macro2/1.0.101")]
|
| 94 | #![cfg_attr(any(proc_macro_span, super_unstable), feature(proc_macro_span))]
|
| 95 | #![cfg_attr(super_unstable, feature(proc_macro_def_site))]
|
| 96 | #![cfg_attr(docsrs, feature(doc_cfg))]
|
| 97 | #![deny(unsafe_op_in_unsafe_fn)]
|
| 98 | #![allow(
|
| 99 | clippy::cast_lossless,
|
| 100 | clippy::cast_possible_truncation,
|
| 101 | clippy::checked_conversions,
|
| 102 | clippy::doc_markdown,
|
| 103 | clippy::elidable_lifetime_names,
|
| 104 | clippy::incompatible_msrv,
|
| 105 | clippy::items_after_statements,
|
| 106 | clippy::iter_without_into_iter,
|
| 107 | clippy::let_underscore_untyped,
|
| 108 | clippy::manual_assert,
|
| 109 | clippy::manual_range_contains,
|
| 110 | clippy::missing_panics_doc,
|
| 111 | clippy::missing_safety_doc,
|
| 112 | clippy::must_use_candidate,
|
| 113 | clippy::needless_doctest_main,
|
| 114 | clippy::needless_lifetimes,
|
| 115 | clippy::new_without_default,
|
| 116 | clippy::return_self_not_must_use,
|
| 117 | clippy::shadow_unrelated,
|
| 118 | clippy::trivially_copy_pass_by_ref,
|
| 119 | clippy::unnecessary_wraps,
|
| 120 | clippy::unused_self,
|
| 121 | clippy::used_underscore_binding,
|
| 122 | clippy::vec_init_then_push
|
| 123 | )]
|
| 124 | #![allow(unknown_lints, mismatched_lifetime_syntaxes)]
|
| 125 |
|
| 126 | #[cfg(all(procmacro2_semver_exempt, wrap_proc_macro, not(super_unstable)))]
|
| 127 | compile_error! {"\
|
| 128 | Something is not right. If you've tried to turn on \
|
| 129 | procmacro2_semver_exempt, you need to ensure that it \
|
| 130 | is turned on for the compilation of the proc-macro2 \
|
| 131 | build script as well.
|
| 132 | "}
|
| 133 |
|
| 134 | #[cfg(all(
|
| 135 | procmacro2_nightly_testing,
|
| 136 | feature = "proc-macro",
|
| 137 | not(proc_macro_span)
|
| 138 | ))]
|
| 139 | compile_error! {"\
|
| 140 | Build script probe failed to compile.
|
| 141 | "}
|
| 142 |
|
| 143 | extern crate alloc;
|
| 144 |
|
| 145 | #[cfg(feature = "proc-macro")]
|
| 146 | extern crate proc_macro;
|
| 147 |
|
| 148 | mod marker;
|
| 149 | mod parse;
|
| 150 | mod probe;
|
| 151 | mod rcvec;
|
| 152 |
|
| 153 | #[cfg(wrap_proc_macro)]
|
| 154 | mod detection;
|
| 155 |
|
| 156 | |
| 157 | |
| 158 | #[doc(hidden)]
|
| 159 | pub mod fallback;
|
| 160 |
|
| 161 | pub mod extra;
|
| 162 |
|
| 163 | #[cfg(not(wrap_proc_macro))]
|
| 164 | use crate::fallback as imp;
|
| 165 | #[path = "wrapper.rs"]
|
| 166 | #[cfg(wrap_proc_macro)]
|
| 167 | mod imp;
|
| 168 |
|
| 169 | #[cfg(span_locations)]
|
| 170 | mod location;
|
| 171 |
|
| 172 | use crate::extra::DelimSpan;
|
| 173 | use crate::marker::{ProcMacroAutoTraits, MARKER};
|
| 174 | use core::cmp::Ordering;
|
| 175 | use core::fmt::{self, Debug, Display};
|
| 176 | use core::hash::{Hash, Hasher};
|
| 177 | #[cfg(span_locations)]
|
| 178 | use core::ops::Range;
|
| 179 | use core::ops::RangeBounds;
|
| 180 | use core::str::FromStr;
|
| 181 | use std::error::Error;
|
| 182 | use std::ffi::CStr;
|
| 183 | #[cfg(span_locations)]
|
| 184 | use std::path::PathBuf;
|
| 185 |
|
| 186 | #[cfg(span_locations)]
|
| 187 | #[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))]
|
| 188 | pub use crate::location::LineColumn;
|
| 189 |
|
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | #[derive(Clone)]
|
| 198 | pub struct TokenStream {
|
| 199 | inner: imp::TokenStream,
|
| 200 | _marker: ProcMacroAutoTraits,
|
| 201 | }
|
| 202 |
|
| 203 | |
| 204 | pub struct LexError {
|
| 205 | inner: imp::LexError,
|
| 206 | _marker: ProcMacroAutoTraits,
|
| 207 | }
|
| 208 |
|
| 209 | impl TokenStream {
|
| 210 | fn _new(inner: imp::TokenStream) -> Self {
|
| 211 | TokenStream {
|
| 212 | inner,
|
| 213 | _marker: MARKER,
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | fn _new_fallback(inner: fallback::TokenStream) -> Self {
|
| 218 | TokenStream {
|
| 219 | inner: imp::TokenStream::from(inner),
|
| 220 | _marker: MARKER,
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | |
| 225 | pub fn new() -> Self {
|
| 226 | TokenStream::_new(imp::TokenStream::new())
|
| 227 | }
|
| 228 |
|
| 229 | |
| 230 | pub fn is_empty(&self) -> bool {
|
| 231 | self.inner.is_empty()
|
| 232 | }
|
| 233 | }
|
| 234 |
|
| 235 | |
| 236 | |
| 237 | impl Default for TokenStream {
|
| 238 | fn default() -> Self {
|
| 239 | TokenStream::new()
|
| 240 | }
|
| 241 | }
|
| 242 |
|
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | impl FromStr for TokenStream {
|
| 252 | type Err = LexError;
|
| 253 |
|
| 254 | fn from_str(src: &str) -> Result<TokenStream, LexError> {
|
| 255 | match imp::TokenStream::from_str_checked(src) {
|
| 256 | Ok(tokens) => Ok(TokenStream::_new(tokens)),
|
| 257 | Err(lex) => Err(LexError {
|
| 258 | inner: lex,
|
| 259 | _marker: MARKER,
|
| 260 | }),
|
| 261 | }
|
| 262 | }
|
| 263 | }
|
| 264 |
|
| 265 | #[cfg(feature = "proc-macro")]
|
| 266 | #[cfg_attr(docsrs, doc(cfg(feature = "proc-macro")))]
|
| 267 | impl From<proc_macro::TokenStream> for TokenStream {
|
| 268 | fn from(inner: proc_macro::TokenStream) -> Self {
|
| 269 | TokenStream::_new(imp::TokenStream::from(inner))
|
| 270 | }
|
| 271 | }
|
| 272 |
|
| 273 | #[cfg(feature = "proc-macro")]
|
| 274 | #[cfg_attr(docsrs, doc(cfg(feature = "proc-macro")))]
|
| 275 | impl From<TokenStream> for proc_macro::TokenStream {
|
| 276 | fn from(inner: TokenStream) -> Self {
|
| 277 | proc_macro::TokenStream::from(inner.inner)
|
| 278 | }
|
| 279 | }
|
| 280 |
|
| 281 | impl From<TokenTree> for TokenStream {
|
| 282 | fn from(token: TokenTree) -> Self {
|
| 283 | TokenStream::_new(imp::TokenStream::from(token))
|
| 284 | }
|
| 285 | }
|
| 286 |
|
| 287 | impl Extend<TokenTree> for TokenStream {
|
| 288 | fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
|
| 289 | self.inner.extend(streams);
|
| 290 | }
|
| 291 | }
|
| 292 |
|
| 293 | impl Extend<TokenStream> for TokenStream {
|
| 294 | fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
|
| 295 | self.inner
|
| 296 | .extend(streams.into_iter().map(|stream| stream.inner));
|
| 297 | }
|
| 298 | }
|
| 299 |
|
| 300 | |
| 301 | impl FromIterator<TokenTree> for TokenStream {
|
| 302 | fn from_iter<I: IntoIterator<Item = TokenTree>>(streams: I) -> Self {
|
| 303 | TokenStream::_new(streams.into_iter().collect())
|
| 304 | }
|
| 305 | }
|
| 306 | impl FromIterator<TokenStream> for TokenStream {
|
| 307 | fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
|
| 308 | TokenStream::_new(streams.into_iter().map(|i| i.inner).collect())
|
| 309 | }
|
| 310 | }
|
| 311 |
|
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | impl Display for TokenStream {
|
| 317 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
| 318 | Display::fmt(&self.inner, f)
|
| 319 | }
|
| 320 | }
|
| 321 |
|
| 322 | |
| 323 | impl Debug for TokenStream {
|
| 324 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
| 325 | Debug::fmt(&self.inner, f)
|
| 326 | }
|
| 327 | }
|
| 328 |
|
| 329 | impl LexError {
|
| 330 | pub fn span(&self) -> Span {
|
| 331 | Span::_new(self.inner.span())
|
| 332 | }
|
| 333 | }
|
| 334 |
|
| 335 | impl Debug for LexError {
|
| 336 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
| 337 | Debug::fmt(&self.inner, f)
|
| 338 | }
|
| 339 | }
|
| 340 |
|
| 341 | impl Display for LexError {
|
| 342 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
| 343 | Display::fmt(&self.inner, f)
|
| 344 | }
|
| 345 | }
|
| 346 |
|
| 347 | impl Error for LexError {}
|
| 348 |
|
| 349 | |
| 350 | #[derive(Copy, Clone)]
|
| 351 | pub struct Span {
|
| 352 | inner: imp::Span,
|
| 353 | _marker: ProcMacroAutoTraits,
|
| 354 | }
|
| 355 |
|
| 356 | impl Span {
|
| 357 | fn _new(inner: imp::Span) -> Self {
|
| 358 | Span {
|
| 359 | inner,
|
| 360 | _marker: MARKER,
|
| 361 | }
|
| 362 | }
|
| 363 |
|
| 364 | fn _new_fallback(inner: fallback::Span) -> Self {
|
| 365 | Span {
|
| 366 | inner: imp::Span::from(inner),
|
| 367 | _marker: MARKER,
|
| 368 | }
|
| 369 | }
|
| 370 |
|
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | pub fn call_site() -> Self {
|
| 377 | Span::_new(imp::Span::call_site())
|
| 378 | }
|
| 379 |
|
| 380 | |
| 381 | |
| 382 | |
| 383 | pub fn mixed_site() -> Self {
|
| 384 | Span::_new(imp::Span::mixed_site())
|
| 385 | }
|
| 386 |
|
| 387 | |
| 388 | |
| 389 | |
| 390 | #[cfg(procmacro2_semver_exempt)]
|
| 391 | #[cfg_attr(docsrs, doc(cfg(procmacro2_semver_exempt)))]
|
| 392 | pub fn def_site() -> Self {
|
| 393 | Span::_new(imp::Span::def_site())
|
| 394 | }
|
| 395 |
|
| 396 | |
| 397 | |
| 398 | pub fn resolved_at(&self, other: Span) -> Span {
|
| 399 | Span::_new(self.inner.resolved_at(other.inner))
|
| 400 | }
|
| 401 |
|
| 402 | |
| 403 | |
| 404 | pub fn located_at(&self, other: Span) -> Span {
|
| 405 | Span::_new(self.inner.located_at(other.inner))
|
| 406 | }
|
| 407 |
|
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | #[cfg(wrap_proc_macro)]
|
| 419 | pub fn unwrap(self) -> proc_macro::Span {
|
| 420 | self.inner.unwrap()
|
| 421 | }
|
| 422 |
|
| 423 | |
| 424 | #[cfg(wrap_proc_macro)]
|
| 425 | #[doc(hidden)]
|
| 426 | pub fn unstable(self) -> proc_macro::Span {
|
| 427 | self.unwrap()
|
| 428 | }
|
| 429 |
|
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | #[cfg(span_locations)]
|
| 440 | #[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))]
|
| 441 | pub fn byte_range(&self) -> Range<usize> {
|
| 442 | self.inner.byte_range()
|
| 443 | }
|
| 444 |
|
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | #[cfg(span_locations)]
|
| 455 | #[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))]
|
| 456 | pub fn start(&self) -> LineColumn {
|
| 457 | self.inner.start()
|
| 458 | }
|
| 459 |
|
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | #[cfg(span_locations)]
|
| 470 | #[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))]
|
| 471 | pub fn end(&self) -> LineColumn {
|
| 472 | self.inner.end()
|
| 473 | }
|
| 474 |
|
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | #[cfg(span_locations)]
|
| 481 | #[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))]
|
| 482 | pub fn file(&self) -> String {
|
| 483 | self.inner.file()
|
| 484 | }
|
| 485 |
|
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | #[cfg(span_locations)]
|
| 493 | #[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))]
|
| 494 | pub fn local_file(&self) -> Option<PathBuf> {
|
| 495 | self.inner.local_file()
|
| 496 | }
|
| 497 |
|
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | pub fn join(&self, other: Span) -> Option<Span> {
|
| 506 | self.inner.join(other.inner).map(Span::_new)
|
| 507 | }
|
| 508 |
|
| 509 | |
| 510 | |
| 511 | |
| 512 | #[cfg(procmacro2_semver_exempt)]
|
| 513 | #[cfg_attr(docsrs, doc(cfg(procmacro2_semver_exempt)))]
|
| 514 | pub fn eq(&self, other: &Span) -> bool {
|
| 515 | self.inner.eq(&other.inner)
|
| 516 | }
|
| 517 |
|
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | pub fn source_text(&self) -> Option<String> {
|
| 526 | self.inner.source_text()
|
| 527 | }
|
| 528 | }
|
| 529 |
|
| 530 | |
| 531 | impl Debug for Span {
|
| 532 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
| 533 | Debug::fmt(&self.inner, f)
|
| 534 | }
|
| 535 | }
|
| 536 |
|
| 537 | |
| 538 | #[derive(Clone)]
|
| 539 | pub enum TokenTree {
|
| 540 | |
| 541 | Group(Group),
|
| 542 | |
| 543 | Ident(Ident),
|
| 544 | |
| 545 | Punct(Punct),
|
| 546 | |
| 547 | Literal(Literal),
|
| 548 | }
|
| 549 |
|
| 550 | impl TokenTree {
|
| 551 | |
| 552 | |
| 553 | pub fn span(&self) -> Span {
|
| 554 | match self {
|
| 555 | TokenTree::Group(t) => t.span(),
|
| 556 | TokenTree::Ident(t) => t.span(),
|
| 557 | TokenTree::Punct(t) => t.span(),
|
| 558 | TokenTree::Literal(t) => t.span(),
|
| 559 | }
|
| 560 | }
|
| 561 |
|
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | pub fn set_span(&mut self, span: Span) {
|
| 568 | match self {
|
| 569 | TokenTree::Group(t) => t.set_span(span),
|
| 570 | TokenTree::Ident(t) => t.set_span(span),
|
| 571 | TokenTree::Punct(t) => t.set_span(span),
|
| 572 | TokenTree::Literal(t) => t.set_span(span),
|
| 573 | }
|
| 574 | }
|
| 575 | }
|
| 576 |
|
| 577 | impl From<Group> for TokenTree {
|
| 578 | fn from(g: Group) -> Self {
|
| 579 | TokenTree::Group(g)
|
| 580 | }
|
| 581 | }
|
| 582 |
|
| 583 | impl From<Ident> for TokenTree {
|
| 584 | fn from(g: Ident) -> Self {
|
| 585 | TokenTree::Ident(g)
|
| 586 | }
|
| 587 | }
|
| 588 |
|
| 589 | impl From<Punct> for TokenTree {
|
| 590 | fn from(g: Punct) -> Self {
|
| 591 | TokenTree::Punct(g)
|
| 592 | }
|
| 593 | }
|
| 594 |
|
| 595 | impl From<Literal> for TokenTree {
|
| 596 | fn from(g: Literal) -> Self {
|
| 597 | TokenTree::Literal(g)
|
| 598 | }
|
| 599 | }
|
| 600 |
|
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | impl Display for TokenTree {
|
| 606 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
| 607 | match self {
|
| 608 | TokenTree::Group(t) => Display::fmt(t, f),
|
| 609 | TokenTree::Ident(t) => Display::fmt(t, f),
|
| 610 | TokenTree::Punct(t) => Display::fmt(t, f),
|
| 611 | TokenTree::Literal(t) => Display::fmt(t, f),
|
| 612 | }
|
| 613 | }
|
| 614 | }
|
| 615 |
|
| 616 | |
| 617 | impl Debug for TokenTree {
|
| 618 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
| 619 | |
| 620 | |
| 621 | match self {
|
| 622 | TokenTree::Group(t) => Debug::fmt(t, f),
|
| 623 | TokenTree::Ident(t) => {
|
| 624 | let mut debug = f.debug_struct("Ident");
|
| 625 | debug.field("sym", &format_args!("{}", t));
|
| 626 | imp::debug_span_field_if_nontrivial(&mut debug, t.span().inner);
|
| 627 | debug.finish()
|
| 628 | }
|
| 629 | TokenTree::Punct(t) => Debug::fmt(t, f),
|
| 630 | TokenTree::Literal(t) => Debug::fmt(t, f),
|
| 631 | }
|
| 632 | }
|
| 633 | }
|
| 634 |
|
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | #[derive(Clone)]
|
| 640 | pub struct Group {
|
| 641 | inner: imp::Group,
|
| 642 | }
|
| 643 |
|
| 644 | |
| 645 | #[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
| 646 | pub enum Delimiter {
|
| 647 | |
| 648 | Parenthesis,
|
| 649 | |
| 650 | Brace,
|
| 651 | |
| 652 | Bracket,
|
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | None,
|
| 673 | }
|
| 674 |
|
| 675 | impl Group {
|
| 676 | fn _new(inner: imp::Group) -> Self {
|
| 677 | Group { inner }
|
| 678 | }
|
| 679 |
|
| 680 | fn _new_fallback(inner: fallback::Group) -> Self {
|
| 681 | Group {
|
| 682 | inner: imp::Group::from(inner),
|
| 683 | }
|
| 684 | }
|
| 685 |
|
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | pub fn new(delimiter: Delimiter, stream: TokenStream) -> Self {
|
| 692 | Group {
|
| 693 | inner: imp::Group::new(delimiter, stream.inner),
|
| 694 | }
|
| 695 | }
|
| 696 |
|
| 697 | |
| 698 | |
| 699 | pub fn delimiter(&self) -> Delimiter {
|
| 700 | self.inner.delimiter()
|
| 701 | }
|
| 702 |
|
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | pub fn stream(&self) -> TokenStream {
|
| 708 | TokenStream::_new(self.inner.stream())
|
| 709 | }
|
| 710 |
|
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | pub fn span(&self) -> Span {
|
| 719 | Span::_new(self.inner.span())
|
| 720 | }
|
| 721 |
|
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | pub fn span_open(&self) -> Span {
|
| 729 | Span::_new(self.inner.span_open())
|
| 730 | }
|
| 731 |
|
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | pub fn span_close(&self) -> Span {
|
| 739 | Span::_new(self.inner.span_close())
|
| 740 | }
|
| 741 |
|
| 742 | |
| 743 | |
| 744 | |
| 745 | pub fn delim_span(&self) -> DelimSpan {
|
| 746 | DelimSpan::new(&self.inner)
|
| 747 | }
|
| 748 |
|
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | pub fn set_span(&mut self, span: Span) {
|
| 756 | self.inner.set_span(span.inner);
|
| 757 | }
|
| 758 | }
|
| 759 |
|
| 760 | |
| 761 | |
| 762 | |
| 763 | impl Display for Group {
|
| 764 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
| 765 | Display::fmt(&self.inner, formatter)
|
| 766 | }
|
| 767 | }
|
| 768 |
|
| 769 | impl Debug for Group {
|
| 770 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
| 771 | Debug::fmt(&self.inner, formatter)
|
| 772 | }
|
| 773 | }
|
| 774 |
|
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | #[derive(Clone)]
|
| 780 | pub struct Punct {
|
| 781 | ch: char,
|
| 782 | spacing: Spacing,
|
| 783 | span: Span,
|
| 784 | }
|
| 785 |
|
| 786 | |
| 787 | |
| 788 | #[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
| 789 | pub enum Spacing {
|
| 790 | |
| 791 | Alone,
|
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | Joint,
|
| 797 | }
|
| 798 |
|
| 799 | impl Punct {
|
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | pub fn new(ch: char, spacing: Spacing) -> Self {
|
| 808 | if let '!' | '#' | '$' | '%' | '&' | '\'' | '*' | '+' | ',' | '-' | '.' | '/' | ':' | ';'
|
| 809 | | '<' | '=' | '>' | '?' | '@' | '^' | '|' | '~' = ch
|
| 810 | {
|
| 811 | Punct {
|
| 812 | ch,
|
| 813 | spacing,
|
| 814 | span: Span::call_site(),
|
| 815 | }
|
| 816 | } else {
|
| 817 | panic!("unsupported proc macro punctuation character {:?}", ch);
|
| 818 | }
|
| 819 | }
|
| 820 |
|
| 821 | |
| 822 | pub fn as_char(&self) -> char {
|
| 823 | self.ch
|
| 824 | }
|
| 825 |
|
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | pub fn spacing(&self) -> Spacing {
|
| 832 | self.spacing
|
| 833 | }
|
| 834 |
|
| 835 | |
| 836 | pub fn span(&self) -> Span {
|
| 837 | self.span
|
| 838 | }
|
| 839 |
|
| 840 | |
| 841 | pub fn set_span(&mut self, span: Span) {
|
| 842 | self.span = span;
|
| 843 | }
|
| 844 | }
|
| 845 |
|
| 846 | |
| 847 | |
| 848 | impl Display for Punct {
|
| 849 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
| 850 | Display::fmt(&self.ch, f)
|
| 851 | }
|
| 852 | }
|
| 853 |
|
| 854 | impl Debug for Punct {
|
| 855 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
| 856 | let mut debug = fmt.debug_struct("Punct");
|
| 857 | debug.field("char", &self.ch);
|
| 858 | debug.field("spacing", &self.spacing);
|
| 859 | imp::debug_span_field_if_nontrivial(&mut debug, self.span.inner);
|
| 860 | debug.finish()
|
| 861 | }
|
| 862 | }
|
| 863 |
|
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | #[derive(Clone)]
|
| 929 | pub struct Ident {
|
| 930 | inner: imp::Ident,
|
| 931 | _marker: ProcMacroAutoTraits,
|
| 932 | }
|
| 933 |
|
| 934 | impl Ident {
|
| 935 | fn _new(inner: imp::Ident) -> Self {
|
| 936 | Ident {
|
| 937 | inner,
|
| 938 | _marker: MARKER,
|
| 939 | }
|
| 940 | }
|
| 941 |
|
| 942 | fn _new_fallback(inner: fallback::Ident) -> Self {
|
| 943 | Ident {
|
| 944 | inner: imp::Ident::from(inner),
|
| 945 | _marker: MARKER,
|
| 946 | }
|
| 947 | }
|
| 948 |
|
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | #[track_caller]
|
| 981 | pub fn new(string: &str, span: Span) -> Self {
|
| 982 | Ident::_new(imp::Ident::new_checked(string, span.inner))
|
| 983 | }
|
| 984 |
|
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | #[track_caller]
|
| 991 | pub fn new_raw(string: &str, span: Span) -> Self {
|
| 992 | Ident::_new(imp::Ident::new_raw_checked(string, span.inner))
|
| 993 | }
|
| 994 |
|
| 995 | |
| 996 | pub fn span(&self) -> Span {
|
| 997 | Span::_new(self.inner.span())
|
| 998 | }
|
| 999 |
|
| 1000 | |
| 1001 | |
| 1002 | pub fn set_span(&mut self, span: Span) {
|
| 1003 | self.inner.set_span(span.inner);
|
| 1004 | }
|
| 1005 | }
|
| 1006 |
|
| 1007 | impl PartialEq for Ident {
|
| 1008 | fn eq(&self, other: &Ident) -> bool {
|
| 1009 | self.inner == other.inner
|
| 1010 | }
|
| 1011 | }
|
| 1012 |
|
| 1013 | impl<T> PartialEq<T> for Ident
|
| 1014 | where
|
| 1015 | T: ?Sized + AsRef<str>,
|
| 1016 | {
|
| 1017 | fn eq(&self, other: &T) -> bool {
|
| 1018 | self.inner == other
|
| 1019 | }
|
| 1020 | }
|
| 1021 |
|
| 1022 | impl Eq for Ident {}
|
| 1023 |
|
| 1024 | impl PartialOrd for Ident {
|
| 1025 | fn partial_cmp(&self, other: &Ident) -> Option<Ordering> {
|
| 1026 | Some(self.cmp(other))
|
| 1027 | }
|
| 1028 | }
|
| 1029 |
|
| 1030 | impl Ord for Ident {
|
| 1031 | fn cmp(&self, other: &Ident) -> Ordering {
|
| 1032 | self.to_string().cmp(&other.to_string())
|
| 1033 | }
|
| 1034 | }
|
| 1035 |
|
| 1036 | impl Hash for Ident {
|
| 1037 | fn hash<H: Hasher>(&self, hasher: &mut H) {
|
| 1038 | self.to_string().hash(hasher);
|
| 1039 | }
|
| 1040 | }
|
| 1041 |
|
| 1042 | |
| 1043 | |
| 1044 | impl Display for Ident {
|
| 1045 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
| 1046 | Display::fmt(&self.inner, f)
|
| 1047 | }
|
| 1048 | }
|
| 1049 |
|
| 1050 | impl Debug for Ident {
|
| 1051 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
| 1052 | Debug::fmt(&self.inner, f)
|
| 1053 | }
|
| 1054 | }
|
| 1055 |
|
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | #[derive(Clone)]
|
| 1063 | pub struct Literal {
|
| 1064 | inner: imp::Literal,
|
| 1065 | _marker: ProcMacroAutoTraits,
|
| 1066 | }
|
| 1067 |
|
| 1068 | macro_rules! suffixed_int_literals {
|
| 1069 | ($($name:ident => $kind:ident,)*) => ($(
|
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | pub fn $name(n: $kind) -> Literal {
|
| 1082 | Literal::_new(imp::Literal::$name(n))
|
| 1083 | }
|
| 1084 | )*)
|
| 1085 | }
|
| 1086 |
|
| 1087 | macro_rules! unsuffixed_int_literals {
|
| 1088 | ($($name:ident => $kind:ident,)*) => ($(
|
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | pub fn $name(n: $kind) -> Literal {
|
| 1103 | Literal::_new(imp::Literal::$name(n))
|
| 1104 | }
|
| 1105 | )*)
|
| 1106 | }
|
| 1107 |
|
| 1108 | impl Literal {
|
| 1109 | fn _new(inner: imp::Literal) -> Self {
|
| 1110 | Literal {
|
| 1111 | inner,
|
| 1112 | _marker: MARKER,
|
| 1113 | }
|
| 1114 | }
|
| 1115 |
|
| 1116 | fn _new_fallback(inner: fallback::Literal) -> Self {
|
| 1117 | Literal {
|
| 1118 | inner: imp::Literal::from(inner),
|
| 1119 | _marker: MARKER,
|
| 1120 | }
|
| 1121 | }
|
| 1122 |
|
| 1123 | suffixed_int_literals! {
|
| 1124 | u8_suffixed => u8,
|
| 1125 | u16_suffixed => u16,
|
| 1126 | u32_suffixed => u32,
|
| 1127 | u64_suffixed => u64,
|
| 1128 | u128_suffixed => u128,
|
| 1129 | usize_suffixed => usize,
|
| 1130 | i8_suffixed => i8,
|
| 1131 | i16_suffixed => i16,
|
| 1132 | i32_suffixed => i32,
|
| 1133 | i64_suffixed => i64,
|
| 1134 | i128_suffixed => i128,
|
| 1135 | isize_suffixed => isize,
|
| 1136 | }
|
| 1137 |
|
| 1138 | unsuffixed_int_literals! {
|
| 1139 | u8_unsuffixed => u8,
|
| 1140 | u16_unsuffixed => u16,
|
| 1141 | u32_unsuffixed => u32,
|
| 1142 | u64_unsuffixed => u64,
|
| 1143 | u128_unsuffixed => u128,
|
| 1144 | usize_unsuffixed => usize,
|
| 1145 | i8_unsuffixed => i8,
|
| 1146 | i16_unsuffixed => i16,
|
| 1147 | i32_unsuffixed => i32,
|
| 1148 | i64_unsuffixed => i64,
|
| 1149 | i128_unsuffixed => i128,
|
| 1150 | isize_unsuffixed => isize,
|
| 1151 | }
|
| 1152 |
|
| 1153 | |
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | |
| 1164 | |
| 1165 | |
| 1166 | pub fn f64_unsuffixed(f: f64) -> Literal {
|
| 1167 | assert!(f.is_finite());
|
| 1168 | Literal::_new(imp::Literal::f64_unsuffixed(f))
|
| 1169 | }
|
| 1170 |
|
| 1171 | |
| 1172 | |
| 1173 | |
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | |
| 1178 | |
| 1179 | |
| 1180 | |
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | pub fn f64_suffixed(f: f64) -> Literal {
|
| 1185 | assert!(f.is_finite());
|
| 1186 | Literal::_new(imp::Literal::f64_suffixed(f))
|
| 1187 | }
|
| 1188 |
|
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | |
| 1197 | |
| 1198 | |
| 1199 | |
| 1200 | |
| 1201 | |
| 1202 | pub fn f32_unsuffixed(f: f32) -> Literal {
|
| 1203 | assert!(f.is_finite());
|
| 1204 | Literal::_new(imp::Literal::f32_unsuffixed(f))
|
| 1205 | }
|
| 1206 |
|
| 1207 | |
| 1208 | |
| 1209 | |
| 1210 | |
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | |
| 1216 | |
| 1217 | |
| 1218 | |
| 1219 | |
| 1220 | pub fn f32_suffixed(f: f32) -> Literal {
|
| 1221 | assert!(f.is_finite());
|
| 1222 | Literal::_new(imp::Literal::f32_suffixed(f))
|
| 1223 | }
|
| 1224 |
|
| 1225 | |
| 1226 | pub fn string(string: &str) -> Literal {
|
| 1227 | Literal::_new(imp::Literal::string(string))
|
| 1228 | }
|
| 1229 |
|
| 1230 | |
| 1231 | pub fn character(ch: char) -> Literal {
|
| 1232 | Literal::_new(imp::Literal::character(ch))
|
| 1233 | }
|
| 1234 |
|
| 1235 | |
| 1236 | pub fn byte_character(byte: u8) -> Literal {
|
| 1237 | Literal::_new(imp::Literal::byte_character(byte))
|
| 1238 | }
|
| 1239 |
|
| 1240 | |
| 1241 | pub fn byte_string(bytes: &[u8]) -> Literal {
|
| 1242 | Literal::_new(imp::Literal::byte_string(bytes))
|
| 1243 | }
|
| 1244 |
|
| 1245 | |
| 1246 | pub fn c_string(string: &CStr) -> Literal {
|
| 1247 | Literal::_new(imp::Literal::c_string(string))
|
| 1248 | }
|
| 1249 |
|
| 1250 | |
| 1251 | pub fn span(&self) -> Span {
|
| 1252 | Span::_new(self.inner.span())
|
| 1253 | }
|
| 1254 |
|
| 1255 | |
| 1256 | pub fn set_span(&mut self, span: Span) {
|
| 1257 | self.inner.set_span(span.inner);
|
| 1258 | }
|
| 1259 |
|
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | |
| 1266 | |
| 1267 | pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> {
|
| 1268 | self.inner.subspan(range).map(Span::_new)
|
| 1269 | }
|
| 1270 |
|
| 1271 | |
| 1272 | |
| 1273 | |
| 1274 | |
| 1275 | #[doc(hidden)]
|
| 1276 | pub unsafe fn from_str_unchecked(repr: &str) -> Self {
|
| 1277 | Literal::_new(unsafe { imp::Literal::from_str_unchecked(repr) })
|
| 1278 | }
|
| 1279 | }
|
| 1280 |
|
| 1281 | impl FromStr for Literal {
|
| 1282 | type Err = LexError;
|
| 1283 |
|
| 1284 | fn from_str(repr: &str) -> Result<Self, LexError> {
|
| 1285 | match imp::Literal::from_str_checked(repr) {
|
| 1286 | Ok(lit) => Ok(Literal::_new(lit)),
|
| 1287 | Err(lex) => Err(LexError {
|
| 1288 | inner: lex,
|
| 1289 | _marker: MARKER,
|
| 1290 | }),
|
| 1291 | }
|
| 1292 | }
|
| 1293 | }
|
| 1294 |
|
| 1295 | impl Debug for Literal {
|
| 1296 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
| 1297 | Debug::fmt(&self.inner, f)
|
| 1298 | }
|
| 1299 | }
|
| 1300 |
|
| 1301 | impl Display for Literal {
|
| 1302 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
| 1303 | Display::fmt(&self.inner, f)
|
| 1304 | }
|
| 1305 | }
|
| 1306 |
|
| 1307 | |
| 1308 | pub mod token_stream {
|
| 1309 | use crate::marker::{ProcMacroAutoTraits, MARKER};
|
| 1310 | use crate::{imp, TokenTree};
|
| 1311 | use core::fmt::{self, Debug};
|
| 1312 |
|
| 1313 | pub use crate::TokenStream;
|
| 1314 |
|
| 1315 | |
| 1316 | |
| 1317 | |
| 1318 | |
| 1319 | #[derive(Clone)]
|
| 1320 | pub struct IntoIter {
|
| 1321 | inner: imp::TokenTreeIter,
|
| 1322 | _marker: ProcMacroAutoTraits,
|
| 1323 | }
|
| 1324 |
|
| 1325 | impl Iterator for IntoIter {
|
| 1326 | type Item = TokenTree;
|
| 1327 |
|
| 1328 | fn next(&mut self) -> Option<TokenTree> {
|
| 1329 | self.inner.next()
|
| 1330 | }
|
| 1331 |
|
| 1332 | fn size_hint(&self) -> (usize, Option<usize>) {
|
| 1333 | self.inner.size_hint()
|
| 1334 | }
|
| 1335 | }
|
| 1336 |
|
| 1337 | impl Debug for IntoIter {
|
| 1338 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
| 1339 | f.write_str("TokenStream ")?;
|
| 1340 | f.debug_list().entries(self.clone()).finish()
|
| 1341 | }
|
| 1342 | }
|
| 1343 |
|
| 1344 | impl IntoIterator for TokenStream {
|
| 1345 | type Item = TokenTree;
|
| 1346 | type IntoIter = IntoIter;
|
| 1347 |
|
| 1348 | fn into_iter(self) -> IntoIter {
|
| 1349 | IntoIter {
|
| 1350 | inner: self.inner.into_iter(),
|
| 1351 | _marker: MARKER,
|
| 1352 | }
|
| 1353 | }
|
| 1354 | }
|
| 1355 | }
|