Debian Codegraph @

cutl/compiler/cxx-indenter.hxx

raw
1 // file : cutl/compiler/cxx-indenter.hxx
2 // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
3 // license : MIT; see accompanying LICENSE file
4
5 #ifndef CUTL_COMPILER_CXX_INDENTER_HXX
6 #define CUTL_COMPILER_CXX_INDENTER_HXX
7
8 #include <set>
9 #include <stack>
10 #include <deque>
11 #include <string>
12 #include <cstddef> // std::size_t
13
14 #include <cutl/compiler/code-stream.hxx>
15
16 namespace cutl
17 {
18 namespace compiler
19 {
20 template <typename C>
21 class cxx_indenter: public code_stream<C>
22 {
23 public:
24 cxx_indenter (code_stream<C>& out);
25
26 private:
27 cxx_indenter (cxx_indenter const&);
28
29 cxx_indenter&
30 operator= (cxx_indenter const&);
31
32 public:
33 virtual void
34 put (C);
35
36 virtual void
37 unbuffer ();
38
39 private:
40 typedef std::basic_string<C> string;
41
42 enum construct
43 {
44 con_other,
45 con_pp_dir,
46 con_c_com,
47 con_cxx_com,
48 con_string_lit,
49 con_char_lit
50 };
51
52 private:
53 void
54 next_token (string const& old, C);
55
56 void
57 ensure_new_line ();
58
59 void
60 output_indentation ();
61
62 void
63 write (C);
64
65 private:
66 void
67 tokenize (C, construct old);
68
69 void
70 retire (C);
71
72 private:
73 enum char_class_type
74 {
75 cc_alpha, // Alpha + '_'.
76 cc_digit,
77 cc_op_punc, // Operator or punctuation.
78 cc_space
79 };
80
81 static char_class_type
82 char_class (C);
83
84 private:
85 enum keyword_type
86 {
87 kw_if,
88 kw_do,
89 kw_for,
90 kw_else,
91 kw_case,
92 kw_while,
93 kw_catch,
94 kw_default
95 };
96
97 static C const*
98 keyword (keyword_type);
99
100 private:
101 code_stream<C>& out_;
102 bool buffering_; // True if write() should buffer the char.
103 std::size_t position_; // Current position on the line.
104 std::size_t paren_balance_; // ( ) balance.
105 std::stack<std::size_t> indentation_;
106 std::size_t spaces_;
107 bool suppress_nl_;
108 construct construct_;
109
110 // Special state stack for the do-while construct. The presence
111 // of an element in the stack indicates that we are in a braced
112 // do-while construct. The value of the element is the brace
113 // balance.
114 std::stack<std::size_t> do_while_state_;
115
116 typedef std::deque<C> hold;
117 hold hold_;
118
119 private:
120 string token_; // previously fully recognized token
121 string lexeme_; // current lexeme (accumulator)
122
123 // Keywords that may be folowed by a single-line block, e.g., if,
124 // else, etc.
125 //
126 std::set<string> single_line_blocks_;
127
128 // Keywords that may follow (and be related) to a previous block,
129 // e.g., else, case, catch.
130 //
131 std::set<string> follow_blocks_;
132
133 string do_;
134 string lbrace_;
135 string rbrace_;
136
137 private:
138 // Single-line indented blocks such as if, else, while, etc. The
139 // newline flag indicates whether a new line has been seen after
140 // the keyword. This is needed to properly distinguish cases such
141 // as:
142 //
143 // else if (...)
144 // foo ();
145 //
146 // else
147 // if (...)
148 // foo ();
149 //
150 struct indent_block
151 {
152 indent_block (bool newline, std::size_t indentation)
153 : newline_ (newline), indentation_ (indentation)
154 {
155 }
156
157 bool newline_;
158 std::size_t indentation_; // Size of the indentation_ stack
159 // corresponding to this block, or
160 // 0 if it is not indented.
161 };
162
163 std::stack<indent_block> indent_stack_;
164 };
165 }
166 }
167
168 #include <cutl/compiler/cxx-indenter.ixx>
169 #include <cutl/compiler/cxx-indenter.txx>
170
171 #endif // CUTL_COMPILER_CXX_INDENTER_HXX