pastey-0.1.1/.cargo_vcs_info.json0000644000000001360000000000100123400ustar { "git": { "sha1": "d9aabae3fb80c0f74940677dadd930f6bffe7496" }, "path_in_vcs": "" }pastey-0.1.1/.github/FUNDING.yml000064400000000000000000000000201046102023000142750ustar 00000000000000github: AS1100K pastey-0.1.1/.github/dependabot.yml000064400000000000000000000010171046102023000153170ustar 00000000000000# To get started with Dependabot version updates, you'll need to specify which # package ecosystems to update and where the package manifests are located. # Please see the documentation for all configuration options: # https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file version: 2 updates: - package-ecosystem: "cargo" # See documentation for possible values directory: "/" # Location of package manifests schedule: interval: "weekly" pastey-0.1.1/.github/release-butler.toml000064400000000000000000000003771046102023000163070ustar 00000000000000version = 1 default_branch = "master" [issues_meta_data] allowed_authors = ["AS1100K"] [packages.pastey] path = "" changelog_file = "CHANGELOG.md" pre_release_changelog_file = "PRERELEASE-CHANGELOG.md" create_gh_release = true package_manager = "cargo"pastey-0.1.1/.github/workflows/ci.yml000064400000000000000000000050431046102023000156450ustar 00000000000000name: CI on: push: pull_request: workflow_dispatch: schedule: [cron: "40 1 * * *"] permissions: contents: read env: RUSTFLAGS: -Dwarnings jobs: pre_ci: uses: dtolnay/.github/.github/workflows/pre_ci.yml@master test: name: Rust ${{matrix.rust}} needs: pre_ci if: needs.pre_ci.outputs.continue runs-on: ubuntu-latest strategy: fail-fast: false matrix: rust: [nightly, beta, stable, 1.56.0] timeout-minutes: 45 steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master with: toolchain: ${{matrix.rust}} - name: Enable type layout randomization run: echo RUSTFLAGS=${RUSTFLAGS}\ -Zrandomize-layout >> $GITHUB_ENV if: matrix.rust == 'nightly' - run: ./test.sh ${{matrix.rust}} - uses: actions/upload-artifact@v4 if: matrix.rust == 'nightly' && always() with: name: Cargo.lock path: Cargo.lock msrv: name: Rust 1.54 needs: pre_ci if: needs.pre_ci.outputs.continue runs-on: ubuntu-latest timeout-minutes: 45 steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@1.54 - run: cargo check doc: name: Documentation needs: pre_ci if: needs.pre_ci.outputs.continue runs-on: ubuntu-latest timeout-minutes: 45 env: RUSTDOCFLAGS: -Dwarnings steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@nightly - uses: dtolnay/install@cargo-docs-rs - run: cargo docs-rs clippy: name: Clippy runs-on: ubuntu-latest if: github.event_name != 'pull_request' timeout-minutes: 45 steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@clippy - run: cargo clippy --tests -- -Dclippy::all -Dclippy::pedantic miri: name: Miri needs: pre_ci if: needs.pre_ci.outputs.continue runs-on: ubuntu-latest timeout-minutes: 45 env: MIRIFLAGS: -Zmiri-strict-provenance steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@miri - run: cd ./pastey-test-suite && cargo miri setup && cargo miri test && cd ../ - run: cd ./paste-compat && cargo miri setup && cargo miri test && cd ../ outdated: name: Outdated runs-on: ubuntu-latest if: github.event_name != 'pull_request' timeout-minutes: 45 steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable - uses: dtolnay/install@cargo-outdated - run: cargo outdated --workspace --exit-code 1 pastey-0.1.1/.github/workflows/release.yml000064400000000000000000000007361046102023000166760ustar 00000000000000on: push: # Pattern matched against refs/tags tags: - "v*" permissions: contents: write jobs: publish: name: Publish runs-on: ubuntu-latest steps: - name: Checkout sources uses: actions/checkout@v4 - name: Install Rust Toolchain uses: rs-workspace/rust-toolchain@v0.2.0 - name: Publish Crate run: cargo publish --token ${CRATES_TOKEN} env: CRATES_TOKEN: ${{ secrets.CRATES_TOKEN }}pastey-0.1.1/.gitignore000064400000000000000000000000351046102023000131160ustar 00000000000000target **/*.rs.bk Cargo.lock pastey-0.1.1/.vscode/settings.json000064400000000000000000000001471046102023000152260ustar 00000000000000{ "rust-analyzer.linkedProjects": ["./paste-compat/Cargo.toml", "./pastey-test-suite/Cargo.toml"] }pastey-0.1.1/CHANGELOG.md000064400000000000000000000015041046102023000127410ustar 00000000000000# Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [0.1.1] - 2025-08-12 ### Removed - `build.rs` and inline literal parsing logic [#16](https://github.com/AS1100K/pastey/pull/16) ## [0.1.0] - 2025-03-12 ### Added - Raw Mode in `paste!` macro [#8](https://github.com/AS1100K/pastey/pull/8) ## [v0.0.1] ### Added - `lower_camel` case conversion modifier [#4](https://github.com/AS1100K/pastey/issues/4) - `upper_camel` case conversion modifier, similar to `camel` - `camel_edge` case coversion modifer [#3](https://github.com/AS1100K/pastey/issues/3) - Internal crate `paste-compat` for testing behaviour against paste crate pastey-0.1.1/CONTRIBUTING.md000064400000000000000000000033031046102023000133600ustar 00000000000000# Contributing to Pastey Thank you for your interest in contributing to Pastey! Here are some guidelines to help you get started. ## Project Goals One of our main goals is to ensure that Pastey is a drop-in replacement for the paste crate. It should not change the behavior or names of existing modifiers. For new behavior, please create a new modifier. For this we have a special test crate named [paste-compat](./paste-compat/), it is always appreciated to add more test cases here that works on paste crate. ## Testing Run `./test.sh` for testing your code ## How to Contribute 1. **Fork the repository**: Click the "Fork" button at the top right of the repository page. 2. **Clone your fork**: ```sh git clone https://github.com/your-username/pastey.git cd pastey ``` 3. **Create a new branch**: ```sh git checkout -b my-feature-branch ``` 4. **Make your changes**: Implement your feature or bug fix. 5. **Commit your changes**: ```sh git commit -am 'Add new feature' ``` 6. **Push to your branch**: ```sh git push origin my-feature-branch ``` 7. **Create a Pull Request**: Go to the repository on GitHub and click "New Pull Request". ## Code Style Please follow the existing code style and conventions. Ensure your code is well-documented and tested. ## Reporting Issues If you find a bug or have a feature request, please create an issue on GitHub. Provide as much detail as possible to help us understand and address the issue. ## License By contributing to Pastey, you agree that your contributions will be licensed under the same license as the project: either the Apache License, Version 2.0 or the MIT license. Thank you for your contributions! pastey-0.1.1/Cargo.lock0000644000000002260000000000100103130ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "pastey" version = "0.1.1" pastey-0.1.1/Cargo.toml0000644000000022650000000000100103430ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" rust-version = "1.54" name = "pastey" version = "0.1.1" authors = [ "Aditya Kumar ", "David Tolnay ", ] build = false autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "Macros for all your token pasting needs. Successor of paste." readme = "README.md" keywords = ["macros"] categories = [ "development-tools", "no-std", "no-std::no-alloc", ] license = "MIT OR Apache-2.0" repository = "https://github.com/as1100k/pastey" [package.metadata.docs.rs] rustdoc-args = ["--generate-link-to-definition"] targets = ["x86_64-unknown-linux-gnu"] [lib] name = "pastey" path = "src/lib.rs" proc-macro = true pastey-0.1.1/Cargo.toml.orig000064400000000000000000000010601046102023000140140ustar 00000000000000[package] name = "pastey" version = "0.1.1" authors = [ "Aditya Kumar ", "David Tolnay ", ] categories = ["development-tools", "no-std", "no-std::no-alloc"] description = "Macros for all your token pasting needs. Successor of paste." edition = "2018" keywords = ["macros"] license = "MIT OR Apache-2.0" repository = "https://github.com/as1100k/pastey" rust-version = "1.54" [lib] proc-macro = true [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] rustdoc-args = ["--generate-link-to-definition"] pastey-0.1.1/LICENSE-APACHE000064400000000000000000000227731046102023000130670ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS pastey-0.1.1/LICENSE-MIT000064400000000000000000000017771046102023000126000ustar 00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pastey-0.1.1/README.md000064400000000000000000000147621046102023000124210ustar 00000000000000Macros for all your token pasting needs ======================================= [github](https://github.com/as1100k/pastey) [crates.io](https://crates.io/crates/pastey) [docs.rs](https://docs.rs/pastey) [build status](https://github.com/as1100k/pastey/actions?query=branch%master) **_`pastey` is the fork of `paste` and is aimed to be a drop-in replacement with additional features for `paste` crate_**
Migrating from paste crate Migrating from `paste` crate to `pastey` is super simple, just change the following in your `Cargo.toml` ```diff [dependencies] - paste = "1" + pastey = "*" # Or any specific version of pastey ``` Or even better way: ```diff [dependencies] - paste = "1" + paste = { package = "pastey", version = "*" } ```

## Quick Start Add `pastey` as your dependency in `Cargo.toml` ```toml [dependencies] # TODO: Replace with latest version available on crates.io pastey = "*" ``` This approach works with any Rust compiler 1.54+. ## Pasting identifiers Within the `paste!` macro, identifiers inside `[<`...`>]` are pasted together to form a single identifier. ```rust use pastey::paste; paste! { // Defines a const called `QRST`. const []: &str = "success!"; } fn main() { assert_eq!( paste! { [].len() }, 8, ); } ```
## More elaborate example The next example shows a macro that generates accessor methods for some struct fields. It demonstrates how you might find it useful to bundle a paste invocation inside of a macro\_rules macro. ```rust use pastey::paste; macro_rules! make_a_struct_and_getters { ($name:ident { $($field:ident),* }) => { // Define a struct. This expands to: // // pub struct S { // a: String, // b: String, // c: String, // } pub struct $name { $( $field: String, )* } // Build an impl block with getters. This expands to: // // impl S { // pub fn get_a(&self) -> &str { &self.a } // pub fn get_b(&self) -> &str { &self.b } // pub fn get_c(&self) -> &str { &self.c } // } paste! { impl $name { $( pub fn [](&self) -> &str { &self.$field } )* } } } } make_a_struct_and_getters!(S { a, b, c }); fn call_some_getters(s: &S) -> bool { s.get_a() == s.get_b() && s.get_c().is_empty() } ```
## Case conversion The `pastey` crate supports the following case modfiers: | Modifier | Description | |------------------------------------|---------------------------------------| | `$var:lower` | Lower Case | | `$var:upper` | Upper Case | | `$var:snake` | [Snake Case] | | `$var:camel` or `$var:upper_camel` | Upper Camel Case | | `$var:lower_camel` | Lower Camel Case [#4] | | `$var:camel_edge` | Covers Edge cases of Camel Case. [#3] | _**NOTE: The pastey crate is going to be a drop in replacement to paste crate, and will not change the behaviour of existing modifier like `lower`, `upper`, `snake` and `camel`. For modifying the behaviour new modifiers will be created, like `camel_edge`**_ You can also use multiple of these modifers like `$var:snake:upper` would give you `SCREAMING_SNAKE_CASE`. Example ```rust use pastey::paste; paste! { const []: &str = "libpastey"; let _ = LIBPASTEY; } ``` The precise Unicode conversions are as defined by [`str::to_lowercase`] and [`str::to_uppercase`]. [#3]: https://github.com/AS1100K/pastey/issues/3 [#4]: https://github.com/AS1100K/pastey/issues/4 [`str::to_lowercase`]: https://doc.rust-lang.org/std/primitive.str.html#method.to_lowercase [`str::to_uppercase`]: https://doc.rust-lang.org/std/primitive.str.html#method.to_uppercase
## Raw Identifier Generation `pastey` now supports raw identifiers using a special raw mode. By prefixing a token with `#` inside the paste syntax, it treats that token as a raw identifier. ```rust use pastey::paste; macro_rules! define_struct_and_impl { ($name:ident $(- $name_tail:ident)*) => { paste!{ struct [< # $name:camel $( $name_tail)* >]; // '#' signals a raw identifier impl [< # $name:camel $( $name_tail)* >] { fn [< # $name:snake $( _ $name_tail:snake)* >]() {} } } } } define_struct_and_impl!(loop); define_struct_and_impl!(loop - xyz); fn test_fn() { let _ = Loop::r#loop(); let _ = Loopxyz::loop_xyz(); } ```
## Pasting documentation strings Within the `paste!` macro, arguments to a #\[doc ...\] attribute are implicitly concatenated together to form a coherent documentation string. ```rust use pastey::paste; macro_rules! method_new { ($ret:ident) => { paste! { #[doc = "Create a new `" $ret "` object."] pub fn new() -> $ret { todo!() } } }; } pub struct Pastey {} method_new!(Pastey); // expands to #[doc = "Create a new `Paste` object"] ```
#### Credits This crate is the fork of paste and I appreciate the efforts of @dtolnay and other contributors. #### License Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. pastey-0.1.1/src/attr.rs000064400000000000000000000130321046102023000132360ustar 00000000000000use crate::error::Result; use crate::segment::{self, Segment}; use proc_macro::{Delimiter, Group, Spacing, Span, TokenStream, TokenTree}; use std::iter; use std::mem; use std::str::FromStr; pub fn expand_attr( attr: TokenStream, span: Span, contains_paste: &mut bool, ) -> Result { let mut tokens = attr.clone().into_iter(); let mut leading_colons = 0; // $(::)? let mut leading_path = 0; // $($ident)::+ let mut token; let group = loop { token = tokens.next(); match token { // colon after `$(:)?` Some(TokenTree::Punct(ref punct)) if punct.as_char() == ':' && leading_colons < 2 && leading_path == 0 => { leading_colons += 1; } // ident after `$(::)? $($ident ::)*` Some(TokenTree::Ident(_)) if leading_colons != 1 && leading_path % 3 == 0 => { leading_path += 1; } // colon after `$(::)? $($ident ::)* $ident $(:)?` Some(TokenTree::Punct(ref punct)) if punct.as_char() == ':' && leading_path % 3 > 0 => { leading_path += 1; } // eq+value after `$(::)? $($ident)::+` Some(TokenTree::Punct(ref punct)) if punct.as_char() == '=' && leading_path % 3 == 1 => { let mut count = 0; if tokens.inspect(|_| count += 1).all(|tt| is_stringlike(&tt)) && count > 1 { *contains_paste = true; let leading = leading_colons + leading_path; return do_paste_name_value_attr(attr, span, leading); } return Ok(attr); } // parens after `$(::)? $($ident)::+` Some(TokenTree::Group(ref group)) if group.delimiter() == Delimiter::Parenthesis && leading_path % 3 == 1 => { break group; } // bail out _ => return Ok(attr), } }; // There can't be anything else after the first group in a valid attribute. if tokens.next().is_some() { return Ok(attr); } let mut group_contains_paste = false; let mut expanded = TokenStream::new(); let mut nested_attr = TokenStream::new(); for tt in group.stream() { match &tt { TokenTree::Punct(punct) if punct.as_char() == ',' => { expanded.extend(expand_attr( nested_attr, group.span(), &mut group_contains_paste, )?); expanded.extend(iter::once(tt)); nested_attr = TokenStream::new(); } _ => nested_attr.extend(iter::once(tt)), } } if !nested_attr.is_empty() { expanded.extend(expand_attr( nested_attr, group.span(), &mut group_contains_paste, )?); } if group_contains_paste { *contains_paste = true; let mut group = Group::new(Delimiter::Parenthesis, expanded); group.set_span(span); Ok(attr .into_iter() // Just keep the initial ident in `#[ident(...)]`. .take(leading_colons + leading_path) .chain(iter::once(TokenTree::Group(group))) .collect()) } else { Ok(attr) } } fn do_paste_name_value_attr(attr: TokenStream, span: Span, leading: usize) -> Result { let mut expanded = TokenStream::new(); let mut tokens = attr.into_iter().peekable(); expanded.extend(tokens.by_ref().take(leading + 1)); // `doc =` let mut segments = segment::parse(&mut tokens)?; for segment in &mut segments { if let Segment::String(string) = segment { if let Some(open_quote) = string.value.find('"') { if open_quote == 0 { string.value.truncate(string.value.len() - 1); string.value.remove(0); } else { let begin = open_quote + 1; let end = string.value.rfind('"').unwrap(); let raw_string = mem::take(&mut string.value); for ch in raw_string[begin..end].chars() { string.value.extend(ch.escape_default()); } } } } } let mut lit = segment::paste(&segments)?; if lit.starts_with("r#") { // Raw mode doesn't have any impact when using in attribute lit.remove(0); lit.remove(0); } lit.insert(0, '"'); lit.push('"'); let mut lit = TokenStream::from_str(&lit) .unwrap() .into_iter() .next() .unwrap(); lit.set_span(span); expanded.extend(iter::once(lit)); Ok(expanded) } fn is_stringlike(token: &TokenTree) -> bool { match token { TokenTree::Ident(_) => true, TokenTree::Literal(literal) => { let repr = literal.to_string(); !repr.starts_with('b') && !repr.starts_with('\'') } TokenTree::Group(group) => { if group.delimiter() != Delimiter::None { return false; } let mut inner = group.stream().into_iter(); match inner.next() { Some(first) => inner.next().is_none() && is_stringlike(&first), None => false, } } TokenTree::Punct(punct) => { punct.as_char() == '\'' || punct.as_char() == ':' && punct.spacing() == Spacing::Alone } } } pastey-0.1.1/src/error.rs000064400000000000000000000025061046102023000134210ustar 00000000000000use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree}; use std::iter::FromIterator; pub type Result = std::result::Result; pub struct Error { begin: Span, end: Span, msg: String, } impl Error { pub fn new(span: Span, msg: &str) -> Self { Self::new2(span, span, msg) } pub fn new2(begin: Span, end: Span, msg: &str) -> Self { Error { begin, end, msg: msg.to_owned(), } } pub fn to_compile_error(&self) -> TokenStream { // compile_error! { $msg } TokenStream::from_iter(vec![ TokenTree::Ident(Ident::new("compile_error", self.begin)), TokenTree::Punct({ let mut punct = Punct::new('!', Spacing::Alone); punct.set_span(self.begin); punct }), TokenTree::Group({ let mut group = Group::new(Delimiter::Brace, { TokenStream::from_iter(vec![TokenTree::Literal({ let mut string = Literal::string(&self.msg); string.set_span(self.end); string })]) }); group.set_span(self.end); group }), ]) } } pastey-0.1.1/src/lib.rs000064400000000000000000000251221046102023000130350ustar 00000000000000#![doc = include_str!("../README.md")] #![allow( clippy::derive_partial_eq_without_eq, clippy::doc_markdown, clippy::match_same_arms, clippy::module_name_repetitions, clippy::needless_doctest_main, clippy::too_many_lines )] extern crate proc_macro; mod attr; mod error; mod segment; use crate::attr::expand_attr; use crate::error::{Error, Result}; use crate::segment::Segment; use proc_macro::{ Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree, }; use std::char; use std::iter; use std::panic; use std::str::FromStr; #[proc_macro] pub fn paste(input: TokenStream) -> TokenStream { let mut contains_paste = false; let flatten_single_interpolation = true; match expand( input.clone(), &mut contains_paste, flatten_single_interpolation, ) { Ok(expanded) => { if contains_paste { expanded } else { input } } Err(err) => err.to_compile_error(), } } #[doc(hidden)] #[proc_macro] pub fn item(input: TokenStream) -> TokenStream { paste(input) } #[doc(hidden)] #[proc_macro] pub fn expr(input: TokenStream) -> TokenStream { paste(input) } fn expand( input: TokenStream, contains_paste: &mut bool, flatten_single_interpolation: bool, ) -> Result { let mut expanded = TokenStream::new(); let mut lookbehind = Lookbehind::Other; let mut prev_none_group = None::; let mut tokens = input.into_iter().peekable(); loop { let token = tokens.next(); if let Some(group) = prev_none_group.take() { if match (&token, tokens.peek()) { (Some(TokenTree::Punct(fst)), Some(TokenTree::Punct(snd))) => { fst.as_char() == ':' && snd.as_char() == ':' && fst.spacing() == Spacing::Joint } _ => false, } { expanded.extend(group.stream()); *contains_paste = true; } else { expanded.extend(iter::once(TokenTree::Group(group))); } } match token { Some(TokenTree::Group(group)) => { let delimiter = group.delimiter(); let content = group.stream(); let span = group.span(); if delimiter == Delimiter::Bracket && is_paste_operation(&content) { let segments = parse_bracket_as_segments(content, span)?; let pasted = segment::paste(&segments)?; let tokens = pasted_to_tokens(pasted, span)?; expanded.extend(tokens); *contains_paste = true; } else if flatten_single_interpolation && delimiter == Delimiter::None && is_single_interpolation_group(&content) { expanded.extend(content); *contains_paste = true; } else { let mut group_contains_paste = false; let is_attribute = delimiter == Delimiter::Bracket && (lookbehind == Lookbehind::Pound || lookbehind == Lookbehind::PoundBang); let mut nested = expand( content, &mut group_contains_paste, flatten_single_interpolation && !is_attribute, )?; if is_attribute { nested = expand_attr(nested, span, &mut group_contains_paste)?; } let group = if group_contains_paste { let mut group = Group::new(delimiter, nested); group.set_span(span); *contains_paste = true; group } else { group.clone() }; if delimiter != Delimiter::None { expanded.extend(iter::once(TokenTree::Group(group))); } else if lookbehind == Lookbehind::DoubleColon { expanded.extend(group.stream()); *contains_paste = true; } else { prev_none_group = Some(group); } } lookbehind = Lookbehind::Other; } Some(TokenTree::Punct(punct)) => { lookbehind = match punct.as_char() { ':' if lookbehind == Lookbehind::JointColon => Lookbehind::DoubleColon, ':' if punct.spacing() == Spacing::Joint => Lookbehind::JointColon, '#' => Lookbehind::Pound, '!' if lookbehind == Lookbehind::Pound => Lookbehind::PoundBang, _ => Lookbehind::Other, }; expanded.extend(iter::once(TokenTree::Punct(punct))); } Some(other) => { lookbehind = Lookbehind::Other; expanded.extend(iter::once(other)); } None => return Ok(expanded), } } } #[derive(PartialEq)] enum Lookbehind { JointColon, DoubleColon, Pound, PoundBang, Other, } // https://github.com/dtolnay/paste/issues/26 fn is_single_interpolation_group(input: &TokenStream) -> bool { #[derive(PartialEq)] enum State { Init, Ident, Literal, Apostrophe, Lifetime, Colon1, Colon2, } let mut state = State::Init; for tt in input.clone() { state = match (state, &tt) { (State::Init, TokenTree::Ident(_)) => State::Ident, (State::Init, TokenTree::Literal(_)) => State::Literal, (State::Init, TokenTree::Punct(punct)) if punct.as_char() == '\'' => State::Apostrophe, (State::Apostrophe, TokenTree::Ident(_)) => State::Lifetime, (State::Ident, TokenTree::Punct(punct)) if punct.as_char() == ':' && punct.spacing() == Spacing::Joint => { State::Colon1 } (State::Colon1, TokenTree::Punct(punct)) if punct.as_char() == ':' && punct.spacing() == Spacing::Alone => { State::Colon2 } (State::Colon2, TokenTree::Ident(_)) => State::Ident, _ => return false, }; } state == State::Ident || state == State::Literal || state == State::Lifetime } fn is_paste_operation(input: &TokenStream) -> bool { let mut tokens = input.clone().into_iter(); match &tokens.next() { Some(TokenTree::Punct(punct)) if punct.as_char() == '<' => {} _ => return false, } let mut has_token = false; loop { match &tokens.next() { Some(TokenTree::Punct(punct)) if punct.as_char() == '>' => { return has_token && tokens.next().is_none(); } Some(_) => has_token = true, None => return false, } } } fn parse_bracket_as_segments(input: TokenStream, scope: Span) -> Result> { let mut tokens = input.into_iter().peekable(); match &tokens.next() { Some(TokenTree::Punct(punct)) if punct.as_char() == '<' => {} Some(wrong) => return Err(Error::new(wrong.span(), "expected `<`")), None => return Err(Error::new(scope, "expected `[< ... >]`")), } let mut segments = segment::parse(&mut tokens)?; match &tokens.next() { Some(TokenTree::Punct(punct)) if punct.as_char() == '>' => {} Some(wrong) => return Err(Error::new(wrong.span(), "expected `>`")), None => return Err(Error::new(scope, "expected `[< ... >]`")), } if let Some(unexpected) = tokens.next() { return Err(Error::new( unexpected.span(), "unexpected input, expected `[< ... >]`", )); } for segment in &mut segments { if let Segment::String(string) = segment { if string.value.starts_with("'\\u{") { let hex = &string.value[4..string.value.len() - 2]; if let Ok(unsigned) = u32::from_str_radix(hex, 16) { if let Some(ch) = char::from_u32(unsigned) { string.value.clear(); string.value.push(ch); continue; } } } if string.value.contains(&['\\', '.', '+'][..]) || string.value.starts_with("b'") || string.value.starts_with("b\"") || string.value.starts_with("br\"") { return Err(Error::new(string.span, "unsupported literal")); } let mut range = 0..string.value.len(); if string.value.starts_with("r\"") { range.start += 2; range.end -= 1; } else if string.value.starts_with(&['"', '\''][..]) { range.start += 1; range.end -= 1; } string.value = string.value[range].replace('-', "_"); } } Ok(segments) } fn pasted_to_tokens(mut pasted: String, span: Span) -> Result { let mut raw_mode = false; let mut tokens = TokenStream::new(); if pasted.starts_with(|ch: char| ch.is_ascii_digit()) { let literal = match panic::catch_unwind(|| Literal::from_str(&pasted)) { Ok(Ok(literal)) => TokenTree::Literal(literal), Ok(Err(LexError { .. })) | Err(_) => { return Err(Error::new( span, &format!("`{:?}` is not a valid literal", pasted), )); } }; tokens.extend(iter::once(literal)); return Ok(tokens); } if pasted.starts_with('\'') { let mut apostrophe = TokenTree::Punct(Punct::new('\'', Spacing::Joint)); apostrophe.set_span(span); tokens.extend(iter::once(apostrophe)); pasted.remove(0); } if pasted.starts_with("r#") { raw_mode = true; } let ident = match panic::catch_unwind(|| { if raw_mode { let mut spasted = pasted.clone(); spasted.remove(0); spasted.remove(0); Ident::new_raw(&spasted, span) } else { Ident::new(&pasted, span) } }) { Ok(ident) => TokenTree::Ident(ident), Err(_) => { return Err(Error::new( span, &format!("`{:?}` is not a valid identifier", pasted), )); } }; tokens.extend(iter::once(ident)); Ok(tokens) } pastey-0.1.1/src/segment.rs000064400000000000000000000264411046102023000137360ustar 00000000000000use crate::error::{Error, Result}; use proc_macro::{token_stream, Delimiter, Ident, Span, TokenTree}; use std::iter::Peekable; pub(crate) enum Segment { String(LitStr), Apostrophe(Span), Env(LitStr), Modifier(Colon, Ident), } pub(crate) struct LitStr { pub value: String, pub span: Span, } pub(crate) struct Colon { pub span: Span, } pub(crate) fn parse(tokens: &mut Peekable) -> Result> { let mut segments = Vec::new(); while match tokens.peek() { None => false, Some(TokenTree::Punct(punct)) => punct.as_char() != '>', Some(_) => true, } { match tokens.next().unwrap() { TokenTree::Ident(ident) => { let mut fragment = ident.to_string(); if fragment.starts_with("r#") { fragment = fragment.split_off(2); } if fragment == "env" && match tokens.peek() { Some(TokenTree::Punct(punct)) => punct.as_char() == '!', _ => false, } { let bang = tokens.next().unwrap(); // `!` let expect_group = tokens.next(); let parenthesized = match &expect_group { Some(TokenTree::Group(group)) if group.delimiter() == Delimiter::Parenthesis => { group } Some(wrong) => return Err(Error::new(wrong.span(), "expected `(`")), None => { return Err(Error::new2( ident.span(), bang.span(), "expected `(` after `env!`", )); } }; let mut inner = parenthesized.stream().into_iter(); let lit = match inner.next() { Some(TokenTree::Literal(lit)) => lit, Some(wrong) => { return Err(Error::new(wrong.span(), "expected string literal")) } None => { return Err(Error::new2( ident.span(), parenthesized.span(), "expected string literal as argument to env! macro", )) } }; let lit_string = lit.to_string(); if lit_string.starts_with('"') && lit_string.ends_with('"') && lit_string.len() >= 2 { // TODO: maybe handle escape sequences in the string if // someone has a use case. segments.push(Segment::Env(LitStr { value: lit_string[1..lit_string.len() - 1].to_owned(), span: lit.span(), })); } else { return Err(Error::new(lit.span(), "expected string literal")); } if let Some(unexpected) = inner.next() { return Err(Error::new( unexpected.span(), "unexpected token in env! macro", )); } } else { segments.push(Segment::String(LitStr { value: fragment, span: ident.span(), })); } } TokenTree::Literal(lit) => { segments.push(Segment::String(LitStr { value: lit.to_string(), span: lit.span(), })); } TokenTree::Punct(punct) => match punct.as_char() { '_' => segments.push(Segment::String(LitStr { value: "_".to_owned(), span: punct.span(), })), '\'' => segments.push(Segment::Apostrophe(punct.span())), ':' => { let colon_span = punct.span(); let colon = Colon { span: colon_span }; let ident = match tokens.next() { Some(TokenTree::Ident(ident)) => ident, wrong => { let span = wrong.as_ref().map_or(colon_span, TokenTree::span); return Err(Error::new(span, "expected identifier after `:`")); } }; segments.push(Segment::Modifier(colon, ident)); } '#' => segments.push(Segment::String(LitStr { value: "#".to_string(), span: punct.span(), })), _ => return Err(Error::new(punct.span(), "unexpected punct")), }, TokenTree::Group(group) => { if group.delimiter() == Delimiter::None { let mut inner = group.stream().into_iter().peekable(); let nested = parse(&mut inner)?; if let Some(unexpected) = inner.next() { return Err(Error::new(unexpected.span(), "unexpected token")); } segments.extend(nested); } else { return Err(Error::new(group.span(), "unexpected token")); } } } } Ok(segments) } pub(crate) fn paste(segments: &[Segment]) -> Result { let mut evaluated = Vec::new(); let mut is_lifetime = false; for (i, segment) in segments.iter().enumerate() { match segment { Segment::String(segment) => { if segment.value.as_str() == "#" { if i == 0 { // Enable Raw mode evaluated.push(String::from("r#")); continue; } return Err(Error::new( segment.span, "`#` is reserved keyword and it enables the raw mode \ (i.e. generate Raw Identifiers) and it is only allowed in \ the beginning like `[< # ... >]`", )); } evaluated.push(segment.value.clone()); } Segment::Apostrophe(span) => { if is_lifetime { return Err(Error::new(*span, "unexpected lifetime")); } is_lifetime = true; } Segment::Env(var) => { let resolved = match std::env::var(&var.value) { Ok(resolved) => resolved, Err(_) => { return Err(Error::new( var.span, &format!("no such env var: {:?}", var.value), )); } }; let resolved = resolved.replace('-', "_"); evaluated.push(resolved); } Segment::Modifier(colon, ident) => { let last = match evaluated.pop() { Some(last) => last, None => { return Err(Error::new2(colon.span, ident.span(), "unexpected modifier")) } }; match ident.to_string().as_str() { "lower" => { evaluated.push(last.to_lowercase()); } "upper" => { evaluated.push(last.to_uppercase()); } "snake" => { let mut acc = String::new(); let mut prev = '_'; for ch in last.chars() { if ch.is_uppercase() && prev != '_' { acc.push('_'); } acc.push(ch); prev = ch; } evaluated.push(acc.to_lowercase()); } "camel" | "upper_camel" | "lower_camel" => { let mut is_lower_camel = ident.to_string().as_str() == "lower_camel"; let mut acc = String::new(); let mut prev = '_'; for ch in last.chars() { if ch != '_' { if prev == '_' { if is_lower_camel { for chl in ch.to_lowercase() { acc.push(chl); } is_lower_camel = false; } else { for chu in ch.to_uppercase() { acc.push(chu); } } } else if prev.is_uppercase() { for chl in ch.to_lowercase() { acc.push(chl); } } else { acc.push(ch); } } prev = ch; } evaluated.push(acc); } "camel_edge" => { let mut acc = String::new(); let mut prev = '_'; for ch in last.chars() { if ch != '_' { if prev == '_' { for chu in ch.to_uppercase() { acc.push(chu); } } else if prev.is_uppercase() { for chl in ch.to_lowercase() { acc.push(chl); } } else { acc.push(ch); } } else if prev == '_' { acc.push(ch); } prev = ch; } evaluated.push(acc); } _ => { return Err(Error::new2( colon.span, ident.span(), "unsupported modifier", )); } } } } } let mut pasted = evaluated.into_iter().collect::(); if is_lifetime { pasted.insert(0, '\''); } Ok(pasted) } pastey-0.1.1/test.sh000075500000000000000000000023621046102023000124510ustar 00000000000000#!/bin/bash set -e # Exit immediately if any command exits with a non-zero status # This is a Temporary Fix for CI fail as CI fails on nightly toolchain due to some formatting issues which I am unable # to reproduce locally. # # For more info, check https://github.com/AS1100K/pastey/pull/15 if [[ "$1" == "nightly" ]]; then cp pastey-test-suite/tests/ui/case-warning.nightly.stderr pastey-test-suite/tests/ui/case-warning.stderr cp pastey-test-suite/tests/ui/raw-mode-wrong-position.nightly.stderr pastey-test-suite/tests/ui/raw-mode-wrong-position.stderr else cp pastey-test-suite/tests/ui/case-warning.stable.stderr pastey-test-suite/tests/ui/case-warning.stderr cp pastey-test-suite/tests/ui/raw-mode-wrong-position.stable.stderr pastey-test-suite/tests/ui/raw-mode-wrong-position.stderr fi echo "========================================" echo "Running tests with pastey crate..." echo "========================================" cargo test echo "========================================" echo "Running tests with pastey-test-suite crate..." echo "========================================" cd pastey-test-suite cargo test rm ./tests/ui/case-warning.stderr rm ./tests/ui/raw-mode-wrong-position.stderr cd ../ cd paste-compat ./test.sh cd ../