Skip to content

Commit

Permalink
Merge pull request #1688 from dtolnay/peeklit
Browse files Browse the repository at this point in the history
Relocate `Lit` peek impls into `lit` module
  • Loading branch information
dtolnay authored Jun 21, 2024
2 parents 38f2ddb + 9b4e478 commit b088d5c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 47 deletions.
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

0 comments on commit b088d5c

Please sign in to comment.