Skip to content

Commit d9af0e3

Browse files
committed
parser revamp adding peek_* fns
1 parent e4dd0fe commit d9af0e3

File tree

6 files changed

+308
-162
lines changed

6 files changed

+308
-162
lines changed

.github/workflows/test.yaml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ jobs:
1818
rust:
1919
- stable
2020
- nightly
21-
features:
22-
- ''
23-
- --no-default-features
24-
- --no-default-features --features proc-macro
25-
- --no-default-features --features proc-macro2
26-
- --no-default-features --features parser
2721
include:
2822
- rust: nightly
2923
cargo_flags: -Z minimal-versions
@@ -42,11 +36,14 @@ jobs:
4236
- uses: hecrj/setup-rust-action@v1
4337
with:
4438
rust-version: ${{ matrix.rust }}
39+
- uses: bruxisma/setup-cargo-hack@v1
40+
with:
41+
cargo-hack-version: "0.5"
4542
- name: Build
46-
run: cargo build ${{ matrix.features }} ${{ matrix.cargo_flags }}
43+
run: cargo hack build --feature-powerset ${{ matrix.cargo_flags }}
4744
- name: Test
48-
run: cargo test ${{ matrix.features }} --all-targets --no-fail-fast --workspace
45+
run: cargo test --feature-powerset --all-targets --no-fail-fast --workspace
4946
- name: Doc Test
50-
run: cargo test ${{ matrix.features }} --doc --no-fail-fast --workspace
47+
run: cargo test --feature-powerset --doc --no-fail-fast --workspace
5148
- name: Build Docs
52-
run: cargo doc ${{ matrix.features }} --workspace --no-deps
49+
run: cargo doc --feature-powerset --workspace --no-deps

CHANGELOG.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,32 @@ All notable changes to this project will be documented in this file.
44
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

7-
<!-- ## [Unreleased] -->
7+
## [Unreleased]
8+
### Added
9+
- `quote::ToTokens` implementation for `TokenParser` (`quote` is a new default feature)
10+
- `peek_{token}` and `peek_n_{token}` to `TokenParser`
11+
12+
### Changed
13+
- **Breaking Change** added const generic buffer size to `TokenParser`
14+
- **Breaking Change** `Peeker::peek` takes `&[TokenTree]` instead of `TokenParser`
15+
- `TokenParser` peeking supports `n` greater than stack buffer, allowing spilling to heap
16+
- increased default `TokenParser` peek buffer to `6`
817

918
## [0.6.0] - 2023-04-29
10-
- `Parser::next_keyword(v)`
19+
- `TokenParser::next_keyword(v)`
1120

1221
## [0.5.2] - 2023-04-06
1322
### Fixed
14-
- `Parser::peek_n()` always returned `None`
15-
- `Parser::next_{token}` did not work correctly because of `peek_n`
23+
- `TokenParser::peek_n()` always returned `None`
24+
- `TokenParser::next_{token}` did not work correctly because of `peek_n`
1625

1726
## [0.5.1] - 2023-03-02
1827
### Fixed
19-
- `Parser::next_string()` did not consume token
28+
- `TokenParser::next_string()` did not consume token
2029

2130
## [0.5.0] - 2023-03-01
2231
### Added
23-
- Parser for parsing simple rust structures
32+
- `TokenParser` for parsing simple rust structures
2433
- `assert_tokens!` macro
2534
- `TokenTreeExt::into_<token>()` functions
2635
- `Delimited` trait for accessing `TokenTree::Group`

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ include = ["src/**/*", "LICENSE-*", "README.md", "CHANGELOG.md"]
1414

1515
[dependencies]
1616
proc-macro2 = { version = "1", optional = true }
17-
smallvec = { version = "1", optional = true }
17+
quote = { version = "1", optional = true }
18+
smallvec = { version = "1.4", optional = true, features = ["const_generics"] }
1819

1920
[features]
20-
default = ["proc-macro2", "proc-macro", "parser"]
21+
default = ["proc-macro2", "proc-macro", "parser", "quote"]
2122
proc-macro = []
2223
parser = ["smallvec", "proc-macro2"]
23-
proc-macro2 = ["dep:proc-macro2"]
2424

2525
[dev-dependencies]
2626
quote = "1"

src/assert.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ macro_rules! assert_tokens {
2222
#[allow(clippy::module_name_repetitions)]
2323
macro_rules! assert_tokens {
2424
($lhs:expr, {$($rhs:tt)*}) => {
25-
let mut lhs = $crate::TokenParser::new($lhs);
25+
let mut lhs = $crate::TokenParser::new_generic::<3, _, _>($lhs);
2626
assert_tokens!(@O lhs, "", $($rhs)*);
2727
};
2828
(@E $prefix:expr, $expected:tt, $found:tt) => {
@@ -33,7 +33,7 @@ macro_rules! assert_tokens {
3333
};
3434
(@G $lhs:ident, $fn:ident, $aggr:expr, $sym:literal, $group:tt, {$($inner:tt)*}, $($rhs:tt)*) => {
3535
if let Some(lhs) = $lhs.$fn() {
36-
let mut lhs = $crate::TokenParser::from(lhs);
36+
let mut lhs = $crate::TokenParser::<_, 3>::from(lhs);
3737
assert_tokens!(@O lhs, concat!($aggr, ' ', $sym), $($inner)*);
3838
} else if let Some(lhs) = $lhs.next() {
3939
assert_tokens!(@E $aggr, ($group), lhs);

src/lib.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
//!
33
//! E.g. [pushing tokens onto `TokenStream`](TokenStreamExt::push) and [testing
44
//! for specific punctuation on `TokenTree` and Punct](TokenTreePunct)
5-
#![warn(clippy::pedantic)]
6-
#![deny(missing_docs)]
5+
#![warn(clippy::pedantic, missing_docs)]
76
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
87

98
#[cfg(feature = "proc-macro")]
@@ -36,18 +35,18 @@ macro_rules! attr {
3635
}
3736

3837
macro_rules! trait_def {
39-
($item_attr:tt, $trait:ident, $($fn_attr:tt, $fn:ident, $args:tt, $($ret:ty)?),*) => {
38+
($item_attr:tt, $trait:ident, $($fn_attr:tt, $fn:ident, $({$($gen:tt)*})?, $args:tt, $($ret:ty)?),*) => {
4039
attr!($item_attr,
4140
pub trait $trait {
42-
$(attr!($fn_attr, fn $fn $args $(-> $ret)?;);)*
41+
$(attr!($fn_attr, fn $fn $($($gen)*)? $args $(-> $ret)?;);)*
4342
});
4443
};
4544
}
4645

4746
macro_rules! trait_impl {
48-
($trait:ident, $type:ident, $($fn_attr:tt, $fn:ident, $args:tt, $($ret:ty)?, $stmts:tt),*) => {
47+
($trait:ident, $type:ident, $($fn_attr:tt, $fn:ident, $({$($gen:tt)*})?, $args:tt, $($ret:ty)?, $stmts:tt),*) => {
4948
impl $trait for $type {
50-
$(attr!($fn_attr, fn $fn $args $(-> $ret)? $stmts);)*
49+
$(attr!($fn_attr, fn $fn $($($gen)*)? $args $(-> $ret)? $stmts);)*
5150
}
5251
};
5352
}
@@ -60,16 +59,16 @@ macro_rules! impl_via_trait {
6059
fn $fn:ident $args:tt $(-> $ret:ty)? { $($stmts:tt)* })*
6160
}
6261
)+) => {
63-
once!($((trait_def!(($($trait_attr)*), $trait, $(($($fn_attr)*), $fn, $args, $($ret)?),*);))+);
62+
once!($((trait_def!(($($trait_attr)*), $trait, $(($($fn_attr)*), $fn,, $args, $($ret)?),*);))+);
6463
#[cfg(feature = "proc-macro")]
6564
const _: () = {
6665
use proc_macro::*;
67-
$(trait_impl!($trait, $type, $(($($fn_attr)*), $fn, $args, $($ret)?, {$($stmts)*}),*);)+
66+
$(trait_impl!($trait, $type, $(($($fn_attr)*), $fn,, $args, $($ret)?, {$($stmts)*}),*);)+
6867
};
6968
#[cfg(feature = "proc-macro2")]
7069
const _:() = {
7170
use proc_macro2::*;
72-
$(trait_impl!($trait, $type, $(($($fn_attr)*), $fn, $args, $($ret)?, {$($stmts)*}),*);)+
71+
$(trait_impl!($trait, $type, $(($($fn_attr)*), $fn,, $args, $($ret)?, {$($stmts)*}),*);)+
7372
};
7473
};
7574
(
@@ -78,7 +77,7 @@ macro_rules! impl_via_trait {
7877
$(#$trait_attr:tt)*
7978
impl $trait:ident$($doc:literal)?, $trait2:ident$($doc2:literal)? for $type:ident {
8079
$($(#$fn_attr:tt)*
81-
fn $fn:ident $args:tt $(-> $ret:ty)? { $($stmts:tt)* })*
80+
fn $fn:ident $({$($gen:tt)*})? ($($args:tt)*) $(-> $ret:ty)? { $($stmts:tt)* })*
8281
}
8382
)+
8483
}
@@ -88,16 +87,16 @@ macro_rules! impl_via_trait {
8887
#[cfg(feature = "proc-macro")]
8988
mod $mod {
9089
use proc_macro::*;
91-
once!($((trait_def!(($($trait_attr)* $([doc=$doc])?), $trait, $(($($fn_attr)*), $fn, $args, $($ret)?),*);))+);
92-
$(trait_impl!($trait, $type, $(($($fn_attr)*), $fn, $args, $($ret)?, {$($stmts)*}),*);)+
90+
once!($((trait_def!(($($trait_attr)* $([doc=$doc])?), $trait, $(($($fn_attr)*), $fn, $({$($gen)*})?, ($($args)*), $($ret)?),*);))+);
91+
$(trait_impl!($trait, $type, $(($($fn_attr)*), $fn, $({$($gen)*})?, ($($args)*), $($ret)?, {$($stmts)*}),*);)+
9392
}
9493
#[cfg(feature = "proc-macro2")]
9594
once!(($(pub use $mod2::$trait2;)+));
9695
#[cfg(feature = "proc-macro2")]
9796
mod $mod2 {
9897
use proc_macro2::*;
99-
once!($((trait_def!(($($trait_attr)*$([doc=$doc2])?), $trait2, $(($($fn_attr)*), $fn, $args, $($ret)?),*);))+);
100-
$(trait_impl!($trait2, $type, $(($($fn_attr)*), $fn, $args, $($ret)?, {$($stmts)*}),*);)+
98+
once!($((trait_def!(($($trait_attr)*$([doc=$doc2])?), $trait2, $(($($fn_attr)*), $fn, $({$($gen)*})?, ($($args)*), $($ret)?),*);))+);
99+
$(trait_impl!($trait2, $type, $(($($fn_attr)*), $fn, $({$($gen)*})?, ($($args)*), $($ret)?, {$($stmts)*}),*);)+
101100
}
102101
};
103102
}
@@ -106,16 +105,25 @@ impl_via_trait! {
106105
mod token_stream_ext, token_stream2_ext {
107106
/// Generic extensions for
108107
impl TokenStreamExt "[`proc_macro::TokenStream`]", TokenStream2Ext "[`proc_macro2::TokenStream`]" for TokenStream {
109-
/// Pushes a single [`TokenTree`] onto the token stream
108+
/// Pushes a single [`TokenTree`] onto the token stream.
110109
fn push(&mut self, token: TokenTree) {
111110
self.extend(std::iter::once(token))
112111
}
113-
/// Creates a [`TokenParser`](crate::TokenParser) from this token stream
112+
/// Creates a [`TokenParser`](crate::TokenParser) from this token stream.
114113
#[cfg(feature = "parser")]
115114
fn parser(self) -> crate::TokenParser<proc_macro2::token_stream::IntoIter> {
116115
#[allow(clippy::useless_conversion)]
117116
proc_macro2::TokenStream::from(self).into()
118117
}
118+
119+
/// Creates a [`TokenParser`](crate::TokenParser) from this token stream.
120+
///
121+
/// Allows to specify the length of the [peeker buffer](crate::TokenParser#peeking).
122+
#[cfg(feature = "parser")]
123+
fn parser_generic{<const PEEKER_LEN: usize>}(self) -> crate::TokenParser<proc_macro2::token_stream::IntoIter, PEEKER_LEN> {
124+
#[allow(clippy::useless_conversion)]
125+
proc_macro2::TokenStream::from(self).into()
126+
}
119127
}
120128
}
121129
}

0 commit comments

Comments
 (0)