Skip to content

Commit e323372

Browse files
committed
Refactor + Features (sorry)
Added: - `alone` to `TokenTreePunct` ensuring if a token is `Punct` its `spacing` is `Alone` - `*_alone` functions to `TokenParser` setting the last token to `Alone` if it is a punctuat on. - `next_n` to `TokenParser` returning the next `n` tokens. - `peek_range` to `TokenParser` returning a range of tokens. Changed: - **Breaking Change** `Peeker::LENGTH` is function `len` now. - **Breaking Change** `TokenParser::*_{punctuation}` got renamed to `*_tt_{punctuation}` and their behaviour was changed to match that of `$tt` in macro_rules instead of requiring `Spac ing::Alone`, returned `spacing` is set to `Alone`. - `next_expr` and `next_type` set the last tokens `spacing` to `Alone`.
1 parent ac665a5 commit e323372

File tree

3 files changed

+487
-90
lines changed

3 files changed

+487
-90
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,27 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
- Increased documentation and test coverage.
9+
810
### Added
911
- `quote::ToTokens` implementation for `TokenParser` (`quote` is a new default feature).
1012
- `peek_{token}` and `peek_n_{token}` to `TokenParser`.
13+
- `alone` to `TokenTreePunct` ensuring if a token is `Punct` its `spacing` is `Alone`
14+
- `*_alone` functions to `TokenParser` setting the last token to `Alone` if it is a punctuation.
15+
- `next_n` to `TokenParser` returning the next `n` tokens.
16+
- `peek_range` to `TokenParser` returning a range of tokens.
1117

1218
### Changed
1319
- **Breaking Change** Added const generic buffer size to `TokenParser`.
1420
- **Breaking Change** `Peeker::peek` takes `&[TokenTree]` instead of `TokenParser`.
21+
- **Breaking Change** `Peeker::LENGTH` is function `len` now.
1522
- **Breaking Change** `*_{delimiter}` returns `Group` instead of the contained stream.
1623
To get to the stream call `.stream()`
24+
- **Breaking Change** `TokenParser::*_{punctuation}` got renamed to `*_tt_{punctuation}` and their behaviour was changed to match that of `$tt` in macro_rules instead of requiring `Spacing::Alone`, returned `spacing` is set to `Alone`.
1725
- `TokenParser` peeking supports `n` greater than stack buffer, allowing spilling to heap.
1826
- Increased default `TokenParser` peek buffer to `6`.
1927
- Marked parser functions as must_use.
28+
- `next_expr` and `next_type` set the last tokens `spacing` to `Alone`.
2029

2130
## [0.6.0] - 2023-04-29
2231
- `TokenParser::next_keyword(v)`

src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
77
#![deny(rustdoc::all)]
88

9+
#[cfg(doc)]
10+
use proc_macro2::{Punct, Spacing};
11+
912
#[cfg(feature = "proc-macro")]
1013
extern crate proc_macro;
1114

@@ -137,10 +140,12 @@ macro_rules! token_tree_ext {
137140
impl TokenTreeExt "[`proc_macro::TokenTree`]", TokenTree2Ext "[`proc_macro2::TokenTree`]" for TokenTree {
138141
$(
139142
#[doc = concat!("Tests if the token tree is ", $a, " ", $token, ".")]
143+
#[must_use]
140144
fn $is(&self) -> bool {
141145
matches!(self, Self::$variant(_))
142146
}
143147
#[doc = concat!("Get the [`", stringify!($variant), "`] inside this token tree, or [`None`] if it isn't ", $a, " ", $token, ".")]
148+
#[must_use]
144149
fn $as(&self) -> Option<&$variant> {
145150
if let Self::$variant(inner) = &self {
146151
Some(inner)
@@ -149,6 +154,7 @@ macro_rules! token_tree_ext {
149154
}
150155
}
151156
#[doc = concat!("Get the [`", stringify!($variant), "`] inside this token tree, or [`None`] if it isn't ", $a, " ", $token, ".")]
157+
#[must_use]
152158
fn $into(self) -> Option<$variant> {
153159
if let Self::$variant(inner) = self {
154160
Some(inner)
@@ -176,18 +182,29 @@ macro_rules! punctuations {
176182
/// Trait to test for punctuation
177183
impl TokenTreePunct for TokenTree {
178184
$(#[doc = concat!("Tests if the token is `", $char, "`")]
185+
#[must_use]
179186
fn $name(&self) -> bool {
180187
matches!(self, TokenTree::Punct(punct) if punct.$name())
181188
})*
182189
/// Tests if token is followed by some none punctuation token or whitespace.
190+
#[must_use]
183191
fn is_alone(&self) -> bool {
184192
matches!(self, TokenTree::Punct(punct) if punct.is_alone())
185193
}
186194
/// Tests if token is followed by another punct and can potentially be combined into
187195
/// a multi-character operator.
196+
#[must_use]
188197
fn is_joint(&self) -> bool {
189198
matches!(self, TokenTree::Punct(punct) if punct.is_joint())
190199
}
200+
/// If sets the [`spacing`](Punct::spacing) of a punct to [`Alone`](Spacing::Alone).
201+
#[must_use]
202+
fn alone(self) -> Self {
203+
match self {
204+
Self::Punct(p) => Self::Punct(p.alone()),
205+
it => it
206+
}
207+
}
191208
}
192209
impl TokenTreePunct for Punct {
193210
$(fn $name(&self) -> bool {
@@ -199,6 +216,15 @@ macro_rules! punctuations {
199216
fn is_joint(&self) -> bool {
200217
self.spacing() == Spacing::Joint
201218
}
219+
fn alone(self) -> Self {
220+
if self.is_alone() {
221+
self
222+
} else {
223+
let mut this = Punct::new(self.as_char(), Spacing::Alone);
224+
this.set_span(self.span());
225+
this
226+
}
227+
}
202228
}
203229
}
204230
};
@@ -235,12 +261,14 @@ macro_rules! delimited {
235261
/// Trait to test for delimiters of groups
236262
impl Delimited for TokenTree {
237263
$(#[doc = concat!("Tests if the token is a group with ", $doc)]
264+
#[must_use]
238265
fn $name(&self) -> bool {
239266
matches!(self, TokenTree::Group(group) if group.$name())
240267
})*
241268
}
242269
impl Delimited for Group {
243270
$(#[doc = concat!("Tests if a group has ", $doc)]
271+
#[must_use]
244272
fn $name(&self) -> bool {
245273
matches!(self.delimiter(), Delimiter::$delimiter)
246274
})*

0 commit comments

Comments
 (0)