Skip to content

Commit

Permalink
Support method receiver parameters (breaking change)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Jun 26, 2022
1 parent f44bcea commit 18c920e
Show file tree
Hide file tree
Showing 21 changed files with 581 additions and 243 deletions.
66 changes: 50 additions & 16 deletions src/parse_fn.rs
Expand Up @@ -3,7 +3,10 @@ use crate::parse_type::{
};
use crate::parse_utils::{consume_attributes, consume_comma, consume_stuff_until, parse_ident};
use crate::punctuated::Punctuated;
use crate::types::{Function, FunctionParameter, FunctionQualifiers, GroupSpan, TyExpr};
use crate::types::{
Function, FunctionParameter, FunctionQualifiers, FunctionReceiverParameter,
FunctionTypedParameter, GroupSpan, TyExpr,
};
use crate::{Attribute, VisMarker};
use proc_macro2::{Delimiter, Ident, Punct, TokenStream, TokenTree};
use std::iter::Peekable;
Expand Down Expand Up @@ -165,27 +168,58 @@ pub(crate) fn parse_fn_params(tokens: TokenStream) -> Punctuated<FunctionParamet
}
let attributes = consume_attributes(&mut tokens);

// TODO - handle non-ident argument names
let ident = parse_ident(tokens.next().unwrap()).unwrap();

// TODO - Handle self parameter
let tk_colon = match tokens.next() {
Some(TokenTree::Punct(punct)) if punct.as_char() == ':' => punct.clone(),
_ => panic!("cannot parse fn params"),
let tk_ref = match tokens.peek() {
Some(TokenTree::Punct(punct)) if punct.as_char() == '&' => {
let ref_symbol = punct.clone();
tokens.next();
Some(ref_symbol)
}
_ => None,
};
let tk_mut = match tokens.peek() {
Some(TokenTree::Ident(ident)) if ident == "mut" => {
let mut_ident = ident.clone();
tokens.next();
Some(mut_ident)
}
_ => None,
};
let tk_self = match tokens.peek() {
Some(TokenTree::Ident(ident)) if ident == "self" => {
let self_ident = ident.clone();
tokens.next();
Some(self_ident)
}
_ => None,
};

let ty_tokens = consume_field_type(&mut tokens);
let comma = consume_comma(&mut tokens);

fields.push(
FunctionParameter {
let param = if let Some(tk_self) = tk_self {
FunctionParameter::Receiver(FunctionReceiverParameter {
attributes,
tk_ref,
tk_mut,
tk_self,
})
} else {
// TODO - handle non-ident argument names
let ident = parse_ident(tokens.next().unwrap()).unwrap();
let tk_colon = match tokens.next() {
Some(TokenTree::Punct(punct)) if punct.as_char() == ':' => punct.clone(),
_ => panic!("cannot parse fn params"),
};
let ty_tokens = consume_field_type(&mut tokens);
FunctionParameter::Typed(FunctionTypedParameter {
attributes,
tk_mut,
name: ident,
tk_colon,
ty: TyExpr { tokens: ty_tokens },
},
comma,
);
})
};

let comma = consume_comma(&mut tokens);

fields.push(param, comma);
}

fields
Expand Down
27 changes: 15 additions & 12 deletions src/snapshots/venial__tests__parse_all_kw_fn.snap
Expand Up @@ -46,19 +46,22 @@ Function(
generic_params: None,
tk_params_parens: (),
params: [
FunctionParameter {
attributes: [],
name: Ident(
b,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
Typed(
FunctionTypedParameter {
attributes: [],
tk_mut: None,
name: Ident(
b,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
},
ty: [
f32,
],
},
ty: [
f32,
],
},
),
],
where_clause: None,
tk_return_arrow: None,
Expand Down
27 changes: 15 additions & 12 deletions src/snapshots/venial__tests__parse_async_fn.snap
Expand Up @@ -26,19 +26,22 @@ Function(
generic_params: None,
tk_params_parens: (),
params: [
FunctionParameter {
attributes: [],
name: Ident(
b,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
Typed(
FunctionTypedParameter {
attributes: [],
tk_mut: None,
name: Ident(
b,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
},
ty: [
f32,
],
},
ty: [
f32,
],
},
),
],
where_clause: None,
tk_return_arrow: None,
Expand Down
27 changes: 15 additions & 12 deletions src/snapshots/venial__tests__parse_attr_fn.snap
Expand Up @@ -32,19 +32,22 @@ Function(
generic_params: None,
tk_params_parens: (),
params: [
FunctionParameter {
attributes: [],
name: Ident(
a,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
Typed(
FunctionTypedParameter {
attributes: [],
tk_mut: None,
name: Ident(
a,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
},
ty: [
i32,
],
},
ty: [
i32,
],
},
),
],
where_clause: None,
tk_return_arrow: None,
Expand Down
27 changes: 15 additions & 12 deletions src/snapshots/venial__tests__parse_const_fn.snap
Expand Up @@ -26,19 +26,22 @@ Function(
generic_params: None,
tk_params_parens: (),
params: [
FunctionParameter {
attributes: [],
name: Ident(
b,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
Typed(
FunctionTypedParameter {
attributes: [],
tk_mut: None,
name: Ident(
b,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
},
ty: [
f32,
],
},
ty: [
f32,
],
},
),
],
where_clause: None,
tk_return_arrow: None,
Expand Down
27 changes: 15 additions & 12 deletions src/snapshots/venial__tests__parse_default_fn.snap
Expand Up @@ -26,19 +26,22 @@ Function(
generic_params: None,
tk_params_parens: (),
params: [
FunctionParameter {
attributes: [],
name: Ident(
b,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
Typed(
FunctionTypedParameter {
attributes: [],
tk_mut: None,
name: Ident(
b,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
},
ty: [
f32,
],
},
ty: [
f32,
],
},
),
],
where_clause: None,
tk_return_arrow: None,
Expand Down
27 changes: 15 additions & 12 deletions src/snapshots/venial__tests__parse_extern_abi_fn.snap
Expand Up @@ -30,19 +30,22 @@ Function(
generic_params: None,
tk_params_parens: (),
params: [
FunctionParameter {
attributes: [],
name: Ident(
b,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
Typed(
FunctionTypedParameter {
attributes: [],
tk_mut: None,
name: Ident(
b,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
},
ty: [
f32,
],
},
ty: [
f32,
],
},
),
],
where_clause: None,
tk_return_arrow: None,
Expand Down
27 changes: 15 additions & 12 deletions src/snapshots/venial__tests__parse_extern_fn.snap
Expand Up @@ -26,19 +26,22 @@ Function(
generic_params: None,
tk_params_parens: (),
params: [
FunctionParameter {
attributes: [],
name: Ident(
b,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
Typed(
FunctionTypedParameter {
attributes: [],
tk_mut: None,
name: Ident(
b,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
},
ty: [
f32,
],
},
ty: [
f32,
],
},
),
],
where_clause: None,
tk_return_arrow: None,
Expand Down
54 changes: 30 additions & 24 deletions src/snapshots/venial__tests__parse_fn.snap
Expand Up @@ -20,32 +20,38 @@ Function(
generic_params: None,
tk_params_parens: (),
params: [
FunctionParameter {
attributes: [],
name: Ident(
a,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
Typed(
FunctionTypedParameter {
attributes: [],
tk_mut: None,
name: Ident(
a,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
},
ty: [
i32,
],
},
ty: [
i32,
],
},
FunctionParameter {
attributes: [],
name: Ident(
b,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
),
Typed(
FunctionTypedParameter {
attributes: [],
tk_mut: None,
name: Ident(
b,
),
tk_colon: Punct {
char: ':',
spacing: Alone,
},
ty: [
f32,
],
},
ty: [
f32,
],
},
),
],
where_clause: None,
tk_return_arrow: Some(
Expand Down

0 comments on commit 18c920e

Please sign in to comment.