Debian Codegraph @

vendor/mruby/mrbgems/mruby-pp/mrblib/0_prettyprint.rb

raw
1 # Copyright (C) 2014 Kouhei Sutou <[email protected]>
2
3 # This file is based on prettyprint.rb in CRuby.
4
5 # This class implements a pretty printing algorithm. It finds line breaks and
6 # nice indentations for grouped structure.
7 #
8 # By default, the class assumes that primitive elements are strings and each
9 # byte in the strings have single column in width. But it can be used for
10 # other situations by giving suitable arguments for some methods:
11 # * newline object and space generation block for PrettyPrint.new
12 # * optional width argument for PrettyPrint#text
13 # * PrettyPrint#breakable
14 #
15 # There are several candidate uses:
16 # * text formatting using proportional fonts
17 # * multibyte characters which has columns different to number of bytes
18 # * non-string formatting
19 #
20 # == Bugs
21 # * Box based formatting?
22 # * Other (better) model/algorithm?
23 #
24 # Report any bugs at http://bugs.ruby-lang.org
25 #
26 # == References
27 # Christian Lindig, Strictly Pretty, March 2000,
28 # http://www.st.cs.uni-sb.de/~lindig/papers/#pretty
29 #
30 # Philip Wadler, A prettier printer, March 1998,
31 # http://homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier
32 #
33 # == Author
34 # Tanaka Akira <[email protected]>
35 #
36 class PrettyPrint
37
38 # This is a convenience method which is same as follows:
39 #
40 # begin
41 # q = PrettyPrint.new(output, maxwidth, newline, &genspace)
42 # ...
43 # q.flush
44 # output
45 # end
46 #
47 def PrettyPrint.format(output='', maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n})
48 q = PrettyPrint.new(output, maxwidth, newline, &genspace)
49 yield q
50 q.flush
51 output
52 end
53
54 # This is similar to PrettyPrint::format but the result has no breaks.
55 #
56 # +maxwidth+, +newline+ and +genspace+ are ignored.
57 #
58 # The invocation of +breakable+ in the block doesn't break a line and is
59 # treated as just an invocation of +text+.
60 #
61 def PrettyPrint.singleline_format(output='', maxwidth=nil, newline=nil, genspace=nil)
62 q = SingleLine.new(output)
63 yield q
64 output
65 end
66
67 # Creates a buffer for pretty printing.
68 #
69 # +output+ is an output target. If it is not specified, '' is assumed. It
70 # should have a << method which accepts the first argument +obj+ of
71 # PrettyPrint#text, the first argument +sep+ of PrettyPrint#breakable, the
72 # first argument +newline+ of PrettyPrint.new, and the result of a given
73 # block for PrettyPrint.new.
74 #
75 # +maxwidth+ specifies maximum line length. If it is not specified, 79 is
76 # assumed. However actual outputs may overflow +maxwidth+ if long
77 # non-breakable texts are provided.
78 #
79 # +newline+ is used for line breaks. "\n" is used if it is not specified.
80 #
81 # The block is used to generate spaces. {|width| ' ' * width} is used if it
82 # is not given.
83 #
84 def initialize(output='', maxwidth=79, newline="\n", &genspace)
85 @output = output
86 @maxwidth = maxwidth
87 @newline = newline
88 @genspace = genspace || lambda {|n| ' ' * n}
89
90 @output_width = 0
91 @buffer_width = 0
92 @buffer = []
93
94 root_group = Group.new(0)
95 @group_stack = [root_group]
96 @group_queue = GroupQueue.new(root_group)
97 @indent = 0
98 end
99
100 # The output object.
101 #
102 # This defaults to '', and should accept the << method
103 attr_reader :output
104
105 # The maximum width of a line, before it is separated in to a newline
106 #
107 # This defaults to 79, and should be a Fixnum
108 attr_reader :maxwidth
109
110 # The value that is appended to +output+ to add a new line.
111 #
112 # This defaults to "\n", and should be String
113 attr_reader :newline
114
115 # A lambda or Proc, that takes one argument, of a Fixnum, and returns
116 # the corresponding number of spaces.
117 #
118 # By default this is:
119 # lambda {|n| ' ' * n}
120 attr_reader :genspace
121
122 # The number of spaces to be indented
123 attr_reader :indent
124
125 # The PrettyPrint::GroupQueue of groups in stack to be pretty printed
126 attr_reader :group_queue
127
128 # Returns the group most recently added to the stack.
129 #
130 # Contrived example:
131 # out = ""
132 # => ""
133 # q = PrettyPrint.new(out)
134 # => #<PrettyPrint:0x82f85c0 @output="", @maxwidth=79, @newline="\n", @genspace=#<Proc:0x82f8368@/home/vbatts/.rvm/rubies/ruby-head/lib/ruby/2.0.0/prettyprint.rb:82 (lambda)>, @output_width=0, @buffer_width=0, @buffer=[], @group_stack=[#<PrettyPrint::Group:0x82f8138 @depth=0, @breakables=[], @break=false>], @group_queue=#<PrettyPrint::GroupQueue:0x82fb7c0 @queue=[[#<PrettyPrint::Group:0x82f8138 @depth=0, @breakables=[], @break=false>]]>, @indent=0>
135 # q.group {
136 # q.text q.current_group.inspect
137 # q.text q.newline
138 # q.group(q.current_group.depth + 1) {
139 # q.text q.current_group.inspect
140 # q.text q.newline
141 # q.group(q.current_group.depth + 1) {
142 # q.text q.current_group.inspect
143 # q.text q.newline
144 # q.group(q.current_group.depth + 1) {
145 # q.text q.current_group.inspect
146 # q.text q.newline
147 # }
148 # }
149 # }
150 # }
151 # => 284
152 # puts out
153 # #<PrettyPrint::Group:0x8354758 @depth=1, @breakables=[], @break=false>
154 # #<PrettyPrint::Group:0x8354550 @depth=2, @breakables=[], @break=false>
155 # #<PrettyPrint::Group:0x83541cc @depth=3, @breakables=[], @break=false>
156 # #<PrettyPrint::Group:0x8347e54 @depth=4, @breakables=[], @break=false>
157 def current_group
158 @group_stack.last
159 end
160
161 # Breaks the buffer into lines that are shorter than #maxwidth
162 def break_outmost_groups
163 while @maxwidth < @output_width + @buffer_width
164 return unless group = @group_queue.deq
165 until group.breakables.empty?
166 data = @buffer.shift
167 @output_width = data.output(@output, @output_width)
168 @buffer_width -= data.width
169 end
170 while [email protected]? && Text === @buffer.first
171 text = @buffer.shift
172 @output_width = text.output(@output, @output_width)
173 @buffer_width -= text.width
174 end
175 end
176 end
177
178 # This adds +obj+ as a text of +width+ columns in width.
179 #
180 # If +width+ is not specified, obj.length is used.
181 #
182 def text(obj, width=obj.length)
183 if @buffer.empty?
184 @output << obj
185 @output_width += width
186 else
187 text = @buffer.last
188 unless Text === text
189 text = Text.new
190 @buffer << text
191 end
192 text.add(obj, width)
193 @buffer_width += width
194 break_outmost_groups
195 end
196 end
197
198 # This is similar to #breakable except
199 # the decision to break or not is determined individually.
200 #
201 # Two #fill_breakable under a group may cause 4 results:
202 # (break,break), (break,non-break), (non-break,break), (non-break,non-break).
203 # This is different to #breakable because two #breakable under a group
204 # may cause 2 results:
205 # (break,break), (non-break,non-break).
206 #
207 # The text +sep+ is inserted if a line is not broken at this point.
208 #
209 # If +sep+ is not specified, " " is used.
210 #
211 # If +width+ is not specified, +sep.length+ is used. You will have to
212 # specify this when +sep+ is a multibyte character, for example.
213 #
214 def fill_breakable(sep=' ', width=sep.length)
215 group { breakable sep, width }
216 end
217
218 # This says "you can break a line here if necessary", and a +width+\-column
219 # text +sep+ is inserted if a line is not broken at the point.
220 #
221 # If +sep+ is not specified, " " is used.
222 #
223 # If +width+ is not specified, +sep.length+ is used. You will have to
224 # specify this when +sep+ is a multibyte character, for example.
225 #
226 def breakable(sep=' ', width=sep.length)
227 group = @group_stack.last
228 if group.break?
229 flush
230 @output << @newline
231 @output << @genspace.call(@indent)
232 @output_width = @indent
233 @buffer_width = 0
234 else
235 @buffer << Breakable.new(sep, width, self)
236 @buffer_width += width
237 break_outmost_groups
238 end
239 end
240
241 # Groups line break hints added in the block. The line break hints are all
242 # to be used or not.
243 #
244 # If +indent+ is specified, the method call is regarded as nested by
245 # nest(indent) { ... }.
246 #
247 # If +open_obj+ is specified, <tt>text open_obj, open_width</tt> is called
248 # before grouping. If +close_obj+ is specified, <tt>text close_obj,
249 # close_width</tt> is called after grouping.
250 #
251 def group(indent=0, open_obj='', close_obj='', open_width=open_obj.length, close_width=close_obj.length)
252 text open_obj, open_width
253 group_sub {
254 nest(indent) {
255 yield
256 }
257 }
258 text close_obj, close_width
259 end
260
261 # Takes a block and queues a new group that is indented 1 level further.
262 def group_sub
263 group = Group.new(@group_stack.last.depth + 1)
264 @group_stack.push group
265 @group_queue.enq group
266 begin
267 yield
268 ensure
269 @group_stack.pop
270 if group.breakables.empty?
271 @group_queue.delete group
272 end
273 end
274 end
275
276 # Increases left margin after newline with +indent+ for line breaks added in
277 # the block.
278 #
279 def nest(indent)
280 @indent += indent
281 begin
282 yield
283 ensure
284 @indent -= indent
285 end
286 end
287
288 # outputs buffered data.
289 #
290 def flush
291 @buffer.each {|data|
292 @output_width = data.output(@output, @output_width)
293 }
294 @buffer.clear
295 @buffer_width = 0
296 end
297
298 # The Text class is the means by which to collect strings from objects.
299 #
300 # This class is intended for internal use of the PrettyPrint buffers.
301 class Text # :nodoc:
302
303 # Creates a new text object.
304 #
305 # This constructor takes no arguments.
306 #
307 # The workflow is to append a PrettyPrint::Text object to the buffer, and
308 # being able to call the buffer.last() to reference it.
309 #
310 # As there are objects, use PrettyPrint::Text#add to include the objects
311 # and the width to utilized by the String version of this object.
312 def initialize
313 @objs = []
314 @width = 0
315 end
316
317 # The total width of the objects included in this Text object.
318 attr_reader :width
319
320 # Render the String text of the objects that have been added to this Text object.
321 #
322 # Output the text to +out+, and increment the width to +output_width+
323 def output(out, output_width)
324 @objs.each {|obj| out << obj}
325 output_width + @width
326 end
327
328 # Include +obj+ in the objects to be pretty printed, and increment
329 # this Text object's total width by +width+
330 def add(obj, width)
331 @objs << obj
332 @width += width
333 end
334 end
335
336 # The Breakable class is used for breaking up object information
337 #
338 # This class is intended for internal use of the PrettyPrint buffers.
339 class Breakable # :nodoc:
340
341 # Create a new Breakable object.
342 #
343 # Arguments:
344 # * +sep+ String of the separator
345 # * +width+ Fixnum width of the +sep+
346 # * +q+ parent PrettyPrint object, to base from
347 def initialize(sep, width, q)
348 @obj = sep
349 @width = width
350 @pp = q
351 @indent = q.indent
352 @group = q.current_group
353 @group.breakables.push self
354 end
355
356 # Holds the separator String
357 #
358 # The +sep+ argument from ::new
359 attr_reader :obj
360
361 # The width of +obj+ / +sep+
362 attr_reader :width
363
364 # The number of spaces to indent.
365 #
366 # This is inferred from +q+ within PrettyPrint, passed in ::new
367 attr_reader :indent
368
369 # Render the String text of the objects that have been added to this
370 # Breakable object.
371 #
372 # Output the text to +out+, and increment the width to +output_width+
373 def output(out, output_width)
374 @group.breakables.shift
375 if @group.break?
376 out << @pp.newline
377 out << @pp.genspace.call(@indent)
378 @indent
379 else
380 @pp.group_queue.delete @group if @group.breakables.empty?
381 out << @obj
382 output_width + @width
383 end
384 end
385 end
386
387 # The Group class is used for making indentation easier.
388 #
389 # While this class does neither the breaking into newlines nor indentation,
390 # it is used in a stack (as well as a queue) within PrettyPrint, to group
391 # objects.
392 #
393 # For information on using groups, see PrettyPrint#group
394 #
395 # This class is intended for internal use of the PrettyPrint buffers.
396 class Group # :nodoc:
397 # Create a Group object
398 #
399 # Arguments:
400 # * +depth+ - this group's relation to previous groups
401 def initialize(depth)
402 @depth = depth
403 @breakables = []
404 @break = false
405 end
406
407 # This group's relation to previous groups
408 attr_reader :depth
409
410 # Array to hold the Breakable objects for this Group
411 attr_reader :breakables
412
413 # Makes a break for this Group, and returns true
414 def break
415 @break = true
416 end
417
418 # Boolean of whether this Group has made a break
419 def break?
420 @break
421 end
422
423 # Boolean of whether this Group has been queried for being first
424 #
425 # This is used as a predicate, and ought to be called first.
426 def first?
427 if defined? @first
428 false
429 else
430 @first = false
431 true
432 end
433 end
434 end
435
436 # The GroupQueue class is used for managing the queue of Group to be pretty
437 # printed.
438 #
439 # This queue groups the Group objects, based on their depth.
440 #
441 # This class is intended for internal use of the PrettyPrint buffers.
442 class GroupQueue # :nodoc:
443 # Create a GroupQueue object
444 #
445 # Arguments:
446 # * +groups+ - one or more PrettyPrint::Group objects
447 def initialize(*groups)
448 @queue = []
449 groups.each {|g| enq g}
450 end
451
452 # Enqueue +group+
453 #
454 # This does not strictly append the group to the end of the queue,
455 # but instead adds it in line, base on the +group.depth+
456 def enq(group)
457 depth = group.depth
458 @queue << [] until depth < @queue.length
459 @queue[depth] << group
460 end
461
462 # Returns the outer group of the queue
463 def deq
464 @queue.each {|gs|
465 (gs.length-1).downto(0) {|i|
466 unless gs[i].breakables.empty?
467 gs = gs[i, 1]
468 group = gs.first
469 group.break
470 return group
471 end
472 }
473 gs.each {|group| group.break}
474 gs.clear
475 }
476 return nil
477 end
478
479 # Remote +group+ from this queue
480 def delete(group)
481 @queue[group.depth].delete(group)
482 end
483 end
484
485 # PrettyPrint::SingleLine is used by PrettyPrint.singleline_format
486 #
487 # It is passed to be similar to a PrettyPrint object itself, by responding to:
488 # * #text
489 # * #breakable
490 # * #nest
491 # * #group
492 # * #flush
493 # * #first?
494 #
495 # but instead, the output has no line breaks
496 #
497 class SingleLine
498 # Create a PrettyPrint::SingleLine object
499 #
500 # Arguments:
501 # * +output+ - String (or similar) to store rendered text. Needs to respond to '<<'
502 # * +maxwidth+ - Argument position expected to be here for compatibility.
503 # This argument is a noop.
504 # * +newline+ - Argument position expected to be here for compatibility.
505 # This argument is a noop.
506 def initialize(output, maxwidth=nil, newline=nil)
507 @output = output
508 @first = [true]
509 end
510
511 # Add +obj+ to the text to be output.
512 #
513 # +width+ argument is here for compatibility. It is a noop argument.
514 def text(obj, width=nil)
515 @output << obj
516 end
517
518 # Appends +sep+ to the text to be output. By default +sep+ is ' '
519 #
520 # +width+ argument is here for compatibility. It is a noop argument.
521 def breakable(sep=' ', width=nil)
522 @output << sep
523 end
524
525 # Takes +indent+ arg, but does nothing with it.
526 #
527 # Yields to a block.
528 def nest(indent) # :nodoc:
529 yield
530 end
531
532 # Opens a block for grouping objects to be pretty printed.
533 #
534 # Arguments:
535 # * +indent+ - noop argument. Present for compatibility.
536 # * +open_obj+ - text appended before the &blok. Default is ''
537 # * +close_obj+ - text appended after the &blok. Default is ''
538 # * +open_width+ - noop argument. Present for compatibility.
539 # * +close_width+ - noop argument. Present for compatibility.
540 def group(indent=nil, open_obj='', close_obj='', open_width=nil, close_width=nil)
541 @first.push true
542 @output << open_obj
543 yield
544 @output << close_obj
545 @first.pop
546 end
547
548 # Method present for compatibility, but is a noop
549 def flush # :nodoc:
550 end
551
552 # This is used as a predicate, and ought to be called first.
553 def first?
554 result = @first[-1]
555 @first[-1] = false
556 result
557 end
558 end
559 end