Skip to content

Commit f53d93e

Browse files
committed
Add ArgList::args_maybe_empty to node_ext
1 parent 9af7449 commit f53d93e

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

crates/syntax/src/ast/node_ext.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ fn text_of_first_token(node: &SyntaxNode) -> TokenText<'_> {
7777
}
7878
}
7979

80+
fn into_comma(it: NodeOrToken<SyntaxNode, SyntaxToken>) -> Option<SyntaxToken> {
81+
let token = match it {
82+
NodeOrToken::Token(it) => it,
83+
NodeOrToken::Node(node) if node.kind() == SyntaxKind::ERROR => node.first_token()?,
84+
NodeOrToken::Node(_) => return None,
85+
};
86+
(token.kind() == T![,]).then_some(token)
87+
}
88+
8089
impl ast::Abi {
8190
pub fn abi_string(&self) -> Option<ast::String> {
8291
support::token(&self.syntax, SyntaxKind::STRING).and_then(ast::String::cast)
@@ -1037,6 +1046,21 @@ impl ast::GenericParamList {
10371046
}
10381047
}
10391048

1049+
impl ast::ArgList {
1050+
/// Comma separated args, argument may be empty
1051+
pub fn args_maybe_empty(&self) -> impl Iterator<Item = Option<ast::Expr>> {
1052+
// (Expr? ','?)*
1053+
let mut after_arg = false;
1054+
self.syntax().children_with_tokens().filter_map(move |it| {
1055+
if into_comma(it.clone()).is_some() {
1056+
if std::mem::take(&mut after_arg) { None } else { Some(None) }
1057+
} else {
1058+
Some(ast::Expr::cast(it.into_node()?).inspect(|_| after_arg = true))
1059+
}
1060+
})
1061+
}
1062+
}
1063+
10401064
impl ast::ForExpr {
10411065
pub fn iterable(&self) -> Option<ast::Expr> {
10421066
// If the iterable is a BlockExpr, check if the body is missing.

0 commit comments

Comments
 (0)