aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-02-19 17:16:54 +0100
committerHampusM <hampus@hampusmat.com>2023-02-19 18:24:57 +0100
commit6b6f25357e25a07d234ae683c1b58b1929b8483e (patch)
tree5ff873b64aa21c0961d32c6aee056aca32481e20
parentb3fc15dc216794419c7d97d9f4d7f596c26e9182 (diff)
feat: add project & for_each_opengl_command macro
-rw-r--r--.gitignore2
-rw-r--r--Cargo.toml20
-rw-r--r--LICENSE-APACHE202
-rw-r--r--LICENSE-MIT19
-rw-r--r--rustfmt.toml11
-rw-r--r--src/lib.rs76
-rw-r--r--src/repeat.rs217
-rw-r--r--src/str.rs61
-rw-r--r--src/token_stream.rs263
9 files changed, 871 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4fffb2f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/target
+/Cargo.lock
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..4ec937f
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,20 @@
+[package]
+name = "opengl-registry-macros"
+version = "0.1.0"
+license = "MIT OR Apache-2.0"
+edition = "2021"
+
+[lib]
+proc-macro = true
+
+[dependencies]
+once_cell = "1.17.0"
+proc-macro2 = "1.0.51"
+convert_case = "0.6.0"
+opengl-registry = { git = "https://git.hampusmat.com/opengl-registry-rs" }
+regex = "1.7.1"
+
+[dev-dependencies]
+quote = "1.0.23"
+pretty_assertions = "1.3.0"
+
diff --git a/LICENSE-APACHE b/LICENSE-APACHE
new file mode 100644
index 0000000..14ef2d6
--- /dev/null
+++ b/LICENSE-APACHE
@@ -0,0 +1,202 @@
+ 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
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright 2023 HampusM
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
diff --git a/LICENSE-MIT b/LICENSE-MIT
new file mode 100644
index 0000000..170fea6
--- /dev/null
+++ b/LICENSE-MIT
@@ -0,0 +1,19 @@
+Copyright (c) 2023 HampusM
+
+Permission 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.
diff --git a/rustfmt.toml b/rustfmt.toml
new file mode 100644
index 0000000..4d1e29f
--- /dev/null
+++ b/rustfmt.toml
@@ -0,0 +1,11 @@
+max_width = 90
+brace_style = "AlwaysNextLine"
+group_imports = "StdExternalCrate"
+wrap_comments = true
+comment_width = 90
+format_code_in_doc_comments = true
+imports_layout = "HorizontalVertical"
+imports_granularity = "Module"
+newline_style = "Unix"
+reorder_impl_items = true
+
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..35d975f
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,76 @@
+//! Macros utilizing the [OpenGL API and Extension Registry].
+//!
+//! [OpenGL API and Extension Registry]: https://github.com/KhronosGroup/OpenGL-Registry
+#![deny(clippy::all, clippy::pedantic, missing_docs)]
+use once_cell::sync::Lazy;
+use opengl_registry::Registry;
+use proc_macro::TokenStream;
+
+use crate::repeat::for_each_opengl_command_impl;
+
+mod repeat;
+mod str;
+mod token_stream;
+
+const OPENGL_CMD_IDENT_REPLACE: &str = "_gl_command_";
+const OPENGL_CMD_RET_TYPE_REPLACE: &str = "_gl_command_ret_type_";
+const OPENGL_CMD_ARGS_REPLACE: &str = "_gl_command_args_";
+const OPENGL_CMD_ARG_NAMES_REPLACE: &str = "_gl_command_arg_names_";
+const OPENGL_CMD_ARG_TYPES_REPLACE: &str = "_gl_command_arg_types_";
+
+static OPENGL_REGISTRY: Lazy<Registry> = Lazy::new(|| Registry::retrieve().unwrap());
+
+/// Repeats the input for each command in the [OpenGL API and Extension Registry].
+///
+/// # Replacements
+/// This macro will replace some specific identifiers. These identifiers are to be put
+/// between underscores.
+///
+/// ### The following identifiers will be replaced
+///
+/// **`gl_command`**<br>
+/// Will be replaced by the command name.
+///
+/// **`gl_command_ret_type`**<br>
+/// Will be replaced by the command return type.
+///
+/// **`gl_command_args`**<br>
+/// Will be replaced by the command arguments formatted as `name: type`.
+///
+/// **`gl_command_arg_names`**<br>
+/// Will be replaced by the command argument names.
+///
+/// **`gl_command_arg_types`**<br>
+/// Will be replaced by the command argument types.
+///
+/// # Examples
+/// ```
+/// # use opengl_registry_macros::for_each_opengl_command;
+/// for_each_opengl_command! {
+/// fn _gl_command_()
+/// {
+/// println!("Hello from {}", stringify!(_gl_command_));
+/// }
+/// }
+/// ```
+///
+/// Would expand to the following if the only OpenGL commands were `glCreateShader` and
+/// `glBindBuffer`.
+///
+/// ```
+/// fn glCreateShader()
+/// {
+/// println!("Hello from {}", stringify!(glCreateShader));
+/// }
+/// fn glBindBuffer()
+/// {
+/// println!("Hello from {}", stringify!(glBindBuffer));
+/// }
+/// ```
+///
+/// [OpenGL API and Extension Registry]: https://github.com/KhronosGroup/OpenGL-Registry
+#[proc_macro]
+pub fn for_each_opengl_command(input_stream: TokenStream) -> TokenStream
+{
+ for_each_opengl_command_impl(&input_stream.into(), &OPENGL_REGISTRY).into()
+}
diff --git a/src/repeat.rs b/src/repeat.rs
new file mode 100644
index 0000000..2f55ee8
--- /dev/null
+++ b/src/repeat.rs
@@ -0,0 +1,217 @@
+use std::str::FromStr;
+
+use convert_case::{Case, Casing};
+use opengl_registry::Registry;
+use proc_macro2::{Delimiter, Group, Ident, Span, TokenStream, TokenTree};
+
+use crate::str::c_ptr_to_rust_ptr;
+use crate::token_stream::TokenStreamExt;
+use crate::{
+ OPENGL_CMD_ARGS_REPLACE,
+ OPENGL_CMD_ARG_NAMES_REPLACE,
+ OPENGL_CMD_ARG_TYPES_REPLACE,
+ OPENGL_CMD_IDENT_REPLACE,
+ OPENGL_CMD_RET_TYPE_REPLACE,
+};
+
+pub fn for_each_opengl_command_impl(
+ input_stream: &TokenStream,
+ opengl_registry: &Registry,
+) -> TokenStream
+{
+ let gl_func_ident_index_paths = input_stream
+ .find_all_ident(&Ident::new(OPENGL_CMD_IDENT_REPLACE, Span::call_site()));
+
+ let gl_func_ret_type_index_paths = input_stream
+ .find_all_ident(&Ident::new(OPENGL_CMD_RET_TYPE_REPLACE, Span::call_site()));
+
+ let out: TokenStream = opengl_registry
+ .commands()
+ .iter()
+ .map(|gl_command| {
+ let stream = input_stream.replace_tokens(
+ &gl_func_ident_index_paths,
+ &TokenTree::Ident(Ident::new(
+ gl_command.prototype().name(),
+ Span::call_site(),
+ )),
+ );
+
+ let return_type = gl_command.prototype().return_type();
+
+ let stream = stream.replace_tokens(
+ &gl_func_ret_type_index_paths,
+ &TokenTree::Group(Group::new(
+ Delimiter::None,
+ TokenStream::from_str(&fix_type(return_type)).unwrap(),
+ )),
+ );
+
+ let gl_func_args_paths = stream
+ .find_all_ident(&Ident::new(OPENGL_CMD_ARGS_REPLACE, Span::call_site()));
+
+ let stream = stream.replace_tokens(
+ &gl_func_args_paths,
+ &TokenTree::Group(Group::new(
+ Delimiter::None,
+ TokenStream::from_str(
+ &gl_command
+ .parameters()
+ .iter()
+ .map(|param| {
+ let param_name = clean_param_name(param.name());
+ let param_type = fix_type(param.get_type());
+
+ format!("{param_name}: {param_type}")
+ })
+ .collect::<Vec<_>>()
+ .join(","),
+ )
+ .unwrap(),
+ )),
+ );
+
+ let gl_func_arg_names_paths = stream.find_all_ident(&Ident::new(
+ OPENGL_CMD_ARG_NAMES_REPLACE,
+ Span::call_site(),
+ ));
+
+ let stream = stream.replace_tokens(
+ &gl_func_arg_names_paths,
+ &TokenTree::Group(Group::new(
+ Delimiter::None,
+ TokenStream::from_str(
+ &gl_command
+ .parameters()
+ .iter()
+ .map(|param| clean_param_name(param.name()))
+ .collect::<Vec<_>>()
+ .join(","),
+ )
+ .unwrap(),
+ )),
+ );
+
+ let gl_func_arg_types_paths = stream.find_all_ident(&Ident::new(
+ OPENGL_CMD_ARG_TYPES_REPLACE,
+ Span::call_site(),
+ ));
+
+ stream.replace_tokens(
+ &gl_func_arg_types_paths,
+ &TokenTree::Group(Group::new(
+ Delimiter::None,
+ TokenStream::from_str(
+ &gl_command
+ .parameters()
+ .iter()
+ .map(|param| fix_type(param.get_type()))
+ .collect::<Vec<_>>()
+ .join(","),
+ )
+ .unwrap(),
+ )),
+ )
+ })
+ .collect();
+
+ out
+}
+
+fn clean_param_name(param_name: &str) -> String
+{
+ match param_name {
+ "ref" => "reference",
+ "type" => "ty",
+ "in" => "inside",
+ "box" => "a_box",
+ name => name,
+ }
+ .to_case(Case::Snake)
+}
+
+fn fix_type(ty: &str) -> String
+{
+ let ty = ty.replace("struct", "");
+
+ let ty = c_ptr_to_rust_ptr(&ty);
+
+ ty.replace("void", "std::ffi::c_void")
+}
+
+#[cfg(test)]
+mod tests
+{
+ use opengl_registry::command::{Command, Parameter, Prototype};
+ use pretty_assertions::assert_eq;
+ use quote::quote;
+
+ use super::*;
+
+ #[test]
+ fn for_each_opengl_command_impl_works()
+ {
+ let registry = Registry::new(vec![
+ Command::new(Prototype::new("glBindBuffer", "void"), vec![]),
+ Command::new(
+ Prototype::new("glActiveTexture", "void"),
+ vec![
+ Parameter::new("abc", "GLuint"),
+ Parameter::new("xyz", "GLshort"),
+ ],
+ ),
+ Command::new(
+ Prototype::new("glDrawArrays", "GLint"),
+ vec![Parameter::new("foo", "GLubyte")],
+ ),
+ ]);
+
+ assert_eq!(
+ for_each_opengl_command_impl(
+ &quote! {
+ unsafe {
+ functions::_gl_command_ = FunctionPtr::new_initialized(
+ get_proc_addr(stringify!(_gl_command_))
+ );
+ }
+ },
+ &registry,
+ )
+ .to_string(),
+ quote! {
+ unsafe {
+ functions::glBindBuffer = FunctionPtr::new_initialized(
+ get_proc_addr(stringify!(glBindBuffer))
+ );
+ }
+ unsafe {
+ functions::glActiveTexture = FunctionPtr::new_initialized(
+ get_proc_addr(stringify!(glActiveTexture))
+ );
+ }
+ unsafe {
+ functions::glDrawArrays = FunctionPtr::new_initialized(
+ get_proc_addr(stringify!(glDrawArrays))
+ );
+ }
+ }
+ .to_string()
+ );
+
+ assert_eq!(
+ for_each_opengl_command_impl(
+ &quote! {
+ fn _gl_command_(_gl_command_args_) -> _gl_command_ret_type_ {}
+ },
+ &registry,
+ )
+ .to_string(),
+ quote! {
+ fn glBindBuffer() -> std::ffi::c_void {}
+ fn glActiveTexture(abc: GLuint, xyz: GLshort) -> std::ffi::c_void {}
+ fn glDrawArrays(foo: GLubyte) -> GLint {}
+ }
+ .to_string(),
+ );
+ }
+}
diff --git a/src/str.rs b/src/str.rs
new file mode 100644
index 0000000..90f3600
--- /dev/null
+++ b/src/str.rs
@@ -0,0 +1,61 @@
+use once_cell::sync::Lazy;
+use regex::{Captures, Regex};
+
+static C_PTR_RE: Lazy<Regex> =
+ Lazy::new(|| Regex::new(r"(const )?([a-zA-Z0-9_]+)\s?\*(.*)").unwrap());
+
+static REST_C_PTR_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(const)?\s?\*").unwrap());
+
+pub fn c_ptr_to_rust_ptr(c_ptr: &str) -> String
+{
+ C_PTR_RE
+ .replace(c_ptr, |captures: &Captures| {
+ let const_or_mut = captures.get(1).map_or_else(|| "mut", |_| "const");
+
+ let type_name = captures.get(2).unwrap().as_str();
+
+ let rest = captures.get(3).unwrap();
+
+ let rest_ptr_matches = REST_C_PTR_RE.captures_iter(rest.as_str());
+
+ let rest_ptrs = rest_ptr_matches
+ .map(|capts| {
+ let const_or_mut = capts.get(1).map_or_else(|| "mut", |_| "const");
+
+ format!("*{const_or_mut} ")
+ })
+ .collect::<Vec<_>>();
+
+ format!("{}*{const_or_mut} {type_name}", rest_ptrs.join(" "))
+ })
+ .to_string()
+}
+
+#[cfg(test)]
+mod tests
+{
+ use pretty_assertions::assert_str_eq;
+
+ use super::*;
+
+ #[test]
+ fn c_ptr_to_rust_ptr_works()
+ {
+ assert_str_eq!(c_ptr_to_rust_ptr("void *"), "*mut void");
+ assert_str_eq!(c_ptr_to_rust_ptr("const void*"), "*const void");
+ assert_str_eq!(c_ptr_to_rust_ptr("GLint *"), "*mut GLint");
+ assert_str_eq!(c_ptr_to_rust_ptr("const GLint*"), "*const GLint");
+ assert_str_eq!(c_ptr_to_rust_ptr("const GLint *"), "*const GLint");
+ assert_str_eq!(c_ptr_to_rust_ptr("const GLint **"), "*mut *const GLint");
+ assert_str_eq!(c_ptr_to_rust_ptr("const GLint * *"), "*mut *const GLint");
+ assert_str_eq!(c_ptr_to_rust_ptr("GLint **"), "*mut *mut GLint");
+ assert_str_eq!(c_ptr_to_rust_ptr("GLuint * const*"), "*const *mut GLuint");
+
+ assert_str_eq!(
+ c_ptr_to_rust_ptr("const GLuint * const*"),
+ "*const *const GLuint"
+ );
+
+ assert_str_eq!(c_ptr_to_rust_ptr("GLchar"), "GLchar");
+ }
+}
diff --git a/src/token_stream.rs b/src/token_stream.rs
new file mode 100644
index 0000000..043de89
--- /dev/null
+++ b/src/token_stream.rs
@@ -0,0 +1,263 @@
+use proc_macro2::{Group, Ident, TokenStream, TokenTree};
+
+pub use self::index_path::IndexPath;
+
+#[allow(clippy::module_name_repetitions)]
+pub trait TokenStreamExt
+{
+ fn find_all_ident(&self, target_ident: &Ident) -> Vec<IndexPath>;
+
+ fn replace_tokens(
+ &self,
+ index_paths_to_replace: &[IndexPath],
+ substitution: &TokenTree,
+ ) -> TokenStream;
+}
+
+impl TokenStreamExt for TokenStream
+{
+ fn find_all_ident(&self, target_ident: &Ident) -> Vec<IndexPath>
+ {
+ let mut found_indices = Vec::new();
+
+ recurse_find_all_ident(
+ self.clone(),
+ target_ident,
+ &mut found_indices,
+ &IndexPath::new(),
+ );
+
+ found_indices
+ }
+
+ fn replace_tokens(
+ &self,
+ index_paths_to_replace: &[IndexPath],
+ substitution: &TokenTree,
+ ) -> TokenStream
+ {
+ self.clone()
+ .into_iter()
+ .enumerate()
+ .map(|(index, mut token_tree)| {
+ for index_path in index_paths_to_replace
+ .iter()
+ .filter(|path| path.indices()[0] == index)
+ {
+ token_tree = match token_tree {
+ TokenTree::Ident(_) => substitution.clone(),
+ TokenTree::Group(group) => TokenTree::Group(Group::new(
+ group.delimiter(),
+ recurse_replace_tokens(
+ group.stream(),
+ substitution,
+ &index_path.indices()[1..],
+ ),
+ )),
+ tt => tt,
+ }
+ }
+
+ token_tree
+ })
+ .collect()
+ }
+}
+
+fn recurse_find_all_ident(
+ input_stream: TokenStream,
+ target_ident: &Ident,
+ found_indices: &mut Vec<IndexPath>,
+ current_index_path: &IndexPath,
+)
+{
+ for (index, token_tree) in input_stream.into_iter().enumerate() {
+ match token_tree {
+ TokenTree::Ident(ident) if &ident == target_ident => {
+ let mut index_path = current_index_path.clone();
+
+ index_path.push_index(index);
+
+ found_indices.push(index_path);
+ }
+ TokenTree::Group(group) => {
+ let mut index_path = current_index_path.clone();
+
+ index_path.push_index(index);
+
+ recurse_find_all_ident(
+ group.stream(),
+ target_ident,
+ found_indices,
+ &index_path,
+ );
+ }
+ _ => {}
+ }
+ }
+}
+
+fn recurse_replace_tokens(
+ token_stream: TokenStream,
+ substitution: &TokenTree,
+ indices: &[usize],
+) -> TokenStream
+{
+ token_stream
+ .into_iter()
+ .enumerate()
+ .map(|(index, token_tree)| match token_tree {
+ TokenTree::Ident(_) if index == indices[0] => substitution.clone(),
+ TokenTree::Group(group) if index == indices[0] => {
+ TokenTree::Group(Group::new(
+ group.delimiter(),
+ recurse_replace_tokens(group.stream(), substitution, &indices[1..]),
+ ))
+ }
+ tt => tt,
+ })
+ .collect()
+}
+
+mod index_path
+{
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ pub struct IndexPath
+ {
+ indices: Vec<usize>,
+ }
+
+ impl IndexPath
+ {
+ pub fn new() -> Self
+ {
+ Self {
+ indices: Vec::new(),
+ }
+ }
+
+ pub fn push_index(&mut self, index: usize)
+ {
+ self.indices.push(index);
+ }
+
+ pub fn indices(&self) -> &[usize]
+ {
+ &self.indices
+ }
+ }
+
+ impl<IntoIter> From<IntoIter> for IndexPath
+ where
+ IntoIter: IntoIterator<Item = usize>,
+ {
+ fn from(value: IntoIter) -> Self
+ {
+ Self {
+ indices: value.into_iter().collect(),
+ }
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests
+{
+ use proc_macro2::Span;
+ use quote::quote;
+
+ use super::*;
+
+ #[test]
+ fn find_all_ident_works()
+ {
+ assert_eq!(
+ quote! {
+ let abc = xyz;
+ }
+ .find_all_ident(&Ident::new("xyz", Span::call_site())),
+ vec![IndexPath::from([3])]
+ );
+
+ assert_eq!(
+ quote! {
+ let abc = (xyz, "123");
+ }
+ .find_all_ident(&Ident::new("xyz", Span::call_site())),
+ vec![IndexPath::from([3, 0])]
+ );
+
+ assert_eq!(
+ quote! {
+ return ("123", (yo, 180, xyz));
+ }
+ .find_all_ident(&Ident::new("xyz", Span::call_site())),
+ vec![IndexPath::from([1, 2, 4])]
+ );
+ }
+
+ #[test]
+ fn find_all_ident_works_with_multiple()
+ {
+ assert_eq!(
+ quote! {
+ unsafe {
+ functions::xyz = FunctionPtr::new_initialized(
+ get_proc_addr(stringify!(xyz))
+ );
+ }
+ }
+ .find_all_ident(&Ident::new("xyz", Span::call_site()),),
+ vec![IndexPath::from([1, 3]), IndexPath::from([1, 9, 1, 2, 0])]
+ );
+ }
+
+ #[test]
+ fn recurse_replace_tokens_works()
+ {
+ assert_eq!(
+ recurse_replace_tokens(
+ quote! {
+ let abc = xyz;
+ },
+ &TokenTree::Ident(Ident::new("foo", Span::call_site())),
+ &[3]
+ )
+ .to_string(),
+ quote! {
+ let abc = foo;
+ }
+ .to_string()
+ );
+
+ assert_eq!(
+ recurse_replace_tokens(
+ quote! {
+ let abc = (xyz, "123");
+ },
+ &TokenTree::Ident(Ident::new("foo", Span::call_site())),
+ &[3, 0]
+ )
+ .to_string(),
+ quote! {
+ let abc = (foo, "123");
+ }
+ .to_string()
+ );
+
+ assert_eq!(
+ recurse_replace_tokens(
+ quote! {
+ let abc = (hello, "123").iter_map(|_| xyz);
+ },
+ &TokenTree::Ident(Ident::new("foo", Span::call_site())),
+ &[6, 3]
+ )
+ .to_string(),
+ quote! {
+ let abc = (hello, "123").iter_map(|_| foo);
+ }
+ .to_string()
+ );
+ }
+}