| 1 | use crate::error::{Error, Result}; |
| 2 | use proc_macro::{token_stream, Delimiter, Group, Ident, Literal, Span, TokenTree}; |
| 3 | use std::iter::Peekable; |
| 4 | |
| 5 | pub(crate) enum Segment { |
| 6 | String(LitStr), |
| 7 | Apostrophe(Span), |
| 8 | Env(LitStr), |
| 9 | Modifier(Colon, Ident), |
| 10 | Replace(Colon, Group), |
| 11 | } |
| 12 | |
| 13 | pub(crate) struct LitStr { |
| 14 | pub value: String, |
| 15 | pub span: Span, |
| 16 | } |
| 17 | |
| 18 | pub(crate) struct Colon { |
| 19 | pub span: Span, |
| 20 | } |
| 21 | |
| 22 | pub(crate) fn parse(tokens: &mut Peekable<token_stream::IntoIter>) -> Result<Vec<Segment>> { |
| 23 | let mut segments = Vec::new(); |
| 24 | while match tokens.peek() { |
| 25 | None => false, |
| 26 | Some(TokenTree::Punct(punct)) => punct.as_char() != '>', |
| 27 | Some(_) => true, |
| 28 | } { |
| 29 | match tokens.next().unwrap() { |
| 30 | TokenTree::Ident(ident) => { |
| 31 | let mut fragment = ident.to_string(); |
| 32 | if fragment.starts_with("r#") { |
| 33 | fragment = fragment.split_off(2); |
| 34 | } |
| 35 | if fragment == "env" |
| 36 | && match tokens.peek() { |
| 37 | Some(TokenTree::Punct(punct)) => punct.as_char() == '!', |
| 38 | _ => false, |
| 39 | } |
| 40 | { |
| 41 | let bang = tokens.next().unwrap(); // `!` |
| 42 | let expect_group = tokens.next(); |
| 43 | let parenthesized = match &expect_group { |
| 44 | Some(TokenTree::Group(group)) |
| 45 | if group.delimiter() == Delimiter::Parenthesis => |
| 46 | { |
| 47 | group |
| 48 | } |
| 49 | Some(wrong) => return Err(Error::new(wrong.span(), "expected `(`")), |
| 50 | None => { |
| 51 | return Err(Error::new2( |
| 52 | ident.span(), |
| 53 | bang.span(), |
| 54 | "expected `(` after `env!`", |
| 55 | )); |
| 56 | } |
| 57 | }; |
| 58 | let mut inner = parenthesized.stream().into_iter(); |
| 59 | let lit = match inner.next() { |
| 60 | Some(TokenTree::Literal(lit)) => lit, |
| 61 | Some(wrong) => { |
| 62 | return Err(Error::new(wrong.span(), "expected string literal")) |
| 63 | } |
| 64 | None => { |
| 65 | return Err(Error::new2( |
| 66 | ident.span(), |
| 67 | parenthesized.span(), |
| 68 | "expected string literal as argument to env! macro", |
| 69 | )) |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | segments.push(Segment::Env(LitStr { |
| 74 | value: get_literal_string_value(&lit, false, false)?, |
| 75 | span: lit.span(), |
| 76 | })); |
| 77 | |
| 78 | if let Some(unexpected) = inner.next() { |
| 79 | return Err(Error::new( |
| 80 | unexpected.span(), |
| 81 | "unexpected token in env! macro", |
| 82 | )); |
| 83 | } |
| 84 | } else { |
| 85 | segments.push(Segment::String(LitStr { |
| 86 | value: fragment, |
| 87 | span: ident.span(), |
| 88 | })); |
| 89 | } |
| 90 | } |
| 91 | TokenTree::Literal(lit) => { |
| 92 | segments.push(Segment::String(LitStr { |
| 93 | value: lit.to_string(), |
| 94 | span: lit.span(), |
| 95 | })); |
| 96 | } |
| 97 | // Note: `_` is always tokenized as an Ident, never a Punct, so there |
| 98 | // is no `'_'` arm here, it would be unreachable dead code. |
| 99 | TokenTree::Punct(punct) => match punct.as_char() { |
| 100 | '\'' => segments.push(Segment::Apostrophe(punct.span())), |
| 101 | ':' => { |
| 102 | let colon_span = punct.span(); |
| 103 | let colon = Colon { span: colon_span }; |
| 104 | let ident = match tokens.next() { |
| 105 | Some(TokenTree::Ident(ident)) => ident, |
| 106 | wrong => { |
| 107 | let span = wrong.as_ref().map_or(colon_span, TokenTree::span); |
| 108 | return Err(Error::new(span, "expected identifier after `:`")); |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | if ident.to_string().as_str() == "replace" { |
| 113 | let replace = tokens.next(); |
| 114 | |
| 115 | match replace { |
| 116 | Some(TokenTree::Group(group)) |
| 117 | if group.delimiter() == Delimiter::Parenthesis => |
| 118 | { |
| 119 | segments.push(Segment::Replace(colon, group)); |
| 120 | } |
| 121 | _ => { |
| 122 | return Err(Error::new2( |
| 123 | colon.span, |
| 124 | ident.span(), |
| 125 | "expected `(` after replace modifier", |
| 126 | )); |
| 127 | } |
| 128 | } |
| 129 | } else { |
| 130 | segments.push(Segment::Modifier(colon, ident)); |
| 131 | } |
| 132 | } |
| 133 | '#' => segments.push(Segment::String(LitStr { |
| 134 | value: "#".to_string(), |
| 135 | span: punct.span(), |
| 136 | })), |
| 137 | _ => return Err(Error::new(punct.span(), "unexpected punct")), |
| 138 | }, |
| 139 | TokenTree::Group(group) => { |
| 140 | if group.delimiter() == Delimiter::None { |
| 141 | let mut inner = group.stream().into_iter().peekable(); |
| 142 | let nested = parse(&mut inner)?; |
| 143 | if let Some(unexpected) = inner.next() { |
| 144 | return Err(Error::new(unexpected.span(), "unexpected token")); |
| 145 | } |
| 146 | segments.extend(nested); |
| 147 | } else { |
| 148 | return Err(Error::new(group.span(), "unexpected token")); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | Ok(segments) |
| 154 | } |
| 155 | |
| 156 | pub(crate) fn paste(segments: &[Segment]) -> Result<String> { |
| 157 | let mut evaluated = Vec::new(); |
| 158 | let mut is_lifetime = false; |
| 159 | |
| 160 | for (i, segment) in segments.iter().enumerate() { |
| 161 | match segment { |
| 162 | Segment::String(segment) => { |
| 163 | if segment.value.as_str() == "#" { |
| 164 | if i == 0 { |
| 165 | // Enable Raw mode |
| 166 | evaluated.push(String::from("r#")); |
| 167 | continue; |
| 168 | } |
| 169 | return Err(Error::new( |
| 170 | segment.span, |
| 171 | "`#` is reserved keyword and it enables the raw mode \ |
| 172 | (i.e. generate Raw Identifiers) and it is only allowed in \ |
| 173 | the beginning like `[< # ... >]`", |
| 174 | )); |
| 175 | } |
| 176 | evaluated.push(segment.value.clone()); |
| 177 | } |
| 178 | Segment::Apostrophe(span) => { |
| 179 | if is_lifetime { |
| 180 | return Err(Error::new(*span, "unexpected lifetime")); |
| 181 | } |
| 182 | is_lifetime = true; |
| 183 | } |
| 184 | Segment::Env(var) => { |
| 185 | let resolved = match std::env::var(&var.value) { |
| 186 | Ok(resolved) => resolved, |
| 187 | Err(_) => { |
| 188 | return Err(Error::new( |
| 189 | var.span, |
| 190 | &format!("no such env var: {:?}", var.value), |
| 191 | )); |
| 192 | } |
| 193 | }; |
| 194 | let resolved = resolved.replace('-', "_"); |
| 195 | evaluated.push(resolved); |
| 196 | } |
| 197 | Segment::Modifier(colon, ident) => { |
| 198 | let last = match evaluated.pop() { |
| 199 | Some(last) => last, |
| 200 | None => { |
| 201 | return Err(Error::new2(colon.span, ident.span(), "unexpected modifier")) |
| 202 | } |
| 203 | }; |
| 204 | match ident.to_string().as_str() { |
| 205 | "lower" => { |
| 206 | evaluated.push(last.to_lowercase()); |
| 207 | } |
| 208 | "upper" => { |
| 209 | evaluated.push(last.to_uppercase()); |
| 210 | } |
| 211 | "snake" => { |
| 212 | let mut acc = String::new(); |
| 213 | let mut prev = '_'; |
| 214 | for ch in last.chars() { |
| 215 | if ch.is_uppercase() && prev != '_' { |
| 216 | acc.push('_'); |
| 217 | } |
| 218 | acc.push(ch); |
| 219 | prev = ch; |
| 220 | } |
| 221 | evaluated.push(acc.to_lowercase()); |
| 222 | } |
| 223 | "camel" | "upper_camel" | "lower_camel" => { |
| 224 | let mut is_lower_camel = ident.to_string().as_str() == "lower_camel"; |
| 225 | let mut acc = String::new(); |
| 226 | let mut prev = '_'; |
| 227 | for ch in last.chars() { |
| 228 | if ch != '_' { |
| 229 | if prev == '_' { |
| 230 | if is_lower_camel { |
| 231 | for chl in ch.to_lowercase() { |
| 232 | acc.push(chl); |
| 233 | } |
| 234 | is_lower_camel = false; |
| 235 | } else { |
| 236 | for chu in ch.to_uppercase() { |
| 237 | acc.push(chu); |
| 238 | } |
| 239 | } |
| 240 | } else if prev.is_uppercase() { |
| 241 | for chl in ch.to_lowercase() { |
| 242 | acc.push(chl); |
| 243 | } |
| 244 | } else { |
| 245 | acc.push(ch); |
| 246 | } |
| 247 | } |
| 248 | prev = ch; |
| 249 | } |
| 250 | evaluated.push(acc); |
| 251 | } |
| 252 | "camel_edge" => { |
| 253 | let mut acc = String::new(); |
| 254 | let mut prev = '_'; |
| 255 | for ch in last.chars() { |
| 256 | if ch != '_' { |
| 257 | if prev == '_' { |
| 258 | for chu in ch.to_uppercase() { |
| 259 | acc.push(chu); |
| 260 | } |
| 261 | } else if prev.is_uppercase() { |
| 262 | for chl in ch.to_lowercase() { |
| 263 | acc.push(chl); |
| 264 | } |
| 265 | } else { |
| 266 | acc.push(ch); |
| 267 | } |
| 268 | } else if prev == '_' { |
| 269 | acc.push(ch); |
| 270 | } |
| 271 | prev = ch; |
| 272 | } |
| 273 | evaluated.push(acc); |
| 274 | } |
| 275 | _ => { |
| 276 | return Err(Error::new2( |
| 277 | colon.span, |
| 278 | ident.span(), |
| 279 | "unsupported modifier", |
| 280 | )); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | Segment::Replace(colon, group) => { |
| 285 | let mut inner_stream = group.stream().into_iter(); |
| 286 | let from = inner_stream.next(); |
| 287 | let punct = inner_stream.next(); |
| 288 | let to = inner_stream.next(); |
| 289 | |
| 290 | if let Some(unexpected_token) = inner_stream.next() { |
| 291 | return Err(Error::new(unexpected_token.span(), "expected `)`")); |
| 292 | } |
| 293 | |
| 294 | match (from, punct, to) { |
| 295 | (Some(from), Some(TokenTree::Punct(punct)), Some(to)) |
| 296 | if punct.as_char() == ',' => |
| 297 | { |
| 298 | let last = |
| 299 | match evaluated.pop() { |
| 300 | Some(last) => last, |
| 301 | None => return Err(Error::new2( |
| 302 | colon.span, |
| 303 | group.span(), |
| 304 | "replace modifier requires a preceding value to operate on.", |
| 305 | )), |
| 306 | }; |
| 307 | |
| 308 | let from_str = get_token_tree_string_value(&from)?; |
| 309 | let to_str = get_token_tree_string_value(&to)?; |
| 310 | |
| 311 | let new_ident = last.replace(&from_str, &to_str); |
| 312 | |
| 313 | evaluated.push(new_ident); |
| 314 | } |
| 315 | _ => { |
| 316 | return Err(Error::new( |
| 317 | group.span(), |
| 318 | "expected replace modifier format: `:replace(\"from\", \"to\")`", |
| 319 | )) |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | let mut pasted = evaluated.into_iter().collect::<String>(); |
| 327 | if is_lifetime { |
| 328 | pasted.insert(0, '\''); |
| 329 | } |
| 330 | Ok(pasted) |
| 331 | } |
| 332 | |
| 333 | // The three defensive sub-conditions below can never evaluate to false when called |
| 334 | // from get_literal_string_value with a real proc_macro::Literal, because- |
| 335 | // |
| 336 | // ends_with('"') — the Rust tokenizer rejects unterminated string literals before |
| 337 | // the macro receives them, so starts_with('"') implies ends_with('"'). |
| 338 | // ends_with('\'') — same: the tokenizer rejects unterminated char literals. |
| 339 | // len() >= 2 — impossible once both prefix and suffix chars are confirmed present. |
| 340 | // |
| 341 | // These checks are kept for correctness in case this function is called with an |
| 342 | // arbitrary &str in the future. The false branches are exercised by the |
| 343 | // unit tests below, which call is_quoted_string_or_char directly |
| 344 | // with raw &str values (e.g. "\"abc", "'abc", "\""). |
| 345 | // proc_macro::Literal has no constructor that produces such malformed tokens, so |
| 346 | // the test_helpers.rs (with paste_test macro) path cannot cover these branches. |
| 347 | fn is_quoted_string_or_char(l_str: &str, parse_char: bool) -> bool { |
| 348 | ((l_str.starts_with('"') && l_str.ends_with('"')) |
| 349 | || (parse_char && l_str.starts_with('\'') && l_str.ends_with('\''))) |
| 350 | && l_str.len() >= 2 |
| 351 | } |
| 352 | |
| 353 | fn get_literal_string_value(l: &Literal, parse_char: bool, parse_numbers: bool) -> Result<String> { |
| 354 | let l_str = l.to_string(); |
| 355 | |
| 356 | if is_quoted_string_or_char(&l_str, parse_char) { |
| 357 | // TODO: maybe handle escape sequences in the string if |
| 358 | // someone has a use case. |
| 359 | Ok(String::from(&l_str[1..l_str.len() - 1])) |
| 360 | } else if parse_numbers { |
| 361 | Ok(l_str) |
| 362 | } else { |
| 363 | Err(Error::new(l.span(), "expected string literal")) |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | pub(crate) fn get_token_tree_string_value(t: &TokenTree) -> Result<String> { |
| 368 | match t { |
| 369 | TokenTree::Ident(ident) => Ok(ident.to_string()), |
| 370 | TokenTree::Literal(literal) => get_literal_string_value(literal, true, true), |
| 371 | TokenTree::Group(group) if group.delimiter() == Delimiter::None => { |
| 372 | // Handle interpolated tokens from macro_rules (common in older Rust versions) |
| 373 | let mut inner = group.stream().into_iter(); |
| 374 | if let Some(first) = inner.next() { |
| 375 | if inner.next().is_none() { |
| 376 | // Single token in the group, recursively process it |
| 377 | return get_token_tree_string_value(&first); |
| 378 | } |
| 379 | } |
| 380 | Err(Error::new(t.span(), "Expected either Ident, or Literal.")) |
| 381 | } |
| 382 | _ => Err(Error::new(t.span(), "Expected either Ident, or Literal.")), |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | #[cfg(test)] |
| 387 | mod tests { |
| 388 | use super::is_quoted_string_or_char; |
| 389 | |
| 390 | #[test] |
| 391 | fn test_is_quoted_string_or_char() { |
| 392 | assert!(is_quoted_string_or_char("\"hello\"", false)); |
| 393 | assert!(!is_quoted_string_or_char("abc", false)); |
| 394 | assert!(!is_quoted_string_or_char("\"", false)); |
| 395 | assert!(!is_quoted_string_or_char("\"abc", false)); |
| 396 | assert!(is_quoted_string_or_char("'a'", true)); |
| 397 | assert!(!is_quoted_string_or_char("'abc", true)); |
| 398 | assert!(!is_quoted_string_or_char("abc", true)); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | #[cfg(doctest)] |
| 403 | #[doc(hidden)] |
| 404 | mod doc_tests { |
| 405 | /// ```compile_fail |
| 406 | /// use pastey::paste; |
| 407 | /// paste! { fn [<a + b>]() {} } |
| 408 | /// ``` |
| 409 | fn test_unexpected_punct() {} |
| 410 | |
| 411 | /// ```compile_fail |
| 412 | /// use pastey::paste; |
| 413 | /// paste! { fn [<{}>]() {} } |
| 414 | /// ``` |
| 415 | fn test_unexpected_group() {} |
| 416 | |
| 417 | /// ```compile_fail |
| 418 | /// use pastey::paste; |
| 419 | /// paste! { fn [<env! foo>]() {} } |
| 420 | /// ``` |
| 421 | fn test_env_missing_paren() {} |
| 422 | |
| 423 | /// ```compile_fail |
| 424 | /// use pastey::paste; |
| 425 | /// paste! { fn [<env!(foo)>]() {} } |
| 426 | /// ``` |
| 427 | fn test_env_non_literal_arg() {} |
| 428 | |
| 429 | /// ```compile_fail |
| 430 | /// use pastey::paste; |
| 431 | /// paste! { fn [<env!()>]() {} } |
| 432 | /// ``` |
| 433 | fn test_env_empty() {} |
| 434 | |
| 435 | /// ```compile_fail |
| 436 | /// use pastey::paste; |
| 437 | /// paste! { fn [<env!("CARGO_PKG_NAME" "extra")>]() {} } |
| 438 | /// ``` |
| 439 | fn test_env_unexpected_token() {} |
| 440 | |
| 441 | /// ```compile_fail |
| 442 | /// use pastey::paste; |
| 443 | /// paste! { fn [<a:>]() {} } |
| 444 | /// ``` |
| 445 | fn test_no_ident_after_colon() {} |
| 446 | |
| 447 | /// ```compile_fail |
| 448 | /// use pastey::paste; |
| 449 | /// paste! { fn [<a:replace x>]() {} } |
| 450 | /// ``` |
| 451 | fn test_replace_missing_paren() {} |
| 452 | |
| 453 | /// ```compile_fail |
| 454 | /// use pastey::paste; |
| 455 | /// paste! { fn [<a:pillow>]() {} } |
| 456 | /// ``` |
| 457 | fn test_unsupported_modifier() {} |
| 458 | |
| 459 | /// ```compile_fail |
| 460 | /// use pastey::paste; |
| 461 | /// paste! { fn [<a:replace("x","y","z")>]() {} } |
| 462 | /// ``` |
| 463 | fn test_replace_extra_token() {} |
| 464 | |
| 465 | /// ```compile_fail |
| 466 | /// use pastey::paste; |
| 467 | /// paste! { struct [<:replace("H","W")>]; } |
| 468 | /// ``` |
| 469 | fn test_replace_no_preceding_value() {} |
| 470 | |
| 471 | /// ```compile_fail |
| 472 | /// use pastey::paste; |
| 473 | /// paste! { fn [<a:replace(x)>]() {} } |
| 474 | /// ``` |
| 475 | fn test_replace_wrong_format() {} |
| 476 | |
| 477 | /// ```compile_fail |
| 478 | /// use pastey::paste; |
| 479 | /// paste! { struct [<foo # bar>]; } |
| 480 | /// ``` |
| 481 | fn test_raw_mode_wrong_position() {} |
| 482 | |
| 483 | /// ```compile_fail |
| 484 | /// use pastey::paste; |
| 485 | /// paste! { fn [<'a 'b>]() {} } |
| 486 | /// ``` |
| 487 | fn test_unexpected_lifetime() {} |
| 488 | |
| 489 | /// ```compile_fail |
| 490 | /// use pastey::paste; |
| 491 | /// paste! { fn [<env!("THIS_VAR_DOES_NOT_EXIST_XYZ_999")>]() {} } |
| 492 | /// ``` |
| 493 | fn test_no_such_env_var() {} |
| 494 | |
| 495 | /// ```compile_fail |
| 496 | /// use pastey::paste; |
| 497 | /// paste! { fn [<:lower>]() {} } |
| 498 | /// ``` |
| 499 | fn test_modifier_no_preceding_value() {} |
| 500 | |
| 501 | /// ```compile_fail |
| 502 | /// use pastey::paste; |
| 503 | /// paste! { fn [<env!["CARGO_PKG_NAME"]>]() {} } |
| 504 | /// ``` |
| 505 | fn test_env_bracket_group() {} |
| 506 | |
| 507 | /// ```compile_fail |
| 508 | /// use pastey::paste; |
| 509 | /// paste! { struct [<a:replace["x", "y"]>]; } |
| 510 | /// ``` |
| 511 | fn test_replace_bracket_group() {} |
| 512 | |
| 513 | /// ```compile_fail |
| 514 | /// use pastey::paste; |
| 515 | /// macro_rules! m { |
| 516 | /// ($e:expr) => { paste! { fn [<$e foo>]() {} } } |
| 517 | /// } |
| 518 | /// m!(1 > 0); |
| 519 | /// ``` |
| 520 | fn test_none_group_unexpected_token() {} |
| 521 | |
| 522 | /// ```compile_fail |
| 523 | /// use pastey::paste; |
| 524 | /// paste! { struct [<a:replace("x"; "y")>]; } |
| 525 | /// ``` |
| 526 | fn test_replace_wrong_separator() {} |
| 527 | |
| 528 | /// ```compile_fail |
| 529 | /// use pastey::paste; |
| 530 | /// paste! { fn [<env!(42)>]() {} } |
| 531 | /// ``` |
| 532 | fn test_env_numeric_literal() {} |
| 533 | |
| 534 | /// ```compile_fail |
| 535 | /// use pastey::paste; |
| 536 | /// paste! { fn [<env!('a')>]() {} } |
| 537 | /// ``` |
| 538 | fn test_env_char_literal() {} |
| 539 | |
| 540 | /// ```compile_fail |
| 541 | /// use pastey::paste; |
| 542 | /// paste! { struct [<Foo:replace(["x"], "y")>]; } |
| 543 | /// ``` |
| 544 | fn test_replace_from_bracket_group() {} |
| 545 | |
| 546 | /// ```compile_fail |
| 547 | /// use pastey::paste; |
| 548 | /// macro_rules! m { |
| 549 | /// ($from:expr) => { paste! { struct [<Foo:replace($from, "y")>]; } } |
| 550 | /// } |
| 551 | /// m!(x + y); |
| 552 | /// ``` |
| 553 | fn test_replace_from_multi_token_none_group() {} |
| 554 | |
| 555 | /// ```compile_fail |
| 556 | /// use pastey::paste; |
| 557 | /// macro_rules! m { |
| 558 | /// ($to:expr) => { paste! { struct [<Foo:replace("o", $to)>]; } } |
| 559 | /// } |
| 560 | /// m!(x + y); |
| 561 | /// ``` |
| 562 | fn test_replace_to_multi_token_none_group() {} |
| 563 | } |