aboutsummaryrefslogtreecommitdiff
path: root/cutl/compiler/cxx-indenter.hxx
blob: 868bc4eed4b4e3f04d307f602520f14e5dbb38c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// file      : cutl/compiler/cxx-indenter.hxx
// copyright : Copyright (c) 2009-2017 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#ifndef CUTL_COMPILER_CXX_INDENTER_HXX
#define CUTL_COMPILER_CXX_INDENTER_HXX

#include <set>
#include <stack>
#include <deque>
#include <string>
#include <cstddef> // std::size_t

#include <cutl/compiler/code-stream.hxx>

namespace cutl
{
  namespace compiler
  {
    template <typename C>
    class cxx_indenter: public code_stream<C>
    {
    public:
      cxx_indenter (code_stream<C>& out);

    private:
      cxx_indenter (cxx_indenter const&);

      cxx_indenter&
      operator= (cxx_indenter const&);

    public:
      virtual void
      put (C);

      virtual void
      unbuffer ();

    private:
      typedef std::basic_string<C> string;

      enum construct
      {
        con_other,
        con_pp_dir,
        con_c_com,
        con_cxx_com,
        con_string_lit,
        con_char_lit
      };

    private:
      void
      next_token (string const& old, C);

      void
      ensure_new_line ();

      void
      output_indentation ();

      void
      write (C);

    private:
      void
      tokenize (C, construct old);

      void
      retire (C);

    private:
      enum char_class_type
      {
        cc_alpha,   // Alpha + '_'.
        cc_digit,
        cc_op_punc, // Operator or punctuation.
        cc_space
      };

      static char_class_type
      char_class (C);

    private:
      enum keyword_type
      {
        kw_if,
        kw_do,
        kw_for,
        kw_else,
        kw_case,
        kw_while,
        kw_catch,
        kw_default
      };

      static C const*
      keyword (keyword_type);

    private:
      code_stream<C>& out_;
      bool buffering_;            // True if write() should buffer the char.
      std::size_t position_;      // Current position on the line.
      std::size_t paren_balance_; // ( ) balance.
      std::stack<std::size_t> indentation_;
      std::size_t spaces_;
      bool suppress_nl_;
      construct construct_;

      // Special state stack for the do-while construct. The presence
      // of an element in the stack indicates that we are in a braced
      // do-while construct. The value of the element is the brace
      // balance.
      std::stack<std::size_t> do_while_state_;

      typedef std::deque<C> hold;
      hold hold_;

    private:
      string token_;  // previously fully recognized token
      string lexeme_; // current lexeme (accumulator)

      // Keywords that may be folowed by a single-line block, e.g., if,
      // else, etc.
      //
      std::set<string> single_line_blocks_;

      // Keywords that may follow (and be related) to a previous block,
      // e.g., else, case, catch.
      //
      std::set<string> follow_blocks_;

      string do_;
      string lbrace_;
      string rbrace_;

    private:
      // Single-line indented blocks such as if, else, while, etc. The
      // newline flag indicates whether a new line has been seen after
      // the keyword. This is needed to properly distinguish cases such
      // as:
      //
      // else if (...)
      //   foo ();
      //
      // else
      //   if (...)
      //     foo ();
      //
      struct indent_block
      {
        indent_block (bool newline, std::size_t indentation)
            : newline_ (newline), indentation_ (indentation)
        {
        }

        bool newline_;
        std::size_t indentation_; // Size of the indentation_ stack
                                  // corresponding to this block, or
                                  // 0 if it is not indented.
      };

      std::stack<indent_block> indent_stack_;
    };
  }
}

#include <cutl/compiler/cxx-indenter.ixx>
#include <cutl/compiler/cxx-indenter.txx>

#endif // CUTL_COMPILER_CXX_INDENTER_HXX