Parse based on proc-macro2
diff --git a/src/ident.rs b/src/ident.rs
index 8f698be..fbdce4f 100644
--- a/src/ident.rs
+++ b/src/ident.rs
@@ -57,63 +57,47 @@
#[cfg(feature = "parsing")]
pub mod parsing {
use super::*;
- use synom::IResult;
- use synom::space::skip_whitespace;
- use unicode_xid::UnicodeXID;
+ use synom::{TokenTree, TokenKind, IResult};
+ #[cfg(feature = "full")]
+ use lit::parsing::int;
- pub fn ident(input: &str) -> IResult<&str, Ident> {
- let (rest, id) = match word(input) {
- IResult::Done(rest, id) => (rest, id),
- IResult::Error => return IResult::Error,
- };
-
- match id.as_ref() {
- // From https://doc.rust-lang.org/grammar.html#keywords
- "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue" |
- "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" |
- "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" |
- "mut" | "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" |
- "return" | "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" |
- "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" |
- "while" | "yield" => IResult::Error,
- _ => IResult::Done(rest, id),
+ pub fn ident(input: &[TokenTree]) -> IResult<&[TokenTree], Ident> {
+ if let IResult::Done(rest, id) = word(input) {
+ match id.as_ref() {
+ // From https://doc.rust-lang.org/grammar.html#keywords
+ "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue" |
+ "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" |
+ "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" |
+ "mut" | "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" |
+ "return" | "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" |
+ "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" |
+ "while" | "yield" => IResult::Error,
+ _ => IResult::Done(rest, id),
+ }
+ } else {
+ IResult::Error
}
}
- pub fn word(mut input: &str) -> IResult<&str, Ident> {
- input = skip_whitespace(input);
-
- let mut chars = input.char_indices();
- match chars.next() {
- Some((_, ch)) if UnicodeXID::is_xid_start(ch) || ch == '_' => {}
- _ => return IResult::Error,
- }
-
- for (i, ch) in chars {
- if !UnicodeXID::is_xid_continue(ch) {
- return IResult::Done(&input[i..], input[..i].into());
+ pub fn word(input: &[TokenTree]) -> IResult<&[TokenTree], Ident> {
+ if let Some(&TokenTree { kind: TokenKind::Word(ref id), .. }) = input.first() {
+ // Check if this word is _actually_ a lifetime, and treat that differently
+ if id.chars().next().unwrap() == '\'' {
+ IResult::Error
+ } else {
+ IResult::Done(&input[1..], Ident(id.to_string()))
}
+ } else {
+ IResult::Error
}
-
- IResult::Done("", input.into())
}
#[cfg(feature = "full")]
- pub fn wordlike(mut input: &str) -> IResult<&str, Ident> {
- input = skip_whitespace(input);
-
- for (i, ch) in input.char_indices() {
- if !UnicodeXID::is_xid_start(ch) && !UnicodeXID::is_xid_continue(ch) {
- return if i == 0 {
- IResult::Error
- } else {
- IResult::Done(&input[i..], input[..i].into())
- };
- }
- }
-
- IResult::Done("", input.into())
- }
+ named!(pub wordlike -> Ident, alt!(
+ word
+ |
+ int => { |d| format!("{}", d).into() }
+ ));
}
#[cfg(feature = "printing")]