Class: Yadriggy::PrettyPrinter
- Defined in:
- lib/yadriggy/pretty_print.rb
Overview
Pretty printer for Ruby
Constant Summary
Constants included from Yadriggy
Instance Attribute Summary collapse
-
#printer ⇒ Printer
readonly
A Printer object.
Class Method Summary collapse
-
.ast_to_s(an_ast) ⇒ String
Obtains the string representation of the given AST.
Instance Method Summary collapse
-
#empty_params?(params_ast) ⇒ Boolean
True if the given parameter list is empty.
-
#initialize(printer) ⇒ PrettyPrinter
constructor
A new instance of PrettyPrinter.
-
#print(an_ast) ⇒ PrettyPrinter
Prints a given AST by #printer.
-
#print_arguments(args_ast, block_arg, block, is_cmd, no_empty_paren = true) ⇒ void
Prints an argument list.
- #print_each(array, first) ⇒ Object
- #print_hash_elements(hash_ast) ⇒ Object
- #print_list(array, first) ⇒ Object
- #print_parameters(params_ast) ⇒ Object
Methods inherited from Checker
all_rules, #apply_typing_rule, #ast, #ast_env, #check, #check_all, check_init_class, #check_later, #error, #error_found!, #error_group, find_rule_entry, #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.
22 23 24 25 |
# File 'lib/yadriggy/pretty_print.rb', line 22 def initialize(printer) super() @printer = printer end |
Instance Attribute Details
#printer ⇒ Printer (readonly)
Returns a Yadriggy::Printer object.
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.
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.
316 317 318 319 320 321 |
# File 'lib/yadriggy/pretty_print.rb', line 316 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 |
#print(an_ast) ⇒ PrettyPrinter
Prints a given AST by #printer.
30 31 32 33 |
# File 'lib/yadriggy/pretty_print.rb', line 30 def print(an_ast) check_all(an_ast) self end |
#print_arguments(args_ast, block_arg, block, is_cmd, no_empty_paren = true) ⇒ void
This method returns an undefined value.
Prints an argument list.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/yadriggy/pretty_print.rb', line 153 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 |
#print_each(array, first) ⇒ Object
462 463 464 465 466 467 468 469 470 471 472 473 474 |
# File 'lib/yadriggy/pretty_print.rb', line 462 def print_each(array, first) array.each do |e| if e if first first = false else @printer << ', ' end print(e) end end first end |
#print_hash_elements(hash_ast) ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/yadriggy/pretty_print.rb', line 127 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 |
#print_list(array, first) ⇒ Object
477 478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/yadriggy/pretty_print.rb', line 477 def print_list(array, first) array.each do |e| if e if first first = false else @printer << ', ' end yield e end end first end |
#print_parameters(params_ast) ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/yadriggy/pretty_print.rb', line 280 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 |