// SPDX-License-Identifier: Apache-2.0 OR MIT
#[cfg(wrap_proc_macro)]
use crate::imp;
#[cfg(span_locations)]
use crate::location::LineColumn;
use crate::parse::{self, Cursor};
use crate::rcvec::{RcVec, RcVecBuilder, RcVecIntoIter, RcVecMut};
use crate::{Delimiter, Spacing, TokenTree};
#[cfg(all(span_locations, not(fuzzing)))]
use alloc::collections::BTreeMap;
#[cfg(all(span_locations, not(fuzzing)))]
use core::cell::RefCell;
#[cfg(span_locations)]
use core::cmp;
#[cfg(all(span_locations, not(fuzzing)))]
use core::cmp::Ordering;
use core::fmt::{self, Debug, Display, Write};
use core::mem::ManuallyDrop;
#[cfg(span_locations)]
use core::ops::Range;
use core::ops::RangeBounds;
use core::ptr;
use core::str;
#[cfg(feature = "proc-macro")]
use core::str::FromStr;
use std::ffi::CStr;
#[cfg(wrap_proc_macro)]
use std::panic;
#[cfg(span_locations)]
use std::path::PathBuf;
/// Force use of proc-macro2's fallback implementation of the API for now, even
/// if the compiler's implementation is available.
pub fn force() {
#[cfg(wrap_proc_macro)]
crate::detection::force_fallback();
}
/// Resume using the compiler's implementation of the proc macro API if it is
/// available.
pub fn unforce() {
#[cfg(wrap_proc_macro)]
crate::detection::unforce_fallback();
}
#[derive(Clone)]
pub(crate) struct TokenStream {
inner: RcVec<TokenTree>,
}
#[derive(Debug)]
pub(crate) struct LexError {
pub(crate) span: Span,
}
impl LexError {
pub(crate) fn span(&self) -> Span {
self.span
}
pub(crate) fn call_site() -> Self {
LexError {
span: Span::call_site(),
}
}
}
impl TokenStream {
pub(crate) fn new() -> Self {
TokenStream {
inner: RcVecBuilder::new().build(),
}
}
pub(crate) fn from_str_checked(src: &str) -> Result<Self, LexError> {
// Create a dummy file & add it to the source map
let mut cursor = get_cursor(src);
// Strip a byte order mark if present
const BYTE_ORDER_MARK: &str = "\u{feff}";
if cursor.starts_with(BYTE_ORDER_MARK) {
cursor = cursor.advance(BYTE_ORDER_MARK.len());
}