Skip to content

Remove redundant namespaces passed around by parser #504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 28 additions & 29 deletions syntax/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn parse_items(
Item::ForeignMod(foreign_mod) => {
parse_foreign_mod(cx, foreign_mod, &mut apis, trusted, namespace)
}
Item::Impl(item) => match parse_impl(item, namespace) {
Item::Impl(item) => match parse_impl(item) {
Ok(imp) => apis.push(imp),
Err(err) => cx.push(err),
},
Expand Down Expand Up @@ -95,7 +95,7 @@ fn parse_struct(cx: &mut Errors, item: ItemStruct, namespace: &Namespace) -> Res
.map(|field| {
Ok(Var {
ident: field.ident.unwrap(),
ty: parse_type(&field.ty, &namespace)?,
ty: parse_type(&field.ty)?,
})
})
.collect::<Result<_>>()?;
Expand Down Expand Up @@ -438,7 +438,7 @@ fn parse_extern_fn(
}
_ => return Err(Error::new_spanned(arg, "unsupported signature")),
};
let ty = parse_type(&arg.ty, &namespace)?;
let ty = parse_type(&arg.ty)?;
if ident != "self" {
args.push_value(Var { ident, ty });
if let Some(comma) = comma {
Expand Down Expand Up @@ -468,7 +468,7 @@ fn parse_extern_fn(
}

let mut throws_tokens = None;
let ret = parse_return_type(&foreign_fn.sig.output, &mut throws_tokens, &namespace)?;
let ret = parse_return_type(&foreign_fn.sig.output, &mut throws_tokens)?;
let throws = throws_tokens.is_some();
let unsafety = foreign_fn.sig.unsafety;
let fn_token = foreign_fn.sig.fn_token;
Expand Down Expand Up @@ -559,7 +559,7 @@ fn parse_extern_verbatim(
}
}

fn parse_impl(imp: ItemImpl, namespace: &Namespace) -> Result<Api> {
fn parse_impl(imp: ItemImpl) -> Result<Api> {
if !imp.items.is_empty() {
let mut span = Group::new(Delimiter::Brace, TokenStream::new());
span.set_span(imp.brace_token.span);
Expand All @@ -585,7 +585,7 @@ fn parse_impl(imp: ItemImpl, namespace: &Namespace) -> Result<Api> {

Ok(Api::Impl(Impl {
impl_token: imp.impl_token,
ty: parse_type(&self_ty, namespace)?,
ty: parse_type(&self_ty)?,
brace_token: imp.brace_token,
}))
}
Expand Down Expand Up @@ -634,20 +634,20 @@ fn parse_include(input: ParseStream) -> Result<Include> {
Err(input.error("expected \"quoted/path/to\" or <bracketed/path/to>"))
}

fn parse_type(ty: &RustType, namespace: &Namespace) -> Result<Type> {
fn parse_type(ty: &RustType) -> Result<Type> {
match ty {
RustType::Reference(ty) => parse_type_reference(ty, namespace),
RustType::Path(ty) => parse_type_path(ty, namespace),
RustType::Slice(ty) => parse_type_slice(ty, namespace),
RustType::Array(ty) => parse_type_array(ty, namespace),
RustType::BareFn(ty) => parse_type_fn(ty, namespace),
RustType::Reference(ty) => parse_type_reference(ty),
RustType::Path(ty) => parse_type_path(ty),
RustType::Slice(ty) => parse_type_slice(ty),
RustType::Array(ty) => parse_type_array(ty),
RustType::BareFn(ty) => parse_type_fn(ty),
RustType::Tuple(ty) if ty.elems.is_empty() => Ok(Type::Void(ty.paren_token.span)),
_ => Err(Error::new_spanned(ty, "unsupported type")),
}
}

fn parse_type_reference(ty: &TypeReference, namespace: &Namespace) -> Result<Type> {
let inner = parse_type(&ty.elem, namespace)?;
fn parse_type_reference(ty: &TypeReference) -> Result<Type> {
let inner = parse_type(&ty.elem)?;
let which = match &inner {
Type::Ident(ident) if ident.rust == "str" => {
if ty.mutability.is_some() {
Expand All @@ -673,7 +673,7 @@ fn parse_type_reference(ty: &TypeReference, namespace: &Namespace) -> Result<Typ
})))
}

fn parse_type_path(ty: &TypePath, namespace: &Namespace) -> Result<Type> {
fn parse_type_path(ty: &TypePath) -> Result<Type> {
let path = &ty.path;
if ty.qself.is_none() && path.leading_colon.is_none() && path.segments.len() == 1 {
let segment = &path.segments[0];
Expand All @@ -683,7 +683,7 @@ fn parse_type_path(ty: &TypePath, namespace: &Namespace) -> Result<Type> {
PathArguments::AngleBracketed(generic) => {
if ident == "UniquePtr" && generic.args.len() == 1 {
if let GenericArgument::Type(arg) = &generic.args[0] {
let inner = parse_type(arg, namespace)?;
let inner = parse_type(arg)?;
return Ok(Type::UniquePtr(Box::new(Ty1 {
name: ident,
langle: generic.lt_token,
Expand All @@ -693,7 +693,7 @@ fn parse_type_path(ty: &TypePath, namespace: &Namespace) -> Result<Type> {
}
} else if ident == "CxxVector" && generic.args.len() == 1 {
if let GenericArgument::Type(arg) = &generic.args[0] {
let inner = parse_type(arg, namespace)?;
let inner = parse_type(arg)?;
return Ok(Type::CxxVector(Box::new(Ty1 {
name: ident,
langle: generic.lt_token,
Expand All @@ -703,7 +703,7 @@ fn parse_type_path(ty: &TypePath, namespace: &Namespace) -> Result<Type> {
}
} else if ident == "Box" && generic.args.len() == 1 {
if let GenericArgument::Type(arg) = &generic.args[0] {
let inner = parse_type(arg, namespace)?;
let inner = parse_type(arg)?;
return Ok(Type::RustBox(Box::new(Ty1 {
name: ident,
langle: generic.lt_token,
Expand All @@ -713,7 +713,7 @@ fn parse_type_path(ty: &TypePath, namespace: &Namespace) -> Result<Type> {
}
} else if ident == "Vec" && generic.args.len() == 1 {
if let GenericArgument::Type(arg) = &generic.args[0] {
let inner = parse_type(arg, namespace)?;
let inner = parse_type(arg)?;
return Ok(Type::RustVec(Box::new(Ty1 {
name: ident,
langle: generic.lt_token,
Expand All @@ -723,7 +723,7 @@ fn parse_type_path(ty: &TypePath, namespace: &Namespace) -> Result<Type> {
}
} else if ident == "Pin" && generic.args.len() == 1 {
if let GenericArgument::Type(arg) = &generic.args[0] {
let inner = parse_type(arg, namespace)?;
let inner = parse_type(arg)?;
let pin_token = kw::Pin(ident.span());
if let Type::Ref(mut inner) = inner {
inner.pinned = true;
Expand All @@ -740,16 +740,16 @@ fn parse_type_path(ty: &TypePath, namespace: &Namespace) -> Result<Type> {
Err(Error::new_spanned(ty, "unsupported type"))
}

fn parse_type_slice(ty: &TypeSlice, namespace: &Namespace) -> Result<Type> {
let inner = parse_type(&ty.elem, namespace)?;
fn parse_type_slice(ty: &TypeSlice) -> Result<Type> {
let inner = parse_type(&ty.elem)?;
Ok(Type::Slice(Box::new(Slice {
bracket: ty.bracket_token,
inner,
})))
}

fn parse_type_array(ty: &TypeArray, namespace: &Namespace) -> Result<Type> {
let inner = parse_type(&ty.elem, namespace)?;
fn parse_type_array(ty: &TypeArray) -> Result<Type> {
let inner = parse_type(&ty.elem)?;

let len_expr = if let Expr::Lit(lit) = &ty.len {
lit
Expand Down Expand Up @@ -778,7 +778,7 @@ fn parse_type_array(ty: &TypeArray, namespace: &Namespace) -> Result<Type> {
})))
}

fn parse_type_fn(ty: &TypeBareFn, namespace: &Namespace) -> Result<Type> {
fn parse_type_fn(ty: &TypeBareFn) -> Result<Type> {
if ty.lifetimes.is_some() {
return Err(Error::new_spanned(
ty,
Expand All @@ -796,7 +796,7 @@ fn parse_type_fn(ty: &TypeBareFn, namespace: &Namespace) -> Result<Type> {
.iter()
.enumerate()
.map(|(i, arg)| {
let ty = parse_type(&arg.ty, namespace)?;
let ty = parse_type(&arg.ty)?;
let ident = match &arg.name {
Some(ident) => ident.0.clone(),
None => format_ident!("_{}", i),
Expand All @@ -805,7 +805,7 @@ fn parse_type_fn(ty: &TypeBareFn, namespace: &Namespace) -> Result<Type> {
})
.collect::<Result<_>>()?;
let mut throws_tokens = None;
let ret = parse_return_type(&ty.output, &mut throws_tokens, namespace)?;
let ret = parse_return_type(&ty.output, &mut throws_tokens)?;
let throws = throws_tokens.is_some();
Ok(Type::Fn(Box::new(Signature {
unsafety: ty.unsafety,
Expand All @@ -823,7 +823,6 @@ fn parse_type_fn(ty: &TypeBareFn, namespace: &Namespace) -> Result<Type> {
fn parse_return_type(
ty: &ReturnType,
throws_tokens: &mut Option<(kw::Result, Token![<], Token![>])>,
namespace: &Namespace,
) -> Result<Option<Type>> {
let mut ret = match ty {
ReturnType::Default => return Ok(None),
Expand All @@ -845,7 +844,7 @@ fn parse_return_type(
}
}
}
match parse_type(ret, namespace)? {
match parse_type(ret)? {
Type::Void(_) => Ok(None),
ty => Ok(Some(ty)),
}
Expand Down