Class: Yadriggy::PrettyPrinter

Inherits:
Checker
  • Object
show all
Defined in:
lib/yadriggy/pretty_print.rb

Overview

Pretty printer for Ruby

Constant Summary

Constants included from Yadriggy

DynType, Undef, VERSION, Void

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Checker

#ast, #ast_env, #check, #check_all, #check_later, #error!, #error_found!, #error_messages, #errors?, #last_error, #make_base_env, #proceed, rule

Methods included from Yadriggy

debug, debug=, define_syntax, reify

Constructor Details

#initialize(printer) ⇒ PrettyPrinter

Returns a new instance of PrettyPrinter.

Parameters:



22
23
24
25
# File 'lib/yadriggy/pretty_print.rb', line 22

def initialize(printer)
  super()
  @printer = printer
end

Instance Attribute Details

#printerPrinter (readonly)

Returns a Yadriggy::Printer object.

Returns:



19
20
21
# File 'lib/yadriggy/pretty_print.rb', line 19

def printer
  @printer
end

Class Method Details

.ast_to_s(an_ast) ⇒ String

Obtains the string representation of the given AST.

Parameters:

Returns:

  • (String)

    the string representation of the AST.



13
14
15
16
# 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

Instance Method Details

#empty_params?(params_ast) ⇒ Boolean

Returns true if the given parameter list is empty.

Parameters:

Returns:

  • (Boolean)

    true if the given parameter list is empty.



324
325
326
327
328
329
# 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.

Parameters:

Returns:



30
31
32
33
# File 'lib/yadriggy/pretty_print.rb', line 30

def print(an_ast)
  check_all(an_ast)
  self
end

This method returns an undefined value.

Prints an argument list.

Parameters:

  • args_ast (Array<ASTnode>)

    an argument list.

  • block_arg (ASTnode)

    a block argument.

  • block (ASTnode)

    a block.

  • is_cmd (Boolean)

    true if opening/closing parentheses are not written (true if the arguments are for a command).

  • no_empty_paren (Boolean) (defaults to: true)

    true if () is not printed when an argument list is empty.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# 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


135
136
137
138
139
140
141
142
# 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