Skip to content
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

fix(syn-solidity): parse modifiers without parens #284

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
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
38 changes: 27 additions & 11 deletions crates/syn-solidity/src/item/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ pub struct ItemFunction {
pub attrs: Vec<Attribute>,
pub kind: FunctionKind,
pub name: Option<SolIdent>,
pub paren_token: Paren,
/// Parens are optional for modifiers:
/// <https://docs.soliditylang.org/en/latest/grammar.html#a4.SolidityParser.modifierDefinition>
pub paren_token: Option<Paren>,
pub arguments: ParameterList,
/// The Solidity attributes of the function.
pub attributes: FunctionAttributes,
Expand All @@ -51,16 +53,30 @@ impl fmt::Debug for ItemFunction {

impl Parse for ItemFunction {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let content;
let attrs = input.call(Attribute::parse_outer)?;
let kind: FunctionKind = input.parse()?;
let name = input.call(SolIdent::parse_opt)?;

let (paren_token, arguments) = if kind.is_modifier() && !input.peek(Paren) {
(None, ParameterList::new())
} else {
let content;
(Some(parenthesized!(content in input)), content.parse()?)
};

let attributes = input.parse()?;
let returns = input.call(Returns::parse_opt)?;
let body = input.parse()?;

Ok(Self {
attrs: input.call(Attribute::parse_outer)?,
kind: input.parse()?,
name: input.call(SolIdent::parse_opt)?,
paren_token: parenthesized!(content in input),
arguments: content.parse()?,
attributes: input.parse()?,
returns: input.call(Returns::parse_opt)?,
body: input.parse()?,
attrs,
kind,
name,
paren_token,
arguments,
attributes,
returns,
body,
})
}
}
Expand Down Expand Up @@ -92,7 +108,7 @@ impl ItemFunction {
attrs: Vec::new(),
kind,
name,
paren_token: Paren(span),
paren_token: Some(Paren(span)),
arguments: Parameters::new(),
attributes: FunctionAttributes::new(),
returns: None,
Expand Down
21 changes: 21 additions & 0 deletions crates/syn-solidity/tests/parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use proc_macro2::Span;
use syn::parse_quote;
use syn_solidity::{FunctionKind, ItemFunction};

#[test]
fn modifiers() {
let none: ItemFunction = parse_quote! {
modifier noParens {
_;
}
};
let some: ItemFunction = parse_quote! {
modifier withParens() {
_;
}
};
assert_eq!(none.kind, FunctionKind::new_modifier(Span::call_site()));
assert_eq!(none.kind, some.kind);
assert_eq!(none.paren_token, None);
assert_eq!(some.paren_token, Some(Default::default()));
}
Loading