mirror of
https://github.com/danog/parser.git
synced 2024-11-27 04:14:55 +01:00
parser: support named args in new expressions
This commit is contained in:
parent
cd45e0a423
commit
4068931aa1
@ -383,7 +383,7 @@ pub enum Expression {
|
||||
},
|
||||
New {
|
||||
target: Box<Self>,
|
||||
args: Vec<Self>
|
||||
args: Vec<Arg>,
|
||||
},
|
||||
ConstantString {
|
||||
value: String,
|
||||
|
@ -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,
|
||||
},
|
||||
],
|
||||
})
|
||||
]);
|
||||
|
Loading…
Reference in New Issue
Block a user