Debian Codegraph @

cutl/compiler/cxx-indenter.txx

raw
1 // file : cutl/compiler/cxx-indenter.txx
2 // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
3 // license : MIT; see accompanying LICENSE file
4
5 namespace cutl
6 {
7 namespace compiler
8 {
9 template <typename C>
10 cxx_indenter<C>::
11 cxx_indenter (code_stream<C>& out)
12 : out_ (out),
13 buffering_ (false),
14 position_ (0),
15 paren_balance_ (0),
16 spaces_ (2),
17 construct_ (con_other),
18 do_ (keyword(kw_do)),
19 lbrace_ (1, '{'),
20 rbrace_ (1, '}')
21 {
22 indentation_.push (0);
23
24 single_line_blocks_.insert (keyword(kw_if));
25 single_line_blocks_.insert (keyword(kw_do));
26 single_line_blocks_.insert (keyword(kw_for));
27 single_line_blocks_.insert (keyword(kw_else));
28 single_line_blocks_.insert (keyword(kw_case));
29 single_line_blocks_.insert (keyword(kw_while));
30
31 follow_blocks_.insert (keyword(kw_else));
32 follow_blocks_.insert (keyword(kw_case));
33 follow_blocks_.insert (keyword(kw_catch));
34 follow_blocks_.insert (keyword(kw_default));
35 }
36
37 template <typename C>
38 void cxx_indenter<C>::
39 put (C c)
40 {
41 // First determine what kind of construct we are in.
42 //
43 construct new_con (construct_);
44 construct old_con (construct_);
45
46 switch (c)
47 {
48 case '\n':
49 {
50 if (construct_ == con_pp_dir ||
51 construct_ == con_cxx_com)
52 construct_ = new_con = con_other;
53
54 break;
55 }
56 case '#':
57 {
58 if (construct_ == con_other)
59 construct_ = new_con = con_pp_dir;
60
61 break;
62 }
63 case '\"':
64 {
65 if (construct_ != con_pp_dir &&
66 construct_ != con_c_com &&
67 construct_ != con_cxx_com &&
68 construct_ != con_char_lit)
69 {
70 // We might be in an escape sequence.
71 //
72 bool es (!hold_.empty () && hold_.back () == '\\');
73
74 if (es)
75 {
76 // Scan the hold sequence backwards to figure out if this
77 // backslash is part of this escape sequence or a preceding
78 // one.
79 //
80 for (typename hold::reverse_iterator i (hold_.rbegin () + 1),
81 e (hold_.rend ()); i != e && *i == '\\'; ++i)
82 es = !es;
83 }
84
85 if (!es)
86 {
87 if (construct_ == con_string_lit)
88 new_con = con_other;
89 else
90 construct_ = new_con = con_string_lit;
91 }
92 }
93
94 break;
95 }
96 case '\'':
97 {
98 if (construct_ != con_pp_dir &&
99 construct_ != con_c_com &&
100 construct_ != con_cxx_com &&
101 construct_ != con_string_lit)
102 {
103 // We might be in an escape sequence.
104 //
105 bool es (!hold_.empty () && hold_.back () == '\\');
106
107 if (es)
108 {
109 // Scan the hold sequence backwards to figure out if this
110 // backslash is part of this escape sequence or a preceding
111 // one.
112 //
113 for (typename hold::reverse_iterator i (hold_.rbegin () + 1),
114 e (hold_.rend ()); i != e && *i == '\\'; ++i)
115 es = !es;
116 }
117
118 if (!es)
119 {
120 if (construct_ == con_char_lit)
121 new_con = con_other;
122 else
123 construct_ = new_con = con_char_lit;
124 }
125 }
126
127 break;
128 }
129 case '/':
130 {
131 if (construct_ == con_other)
132 {
133 if (!hold_.empty () && hold_.back () == '/')
134 construct_ = new_con = con_cxx_com;
135 }
136
137 if (construct_ == con_c_com)
138 {
139 if (!hold_.empty () && hold_.back () == '*')
140 construct_ = new_con = con_other;
141 }
142
143 break;
144 }
145 case '*':
146 {
147 if (construct_ == con_other)
148 {
149 if (!hold_.empty () && hold_.back () == '/')
150 construct_ = new_con = con_c_com;
151 }
152
153 break;
154 }
155 default:
156 {
157 break;
158 }
159 }
160
161 // Special handling of CPP directives.
162 //
163 if (construct_ == con_pp_dir)
164 {
165 write (c);
166 position_++;
167 return;
168 }
169
170 //
171 //
172 tokenize (c, old_con);
173
174
175 // Indentation in parenthesis. We don't need to make sure
176 // we are not in a comments, etc., because we make sure we
177 // don't hold anything in those states.
178 //
179 if (!hold_.empty () && hold_.back () == '(')
180 {
181 unbuffer (); // We don't need to hold it anymore.
182
183 if (c == '\n')
184 indentation_.push (indentation_.top () + spaces_);
185 else
186 indentation_.push (position_);
187 }
188
189
190 //
191 //
192 bool defaulting (false);
193
194 switch (c)
195 {
196 case '\n':
197 {
198 if (!indent_stack_.empty () && construct_ == con_other)
199 indent_stack_.top ().newline_ = true;
200
201 hold_.push_back (c);
202 position_ = 0; // Starting a new line.
203
204 break;
205 }
206 case '{':
207 {
208 if (construct_ == con_other)
209 {
210 if (!indent_stack_.empty ())
211 {
212 // Pop all the blocks until the one that was indented.
213 //
214 while (indent_stack_.top ().indentation_ == 0)
215 indent_stack_.pop ();
216
217 // Pop the indented block and one level of indentation.
218 //
219 if (indentation_.size () > 1)
220 indentation_.pop ();
221
222 indent_stack_.pop ();
223 }
224
225 ensure_new_line ();
226 output_indentation ();
227 write (c);
228 ensure_new_line ();
229
230 indentation_.push (indentation_.top () + spaces_);
231 }
232 else
233 defaulting = true;
234
235 break;
236 }
237 case '}':
238 {
239 if (construct_ == con_other)
240 {
241 if (indentation_.size () > 1)
242 indentation_.pop ();
243
244 // Reduce multiple newlines to one.
245 //
246 while (hold_.size () > 1)
247 {
248 typename hold::reverse_iterator i (hold_.rbegin ());
249
250 if (*i == '\n' && *(i + 1) == '\n')
251 hold_.pop_back ();
252 else
253 break;
254 }
255
256 ensure_new_line ();
257 output_indentation ();
258
259 hold_.push_back (c);
260
261 // Add double newline after '}'.
262 //
263 hold_.push_back ('\n');
264 hold_.push_back ('\n');
265 position_ = 0;
266
267 if (!indent_stack_.empty ())
268 {
269 // Pop all the blocks until the one that was indented.
270 //
271 while (indent_stack_.top ().indentation_ == 0)
272 indent_stack_.pop ();
273
274 // Now pop all the indented blocks while also popping the
275 // indentation stack. Do it only if the indentation match.
276 // If it doesn't then that means this inden_stack entry is
277 // for some other, outer block.
278 //
279 while (!indent_stack_.empty () &&
280 indent_stack_.top ().indentation_ ==
281 indentation_.size ())
282 {
283 if (indentation_.size () > 1)
284 indentation_.pop ();
285
286 indent_stack_.pop ();
287 }
288 }
289
290 buffering_ = true;
291 }
292 else
293 defaulting = true;
294
295 break;
296 }
297 case ';':
298 {
299 if (construct_ == con_other)
300 {
301 // for (;;)
302 //
303 if (!indent_stack_.empty () && paren_balance_ == 0)
304 {
305 // Pop all the blocks until the one that was indented.
306 //
307 while (indent_stack_.top ().indentation_ == 0)
308 indent_stack_.pop ();
309
310 // Now pop all the indented blocks while also popping the
311 // indentation stack. Do it only if the indentation match.
312 // If they don't then it means we are inside a block and
313 // the stack should be popped after seeing '}', not ';'.
314 //
315 while (!indent_stack_.empty () &&
316 indent_stack_.top ().indentation_ ==
317 indentation_.size ())
318 {
319 if (indentation_.size () > 1)
320 indentation_.pop ();
321
322 indent_stack_.pop ();
323 }
324 }
325
326 if (paren_balance_ != 0)
327 {
328 // We are inside for (;;) statement. Nothing to do here.
329 //
330 defaulting = true;
331 }
332 else
333 {
334 // Handling '};' case.
335 //
336
337 bool brace (false);
338
339 if (hold_.size () > 1 && hold_.back () == '\n')
340 {
341 bool pop_nl (false);
342
343 for (typename hold::reverse_iterator
344 i (hold_.rbegin ()), e (hold_.rend ());
345 i != e; ++i)
346 {
347 if (*i != '\n')
348 {
349 if (*i == '}')
350 brace = pop_nl = true;
351
352 break;
353 }
354 }
355
356 if (pop_nl)
357 while (hold_.back () == '\n')
358 hold_.pop_back ();
359 }
360
361 output_indentation ();
362 write (c);
363 position_++;
364
365 if (brace)
366 {
367 hold_.push_back ('\n');
368 hold_.push_back ('\n');
369 }
370
371 ensure_new_line ();
372 }
373 }
374 else
375 defaulting = true;
376
377 break;
378 }
379 case ',':
380 {
381 if (construct_ == con_other)
382 {
383 // Handling '},' case.
384 //
385
386 bool brace (false);
387
388 if (hold_.size () > 1 && hold_.back () == '\n')
389 {
390 bool pop_nl (false);
391
392 for (typename hold::reverse_iterator
393 i (hold_.rbegin ()), e (hold_.rend ());
394 i != e; ++i)
395 {
396 if (*i != '\n')
397 {
398 if (*i == '}')
399 brace = pop_nl = true;
400
401 break;
402 }
403 }
404
405 if (pop_nl)
406 while (hold_.back () == '\n')
407 hold_.pop_back ();
408 }
409
410 output_indentation ();
411 write (c);
412 position_++;
413
414 if (brace)
415 hold_.push_back ('\n');
416 }
417 else
418 defaulting = true;
419
420 break;
421 }
422 case ' ':
423 {
424 if (construct_ == con_other)
425 {
426 // Handling '} foo_;' case.
427 //
428 if (hold_.size () > 1 && hold_.back () == '\n')
429 {
430 bool pop_nl (false);
431
432 for (typename hold::reverse_iterator
433 i (hold_.rbegin ()), e (hold_.rend ());
434 i != e; ++i)
435 {
436 if (*i != '\n')
437 {
438 if (*i == '}')
439 pop_nl = true;
440
441 break;
442 }
443 }
444
445 if (pop_nl)
446 while (hold_.back () == '\n')
447 hold_.pop_back ();
448 }
449 }
450
451 defaulting = true;
452 break;
453 }
454 case '\\':
455 {
456 if (construct_ != con_pp_dir &&
457 construct_ != con_c_com &&
458 construct_ != con_cxx_com)
459 {
460 output_indentation ();
461 hold_.push_back (c);
462 position_++;
463 }
464 else
465 defaulting = true;
466
467 break;
468
469 }
470 case '(':
471 {
472 if (construct_ == con_other)
473 {
474 // Hold it so that we can see what's coming next.
475 //
476 output_indentation ();
477 hold_.push_back (c);
478 position_++;
479 paren_balance_++;
480 }
481 else
482 defaulting = true;
483 break;
484 }
485 case ')':
486 {
487 if (construct_ == con_other)
488 {
489 if (indentation_.size () > 1)
490 indentation_.pop ();
491
492 if (paren_balance_ > 0)
493 paren_balance_--;
494 }
495
496 defaulting = true;
497 break;
498 }
499 case '/':
500 {
501 if (construct_ == con_other)
502 {
503 output_indentation ();
504 hold_.push_back (c);
505 position_++;
506 }
507 else
508 defaulting = true;
509
510 break;
511 }
512 case '*':
513 {
514 if (construct_ == con_c_com)
515 {
516 output_indentation ();
517 hold_.push_back (c);
518 position_++;
519 }
520 else
521 defaulting = true;
522
523 break;
524 }
525 default:
526 {
527 defaulting = true;
528 break;
529 }
530 }
531
532 if (defaulting)
533 {
534 output_indentation ();
535 write (c);
536 position_++;
537 }
538
539 construct_ = new_con;
540 }
541
542 template <typename C>
543 void cxx_indenter<C>::
544 unbuffer ()
545 {
546 for (; !hold_.empty (); hold_.pop_front ())
547 out_.put (hold_.front ());
548 }
549
550 template <typename C>
551 void cxx_indenter<C>::
552 next_token (string const& old, C c)
553 {
554 // Handle one line indentation blocks (if, else, etc).
555 //
556 if (single_line_blocks_.find (token_) != single_line_blocks_.end ())
557 {
558 // Only indent sub-blocks if we are on a new line.
559 //
560 bool indent (indent_stack_.empty () ||
561 indent_stack_.top ().newline_);
562
563 if (indent)
564 indentation_.push (indentation_.top () + spaces_);
565
566 indent_stack_.push (
567 indent_block (c == '\n', (indent ? indentation_.size () : 0)));
568 }
569
570 // Keep track of the do ... while construct in order to suppress
571 // the newline after } and before while.
572 //
573 if (old == do_ && token_ == lbrace_)
574 do_while_state_.push (0);
575
576 if (!do_while_state_.empty ())
577 {
578 if (token_ == lbrace_)
579 do_while_state_.top ()++;
580
581 if (token_ == rbrace_)
582 do_while_state_.top ()--;
583 }
584
585 // Suppress double newline in the "}else", etc., cases.
586 //
587 if (old == rbrace_)
588 {
589 bool dw (!do_while_state_.empty () && do_while_state_.top () == 0);
590
591 if (follow_blocks_.find (token_) != follow_blocks_.end () || dw)
592 {
593 if (dw)
594 do_while_state_.pop ();
595
596 // Reduce double newline after "}" into a single one.
597 //
598 typename hold::iterator i (hold_.end ()), b (hold_.begin ());
599
600 for (--i; i != b; --i)
601 {
602 // See if this is the end of the "}\n\n" sequence.
603 //
604 if (*i == '\n')
605 {
606 --i;
607 if (i != b && *i == '\n')
608 {
609 --i;
610 if (*i == '}')
611 {
612 ++i;
613 hold_.erase (i);
614 break;
615 }
616 }
617 }
618 }
619 }
620
621 // Stop buffering unless we have another closing brace.
622 //
623 if (token_ != rbrace_)
624 buffering_ = false;
625 }
626 }
627
628 template <typename C>
629 void cxx_indenter<C>::
630 ensure_new_line ()
631 {
632 if (hold_.empty () || hold_.back () != '\n')
633 {
634 hold_.push_back ('\n');
635 position_ = 0; // Starting a new line.
636 }
637 }
638
639
640 template <typename C>
641 void cxx_indenter<C>::
642 output_indentation ()
643 {
644 if (!hold_.empty () && hold_.back () == '\n')
645 {
646 for (std::size_t i (0); i < indentation_.top (); ++i)
647 write (' ');
648
649 position_ += indentation_.top ();
650 }
651 }
652
653 template <typename C>
654 void cxx_indenter<C>::
655 write (C c)
656 {
657 hold_.push_back (c);
658
659 if (!buffering_)
660 {
661 for (; !hold_.empty (); hold_.pop_front ())
662 out_.put (hold_.front ());
663 }
664 }
665
666 template <typename C>
667 void cxx_indenter<C>::
668 tokenize (C c, construct old)
669 {
670 //
671 //
672 switch (construct_)
673 {
674 case con_pp_dir:
675 {
676 if (old == con_other) // Start PP directive
677 retire (c);
678
679 return;
680 }
681 case con_c_com:
682 {
683 if (old == con_other) // Start C comment.
684 lexeme_.clear ();
685
686 return;
687 }
688 case con_cxx_com:
689 {
690 if (old == con_other) // Start C++ comment.
691 lexeme_.clear ();
692
693 return;
694 }
695 case con_string_lit:
696 {
697 if (old == con_other) // Start string literal
698 retire (c);
699
700 lexeme_ += c;
701 return;
702 }
703 case con_char_lit:
704 {
705 if (old == con_other) // Start char literal
706 retire (c);
707
708 lexeme_ += c;
709 return;
710 }
711 default:
712 break;
713 }
714
715 // construct_ == other
716 //
717 switch (old)
718 {
719 case con_pp_dir:
720 {
721 // End PP directive (newline).
722 //
723 return;
724 }
725 case con_c_com:
726 {
727 // End C comment.
728 //
729 return;
730 }
731 case con_cxx_com:
732 {
733 // End C++ comment (newline).
734 //
735 return;
736 }
737 case con_string_lit:
738 {
739 // End string literal (").
740 //
741 lexeme_ += c;
742 return;
743 }
744 case con_char_lit:
745 {
746 // End char literal (').
747 //
748 lexeme_ += c;
749 return;
750 }
751 default:
752 break;
753 }
754
755
756 // construct_ == old == other
757 //
758
759 switch (char_class (c))
760 {
761 case cc_alpha:
762 {
763 if (lexeme_.empty () ||
764 char_class (lexeme_[0]) == cc_alpha)
765 lexeme_ += c;
766 else
767 {
768 retire (c);
769 lexeme_ += c;
770 }
771 break;
772 }
773 case cc_digit:
774 {
775 if (lexeme_.empty ())
776 lexeme_ += c;
777 else
778 {
779 char_class_type cc (char_class (lexeme_[0]));
780
781 if (cc == cc_alpha || cc == cc_digit)
782 lexeme_ += c;
783 else
784 {
785 retire (c);
786 lexeme_ += c;
787 }
788 }
789 break;
790 }
791 case cc_op_punc:
792 {
793 retire (c);
794 lexeme_ += c;
795 break;
796 }
797 case cc_space:
798 {
799 retire (c);
800 break;
801 }
802 }
803 }
804
805 template <typename C>
806 void cxx_indenter<C>::
807 retire (C c)
808 {
809 if (!lexeme_.empty ())
810 {
811 token_.swap (lexeme_);
812 next_token (lexeme_, c);
813 lexeme_.clear ();
814 }
815 }
816 }
817 }