Skip to content

Commit b2c05b7

Browse files
committed
Pretty-print lambdef and yield.
1 parent 4305a78 commit b2c05b7

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

src/visitors/printer.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,47 @@ fn format_typed_param(param: &(Name, Option<Expression>, Option<Expression>)) ->
405405
s
406406
}
407407

408+
fn format_untyped_params(param: &UntypedArgsList) -> String {
409+
let UntypedArgsList { ref positional_args, ref star_args, ref keyword_args, ref star_kwargs } = *param;
410+
let mut s = String::new();
411+
412+
s.push_str(&comma_join(positional_args.iter().map(format_untyped_param)));
413+
if positional_args.len() > 0 {
414+
s.push_str(", ");
415+
}
416+
417+
match star_args {
418+
StarParams::No => (),
419+
StarParams::Anonymous => s.push_str("*, "),
420+
StarParams::Named(ref name) => {
421+
s.push_str(name);
422+
},
423+
}
424+
425+
s.push_str(&comma_join(keyword_args.iter().map(format_untyped_param)));
426+
if keyword_args.len() > 0 {
427+
s.push_str(", ");
428+
}
429+
430+
if let Some(name) = star_kwargs {
431+
s.push_str("**");
432+
s.push_str(name);
433+
s.push_str(", ");
434+
}
435+
436+
s
437+
}
438+
439+
fn format_untyped_param(param: &(Name, Option<Expression>)) -> String {
440+
let (name, value) = param;
441+
let mut s = name.to_string();
442+
if let Some(ref value) = value {
443+
s.push_str("=");
444+
s.push_str(&format_expr(value));
445+
}
446+
s
447+
}
448+
408449
fn format_subscript(sub: &Subscript) -> String {
409450
match *sub {
410451
Subscript::Simple(ref e) => format_expr(e),
@@ -496,9 +537,11 @@ fn format_expr(e: &Expression) -> String {
496537
format!("({}) if ({}) else ({})", format_expr(e1), format_expr(e2), format_expr(e3)),
497538
Expression::Star(ref e) =>
498539
format!("*{}", format_expr(e)),
499-
Expression::Yield(_) => unimplemented!(),
500-
Expression::YieldFrom(_) => unimplemented!(),
501-
Expression::Lambdef(_, _) => unimplemented!(),
540+
Expression::Yield(ref items) =>
541+
format!("yield {}", comma_join(items.iter().map(format_expr))),
542+
Expression::YieldFrom(ref iterable) => format!("yield from {}", format_expr(iterable)),
543+
Expression::Lambdef(ref params, ref body) =>
544+
format!("lambda {}: {}", format_untyped_params(params), format_expr(body))
502545
}
503546
}
504547

0 commit comments

Comments
 (0)