parser: support named args in new expressions

This commit is contained in:
Ryan Chandler 2022-08-10 00:34:22 +01:00
parent cd45e0a423
commit 4068931aa1
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 41 additions and 5 deletions

View File

@ -383,7 +383,7 @@ pub enum Expression {
},
New {
target: Box<Self>,
args: Vec<Self>
args: Vec<Arg>,
},
ConstantString {
value: String,

View File

@ -1279,9 +1279,23 @@ impl Parser {
self.lparen()?;
while self.current.kind != TokenKind::RightParen {
let mut name = None;
let mut unpack = false;
if matches!(self.current.kind, TokenKind::Identifier(_)) && self.peek.kind == TokenKind::Colon {
name = Some(self.ident_maybe_reserved()?);
self.next();
} else if self.current.kind == TokenKind::Ellipsis {
self.next();
unpack = true;
}
let value = self.expression(0)?;
args.push(value);
args.push(Arg {
name,
unpack,
value
});
self.optional_comma()?;
}
@ -1325,9 +1339,23 @@ impl Parser {
self.lparen()?;
while self.current.kind != TokenKind::RightParen {
let mut name = None;
let mut unpack = false;
if matches!(self.current.kind, TokenKind::Identifier(_)) && self.peek.kind == TokenKind::Colon {
name = Some(self.ident_maybe_reserved()?);
self.next();
} else if self.current.kind == TokenKind::Ellipsis {
self.next();
unpack = true;
}
let value = self.expression(0)?;
args.push(value);
args.push(Arg {
name,
unpack,
value
});
self.optional_comma()?;
}
@ -2399,8 +2427,16 @@ mod tests {
body: vec![]
}),
args: vec![
Expression::Int { i: 1 },
Expression::Int { i: 2 },
Arg {
name: None,
value: Expression::Int { i: 1 },
unpack: false,
},
Arg {
name: None,
value: Expression::Int { i: 2 },
unpack: false,
},
],
})
]);