blob: 6271ac7425739efde2d36a8a720abb7c11895649 [file] [log] [blame]
Cary Clark2da9fb82018-11-01 09:29:36 -04001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef includeWriter_DEFINED
9#define includeWriter_DEFINED
10
11#include "includeParser.h"
12
13class IncludeWriter : public IncludeParser {
14public:
15 enum class Word {
16 kStart,
17 kCap,
18 kFirst,
19 kUnderline,
20 kMixed,
21 };
22
23 enum class Phrase {
24 kNo,
25 kYes,
26 };
27
28 enum class PunctuationState {
29 kStart,
30 kDelimiter,
31 kParen, // treated as a delimiter unless following a space, and followed by word
32 kPeriod,
33 kSpace,
34 };
35
36 enum class RefType {
37 kUndefined,
38 kNormal,
39 kExternal,
40 };
41
42 enum class SkipFirstLine {
43 kNo,
44 kYes,
45 };
46
47 enum class Wrote {
48 kNone,
49 kLF,
50 kChars,
51 };
52
53 enum class MemberPass {
54 kCount,
55 kOut,
56 };
57
58 enum class ItemState {
59 kNone,
60 kName,
61 kValue,
62 kComment,
63 };
64
65 struct IterState {
66 IterState (list<Definition>::iterator tIter, list<Definition>::iterator tIterEnd)
67 : fDefIter(tIter)
68 , fDefEnd(tIterEnd) {
69 }
70 list<Definition>::iterator fDefIter;
71 list<Definition>::iterator fDefEnd;
72 };
73
74 struct ParentPair {
75 const Definition* fParent;
76 const ParentPair* fPrev;
77 };
78
79 struct Preprocessor {
80 Preprocessor() {
81 reset();
82 }
83
84 void reset() {
85 fDefinition = nullptr;
86 fStart = nullptr;
87 fEnd = nullptr;
88 fWord = false;
89 }
90
91 const Definition* fDefinition;
92 const char* fStart;
93 const char* fEnd;
94 bool fWord;
95 };
96
97 struct Item {
98 void reset() {
99 fName = "";
100 fValue = "";
101 }
102
103 string fName;
104 string fValue;
105 };
106
107 struct LastItem {
108 const char* fStart;
109 const char* fEnd;
110 };
111
112 struct ItemLength {
113 int fCurName;
114 int fCurValue;
115 int fLongestName;
116 int fLongestValue;
117 };
118
119 IncludeWriter() : IncludeParser() {
120 this->reset();
121 }
122
123 ~IncludeWriter() override {}
124
125 bool contentFree(int size, const char* data) const {
126 while (size > 0 && data[0] <= ' ') {
127 --size;
128 ++data;
129 }
130 while (size > 0 && data[size - 1] <= ' ') {
131 --size;
132 }
133 return 0 == size;
134 }
135
136 bool checkChildCommentLength(const Definition* parent, MarkType childType) const;
137 void checkEnumLengths(const Definition& child, string enumName, ItemLength* length) const;
138 void constOut(const Definition* memberStart, const Definition* bmhConst);
139 void constSizeMembers(const RootDefinition* root);
140 bool defineOut(const Definition& );
141 bool descriptionOut(const Definition* def, SkipFirstLine , Phrase );
142 void enumHeaderOut(RootDefinition* root, const Definition& child);
143 string enumMemberComment(const Definition* currentEnumItem, const Definition& child) const;
144 const Definition* enumMemberForComment(const Definition* currentEnumItem) const;
145 ItemState enumMemberName(const Definition& child,
146 const Definition* token, Item* , LastItem* , const Definition** enumItem);
147 void enumMemberOut(const Definition* currentEnumItem, const Definition& child,
148 const Item& , Preprocessor& );
149 void enumMembersOut(Definition& child);
150 bool enumPreprocessor(Definition* token, MemberPass pass,
151 vector<IterState>& iterStack, IterState** iterState, Preprocessor* );
152 void enumSizeItems(const Definition& child);
153 bool findEnumSubtopic(string undername, const Definition** ) const;
154 void firstBlock(int size, const char* data);
155 bool firstBlockTrim(int size, const char* data);
156 Definition* findMemberCommentBlock(const vector<Definition*>& bmhChildren, string name) const;
157 Definition* findMethod(string name, RootDefinition* ) const;
158
159 void indentDeferred(IndentKind kind) {
160 if (fIndentNext) {
161 this->indentIn(kind);
162 fIndentNext = false;
163 }
164 }
165
166 int lookupMethod(const PunctuationState punctuation, const Word word,
167 const int start, const int run, int lastWrite,
168 const char* data, bool hasIndirection);
169 int lookupReference(const PunctuationState punctuation, const Word word,
170 const int start, const int run, int lastWrite, const char last,
171 const char* data);
172 const Definition* matchMemberName(string matchName, const Definition& child) const;
173 void methodOut(Definition* method, const Definition& child);
174 bool populate(Definition* def, ParentPair* parentPair, RootDefinition* root);
175 bool populate(BmhParser& bmhParser);
176
177 void reset() override {
178 INHERITED::resetCommon();
179 fBmhParser = nullptr;
180 fDeferComment = nullptr;
181 fBmhMethod = nullptr;
182 fEnumDef = nullptr;
183 fMethodDef = nullptr;
184 fBmhConst = nullptr;
185 fConstDef = nullptr;
186 fLastDescription = nullptr;
187 fStartSetter = nullptr;
188 fBmhStructDef = nullptr;
189 fContinuation = nullptr;
190 fInStruct = false;
191 fWroteMethod = false;
192 fIndentNext = false;
193 fPendingMethod = false;
194 fFirstWrite = false;
195 fStructEnded = false;
196 fWritingIncludes = true;
197 }
198
199 string resolveAlias(const Definition* );
200 string resolveMethod(const char* start, const char* end, bool first);
201 string resolveRef(const char* start, const char* end, bool first, RefType* refType);
202 Wrote rewriteBlock(int size, const char* data, Phrase phrase);
203 void setStart(const char* start, const Definition * );
204 void setStartBack(const char* start, const Definition * );
205 Definition* structMemberOut(const Definition* memberStart, const Definition& child);
206 void structOut(const Definition* root, const Definition& child,
207 const char* commentStart, const char* commentEnd);
208 void structSizeMembers(const Definition& child);
209 bool writeHeader(std::pair<const string, Definition>& );
210private:
211 vector<const Definition* > fICSStack;
212 BmhParser* fBmhParser;
213 Definition* fDeferComment;
214 const Definition* fBmhMethod;
215 const Definition* fEnumDef;
216 const Definition* fMethodDef;
217 const Definition* fBmhConst;
218 const Definition* fConstDef;
219 const Definition* fLastDescription;
220 const Definition* fStartSetter;
221 Definition* fBmhStructDef;
222 const char* fContinuation; // used to construct paren-qualified method name
223 int fAnonymousEnumCount;
224 int fEnumItemValueTab;
225 int fEnumItemCommentTab;
226 int fStructMemberTab;
227 int fStructValueTab;
228 int fStructCommentTab;
229 int fStructMemberLength;
230 int fConstValueTab;
231 int fConstCommentTab;
232 int fConstLength;
233 bool fInStruct; // set if struct is inside class
234 bool fWroteMethod;
235 bool fIndentNext;
236 bool fPendingMethod;
237 bool fFirstWrite; // set to write file information just after includes
238 bool fStructEnded; // allow resetting indent after struct is complete
239
240 typedef IncludeParser INHERITED;
241};
242
243#endif