class Yadriggy::PrettyPrinter
Pretty printer for Ruby
Attributes
@return [Printer] a {Printer} object.
Public Class Methods
Obtains the string representation of the given AST.
@param [ASTree|ASTnode] an_ast the AST. @return [String] the string representation of the AST.
# File lib/yadriggy/pretty_print.rb, line 13 def self.ast_to_s(an_ast) pp = PrettyPrinter.new(Printer.new) pp.print(an_ast).printer.output end
@param [Printer] printer
Yadriggy::Checker::new
# File lib/yadriggy/pretty_print.rb, line 22 def initialize(printer) super() @printer = printer end
Public Instance Methods
@param [Parameters] params_ast a parameter list. @return [Boolean] true if the given parameter list is empty.
# File lib/yadriggy/pretty_print.rb, line 324 def empty_params?(params_ast) params_ast.params.empty? && params_ast.optionals.empty? && params_ast.rest_of_params.nil? && params_ast.params_after_rest.empty? && params_ast.keywords.empty? && params_ast.rest_of_keywords.nil? && params_ast.block_param.nil? end
Prints a given AST by {#printer}. @param [ASTree|ASTnode] an_ast the AST. @return [PrettyPrinter] the `self` object.
# File lib/yadriggy/pretty_print.rb, line 30 def print(an_ast) check_all(an_ast) self end
Prints an argument list.
@param [Array<ASTnode>] args_ast an argument list. @param [ASTnode] block_arg a block argument. @param [ASTnode] block a block. @param [Boolean] is_cmd true if opening/closing parentheses are not written
(true if the arguments are for a command).
@param [Boolean] no_empty_paren true if `()` is not printed when an argument list is empty. @return [void]
# File lib/yadriggy/pretty_print.rb, line 161 def print_arguments(args_ast, block_arg, block, is_cmd, no_empty_paren=true) if is_cmd @printer << ' ' else @printer << '(' unless no_empty_paren && args_ast.empty? end is_first = print_list(args_ast, true) do |a| if a.is_a?(HashLiteral) && args_ast.last == a print_hash_elements(a) else print(a) end end if block_arg @printer << ', ' unless is_first @printer << '&' print(block_arg) end unless is_cmd @printer << ')' unless no_empty_paren && args_ast.empty? end if block @printer << ' ' print(block) end end
@api private
# File lib/yadriggy/pretty_print.rb, line 470 def print_each(array, first) array.each do |e| if e if first first = false else @printer << ', ' end print(e) end end first end
# File lib/yadriggy/pretty_print.rb, line 135 def print_hash_elements(hash_ast) print_list(hash_ast.pairs, true) do |pair| print(pair[0]) @printer << ' ' @printer << '=> ' unless pair[0].is_a?(Label) print(pair[1]) end end
@api private
# File lib/yadriggy/pretty_print.rb, line 485 def print_list(array, first) array.each do |e| if e if first first = false else @printer << ', ' end yield e end end first end
@api private
# File lib/yadriggy/pretty_print.rb, line 288 def print_parameters(params_ast) is_first = true is_first = print_each(params_ast.params, is_first) is_first = print_list(params_ast.optionals, is_first) do |p| print(p[0]) @printer << '=' print(p[1]) end is_first = print_list([params_ast.rest_of_params], is_first) do |p| @printer << '*' print(p) end is_first = print_each(params_ast.params_after_rest, is_first) is_first = print_list(params_ast.keywords, is_first) do |kv| print(kv[0]) @printer << ': ' print(kv[1]) end is_first = print_list([params_ast.rest_of_keywords], is_first) do |p| @printer << '**' print(p) end is_first = print_list([params_ast.block_param], is_first) do |p| @printer << '&' print(p) end end