Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relocate Lit peek impls into lit module #1688

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions src/lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,11 @@ pub(crate) mod parsing {
value, Lit, LitBool, LitByte, LitByteStr, LitCStr, LitChar, LitFloat, LitFloatRepr, LitInt,
LitIntRepr, LitStr,
};
use crate::parse::{Parse, ParseStream};
use proc_macro2::{Literal, Punct};
use crate::parse::{Parse, ParseStream, Unexpected};
use crate::token::{self, Token};
use proc_macro2::{Literal, Punct, Span};
use std::cell::Cell;
use std::rc::Rc;

#[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
impl Parse for Lit {
Expand Down Expand Up @@ -1017,6 +1020,42 @@ pub(crate) mod parsing {
}
}
}

fn peek_impl(cursor: Cursor, peek: fn(ParseStream) -> bool) -> bool {
let scope = Span::call_site();
let unexpected = Rc::new(Cell::new(Unexpected::None));
let buffer = crate::parse::new_parse_buffer(scope, cursor, unexpected);
peek(&buffer)
}

macro_rules! impl_token {
($display:literal $name:ty) => {
impl Token for $name {
fn peek(cursor: Cursor) -> bool {
fn peek(input: ParseStream) -> bool {
<$name as Parse>::parse(input).is_ok()
}
peek_impl(cursor, peek)
}

fn display() -> &'static str {
$display
}
}

impl token::private::Sealed for $name {}
};
}

impl_token!("literal" Lit);
impl_token!("string literal" LitStr);
impl_token!("byte string literal" LitByteStr);
impl_token!("C-string literal" LitCStr);
impl_token!("byte literal" LitByte);
impl_token!("character literal" LitChar);
impl_token!("integer literal" LitInt);
impl_token!("floating point literal" LitFloat);
impl_token!("boolean literal" LitBool);
}

#[cfg(feature = "printing")]
Expand Down
45 changes: 0 additions & 45 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ use crate::error::Result;
#[cfg(feature = "parsing")]
use crate::lifetime::Lifetime;
#[cfg(feature = "parsing")]
use crate::lit::{Lit, LitBool, LitByte, LitByteStr, LitCStr, LitChar, LitFloat, LitInt, LitStr};
#[cfg(feature = "parsing")]
use crate::parse::{Parse, ParseStream};
use crate::span::IntoSpans;
use proc_macro2::extra::DelimSpan;
Expand Down Expand Up @@ -162,49 +160,6 @@ pub(crate) mod private {
#[cfg(feature = "parsing")]
impl private::Sealed for Ident {}

#[cfg(feature = "parsing")]
fn peek_impl(cursor: Cursor, peek: fn(ParseStream) -> bool) -> bool {
use crate::parse::Unexpected;
use std::cell::Cell;
use std::rc::Rc;

let scope = Span::call_site();
let unexpected = Rc::new(Cell::new(Unexpected::None));
let buffer = crate::parse::new_parse_buffer(scope, cursor, unexpected);
peek(&buffer)
}

macro_rules! impl_token {
($display:literal $name:ty) => {
#[cfg(feature = "parsing")]
impl Token for $name {
fn peek(cursor: Cursor) -> bool {
fn peek(input: ParseStream) -> bool {
<$name as Parse>::parse(input).is_ok()
}
peek_impl(cursor, peek)
}

fn display() -> &'static str {
$display
}
}

#[cfg(feature = "parsing")]
impl private::Sealed for $name {}
};
}

impl_token!("literal" Lit);
impl_token!("string literal" LitStr);
impl_token!("byte string literal" LitByteStr);
impl_token!("C-string literal" LitCStr);
impl_token!("byte literal" LitByte);
impl_token!("character literal" LitChar);
impl_token!("integer literal" LitInt);
impl_token!("floating point literal" LitFloat);
impl_token!("boolean literal" LitBool);

macro_rules! impl_low_level_token {
($display:literal $($path:ident)::+ $get:ident) => {
#[cfg(feature = "parsing")]
Expand Down
Loading