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

Indexed event param #22

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
89 changes: 70 additions & 19 deletions crates/ast/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ fn sol_type<'tokens, 'src: 'tokens>() -> impl Parser<'tokens, 'src, Spanned<DynS
recursive(|sol_raw_type| {
let sol_raw_primitive_type = ident().map(|(typ, _)| typ.to_string());

let sol_raw_event_primitive_type = ident()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would also allow indexed args inside tuples and inside function args. Maybe we should give the sol_type_parser a flag to enable "indexed" args.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, let me see how I can fix this.

.map(|(typ, _)| typ.to_string())
.then_ignore(just(Ident("indexed")));

let sol_raw_tuple_type = sol_raw_type
.separated_by(punct(','))
.collect::<Vec<_>>()
Expand All @@ -353,26 +357,30 @@ fn sol_type<'tokens, 'src: 'tokens>() -> impl Parser<'tokens, 'src, Spanned<DynS
result
});

choice((sol_raw_primitive_type, sol_raw_tuple_type))
.then(
punct('[')
.ignore_then(dec().or_not())
.then_ignore(punct(']'))
.or_not(),
)
.then_ignore(ident().or_not())
.map(|(typ, slice)| {
let mut result = typ;
if let Some(size) = slice {
result.push('[');
if let Some((n, _span)) = size {
result.push_str(&n.to_string());
}
result.push(']');
choice((
sol_raw_event_primitive_type,
sol_raw_primitive_type,
sol_raw_tuple_type,
))
.then(
punct('[')
.ignore_then(dec().or_not())
.then_ignore(punct(']'))
.or_not(),
)
.then_ignore(ident().or_not())
.map(|(typ, slice)| {
let mut result = typ;
if let Some(size) = slice {
result.push('[');
if let Some((n, _span)) = size {
result.push_str(&n.to_string());
}
result
})
.boxed()
result.push(']');
}
result
})
.boxed()
})
.try_map_with(|typ, ex| {
DynSolType::parse(&typ)
Expand Down Expand Up @@ -812,6 +820,22 @@ mod tests {
rets: Box::new([(DynSolType::parse("uint256").unwrap(), span)]),
})
);
assert_err!(
sol_function(),
vec![
Ident("function"),
Ident("balanceOf"),
Punct('('),
Ident("address"),
Ident("indexed"),
Punct(')'),
Ident("returns"),
Punct('('),
Ident("uint256"),
Punct(')')
],
"word overflows"
);
}

#[test]
Expand Down Expand Up @@ -839,6 +863,33 @@ mod tests {
]),
})
);
assert_ok!(
sol_event(),
vec![
Ident("event"),
Ident("Transfer"),
Punct('('),
Ident("address"),
Ident("indexed"),
Ident("sender"),
Punct(','),
Ident("address"),
Ident("indexed"),
Ident("recipient"),
Punct(','),
Ident("uint256"),
Ident("amount"),
Punct(')')
],
ast::Definition::SolEvent(ast::SolEvent {
name: ("Transfer", span),
args: Box::new([
(DynSolType::parse("address").unwrap(), span),
(DynSolType::parse("address").unwrap(), span),
(DynSolType::parse("uint256").unwrap(), span),
]),
})
);
}

#[test]
Expand Down
Loading