From a7d69fc74fe0a5efa68766ec69772fa06b81e090 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 26 Aug 2018 13:30:24 -0400 Subject: Parse types --- src/path.rs | 107 +++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 62 insertions(+), 45 deletions(-) (limited to 'src/path.rs') diff --git a/src/path.rs b/src/path.rs index 12c7549b..c2ad34af 100644 --- a/src/path.rs +++ b/src/path.rs @@ -229,7 +229,6 @@ pub mod parsing { use super::*; use parse::{Parse, ParseStream, Result}; use synom::ext::IdentExt; - use synom::Synom; impl Parse for Path { fn parse(input: ParseStream) -> Result { @@ -303,7 +302,7 @@ pub mod parsing { Ok(ParenthesizedGenericArguments { paren_token: parenthesized!(content in input), inputs: content.parse_synom(Punctuated::parse_terminated)?, - output: input.parse_synom(ReturnType::without_plus)?, + output: input.call(ReturnType::without_plus)?, }) } } @@ -321,7 +320,7 @@ pub mod parsing { } let ident = input.parse()?; - if input.peek(Token![<]) && !input.peek2(Token![=]) + if input.peek(Token![<]) && !input.peek(Token![<=]) || input.peek(Token![::]) && input.peek3(Token![<]) { Ok(PathSegment { @@ -339,16 +338,49 @@ pub mod parsing { Ok(Binding { ident: input.parse()?, eq_token: input.parse()?, - ty: input.parse_synom(Type::parse)?, + ty: input.parse()?, }) } } impl Path { - named!(pub parse_mod_style -> Self, do_parse!( + pub fn parse_mod_style(input: ParseStream) -> Result { + Ok(Path { + leading_colon: input.parse()?, + segments: { + let mut segments = Punctuated::new(); + loop { + if !input.peek(Ident) + && !input.peek(Token![super]) + && !input.peek(Token![self]) + && !input.peek(Token![Self]) + && !input.peek(Token![crate]) + && !input.peek(Token![extern]) + { + break; + } + let ident = Ident::parse_any2(input)?; + segments.push_value(PathSegment::from(ident)); + if !input.peek(Token![::]) { + break; + } + let punct = input.parse()?; + segments.push_punct(punct); + } + if segments.is_empty() { + return Err(input.error("expected path")); + } else if segments.trailing_punct() { + return Err(input.error("expected path segment")); + } + segments + }, + }) + } + + named!(pub old_parse_mod_style -> Self, do_parse!( colon: option!(punct!(::)) >> segments: call!(Punctuated::parse_separated_nonempty_with, - mod_style_path_segment) >> + old_mod_style_path_segment) >> (Path { leading_colon: colon, segments: segments, @@ -356,7 +388,30 @@ pub mod parsing { )); } - named!(pub mod_style_path_segment -> PathSegment, alt!( + // FIXME + /* + pub fn mod_style_path_segment(input: ParseStream) -> Result { + let lookahead = input.lookahead1(); + let ident = if lookahead.peek(Ident) { + input.parse()? + } else if lookahead.peek(Token![super]) { + Ident::from(input.parse::()?) + } else if lookahead.peek(Token![self]) { + Ident::from(input.parse::()?) + } else if lookahead.peek(Token![Self]) { + Ident::from(input.parse::()?) + } else if lookahead.peek(Token![crate]) { + Ident::from(input.parse::()?) + } else if lookahead.peek(Token![extern]) { + Ident::from(input.parse::()?) + } else { + return Err(lookahead.error()); + }; + Ok(PathSegment::from(ident)) + } + */ + + named!(pub old_mod_style_path_segment -> PathSegment, alt!( syn!(Ident) => { Into::into } | keyword!(super) => { Into::into } @@ -370,44 +425,6 @@ pub mod parsing { keyword!(extern) => { Into::into } )); - named!(pub qpath -> (Option, Path), alt!( - map!(syn!(Path), |p| (None, p)) - | - do_parse!( - lt: punct!(<) >> - this: syn!(Type) >> - path: option!(tuple!(keyword!(as), syn!(Path))) >> - gt: punct!(>) >> - colon2: punct!(::) >> - rest: call!(Punctuated::parse_separated_nonempty) >> - ({ - let (pos, as_, path) = match path { - Some((as_, mut path)) => { - let pos = path.segments.len(); - path.segments.push_punct(colon2); - path.segments.extend(rest.into_pairs()); - (pos, Some(as_), path) - } - None => { - (0, None, Path { - leading_colon: Some(colon2), - segments: rest, - }) - } - }; - (Some(QSelf { - lt_token: lt, - ty: Box::new(this), - position: pos, - as_token: as_, - gt_token: gt, - }), path) - }) - ) - | - map!(keyword!(self), |s| (None, s.into())) - )); - named!(pub ty_no_eq_after -> Type, do_parse!( ty: syn!(Type) >> not!(punct!(=)) >> -- cgit v1.2.3