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_hash_elements(hash_ast) ⇒ Object
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.
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.
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 |
#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.
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 |
#print_hash_elements(hash_ast) ⇒ Object
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 |