Skip to content

Commit a13d142

Browse files
committed
Support raw identifiers
1 parent 639e4ba commit a13d142

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/stable.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,12 @@ fn symbol(mut input: Cursor) -> PResult<TokenTree> {
652652
chars.next();
653653
}
654654

655+
let raw = !lifetime && input.starts_with("r#");
656+
if raw {
657+
chars.next();
658+
chars.next();
659+
}
660+
655661
match chars.next() {
656662
Some((_, ch)) if UnicodeXID::is_xid_start(ch) || ch == '_' => {}
657663
_ => return Err(LexError),
@@ -665,15 +671,13 @@ fn symbol(mut input: Cursor) -> PResult<TokenTree> {
665671
}
666672
}
667673

668-
if lifetime && &input.rest[..end] != "'static" && KEYWORDS.contains(&&input.rest[1..end]) {
674+
let a = &input.rest[..end];
675+
if a == "r#_" || lifetime && a != "'static" && KEYWORDS.contains(&&a[1..]) {
669676
Err(LexError)
677+
} else if a == "_" {
678+
Ok((input.advance(end), Op::new('_', Spacing::Alone).into()))
670679
} else {
671-
let a = &input.rest[..end];
672-
if a == "_" {
673-
Ok((input.advance(end), Op::new('_', Spacing::Alone).into()))
674-
} else {
675-
Ok((input.advance(end), ::Term::new(a, ::Span::call_site()).into()))
676-
}
680+
Ok((input.advance(end), ::Term::new(a, ::Span::call_site()).into()))
677681
}
678682
}
679683

tests/test.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
extern crate proc_macro2;
22

3-
use std::str;
3+
use std::str::{self, FromStr};
44

5-
use proc_macro2::{Term, Literal, TokenStream, Span};
5+
use proc_macro2::{Term, Literal, TokenStream, Span, TokenTree};
66

77
#[test]
88
fn symbols() {
@@ -66,6 +66,8 @@ fn fail() {
6666
fail("1f320");
6767
fail("' static");
6868
fail("'mut");
69+
fail("r#1");
70+
fail("r#_");
6971
}
7072

7173
#[cfg(procmacro2_semver_exempt)]
@@ -181,3 +183,12 @@ fn tricky_doc_comment() {
181183
}
182184
}
183185

186+
#[test]
187+
fn raw_identifier() {
188+
let mut tts = TokenStream::from_str("r#dyn").unwrap().into_iter();
189+
match tts.next().unwrap() {
190+
TokenTree::Term(raw) => assert_eq!("r#dyn", raw.as_str()),
191+
wrong => panic!("wrong token {:?}", wrong),
192+
}
193+
assert!(tts.next().is_none());
194+
}

0 commit comments

Comments
 (0)