Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
Michael Forster | 523dbd4 | 2021-10-12 11:05:44 +0000 | [diff] [blame] | 5 | use anyhow::bail; |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 6 | use anyhow::Result; |
| 7 | use proc_macro2::TokenStream; |
| 8 | use proc_macro2::TokenTree; |
| 9 | |
| 10 | use std::fmt::Write; |
| 11 | |
Googler | 42d540f | 2021-09-29 06:37:23 +0000 | [diff] [blame] | 12 | fn is_ident_or_literal(tt: &TokenTree) -> bool { |
| 13 | matches!(tt, TokenTree::Ident(_) | TokenTree::Literal(_)) |
| 14 | } |
| 15 | |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 16 | /// Produces C++ source code out of the token stream. |
| 17 | /// |
| 18 | /// Notable features: |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 19 | /// * quote! cannot produce a single `#` token (that is not immediately followed |
| 20 | /// by `(`, `[`, `{`, or variable interpolation). For cases when we need `#` |
| 21 | /// to be produced in the C++ source code use the placeholder |
| 22 | /// `__HASH_TOKEN__`. |
| 23 | /// * The Rust tokenizer ignores newlines as they are not significant for Rust. |
| 24 | /// For C++ they are (for example there needs to be a newline after `#include |
| 25 | /// "foo/bar.h"`). We are also using explict newlines for making the generated |
| 26 | /// Rust/C++ source code more readable. Use the placeholder `__NEWLINE__` to |
| 27 | /// insert a newline character. |
| 28 | /// * `TokenStream` cannot encode comments, so we use the placeholder |
| 29 | /// `__COMMENT__`, followed by a string literal. |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 30 | pub fn tokens_to_string(tokens: TokenStream) -> Result<String> { |
Michael Forster | 0de2b8c | 2021-10-11 08:28:49 +0000 | [diff] [blame] | 31 | let mut result = String::new(); |
| 32 | let mut it = tokens.into_iter().peekable(); |
| 33 | while let Some(tt) = it.next() { |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 34 | match tt { |
| 35 | TokenTree::Ident(ref tt) if tt == "__NEWLINE__" => writeln!(result)?, |
| 36 | TokenTree::Ident(ref tt) if tt == "__HASH_TOKEN__" => write!(result, "#")?, |
| 37 | |
Michael Forster | 523dbd4 | 2021-10-12 11:05:44 +0000 | [diff] [blame] | 38 | TokenTree::Ident(ref tt) if tt == "__COMMENT__" => { |
| 39 | if let Some(TokenTree::Literal(lit)) = it.next() { |
| 40 | writeln!( |
| 41 | result, |
| 42 | "// {}", |
| 43 | lit.to_string().trim_matches('"').replace("\\n", "\n// ") |
| 44 | )?; |
| 45 | } else { |
| 46 | bail!("__COMMENT__ must be followed by a literal") |
| 47 | } |
| 48 | } |
| 49 | |
Michael Forster | 0de2b8c | 2021-10-11 08:28:49 +0000 | [diff] [blame] | 50 | _ => { |
| 51 | write!(result, "{}", tt)?; |
Googler | 42d540f | 2021-09-29 06:37:23 +0000 | [diff] [blame] | 52 | |
Michael Forster | 0de2b8c | 2021-10-11 08:28:49 +0000 | [diff] [blame] | 53 | // Insert spaces between tokens only when they are needed to separate |
| 54 | // identifiers or literals from each other. |
| 55 | if is_ident_or_literal(&tt) |
| 56 | && matches!(it.peek(), Some(tt_next) if is_ident_or_literal(tt_next)) |
| 57 | { |
| 58 | write!(result, " ")?; |
| 59 | } |
| 60 | } |
| 61 | } |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 62 | } |
| 63 | Ok(result) |
| 64 | } |
| 65 | |
| 66 | #[cfg(test)] |
| 67 | mod tests { |
| 68 | use super::*; |
| 69 | |
| 70 | use super::Result; |
| 71 | use quote::quote; |
| 72 | |
| 73 | #[test] |
| 74 | fn test_simple_token_stream() -> Result<()> { |
| 75 | let token_stream = quote! { |
| 76 | struct Foo {} |
| 77 | |
| 78 | impl Bar for Foo { |
| 79 | fn bar(&self) {} |
| 80 | } |
| 81 | }; |
Googler | 42d540f | 2021-09-29 06:37:23 +0000 | [diff] [blame] | 82 | assert_eq!( |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 83 | tokens_to_string(token_stream.clone())?, |
Googler | 42d540f | 2021-09-29 06:37:23 +0000 | [diff] [blame] | 84 | "struct Foo{ }impl Bar for Foo{ fn bar (& self) { } }" |
| 85 | ); |
| 86 | Ok(()) |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn test_space_idents_and_literals() -> Result<()> { |
| 91 | let token_stream = quote! { foo 42 bar 23 }; |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 92 | assert_eq!(tokens_to_string(token_stream.clone())?, "foo 42 bar 23"); |
Googler | 42d540f | 2021-09-29 06:37:23 +0000 | [diff] [blame] | 93 | Ok(()) |
| 94 | } |
| 95 | |
| 96 | #[test] |
| 97 | fn test_dont_space_punctuation() -> Result<()> { |
| 98 | let token_stream = quote! { foo+42+bar+23 }; |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 99 | assert_eq!(tokens_to_string(token_stream.clone())?, "foo+42+bar+23"); |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 100 | Ok(()) |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn test_newline_token() -> Result<()> { |
| 105 | let token_stream = quote! { a __NEWLINE__ b }; |
Michael Forster | 0de2b8c | 2021-10-11 08:28:49 +0000 | [diff] [blame] | 106 | assert_eq!(tokens_to_string(token_stream.clone())?, "a \nb"); |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 107 | Ok(()) |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn test_hash_token() -> Result<()> { |
| 112 | let token_stream = quote! { a __HASH_TOKEN__ b }; |
Michael Forster | 0de2b8c | 2021-10-11 08:28:49 +0000 | [diff] [blame] | 113 | assert_eq!(tokens_to_string(token_stream.clone())?, "a #b"); |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 114 | Ok(()) |
| 115 | } |
Googler | 42d540f | 2021-09-29 06:37:23 +0000 | [diff] [blame] | 116 | |
| 117 | #[test] |
| 118 | fn test_include_standard_header() -> Result<()> { |
| 119 | let token_stream = quote! { __HASH_TOKEN__ include <cstddef> }; |
Michael Forster | 0de2b8c | 2021-10-11 08:28:49 +0000 | [diff] [blame] | 120 | assert_eq!(tokens_to_string(token_stream.clone())?, "#include<cstddef>"); |
Googler | 42d540f | 2021-09-29 06:37:23 +0000 | [diff] [blame] | 121 | Ok(()) |
| 122 | } |
Michael Forster | 523dbd4 | 2021-10-12 11:05:44 +0000 | [diff] [blame] | 123 | |
| 124 | #[test] |
| 125 | fn test_comments() -> Result<()> { |
| 126 | let token_stream = quote! { __COMMENT__ "line1\nline2" }; |
| 127 | assert_eq!(tokens_to_string(token_stream.clone())?, "// line1\n// line2\n"); |
| 128 | Ok(()) |
| 129 | } |
| 130 | |
| 131 | #[test] |
| 132 | fn test_invalid_comment() -> Result<()> { |
| 133 | assert!(tokens_to_string(quote! { __COMMENT__ }).is_err()); |
| 134 | assert!(tokens_to_string(quote! { __COMMENT__ ident }).is_err()); |
| 135 | Ok(()) |
| 136 | } |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 137 | } |