blob: 18beff4809315f7220932e4e082162e62b7bb6f9 [file] [log] [blame]
Cary Clark8032b982017-07-28 11:04:54 -04001/*
2 * Copyright 2017 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#include "bookmaker.h"
Cary Clarkb94f6da2018-06-08 11:54:32 -04009#include <chrono>
10#include <ctime>
11#include <string>
Cary Clark8032b982017-07-28 11:04:54 -040012
Cary Clark682c58d2018-05-16 07:07:07 -040013bool IncludeWriter::checkChildCommentLength(const Definition* parent, MarkType childType) const {
14 bool oneMember = false;
15 for (auto& item : parent->fChildren) {
16 if (childType != item->fMarkType) {
17 continue;
18 }
19 oneMember = true;
20 int lineLen = 0;
21 for (auto& itemChild : item->fChildren) {
22 if (MarkType::kExperimental == itemChild->fMarkType) {
23 lineLen = sizeof("experimental") - 1;
24 break;
25 }
26 if (MarkType::kDeprecated == itemChild->fMarkType) {
27 lineLen = sizeof("deprecated") - 1;
28 // todo: look for 'soon'
29 break;
30 }
31 if (MarkType::kLine == itemChild->fMarkType) {
32 lineLen = itemChild->length();
33 break;
34 }
35 }
36 if (!lineLen) {
37 item->reportError<void>("missing #Line");
38 }
39 if (fEnumItemCommentTab + lineLen >= 100) {
40// if too long, remove spaces until it fits, or wrap
41// item->reportError<void>("#Line comment too long");
42 }
43 }
44 return oneMember;
45}
46
47void IncludeWriter::checkEnumLengths(const Definition& child, string enumName, ItemLength* length) const {
48 const Definition* enumItem = this->matchMemberName(enumName, child);
49 if (std::any_of(enumItem->fChildren.begin(), enumItem->fChildren.end(),
50 [](Definition* child){return MarkType::kNoJustify == child->fMarkType;})) {
51 return;
52 }
53 string comment = this->enumMemberComment(enumItem, child);
54 int lineLimit = 100 - fIndent - 7; // 7: , space //!< space
55 if (length->fCurValue) {
56 lineLimit -= 3; // space = space
57 }
58 if (length->fCurName + length->fCurValue + (int) comment.length() < lineLimit) {
59 length->fLongestName = SkTMax(length->fLongestName, length->fCurName);
60 length->fLongestValue = SkTMax(length->fLongestValue, length->fCurValue);
61 }
62}
63
Cary Clark137b8742018-05-30 09:21:49 -040064void IncludeWriter::constOut(const Definition* memberStart, const Definition* bmhConst) {
Cary Clarkc68ba1d2018-02-20 10:35:29 -050065 const char* bodyEnd = fDeferComment ? fDeferComment->fContentStart - 1 :
66 memberStart->fContentStart;
Cary Clark0d225392018-06-07 09:59:07 -040067 this->firstBlockTrim((int) (bodyEnd - fStart), fStart); // may write nothing
Cary Clarkc68ba1d2018-02-20 10:35:29 -050068 this->lf(2);
Cary Clark0d225392018-06-07 09:59:07 -040069 this->indentDeferred(IndentKind::kConstOut);
70 if (fStructEnded) {
71 fIndent = fICSStack.size() * 4;
72 fStructEnded = false;
Cary Clark137b8742018-05-30 09:21:49 -040073 }
Cary Clark0d225392018-06-07 09:59:07 -040074 // comment may be legitimately empty; typedef may not have separate comment (for now)
75 fReturnOnWrite = true;
76 bool commentHasLength = this->descriptionOut(bmhConst, SkipFirstLine::kYes, Phrase::kNo);
77 fReturnOnWrite = false;
78 if (commentHasLength) {
79 this->writeCommentHeader();
80 fIndent += 4;
81 if (!this->descriptionOut(bmhConst, SkipFirstLine::kYes, Phrase::kNo)) {
82 return memberStart->reportError<void>("expected description for const");
83 }
84 fIndent -= 4;
85 this->writeCommentTrailer(OneLine::kNo);
86 }
87 this->setStart(memberStart->fContentStart, memberStart);
Cary Clark56356312018-02-08 14:45:18 -050088}
89
Cary Clark137b8742018-05-30 09:21:49 -040090bool IncludeWriter::descriptionOut(const Definition* def, SkipFirstLine skipFirstLine,
Cary Clark1a8d7622018-03-05 13:26:16 -050091 Phrase phrase) {
Cary Clark137b8742018-05-30 09:21:49 -040092 bool wroteSomething = false;
Cary Clark6fc50412017-09-21 12:31:06 -040093 const char* commentStart = def->fContentStart;
Cary Clarkc68ba1d2018-02-20 10:35:29 -050094 if (SkipFirstLine::kYes == skipFirstLine) {
95 TextParser parser(def);
96 SkAssertResult(parser.skipLine());
97 commentStart = parser.fChar;
98 }
99 int commentLen = (int) (def->fContentEnd - commentStart);
100 bool breakOut = false;
Cary Clark154beea2017-10-26 07:58:48 -0400101 SkDEBUGCODE(bool wroteCode = false);
Cary Clarkc68ba1d2018-02-20 10:35:29 -0500102 if (def->fDeprecated) {
Cary Clark0d225392018-06-07 09:59:07 -0400103 if (fReturnOnWrite) {
104 return true;
105 }
Cary Clark682c58d2018-05-16 07:07:07 -0400106 string message = def->incompleteMessage(Definition::DetailsType::kSentence);
107 this->writeString(message);
Cary Clarkc68ba1d2018-02-20 10:35:29 -0500108 this->lfcr();
Cary Clark137b8742018-05-30 09:21:49 -0400109 wroteSomething = true;
Cary Clarkc68ba1d2018-02-20 10:35:29 -0500110 }
Cary Clark137b8742018-05-30 09:21:49 -0400111 const Definition* lastDescription = def;
Cary Clark6fc50412017-09-21 12:31:06 -0400112 for (auto prop : def->fChildren) {
Cary Clark137b8742018-05-30 09:21:49 -0400113 fLastDescription = lastDescription;
114 lastDescription = prop;
Cary Clark6fc50412017-09-21 12:31:06 -0400115 switch (prop->fMarkType) {
Cary Clark154beea2017-10-26 07:58:48 -0400116 case MarkType::kCode: {
117 bool literal = false;
118 bool literalOutdent = false;
119 commentLen = (int) (prop->fStart - commentStart);
120 if (commentLen > 0) {
121 SkASSERT(commentLen < 1000);
122 if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart, Phrase::kNo)) {
Cary Clark0d225392018-06-07 09:59:07 -0400123 if (fReturnOnWrite) {
124 return true;
125 }
Cary Clark154beea2017-10-26 07:58:48 -0400126 this->lf(2);
Cary Clark137b8742018-05-30 09:21:49 -0400127 wroteSomething = true;
Cary Clark154beea2017-10-26 07:58:48 -0400128 }
129 }
130 size_t childSize = prop->fChildren.size();
131 if (childSize) {
Cary Clarkb94f6da2018-06-08 11:54:32 -0400132 if (MarkType::kLiteral == prop->fChildren[0]->fMarkType) {
133 SkASSERT(1 == childSize || 2 == childSize); // incomplete
134 SkASSERT(1 == childSize || MarkType::kOutdent == prop->fChildren[1]->fMarkType);
135 commentStart = prop->fChildren[childSize - 1]->fContentStart;
136 literal = true;
137 literalOutdent = 2 == childSize &&
138 MarkType::kOutdent == prop->fChildren[1]->fMarkType;
139 }
Cary Clark154beea2017-10-26 07:58:48 -0400140 }
141 commentLen = (int) (prop->fContentEnd - commentStart);
142 SkASSERT(commentLen > 0);
143 if (literal) {
Cary Clark0d225392018-06-07 09:59:07 -0400144 if (!fReturnOnWrite && !literalOutdent) {
Cary Clark154beea2017-10-26 07:58:48 -0400145 fIndent += 4;
146 }
Cary Clark137b8742018-05-30 09:21:49 -0400147 wroteSomething |= this->writeBlockIndent(commentLen, commentStart);
Cary Clark0d225392018-06-07 09:59:07 -0400148 if (fReturnOnWrite) {
149 return true;
150 }
151 if (!fReturnOnWrite) {
152 this->lf(2);
153 if (!literalOutdent) {
154 fIndent -= 4;
155 }
Cary Clark154beea2017-10-26 07:58:48 -0400156 }
Cary Clark154beea2017-10-26 07:58:48 -0400157 SkDEBUGCODE(wroteCode = true);
158 }
Cary Clark137b8742018-05-30 09:21:49 -0400159 commentStart = prop->fTerminator;
Cary Clark154beea2017-10-26 07:58:48 -0400160 } break;
Cary Clarkc68ba1d2018-02-20 10:35:29 -0500161 case MarkType::kBug: {
Cary Clark0d225392018-06-07 09:59:07 -0400162 if (fReturnOnWrite) {
163 return true;
164 }
Cary Clarkc68ba1d2018-02-20 10:35:29 -0500165 string bugstr("(see skbug.com/" + string(prop->fContentStart,
166 prop->fContentEnd - prop->fContentStart) + ')');
167 this->writeString(bugstr);
168 this->lfcr();
Cary Clark137b8742018-05-30 09:21:49 -0400169 wroteSomething = true;
Cary Clarkc68ba1d2018-02-20 10:35:29 -0500170 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400171 case MarkType::kDeprecated:
Cary Clark6fc50412017-09-21 12:31:06 -0400172 case MarkType::kPrivate:
173 commentLen = (int) (prop->fStart - commentStart);
174 if (commentLen > 0) {
175 SkASSERT(commentLen < 1000);
176 if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart, Phrase::kNo)) {
Cary Clark0d225392018-06-07 09:59:07 -0400177 if (fReturnOnWrite) {
178 return true;
179 }
Cary Clark6fc50412017-09-21 12:31:06 -0400180 this->lfcr();
Cary Clark137b8742018-05-30 09:21:49 -0400181 wroteSomething = true;
Cary Clark6fc50412017-09-21 12:31:06 -0400182 }
183 }
184 commentStart = prop->fContentStart;
Cary Clark137b8742018-05-30 09:21:49 -0400185 if (MarkType::kPrivate != prop->fMarkType && ' ' < commentStart[0]) {
Cary Clark682c58d2018-05-16 07:07:07 -0400186 commentStart = strchr(commentStart, '\n');
187 }
188 if (MarkType::kBug == prop->fMarkType) {
Cary Clarkc68ba1d2018-02-20 10:35:29 -0500189 commentStart = prop->fContentEnd;
190 }
Cary Clark6fc50412017-09-21 12:31:06 -0400191 commentLen = (int) (prop->fContentEnd - commentStart);
192 if (commentLen > 0) {
Cary Clark137b8742018-05-30 09:21:49 -0400193 wroteSomething |= this->writeBlockIndent(commentLen, commentStart);
Cary Clark0d225392018-06-07 09:59:07 -0400194 if (wroteSomething && fReturnOnWrite) {
195 return true;
196 }
Cary Clark1a8d7622018-03-05 13:26:16 -0500197 const char* end = commentStart + commentLen;
198 while (end > commentStart && ' ' == end[-1]) {
199 --end;
200 }
201 if (end > commentStart && '\n' == end[-1]) {
Cary Clark61dfc3a2018-01-03 08:37:53 -0500202 this->lfcr();
203 }
Cary Clark6fc50412017-09-21 12:31:06 -0400204 }
205 commentStart = prop->fTerminator;
206 commentLen = (int) (def->fContentEnd - commentStart);
207 break;
208 case MarkType::kExperimental:
Cary Clark6fc50412017-09-21 12:31:06 -0400209 commentStart = prop->fContentStart;
Cary Clark682c58d2018-05-16 07:07:07 -0400210 if (' ' < commentStart[0]) {
211 commentStart = strchr(commentStart, '\n');
212 }
Cary Clark6fc50412017-09-21 12:31:06 -0400213 commentLen = (int) (prop->fContentEnd - commentStart);
214 if (commentLen > 0) {
215 if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart, Phrase::kNo)) {
Cary Clark0d225392018-06-07 09:59:07 -0400216 if (fReturnOnWrite) {
217 return true;
218 }
Cary Clark6fc50412017-09-21 12:31:06 -0400219 this->lfcr();
Cary Clark137b8742018-05-30 09:21:49 -0400220 wroteSomething = true;
Cary Clark6fc50412017-09-21 12:31:06 -0400221 }
222 }
223 commentStart = prop->fTerminator;
224 commentLen = (int) (def->fContentEnd - commentStart);
225 break;
Cary Clark154beea2017-10-26 07:58:48 -0400226 case MarkType::kFormula: {
Cary Clark6fc50412017-09-21 12:31:06 -0400227 commentLen = prop->fStart - commentStart;
228 if (commentLen > 0) {
229 if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart, Phrase::kNo)) {
Cary Clark0d225392018-06-07 09:59:07 -0400230 if (fReturnOnWrite) {
231 return true;
232 }
Cary Clark154beea2017-10-26 07:58:48 -0400233 if (commentLen > 1 && '\n' == prop->fStart[-1] &&
234 '\n' == prop->fStart[-2]) {
235 this->lf(1);
236 } else {
237 this->writeSpace();
238 }
Cary Clark137b8742018-05-30 09:21:49 -0400239 wroteSomething = true;
Cary Clark6fc50412017-09-21 12:31:06 -0400240 }
241 }
Cary Clark154beea2017-10-26 07:58:48 -0400242 int saveIndent = fIndent;
243 if (fIndent < fColumn + 1) {
244 fIndent = fColumn + 1;
245 }
Cary Clark137b8742018-05-30 09:21:49 -0400246 wroteSomething |= this->writeBlockIndent(prop->length(), prop->fContentStart);
Cary Clark154beea2017-10-26 07:58:48 -0400247 fIndent = saveIndent;
Cary Clark0d225392018-06-07 09:59:07 -0400248 if (wroteSomething && fReturnOnWrite) {
249 return true;
250 }
Cary Clark6fc50412017-09-21 12:31:06 -0400251 commentStart = prop->fTerminator;
252 commentLen = (int) (def->fContentEnd - commentStart);
Cary Clark0d225392018-06-07 09:59:07 -0400253 if (!fReturnOnWrite) {
254 if (commentLen > 1 && '\n' == commentStart[0] && '\n' == commentStart[1]) {
255 this->lf(2);
256 } else {
257 SkASSERT('\n' == prop->fTerminator[0]);
258 if ('.' != prop->fTerminator[1] && !fLinefeeds) {
259 this->writeSpace();
260 }
Cary Clark154beea2017-10-26 07:58:48 -0400261 }
Cary Clark6fc50412017-09-21 12:31:06 -0400262 }
Cary Clark154beea2017-10-26 07:58:48 -0400263 } break;
Cary Clark0d225392018-06-07 09:59:07 -0400264 case MarkType::kDetails:
Cary Clarkab2621d2018-01-30 10:08:57 -0500265 case MarkType::kIn:
266 case MarkType::kLine:
Cary Clark6fc50412017-09-21 12:31:06 -0400267 case MarkType::kToDo:
268 commentLen = (int) (prop->fStart - commentStart);
269 if (commentLen > 0) {
270 SkASSERT(commentLen < 1000);
271 if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart, Phrase::kNo)) {
Cary Clark0d225392018-06-07 09:59:07 -0400272 if (fReturnOnWrite) {
273 return true;
274 }
Cary Clark6fc50412017-09-21 12:31:06 -0400275 this->lfcr();
Cary Clark137b8742018-05-30 09:21:49 -0400276 wroteSomething = true;
Cary Clark6fc50412017-09-21 12:31:06 -0400277 }
278 }
279 commentStart = prop->fTerminator;
280 commentLen = (int) (def->fContentEnd - commentStart);
281 break;
282 case MarkType::kList:
283 commentLen = prop->fStart - commentStart;
284 if (commentLen > 0) {
285 if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart,
286 Phrase::kNo)) {
Cary Clark0d225392018-06-07 09:59:07 -0400287 if (fReturnOnWrite) {
288 return true;
289 }
Cary Clark6fc50412017-09-21 12:31:06 -0400290 this->lfcr();
Cary Clark137b8742018-05-30 09:21:49 -0400291 wroteSomething = true;
Cary Clark6fc50412017-09-21 12:31:06 -0400292 }
293 }
294 for (auto row : prop->fChildren) {
295 SkASSERT(MarkType::kRow == row->fMarkType);
296 for (auto column : row->fChildren) {
297 SkASSERT(MarkType::kColumn == column->fMarkType);
Cary Clark0d225392018-06-07 09:59:07 -0400298 if (fReturnOnWrite) {
299 return true;
300 }
Cary Clark6fc50412017-09-21 12:31:06 -0400301 this->writeString("-");
302 this->writeSpace();
Cary Clark137b8742018-05-30 09:21:49 -0400303 wroteSomething |= this->descriptionOut(column, SkipFirstLine::kNo, Phrase::kNo);
Cary Clark6fc50412017-09-21 12:31:06 -0400304 this->lf(1);
305 }
306 }
307 commentStart = prop->fTerminator;
308 commentLen = (int) (def->fContentEnd - commentStart);
309 if ('\n' == commentStart[0] && '\n' == commentStart[1]) {
310 this->lf(2);
311 }
312 break;
Cary Clark1a8d7622018-03-05 13:26:16 -0500313 case MarkType::kPhraseRef: {
314 commentLen = prop->fStart - commentStart;
315 if (commentLen > 0) {
Cary Clark0d225392018-06-07 09:59:07 -0400316 if (fReturnOnWrite) {
317 return true;
318 }
Cary Clark1a8d7622018-03-05 13:26:16 -0500319 this->rewriteBlock(commentLen, commentStart, Phrase::kNo);
320 // ince we don't do line wrapping, always insert LF before phrase
321 this->lfcr(); // TODO: remove this once rewriteBlock rewraps paragraphs
Cary Clark137b8742018-05-30 09:21:49 -0400322 wroteSomething = true;
Cary Clark1a8d7622018-03-05 13:26:16 -0500323 }
324 auto iter = fBmhParser->fPhraseMap.find(prop->fName);
325 if (fBmhParser->fPhraseMap.end() == iter) {
Cary Clark137b8742018-05-30 09:21:49 -0400326 return this->reportError<bool>("missing phrase definition");
Cary Clark1a8d7622018-03-05 13:26:16 -0500327 }
328 Definition* phraseDef = iter->second;
Cary Clark682c58d2018-05-16 07:07:07 -0400329 // TODO: given TextParser(commentStart, prop->fStart + up to #) return if
330 // it ends with two of more linefeeds, ignoring other whitespace
331 Phrase defIsPhrase = '\n' == prop->fStart[0] && '\n' == prop->fStart[-1] ?
332 Phrase::kNo : Phrase::kYes;
333 if (Phrase::kNo == defIsPhrase) {
334 this->lf(2);
335 }
336 const char* start = phraseDef->fContentStart;
337 int length = phraseDef->length();
338 auto propParams = prop->fChildren.begin();
339 // can this share code or logic with mdout somehow?
340 for (auto child : phraseDef->fChildren) {
341 if (MarkType::kPhraseParam == child->fMarkType) {
342 continue;
343 }
344 int localLength = child->fStart - start;
Cary Clark0d225392018-06-07 09:59:07 -0400345 if (fReturnOnWrite) {
346 return true;
347 }
Cary Clark682c58d2018-05-16 07:07:07 -0400348 this->rewriteBlock(localLength, start, defIsPhrase);
349 start += localLength;
350 length -= localLength;
351 SkASSERT(propParams != prop->fChildren.end());
352 if (fColumn > 0) {
353 this->writeSpace();
354 }
355 this->writeString((*propParams)->fName);
356 localLength = child->fContentEnd - child->fStart;
357 start += localLength;
358 length -= localLength;
359 if (isspace(start[0])) {
360 this->writeSpace();
361 }
362 defIsPhrase = Phrase::kYes;
Cary Clark137b8742018-05-30 09:21:49 -0400363 wroteSomething = true;
Cary Clark682c58d2018-05-16 07:07:07 -0400364 }
365 if (length > 0) {
Cary Clark0d225392018-06-07 09:59:07 -0400366 if (fReturnOnWrite) {
367 return true;
368 }
Cary Clark682c58d2018-05-16 07:07:07 -0400369 this->rewriteBlock(length, start, defIsPhrase);
370 }
Cary Clark1a8d7622018-03-05 13:26:16 -0500371 commentStart = prop->fContentStart;
372 commentLen = (int) (def->fContentEnd - commentStart);
Cary Clark0d225392018-06-07 09:59:07 -0400373 if (!fReturnOnWrite) {
374 if ('\n' == commentStart[0] && '\n' == commentStart[1]) {
375 this->lf(2);
376 }
Cary Clark137b8742018-05-30 09:21:49 -0400377 }
Cary Clark1a8d7622018-03-05 13:26:16 -0500378 } break;
Cary Clark6fc50412017-09-21 12:31:06 -0400379 default:
380 commentLen = (int) (prop->fStart - commentStart);
381 breakOut = true;
382 }
383 if (breakOut) {
384 break;
385 }
386 }
Cary Clark0d225392018-06-07 09:59:07 -0400387 if (!breakOut) {
388 commentLen = (int) (def->fContentEnd - commentStart);
389 }
Cary Clark4855f782018-02-06 09:41:53 -0500390 SkASSERT(wroteCode || (commentLen > 0 && commentLen < 1500) || def->fDeprecated);
Cary Clark154beea2017-10-26 07:58:48 -0400391 if (commentLen > 0) {
Cary Clark0d225392018-06-07 09:59:07 -0400392 if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart, phrase)) {
393 if (fReturnOnWrite) {
394 return true;
395 }
396 wroteSomething = true;
397 }
Cary Clark154beea2017-10-26 07:58:48 -0400398 }
Cary Clark0d225392018-06-07 09:59:07 -0400399 SkASSERT(!fReturnOnWrite || !wroteSomething);
Cary Clark137b8742018-05-30 09:21:49 -0400400 return wroteSomething;
Cary Clark6fc50412017-09-21 12:31:06 -0400401}
402
Cary Clark682c58d2018-05-16 07:07:07 -0400403void IncludeWriter::enumHeaderOut(RootDefinition* root, const Definition& child) {
Cary Clark8032b982017-07-28 11:04:54 -0400404 const Definition* enumDef = nullptr;
405 const char* bodyEnd = fDeferComment ? fDeferComment->fContentStart - 1 :
406 child.fContentStart;
Cary Clark0d225392018-06-07 09:59:07 -0400407 this->firstBlockTrim((int) (bodyEnd - fStart), fStart); // may write nothing
Cary Clark8032b982017-07-28 11:04:54 -0400408 this->lf(2);
Cary Clark0d225392018-06-07 09:59:07 -0400409 this->indentDeferred(IndentKind::kEnumHeader);
Cary Clark8032b982017-07-28 11:04:54 -0400410 fDeferComment = nullptr;
Cary Clark0d225392018-06-07 09:59:07 -0400411 this->setStart(child.fContentStart, &child);
Cary Clark8032b982017-07-28 11:04:54 -0400412 const auto& nameDef = child.fTokens.front();
413 string fullName;
414 if (nullptr != nameDef.fContentEnd) {
Cary Clarkbad5ad72017-08-03 17:14:08 -0400415 TextParser enumClassCheck(&nameDef);
416 const char* start = enumClassCheck.fStart;
417 size_t len = (size_t) (enumClassCheck.fEnd - start);
418 bool enumClass = enumClassCheck.skipExact("class ");
419 if (enumClass) {
420 start = enumClassCheck.fChar;
421 const char* end = enumClassCheck.anyOf(" \n;{");
422 len = (size_t) (end - start);
423 }
424 string enumName(start, len);
425 if (enumClass) {
426 child.fChildren[0]->fName = enumName;
427 }
Cary Clark8032b982017-07-28 11:04:54 -0400428 fullName = root->fName + "::" + enumName;
Cary Clarkce101242017-09-01 15:51:02 -0400429 enumDef = root->find(enumName, RootDefinition::AllowParens::kNo);
Cary Clark8032b982017-07-28 11:04:54 -0400430 if (!enumDef) {
Cary Clarkce101242017-09-01 15:51:02 -0400431 enumDef = root->find(fullName, RootDefinition::AllowParens::kNo);
Cary Clark8032b982017-07-28 11:04:54 -0400432 }
Cary Clark06c20f32018-03-20 15:53:27 -0400433 if (!enumDef) {
434 auto mapEntry = fBmhParser->fEnumMap.find(enumName);
435 if (fBmhParser->fEnumMap.end() != mapEntry) {
436 enumDef = &mapEntry->second;
437 }
438 }
Cary Clark8032b982017-07-28 11:04:54 -0400439 SkASSERT(enumDef);
440 // child[0] should be #Code comment starts at child[0].fTerminator
441 // though skip until #Code is found (in case there's a #ToDo, etc)
442 // child[1] should be #Const comment ends at child[1].fStart
443 // comment becomes enum header (if any)
444 } else {
445 string enumName(root->fName);
446 enumName += "::_anonymous";
447 if (fAnonymousEnumCount > 1) {
448 enumName += '_' + to_string(fAnonymousEnumCount);
449 }
Cary Clarkce101242017-09-01 15:51:02 -0400450 enumDef = root->find(enumName, RootDefinition::AllowParens::kNo);
Cary Clark8032b982017-07-28 11:04:54 -0400451 SkASSERT(enumDef);
452 ++fAnonymousEnumCount;
453 }
454 Definition* codeBlock = nullptr;
455 const char* commentStart = nullptr;
456 bool wroteHeader = false;
Cary Clark6fc50412017-09-21 12:31:06 -0400457 bool lastAnchor = false;
Cary Clark8032b982017-07-28 11:04:54 -0400458 SkDEBUGCODE(bool foundConst = false);
459 for (auto test : enumDef->fChildren) {
Cary Clark682c58d2018-05-16 07:07:07 -0400460 if (MarkType::kCode == test->fMarkType && !codeBlock) {
Cary Clark8032b982017-07-28 11:04:54 -0400461 codeBlock = test;
462 commentStart = codeBlock->fTerminator;
463 continue;
464 }
465 if (!codeBlock) {
466 continue;
467 }
468 const char* commentEnd = test->fStart;
469 if (!wroteHeader &&
470 !this->contentFree((int) (commentEnd - commentStart), commentStart)) {
Cary Clark154beea2017-10-26 07:58:48 -0400471 if (fIndentNext) {
Cary Clark0d225392018-06-07 09:59:07 -0400472 // FIXME: how can I tell where fIdentNext gets cleared?
473 this->indentIn(IndentKind::kEnumChild);
Cary Clark154beea2017-10-26 07:58:48 -0400474 }
Cary Clark8032b982017-07-28 11:04:54 -0400475 this->writeCommentHeader();
476 this->writeString("\\enum");
Cary Clarkbad5ad72017-08-03 17:14:08 -0400477 if (fullName.length() > 0) {
478 this->writeSpace();
479 this->writeString(fullName.c_str());
480 }
Cary Clark0d225392018-06-07 09:59:07 -0400481 this->indentIn(IndentKind::kEnumChild2);
Cary Clark8032b982017-07-28 11:04:54 -0400482 this->lfcr();
483 wroteHeader = true;
484 }
Cary Clark6fc50412017-09-21 12:31:06 -0400485 if (lastAnchor) {
486 if (commentEnd - commentStart > 1) {
487 SkASSERT('\n' == commentStart[0]);
488 if (' ' == commentStart[1]) {
489 this->writeSpace();
490 }
491 }
492 lastAnchor = false;
493 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400494 this->rewriteBlock((int) (commentEnd - commentStart), commentStart, Phrase::kNo);
Cary Clark682c58d2018-05-16 07:07:07 -0400495 if (MarkType::kAnchor == test->fMarkType || MarkType::kCode == test->fMarkType) {
Cary Clark154beea2017-10-26 07:58:48 -0400496 bool newLine = commentEnd - commentStart > 1 &&
497 '\n' == commentEnd[-1] && '\n' == commentEnd[-2];
Cary Clark8032b982017-07-28 11:04:54 -0400498 commentStart = test->fContentStart;
Cary Clark682c58d2018-05-16 07:07:07 -0400499 commentEnd = MarkType::kAnchor == test->fMarkType ? test->fChildren[0]->fStart :
500 test->fContentEnd;
Cary Clark154beea2017-10-26 07:58:48 -0400501 if (newLine) {
502 this->lf(2);
503 } else {
504 this->writeSpace();
505 }
Cary Clark682c58d2018-05-16 07:07:07 -0400506 if (MarkType::kAnchor == test->fMarkType) {
507 this->rewriteBlock((int) (commentEnd - commentStart), commentStart, Phrase::kNo);
508 } else {
509 this->writeBlock((int) (commentEnd - commentStart), commentStart);
510 this->lf(2);
511 }
Cary Clark6fc50412017-09-21 12:31:06 -0400512 lastAnchor = true; // this->writeSpace();
Cary Clark8032b982017-07-28 11:04:54 -0400513 }
514 commentStart = test->fTerminator;
515 if (MarkType::kConst == test->fMarkType) {
516 SkASSERT(codeBlock); // FIXME: check enum for correct order earlier
517 SkDEBUGCODE(foundConst = true);
518 break;
519 }
520 }
521 SkASSERT(codeBlock);
522 SkASSERT(foundConst);
523 if (wroteHeader) {
Cary Clark0d225392018-06-07 09:59:07 -0400524 this->indentOut();
Cary Clark8032b982017-07-28 11:04:54 -0400525 this->lfcr();
Cary Clark137b8742018-05-30 09:21:49 -0400526 this->writeCommentTrailer(OneLine::kNo);
Cary Clark8032b982017-07-28 11:04:54 -0400527 }
Cary Clarkbad5ad72017-08-03 17:14:08 -0400528 Definition* braceHolder = child.fChildren[0];
529 if (KeyWord::kClass == braceHolder->fKeyWord) {
530 braceHolder = braceHolder->fChildren[0];
531 }
532 bodyEnd = braceHolder->fContentStart;
Cary Clark8032b982017-07-28 11:04:54 -0400533 SkASSERT('{' == bodyEnd[0]);
534 ++bodyEnd;
535 this->lfcr();
536 this->writeBlock((int) (bodyEnd - fStart), fStart); // write include "enum Name {"
Cary Clark0d225392018-06-07 09:59:07 -0400537 this->indentIn(IndentKind::kEnumHeader2);
Cary Clark8032b982017-07-28 11:04:54 -0400538 this->singleLF();
Cary Clark0d225392018-06-07 09:59:07 -0400539 this->setStart(bodyEnd, braceHolder);
Cary Clark8032b982017-07-28 11:04:54 -0400540 fEnumDef = enumDef;
541}
542
Cary Clark682c58d2018-05-16 07:07:07 -0400543const Definition* IncludeWriter::enumMemberForComment(const Definition* currentEnumItem) const {
544 for (auto constItem : currentEnumItem->fChildren) {
545 if (MarkType::kLine == constItem->fMarkType
546 || MarkType::kExperimental == constItem->fMarkType
547 || MarkType::kDeprecated == constItem->fMarkType) {
548 return constItem;
549 }
550 }
551 SkASSERT(0);
552 return nullptr;
553}
554
555string IncludeWriter::enumMemberComment(const Definition* currentEnumItem,
556 const Definition& child) const {
557 // #Const should always be followed by #Line, so description follows that
558 string shortComment;
559 for (auto constItem : currentEnumItem->fChildren) {
560 if (MarkType::kLine == constItem->fMarkType) {
561 shortComment = string(constItem->fContentStart, constItem->length());
562 break;
563 }
564 if (IncompleteAllowed(constItem->fMarkType)) {
Cary Clark137b8742018-05-30 09:21:49 -0400565 shortComment = constItem->fParent->incompleteMessage(Definition::DetailsType::kPhrase);
Cary Clark682c58d2018-05-16 07:07:07 -0400566 }
567 }
568 if (!shortComment.length()) {
569 currentEnumItem->reportError<void>("missing #Line or #Deprecated or #Experimental");
570 }
571 return shortComment;
572}
573
574IncludeWriter::ItemState IncludeWriter::enumMemberName(
575 const Definition& child, const Definition* token, Item* item, LastItem* last,
576 const Definition** currentEnumItem) {
577 TextParser parser(fFileName, last->fStart, last->fEnd, fLineCount);
578 parser.skipWhiteSpace();
579 item->fName = string(parser.fChar, (int) (last->fEnd - parser.fChar));
580 *currentEnumItem = this->matchMemberName(item->fName, child);
581 if (token) {
Cary Clark0d225392018-06-07 09:59:07 -0400582 this->setStart(token->fContentEnd, token);
Cary Clark682c58d2018-05-16 07:07:07 -0400583 TextParser enumLine(token->fFileName, last->fEnd, token->fContentStart, token->fLineCount);
584 const char* end = enumLine.anyOf(",}=");
585 SkASSERT(end);
586 if ('=' == *end) { // write enum value
587 last->fEnd = token->fContentEnd;
588 item->fValue = string(token->fContentStart, (int) (last->fEnd - token->fContentStart));
589 return ItemState::kValue;
590 }
591 }
592 return ItemState::kComment;
593}
594
595void IncludeWriter::enumMemberOut(const Definition* currentEnumItem, const Definition& child,
596 const Item& item, Preprocessor& preprocessor) {
597 SkASSERT(currentEnumItem);
598 string shortComment = this->enumMemberComment(currentEnumItem, child);
599 int enumItemValueTab =
600 SkTMax((int) item.fName.length() + fIndent + 1, fEnumItemValueTab); // 1: ,
601 int valueLength = item.fValue.length();
602 int assignLength = valueLength ? valueLength + 3 : 0; // 3: space = space
603 int enumItemCommentTab = SkTMax(enumItemValueTab + assignLength, fEnumItemCommentTab);
604 int trimNeeded = enumItemCommentTab + shortComment.length() - (100 - (sizeof("//!< ") - 1));
605 bool crAfterName = false;
606 if (trimNeeded > 0) {
607 if (item.fValue.length()) {
608 int valueSpare = SkTMin(trimNeeded, // 3 below: space = space
609 (int) (enumItemCommentTab - enumItemValueTab - item.fValue.length() - 3));
610 SkASSERT(valueSpare >= 0);
611 trimNeeded -= valueSpare;
612 enumItemCommentTab -= valueSpare;
613 }
614 if (trimNeeded > 0) {
615 int nameSpare = SkTMin(trimNeeded, (int) (enumItemValueTab - item.fName.length()
616 - fIndent - 1)); // 1: ,
617 SkASSERT(nameSpare >= 0);
618 trimNeeded -= nameSpare;
619 enumItemValueTab -= nameSpare;
620 enumItemCommentTab -= nameSpare;
621 }
622 if (trimNeeded > 0) {
623 crAfterName = true;
624 if (!valueLength) {
625 this->enumMemberForComment(currentEnumItem)->reportError<void>("comment too long");
626 } else if (valueLength + fIndent + 8 + shortComment.length() > // 8: addtional indent
627 100 - (sizeof(", //!< ") - 1)) { // -1: zero-terminated string
628 this->enumMemberForComment(currentEnumItem)->reportError<void>("comment 2 long");
629 } // 2: = space
630 enumItemValueTab = fEnumItemValueTab + 2 // 2: , space
631 - SkTMax(0, fEnumItemValueTab + 2 + valueLength + 2 - fEnumItemCommentTab);
632 enumItemCommentTab = SkTMax(enumItemValueTab + valueLength + 2, fEnumItemCommentTab);
633 }
634 }
635 this->lfcr();
636 this->writeString(item.fName);
637 int saveIndent = fIndent;
638 if (item.fValue.length()) {
639 if (!crAfterName) {
640 this->indentToColumn(enumItemValueTab);
641 } else {
642 this->writeSpace();
643 }
644 this->writeString("=");
645 if (crAfterName) {
646 this->lfcr();
647 fIndent = enumItemValueTab;
648 } else {
649 this->writeSpace();
650 }
651 this->writeString(item.fValue);
652 }
653 this->writeString(",");
654 this->indentToColumn(enumItemCommentTab);
655 this->writeString("//!<");
656 this->writeSpace();
657 this->rewriteBlock(shortComment.length(), shortComment.c_str(), Phrase::kYes);
658 this->lfcr();
659 fIndent = saveIndent;
660 if (preprocessor.fStart) {
661 SkASSERT(preprocessor.fEnd);
662 int saveIndent = fIndent;
663 fIndent = SkTMax(0, fIndent - 8);
664 this->lf(2);
665 this->writeBlock(
666 (int) (preprocessor.fEnd - preprocessor.fStart), preprocessor.fStart);
667 this->lfcr();
668 fIndent = saveIndent;
669 preprocessor.reset();
670 }
671}
672
673// iterate through include tokens and find how much remains for 1 line comments
674// put ones that fit on same line, ones that are too big wrap
675void IncludeWriter::enumMembersOut(Definition& child) {
676 ItemState state = ItemState::kNone;
677 const Definition* currentEnumItem;
678 LastItem last = { nullptr, nullptr };
679 auto brace = child.fChildren[0];
680 if (KeyWord::kClass == brace->fKeyWord) {
681 brace = brace->fChildren[0];
682 }
683 SkASSERT(Bracket::kBrace == brace->fBracket);
Cary Clarkbad5ad72017-08-03 17:14:08 -0400684 vector<IterState> iterStack;
Cary Clark682c58d2018-05-16 07:07:07 -0400685 iterStack.emplace_back(brace->fTokens.begin(), brace->fTokens.end());
Cary Clarkbad5ad72017-08-03 17:14:08 -0400686 IterState* iterState = &iterStack[0];
Cary Clark186d08f2018-04-03 08:43:27 -0400687 Preprocessor preprocessor;
Cary Clark682c58d2018-05-16 07:07:07 -0400688 Item item;
689 while (iterState->fDefIter != iterState->fDefEnd) {
690 auto& token = *iterState->fDefIter++;
691 if (this->enumPreprocessor(&token, MemberPass::kOut, iterStack, &iterState,
Cary Clark186d08f2018-04-03 08:43:27 -0400692 &preprocessor)) {
Cary Clarkbad5ad72017-08-03 17:14:08 -0400693 continue;
694 }
Cary Clark682c58d2018-05-16 07:07:07 -0400695 if (ItemState::kName == state) {
696 state = this->enumMemberName(child, &token, &item, &last, &currentEnumItem);
Cary Clark8032b982017-07-28 11:04:54 -0400697 }
Cary Clark682c58d2018-05-16 07:07:07 -0400698 if (ItemState::kValue == state) {
699 TextParser valueEnd(token.fFileName, last.fEnd, token.fContentStart, token.fLineCount);
Cary Clark8032b982017-07-28 11:04:54 -0400700 const char* end = valueEnd.anyOf(",}");
701 if (!end) { // write expression continuation
Cary Clark682c58d2018-05-16 07:07:07 -0400702 item.fValue += string(last.fEnd, (int) (token.fContentEnd - last.fEnd));
Cary Clark8032b982017-07-28 11:04:54 -0400703 continue;
704 }
705 }
Cary Clark682c58d2018-05-16 07:07:07 -0400706 if (ItemState::kNone != state) {
707 this->enumMemberOut(currentEnumItem, child, item, preprocessor);
Cary Clark137b8742018-05-30 09:21:49 -0400708 item.reset();
Cary Clark0d225392018-06-07 09:59:07 -0400709 this->setStartBack(token.fContentStart, &token);
Cary Clark682c58d2018-05-16 07:07:07 -0400710 state = ItemState::kNone;
711 last.fStart = nullptr;
Cary Clark8032b982017-07-28 11:04:54 -0400712 }
Cary Clark682c58d2018-05-16 07:07:07 -0400713 SkASSERT(ItemState::kNone == state);
714 if (!last.fStart) {
715 last.fStart = fStart;
Cary Clark8032b982017-07-28 11:04:54 -0400716 }
Cary Clark682c58d2018-05-16 07:07:07 -0400717 last.fEnd = token.fContentEnd;
718 state = ItemState::kName;
Cary Clark8032b982017-07-28 11:04:54 -0400719 }
Cary Clark682c58d2018-05-16 07:07:07 -0400720 if (ItemState::kName == state) {
721 state = this->enumMemberName(child, nullptr, &item, &last, &currentEnumItem);
722 }
723 if (ItemState::kValue == state || ItemState::kComment == state) {
724 this->enumMemberOut(currentEnumItem, child, item, preprocessor);
725 }
Cary Clark0d225392018-06-07 09:59:07 -0400726 this->indentOut();
Cary Clark8032b982017-07-28 11:04:54 -0400727}
728
Cary Clark186d08f2018-04-03 08:43:27 -0400729bool IncludeWriter::enumPreprocessor(Definition* token, MemberPass pass,
730 vector<IterState>& iterStack, IterState** iterState, Preprocessor* preprocessor) {
731 if (token && Definition::Type::kBracket == token->fType) {
732 if (Bracket::kSlashSlash == token->fBracket) {
733 if (MemberPass::kOut == pass) {
Cary Clark0d225392018-06-07 09:59:07 -0400734 this->setStart(token->fContentEnd, token);
Cary Clark186d08f2018-04-03 08:43:27 -0400735 }
736 return true; // ignore old inline comments
737 }
738 if (Bracket::kSlashStar == token->fBracket) {
739 if (MemberPass::kOut == pass) {
Cary Clark0d225392018-06-07 09:59:07 -0400740 this->setStart(token->fContentEnd + 1, token);
Cary Clark186d08f2018-04-03 08:43:27 -0400741 }
742 return true; // ignore old inline comments
743 }
744 if (Bracket::kPound == token->fBracket) { // preprocessor wraps member
Cary Clarka4f581a2018-04-03 15:31:59 -0400745 preprocessor->fDefinition = token;
Cary Clark186d08f2018-04-03 08:43:27 -0400746 preprocessor->fStart = token->fContentStart;
747 if (KeyWord::kIf == token->fKeyWord || KeyWord::kIfdef == token->fKeyWord) {
748 iterStack.emplace_back(token->fTokens.begin(), token->fTokens.end());
749 *iterState = &iterStack.back();
750 preprocessor->fWord = true;
751 } else if (KeyWord::kEndif == token->fKeyWord || KeyWord::kElif == token->fKeyWord
752 || KeyWord::kElse == token->fKeyWord) {
753 iterStack.pop_back();
754 *iterState = &iterStack.back();
755 preprocessor->fEnd = token->fContentEnd;
756 if (KeyWord::kElif == token->fKeyWord) {
757 iterStack.emplace_back(token->fTokens.begin(), token->fTokens.end());
758 *iterState = &iterStack.back();
759 preprocessor->fWord = true;
760 }
761 } else {
762 SkASSERT(0); // incomplete
763 }
764 return true;
765 }
Cary Clarka4f581a2018-04-03 15:31:59 -0400766 if (preprocessor->fDefinition) {
767 if (Bracket::kParen == token->fBracket) {
768 preprocessor->fEnd = token->fContentEnd;
769 SkASSERT(')' == *preprocessor->fEnd);
770 ++preprocessor->fEnd;
771 return true;
772 }
773 SkASSERT(0); // incomplete
774 }
Cary Clark186d08f2018-04-03 08:43:27 -0400775 return true;
776 }
777 if (token && Definition::Type::kWord != token->fType) {
778 SkASSERT(0); // incomplete
779 }
780 if (preprocessor->fWord) {
781 preprocessor->fWord = false;
782 preprocessor->fEnd = token->fContentEnd;
783 return true;
784 }
785 return false;
786}
787
Cary Clark8032b982017-07-28 11:04:54 -0400788void IncludeWriter::enumSizeItems(const Definition& child) {
Cary Clark682c58d2018-05-16 07:07:07 -0400789 ItemState state = ItemState::kNone;
790 ItemLength lengths = { 0, 0, 0, 0 };
Cary Clark8032b982017-07-28 11:04:54 -0400791 const char* lastEnd = nullptr;
Cary Clark8032b982017-07-28 11:04:54 -0400792 auto brace = child.fChildren[0];
Cary Clarkbad5ad72017-08-03 17:14:08 -0400793 if (KeyWord::kClass == brace->fKeyWord) {
794 brace = brace->fChildren[0];
795 }
Cary Clark8032b982017-07-28 11:04:54 -0400796 SkASSERT(Bracket::kBrace == brace->fBracket);
Cary Clarkbad5ad72017-08-03 17:14:08 -0400797 vector<IterState> iterStack;
798 iterStack.emplace_back(brace->fTokens.begin(), brace->fTokens.end());
799 IterState* iterState = &iterStack[0];
Cary Clark186d08f2018-04-03 08:43:27 -0400800 Preprocessor preprocessor;
Cary Clark682c58d2018-05-16 07:07:07 -0400801 string enumName;
Cary Clarkbad5ad72017-08-03 17:14:08 -0400802 while (iterState->fDefIter != iterState->fDefEnd) {
803 auto& token = *iterState->fDefIter++;
Cary Clark186d08f2018-04-03 08:43:27 -0400804 if (this->enumPreprocessor(&token, MemberPass::kCount, iterStack, &iterState,
805 &preprocessor)) {
Cary Clarkbad5ad72017-08-03 17:14:08 -0400806 continue;
807 }
Cary Clark682c58d2018-05-16 07:07:07 -0400808 if (ItemState::kName == state) {
809 TextParser enumLine(token.fFileName, lastEnd, token.fContentStart, token.fLineCount);
Cary Clark8032b982017-07-28 11:04:54 -0400810 const char* end = enumLine.anyOf(",}=");
811 SkASSERT(end);
Cary Clark682c58d2018-05-16 07:07:07 -0400812 state = '=' == *end ? ItemState::kValue : ItemState::kComment;
813 if (ItemState::kValue == state) {
Cary Clark8032b982017-07-28 11:04:54 -0400814 lastEnd = token.fContentEnd;
Cary Clark682c58d2018-05-16 07:07:07 -0400815 lengths.fCurValue = (int) (lastEnd - token.fContentStart);
Cary Clark8032b982017-07-28 11:04:54 -0400816 continue;
817 }
818 }
Cary Clark682c58d2018-05-16 07:07:07 -0400819 if (ItemState::kValue == state) {
820 TextParser valueEnd(token.fFileName, lastEnd, token.fContentStart, token.fLineCount);
Cary Clark8032b982017-07-28 11:04:54 -0400821 const char* end = valueEnd.anyOf(",}");
822 if (!end) { // write expression continuation
Cary Clark682c58d2018-05-16 07:07:07 -0400823 lengths.fCurValue += (int) (token.fContentEnd - lastEnd);
Cary Clark8032b982017-07-28 11:04:54 -0400824 continue;
825 }
826 }
Cary Clark682c58d2018-05-16 07:07:07 -0400827 if (ItemState::kNone != state) {
828 this->checkEnumLengths(child, enumName, &lengths);
829 lengths.fCurValue = 0;
830 state = ItemState::kNone;
Cary Clark8032b982017-07-28 11:04:54 -0400831 }
Cary Clark682c58d2018-05-16 07:07:07 -0400832 SkASSERT(ItemState::kNone == state);
Cary Clark8032b982017-07-28 11:04:54 -0400833 lastEnd = token.fContentEnd;
Cary Clark682c58d2018-05-16 07:07:07 -0400834 lengths.fCurName = (int) (lastEnd - token.fContentStart);
835 enumName = string(token.fContentStart, lengths.fCurName);
836 state = ItemState::kName;
Cary Clark8032b982017-07-28 11:04:54 -0400837 }
Cary Clark682c58d2018-05-16 07:07:07 -0400838 if (ItemState::kNone != state) {
839 this->checkEnumLengths(child, enumName, &lengths);
Cary Clark8032b982017-07-28 11:04:54 -0400840 }
Cary Clark682c58d2018-05-16 07:07:07 -0400841 fEnumItemValueTab = lengths.fLongestName + fIndent + 1 /* 1: , */ ;
842 if (lengths.fLongestValue) {
843 lengths.fLongestValue += 3; // 3: space = space
Cary Clark8032b982017-07-28 11:04:54 -0400844 }
Cary Clark682c58d2018-05-16 07:07:07 -0400845 fEnumItemCommentTab = fEnumItemValueTab + lengths.fLongestValue + 1 ; // 1: space before //!<
Cary Clark8032b982017-07-28 11:04:54 -0400846 // iterate through bmh children and see which comments fit on include lines
Cary Clark682c58d2018-05-16 07:07:07 -0400847 if (!this->checkChildCommentLength(fEnumDef, MarkType::kConst)) {
848 fEnumDef->reportError<void>("expected at least one #Const in #Enum");
Cary Clark8032b982017-07-28 11:04:54 -0400849 }
850}
851
Cary Clark682c58d2018-05-16 07:07:07 -0400852const Definition* IncludeWriter::matchMemberName(string matchName, const Definition& child) const {
853 const Definition* parent = &child;
854 if (KeyWord::kEnum == child.fKeyWord && child.fChildren.size() > 0
855 && KeyWord::kClass == child.fChildren[0]->fKeyWord) {
856 matchName = child.fChildren[0]->fName + "::" + matchName;
857 }
858 do {
859 if (KeyWord::kStruct == parent->fKeyWord || KeyWord::kClass == parent->fKeyWord) {
860 matchName = parent->fName + "::" + matchName;
861 }
862 } while ((parent = parent->fParent));
863 const Definition* enumItem = nullptr;
864 for (auto testItem : fEnumDef->fChildren) {
865 if (MarkType::kConst != testItem->fMarkType) {
866 continue;
867 }
868 if (matchName != testItem->fName) {
869 continue;
870 }
871 enumItem = testItem;
872 break;
873 }
874 SkASSERT(enumItem);
875 return enumItem;
876}
877
Cary Clark8032b982017-07-28 11:04:54 -0400878// walk children and output complete method doxygen description
Cary Clark682c58d2018-05-16 07:07:07 -0400879void IncludeWriter::methodOut(Definition* method, const Definition& child) {
Cary Clark154beea2017-10-26 07:58:48 -0400880 if (fPendingMethod) {
Cary Clark0d225392018-06-07 09:59:07 -0400881 this->indentOut();
Cary Clark154beea2017-10-26 07:58:48 -0400882 fPendingMethod = false;
883 }
Cary Clark579985c2017-07-31 11:48:27 -0400884 fBmhMethod = method;
885 fMethodDef = &child;
Cary Clark8032b982017-07-28 11:04:54 -0400886 fContinuation = nullptr;
887 fDeferComment = nullptr;
Cary Clark682c58d2018-05-16 07:07:07 -0400888 Definition* csParent = method->csParent();
Cary Clarka4f581a2018-04-03 15:31:59 -0400889 if (csParent && (0 == fIndent || fIndentNext)) {
Cary Clark0d225392018-06-07 09:59:07 -0400890 this->indentIn(IndentKind::kMethodOut);
Cary Clark154beea2017-10-26 07:58:48 -0400891 fIndentNext = false;
Cary Clark8032b982017-07-28 11:04:54 -0400892 }
893 this->writeCommentHeader();
894 fIndent += 4;
Cary Clark1a8d7622018-03-05 13:26:16 -0500895 this->descriptionOut(method, SkipFirstLine::kNo, Phrase::kNo);
Cary Clark8032b982017-07-28 11:04:54 -0400896 // compute indention column
897 size_t column = 0;
898 bool hasParmReturn = false;
899 for (auto methodPart : method->fChildren) {
900 if (MarkType::kParam == methodPart->fMarkType) {
901 column = SkTMax(column, methodPart->fName.length());
902 hasParmReturn = true;
903 } else if (MarkType::kReturn == methodPart->fMarkType) {
904 hasParmReturn = true;
905 }
906 }
907 if (hasParmReturn) {
908 this->lf(2);
909 column += fIndent + sizeof("@return ");
910 int saveIndent = fIndent;
911 for (auto methodPart : method->fChildren) {
Cary Clark8032b982017-07-28 11:04:54 -0400912 if (MarkType::kParam == methodPart->fMarkType) {
913 this->writeString("@param");
914 this->writeSpace();
915 this->writeString(methodPart->fName.c_str());
916 } else if (MarkType::kReturn == methodPart->fMarkType) {
917 this->writeString("@return");
918 } else {
919 continue;
920 }
Cary Clark1a8d7622018-03-05 13:26:16 -0500921 this->indentToColumn(column);
922 fIndent = column;
Cary Clark1a8d7622018-03-05 13:26:16 -0500923 this->descriptionOut(methodPart, SkipFirstLine::kNo, Phrase::kYes);
Cary Clark8032b982017-07-28 11:04:54 -0400924 fIndent = saveIndent;
925 this->lfcr();
926 }
927 } else {
928 this->lfcr();
929 }
930 fIndent -= 4;
931 this->lfcr();
Cary Clark137b8742018-05-30 09:21:49 -0400932 this->writeCommentTrailer(OneLine::kNo);
Cary Clark579985c2017-07-31 11:48:27 -0400933 fBmhMethod = nullptr;
934 fMethodDef = nullptr;
Cary Clark78c110e2018-02-09 16:49:09 -0500935 fEnumDef = nullptr;
Cary Clarkd0530ba2017-09-14 11:25:39 -0400936 fWroteMethod = true;
Cary Clark8032b982017-07-28 11:04:54 -0400937}
938
939void IncludeWriter::structOut(const Definition* root, const Definition& child,
940 const char* commentStart, const char* commentEnd) {
941 this->writeCommentHeader();
942 this->writeString("\\");
943 SkASSERT(MarkType::kClass == child.fMarkType || MarkType::kStruct == child.fMarkType);
944 this->writeString(MarkType::kClass == child.fMarkType ? "class" : "struct");
945 this->writeSpace();
946 this->writeString(child.fName.c_str());
947 fIndent += 4;
948 this->lfcr();
Cary Clarkc68ba1d2018-02-20 10:35:29 -0500949 if (child.fDeprecated) {
Cary Clark682c58d2018-05-16 07:07:07 -0400950 this->writeString(child.incompleteMessage(Definition::DetailsType::kSentence));
Cary Clarkc68ba1d2018-02-20 10:35:29 -0500951 } else {
952 this->rewriteBlock((int)(commentEnd - commentStart), commentStart, Phrase::kNo);
953 }
Cary Clark8032b982017-07-28 11:04:54 -0400954 fIndent -= 4;
955 this->lfcr();
Cary Clark137b8742018-05-30 09:21:49 -0400956 this->writeCommentTrailer(OneLine::kNo);
Cary Clark8032b982017-07-28 11:04:54 -0400957}
958
Cary Clark186d08f2018-04-03 08:43:27 -0400959bool IncludeWriter::findEnumSubtopic(string undername, const Definition** rootDefPtr) const {
960 const Definition* subtopic = fEnumDef->fParent;
961 string subcheck = subtopic->fFiddle + '_' + undername;
962 auto iter = fBmhParser->fTopicMap.find(subcheck);
963 if (iter == fBmhParser->fTopicMap.end()) {
964 return false;
965 }
966 *rootDefPtr = iter->second;
967 return true;
968}
969
Cary Clark2dc84ad2018-01-26 12:56:22 -0500970Definition* IncludeWriter::findMemberCommentBlock(const vector<Definition*>& bmhChildren,
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400971 string name) const {
Cary Clarkc68ba1d2018-02-20 10:35:29 -0500972 for (auto memberDef : bmhChildren) {
973 if (MarkType::kMember != memberDef->fMarkType) {
974 continue;
975 }
976 string match = memberDef->fName;
977 // if match.endsWith(name) ...
Cary Clark300cc5b2018-02-20 12:50:35 -0500978 if (match.length() >= name.length() &&
Cary Clarkc68ba1d2018-02-20 10:35:29 -0500979 0 == match.compare(match.length() - name.length(), name.length(), name)) {
980 return memberDef;
981 }
982 }
983 for (auto memberDef : bmhChildren) {
984 if (MarkType::kSubtopic != memberDef->fMarkType && MarkType::kTopic != memberDef->fMarkType) {
985 continue;
986 }
987 Definition* result = this->findMemberCommentBlock(memberDef->fChildren, name);
988 if (result) {
989 return result;
990 }
991 }
992 return nullptr;
Cary Clark2dc84ad2018-01-26 12:56:22 -0500993}
994
Cary Clark0d225392018-06-07 09:59:07 -0400995Definition* IncludeWriter::findMethod(string name, RootDefinition* root) const {
996 if (root) {
997 return root->find(name, RootDefinition::AllowParens::kNo);
998 }
999 auto methodIter = fBmhParser->fMethodMap.find(name);
1000 if (fBmhParser->fMethodMap.end() == methodIter) {
1001 return nullptr;
1002 }
1003 return &methodIter->second;
1004}
1005
1006
1007void IncludeWriter::firstBlock(int size, const char* data) {
1008 SkAssertResult(this->firstBlockTrim(size, data));
1009}
1010
1011bool IncludeWriter::firstBlockTrim(int size, const char* data) {
1012 bool result = this->writeBlockTrim(size, data);
1013 if (fFirstWrite) {
1014 auto fileInfo = std::find_if(fRootTopic->fChildren.begin(), fRootTopic->fChildren.end(),
1015 [](const Definition* def){ return MarkType::kFile == def->fMarkType; } );
1016 if (fRootTopic->fChildren.end() != fileInfo) {
1017 this->writeCommentHeader();
1018 this->writeString("\\file");
1019 this->writeSpace();
1020 size_t lastSlash = fFileName.rfind('/');
1021 if (string::npos == lastSlash) {
1022 lastSlash = fFileName.rfind('\\');
1023 }
1024 string fileName = fFileName.substr(lastSlash + 1);
1025 this->writeString(fileName);
1026 this->lf(2);
1027 fIndent += 4;
1028 this->descriptionOut(*fileInfo, SkipFirstLine::kNo, Phrase::kNo);
1029 fIndent -= 4;
1030 this->writeCommentTrailer(OneLine::kNo);
1031 }
1032 fFirstWrite = false;
1033 }
1034 return result;
1035}
1036
1037void IncludeWriter::setStart(const char* start, const Definition* def) {
1038 SkASSERT(start >= fStart);
1039 this->setStartBack(start, def);
1040}
1041
1042void IncludeWriter::setStartBack(const char* start, const Definition* def) {
1043 fStartSetter = def;
1044 fStart = start;
1045}
1046
Cary Clarkbad5ad72017-08-03 17:14:08 -04001047Definition* IncludeWriter::structMemberOut(const Definition* memberStart, const Definition& child) {
Cary Clark0d225392018-06-07 09:59:07 -04001048 const char* blockStart = !fWroteMethod && fDeferComment ? fDeferComment->fContentEnd : fStart;
Cary Clarkd0530ba2017-09-14 11:25:39 -04001049 const char* blockEnd = fWroteMethod && fDeferComment ? fDeferComment->fStart - 1 :
1050 memberStart->fStart;
Cary Clark0d225392018-06-07 09:59:07 -04001051 this->firstBlockTrim((int) (blockEnd - blockStart), blockStart);
1052 this->indentDeferred(IndentKind::kStructMember);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001053 fWroteMethod = false;
Cary Clark8032b982017-07-28 11:04:54 -04001054 string name(child.fContentStart, (int) (child.fContentEnd - child.fContentStart));
Cary Clark2dc84ad2018-01-26 12:56:22 -05001055 Definition* commentBlock = this->findMemberCommentBlock(fBmhStructDef->fChildren, name);
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001056 if (!commentBlock) {
Cary Clark137b8742018-05-30 09:21:49 -04001057 return memberStart->reportError<Definition*>("member missing comment block 2");
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001058 }
Cary Clark137b8742018-05-30 09:21:49 -04001059 auto lineIter = std::find_if(commentBlock->fChildren.begin(), commentBlock->fChildren.end(),
1060 [](const Definition* def){ return MarkType::kLine == def->fMarkType; } );
1061 SkASSERT(commentBlock->fChildren.end() != lineIter);
1062 const Definition* lineDef = *lineIter;
1063 if (fStructMemberLength > 100) {
Cary Clark8032b982017-07-28 11:04:54 -04001064 this->writeCommentHeader();
Cary Clark137b8742018-05-30 09:21:49 -04001065 this->writeSpace();
1066 this->rewriteBlock(lineDef->length(), lineDef->fContentStart, Phrase::kYes);
1067 this->writeCommentTrailer(OneLine::kYes);
Cary Clark8032b982017-07-28 11:04:54 -04001068 }
1069 this->lfcr();
Cary Clarkbad5ad72017-08-03 17:14:08 -04001070 this->writeBlock((int) (child.fStart - memberStart->fContentStart),
Cary Clark8032b982017-07-28 11:04:54 -04001071 memberStart->fContentStart);
1072 this->indentToColumn(fStructMemberTab);
1073 this->writeString(name.c_str());
Cary Clark6fc50412017-09-21 12:31:06 -04001074 auto tokenIter = child.fParent->fTokens.begin();
Cary Clarkbad5ad72017-08-03 17:14:08 -04001075 std::advance(tokenIter, child.fParentIndex + 1);
1076 Definition* valueStart = &*tokenIter;
1077 while (Definition::Type::kPunctuation != tokenIter->fType) {
1078 std::advance(tokenIter, 1);
1079 SkASSERT(child.fParent->fTokens.end() != tokenIter);
1080 }
1081 Definition* valueEnd = &*tokenIter;
1082 if (valueStart != valueEnd) {
1083 this->indentToColumn(fStructValueTab);
1084 this->writeString("=");
1085 this->writeSpace();
1086 this->writeBlock((int) (valueEnd->fStart - valueStart->fContentStart),
Ben Wagner63fd7602017-10-09 15:45:33 -04001087 valueStart->fContentStart);
Cary Clarkbad5ad72017-08-03 17:14:08 -04001088 }
Cary Clark8032b982017-07-28 11:04:54 -04001089 this->writeString(";");
Cary Clark137b8742018-05-30 09:21:49 -04001090 if (fStructMemberLength <= 100) {
Cary Clark8032b982017-07-28 11:04:54 -04001091 this->indentToColumn(fStructCommentTab);
1092 this->writeString("//!<");
1093 this->writeSpace();
Cary Clark137b8742018-05-30 09:21:49 -04001094 this->rewriteBlock(lineDef->length(), lineDef->fContentStart, Phrase::kYes);
Cary Clark8032b982017-07-28 11:04:54 -04001095 }
Cary Clark137b8742018-05-30 09:21:49 -04001096 this->lf(1);
Cary Clarkbad5ad72017-08-03 17:14:08 -04001097 return valueEnd;
Cary Clark8032b982017-07-28 11:04:54 -04001098}
1099
Cary Clark137b8742018-05-30 09:21:49 -04001100// const and constexpr and #define aren't contained in a braces like struct and enum.
1101// use a bmh subtopic to group like ones together, then measure them in the include as if
1102// they were formally linked together
1103void IncludeWriter::constSizeMembers(const RootDefinition* root) {
1104 // fBmhConst->fParent is subtopic containing all grouped const expressions
1105 // fConstDef is token of const include name, hopefully on same line as const start
1106 string rootPrefix = root ? root->fName + "::" : "";
1107 const Definition* test = fConstDef;
1108 int tokenIndex = test->fParentIndex;
1109 int longestName = 0;
1110 int longestValue = 0;
1111 int longestComment = 0;
1112 const Definition* subtopic = fBmhConst->fParent;
1113 SkASSERT(subtopic);
1114 SkASSERT(MarkType::kSubtopic == subtopic->fMarkType);
1115 // back up to first token on line
1116 size_t lineCount = test->fLineCount;
1117 const Definition* last;
1118 auto tokenIter = test->fParent->fTokens.begin();
1119 std::advance(tokenIter, tokenIndex);
1120 do {
1121 last = test;
1122 std::advance(tokenIter, -1);
1123 test = &*tokenIter;
1124 SkASSERT(test->fParentIndex == --tokenIndex);
1125 } while (lineCount == test->fLineCount);
1126 test = last;
1127 for (auto child : subtopic->fChildren) {
1128 if (MarkType::kConst != child->fMarkType) {
1129 continue;
1130 }
1131 // expect found name to be on the left of assign
1132 // expect assign
1133 // expect semicolon
1134 // no parens, no braces
1135 while (rootPrefix + test->fName != child->fName) {
1136 std::advance(tokenIter, 1);
1137 test = &*tokenIter;
1138 SkASSERT(lineCount >= test->fLineCount);
1139 }
1140 ++lineCount;
1141 TextParser constText(test);
1142 const char* nameEnd = constText.trimmedBracketEnd('=');
1143 SkAssertResult(constText.skipToEndBracket('='));
1144 const char* valueEnd = constText.trimmedBracketEnd(';');
1145 auto lineIter = std::find_if(child->fChildren.begin(), child->fChildren.end(),
1146 [](const Definition* def){ return MarkType::kLine == def->fMarkType; });
1147 SkASSERT(child->fChildren.end() != lineIter);
1148 longestName = SkTMax(longestName, (int) (nameEnd - constText.fStart));
1149 longestValue = SkTMax(longestValue, (int) (valueEnd - constText.fChar));
1150 longestComment = SkTMax(longestComment, (*lineIter)->length());
1151 }
1152 // write fStructValueTab, fStructCommentTab
1153 fConstValueTab = longestName + fIndent + 1;
1154 fConstCommentTab = fConstValueTab + longestValue + 2;
1155 fConstLength = fConstCommentTab + longestComment + (int) sizeof("//!<");
1156}
1157
Cary Clarkb94f6da2018-06-08 11:54:32 -04001158bool IncludeWriter::defineOut(const Definition& def) {
1159 if (def.fTokens.size() < 1) {
1160 return false;
1161 }
1162 auto& child = def.fTokens.front();
1163 string name(child.fContentStart, child.length());
1164 auto defIter = fBmhParser->fDefineMap.find(name);
1165 if (fBmhParser->fDefineMap.end() == defIter) {
1166 return false;
1167 }
1168 const Definition& bmhDef = defIter->second;
1169 this->constOut(&def, &bmhDef);
Cary Clarkb94f6da2018-06-08 11:54:32 -04001170 return true;
1171}
1172
Cary Clark884dd7d2017-10-11 10:37:52 -04001173void IncludeWriter::structSizeMembers(const Definition& child) {
Cary Clark8032b982017-07-28 11:04:54 -04001174 int longestType = 0;
1175 Definition* typeStart = nullptr;
1176 int longestName = 0;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001177 int longestValue = 0;
Cary Clark137b8742018-05-30 09:21:49 -04001178 int longestComment = 0;
Cary Clark8032b982017-07-28 11:04:54 -04001179 SkASSERT(child.fChildren.size() == 1 || child.fChildren.size() == 2);
1180 bool inEnum = false;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001181 bool inMethod = false;
1182 bool inMember = false;
Cary Clark8032b982017-07-28 11:04:54 -04001183 auto brace = child.fChildren[0];
1184 SkASSERT(Bracket::kBrace == brace->fBracket);
1185 for (auto& token : brace->fTokens) {
1186 if (Definition::Type::kBracket == token.fType) {
1187 if (Bracket::kSlashSlash == token.fBracket) {
1188 continue; // ignore old inline comments
1189 }
1190 if (Bracket::kSlashStar == token.fBracket) {
1191 continue; // ignore old inline comments
1192 }
1193 if (Bracket::kParen == token.fBracket) {
Cary Clarkbad5ad72017-08-03 17:14:08 -04001194 if (inMethod) {
1195 continue;
1196 }
Cary Clark8032b982017-07-28 11:04:54 -04001197 break;
1198 }
1199 SkASSERT(0); // incomplete
1200 }
1201 if (Definition::Type::kKeyWord == token.fType) {
1202 switch (token.fKeyWord) {
1203 case KeyWord::kEnum:
1204 inEnum = true;
1205 break;
1206 case KeyWord::kConst:
1207 case KeyWord::kConstExpr:
1208 case KeyWord::kStatic:
1209 case KeyWord::kInt:
Cary Clarkd0530ba2017-09-14 11:25:39 -04001210 case KeyWord::kUint8_t:
1211 case KeyWord::kUint16_t:
Cary Clark8032b982017-07-28 11:04:54 -04001212 case KeyWord::kUint32_t:
Cary Clarkd0530ba2017-09-14 11:25:39 -04001213 case KeyWord::kUint64_t:
Cary Clark4dc5a452018-05-21 11:56:57 -04001214 case KeyWord::kUintPtr_t:
Cary Clark8032b982017-07-28 11:04:54 -04001215 case KeyWord::kSize_t:
1216 case KeyWord::kFloat:
1217 case KeyWord::kBool:
1218 case KeyWord::kVoid:
1219 if (!typeStart) {
1220 typeStart = &token;
1221 }
1222 break;
1223 default:
1224 break;
1225 }
1226 continue;
1227 }
1228 if (Definition::Type::kPunctuation == token.fType) {
1229 if (inEnum) {
1230 SkASSERT(Punctuation::kSemicolon == token.fPunctuation);
1231 inEnum = false;
1232 }
Cary Clarkbad5ad72017-08-03 17:14:08 -04001233 if (inMethod) {
1234 if (Punctuation::kColon == token.fPunctuation) {
1235 inMethod = false;
1236 } else if (Punctuation::kLeftBrace == token.fPunctuation) {
1237 inMethod = false;
Cary Clark73fa9722017-08-29 17:36:51 -04001238 } else if (Punctuation::kSemicolon == token.fPunctuation) {
1239 inMethod = false;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001240 } else {
1241 SkASSERT(0); // incomplete
1242 }
1243 }
1244 if (inMember) {
1245 SkASSERT(Punctuation::kSemicolon == token.fPunctuation);
1246 typeStart = nullptr;
1247 inMember = false;
1248 }
Cary Clark8032b982017-07-28 11:04:54 -04001249 continue;
1250 }
1251 if (Definition::Type::kWord != token.fType) {
1252 SkASSERT(0); // incomplete
1253 }
1254 if (MarkType::kMember == token.fMarkType) {
1255 TextParser typeStr(token.fFileName, typeStart->fContentStart, token.fContentStart,
1256 token.fLineCount);
1257 typeStr.trimEnd();
Cary Clarkbad5ad72017-08-03 17:14:08 -04001258 longestType = SkTMax(longestType, (int) (typeStr.fEnd - typeStr.fStart));
Cary Clark8032b982017-07-28 11:04:54 -04001259 longestName = SkTMax(longestName, (int) (token.fContentEnd - token.fContentStart));
1260 typeStart->fMemberStart = true;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001261 inMember = true;
Cary Clark137b8742018-05-30 09:21:49 -04001262 string tokenName(token.fContentStart, (int) (token.fContentEnd - token.fContentStart));
1263 Definition* commentBlock = this->findMemberCommentBlock(fBmhStructDef->fChildren,
1264 tokenName);
1265 if (!commentBlock) {
1266 return token.reportError<void>("member missing comment block 1");
1267 }
1268 auto lineIter = std::find_if(commentBlock->fChildren.begin(),
1269 commentBlock->fChildren.end(),
1270 [](const Definition* def){ return MarkType::kLine == def->fMarkType; } );
1271 SkASSERT(commentBlock->fChildren.end() != lineIter);
1272 const Definition* lineDef = *lineIter;
1273 longestComment = SkTMax(longestComment, lineDef->length());
Cary Clarkbad5ad72017-08-03 17:14:08 -04001274 continue;
1275 }
1276 if (MarkType::kMethod == token.fMarkType) {
1277 inMethod = true;
Cary Clark8032b982017-07-28 11:04:54 -04001278 continue;
1279 }
1280 SkASSERT(MarkType::kNone == token.fMarkType);
Cary Clarkbad5ad72017-08-03 17:14:08 -04001281 if (typeStart) {
1282 if (inMember) {
1283 longestValue =
1284 SkTMax(longestValue, (int) (token.fContentEnd - token.fContentStart));
1285 }
1286 } else {
Cary Clark8032b982017-07-28 11:04:54 -04001287 typeStart = &token;
1288 }
1289 }
1290 fStructMemberTab = longestType + fIndent + 1 /* space before name */ ;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001291 fStructValueTab = fStructMemberTab + longestName + 2 /* space ; */ ;
1292 fStructCommentTab = fStructValueTab;
1293 if (longestValue) {
1294 fStructCommentTab += longestValue + 3 /* space = space */ ;
1295 fStructValueTab -= 1 /* ; */ ;
1296 }
Cary Clark137b8742018-05-30 09:21:49 -04001297 fStructMemberLength = fStructCommentTab + longestComment;
Cary Clark682c58d2018-05-16 07:07:07 -04001298 // iterate through struct to ensure that members' comments fit on line
1299 // struct or class may not have any members
1300 (void) this->checkChildCommentLength(fBmhStructDef, MarkType::kMember);
Cary Clark8032b982017-07-28 11:04:54 -04001301}
1302
Cary Clark154beea2017-10-26 07:58:48 -04001303static bool find_start(const Definition* startDef, const char* start) {
1304 for (const auto& child : startDef->fTokens) {
1305 if (child.fContentStart == start) {
1306 return MarkType::kMethod == child.fMarkType;
1307 }
1308 if (child.fContentStart >= start) {
1309 break;
1310 }
1311 if (find_start(&child, start)) {
1312 return true;
1313 }
1314 }
1315 return false;
1316}
1317
Cary Clark73fa9722017-08-29 17:36:51 -04001318bool IncludeWriter::populate(Definition* def, ParentPair* prevPair, RootDefinition* root) {
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001319 if (!def->fTokens.size()) {
1320 return true;
1321 }
Cary Clark73fa9722017-08-29 17:36:51 -04001322 ParentPair pair = { def, prevPair };
Cary Clark8032b982017-07-28 11:04:54 -04001323 // write bulk of original include up to class, method, enum, etc., excepting preceding comment
1324 // find associated bmh object
1325 // write any associated comments in Doxygen form
1326 // skip include comment
1327 // if there is a series of same named methods, write one set of comments, then write all methods
1328 string methodName;
Cary Clark682c58d2018-05-16 07:07:07 -04001329 Definition* method = nullptr;
1330 Definition* clonedMethod = nullptr;
Cary Clark8032b982017-07-28 11:04:54 -04001331 const Definition* memberStart = nullptr;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001332 const Definition* memberEnd = nullptr;
Cary Clark8032b982017-07-28 11:04:54 -04001333 fContinuation = nullptr;
1334 bool inStruct = false;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001335 bool inConstructor = false;
Cary Clark884dd7d2017-10-11 10:37:52 -04001336 bool inInline = false;
Cary Clark154beea2017-10-26 07:58:48 -04001337 bool eatOperator = false;
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001338 bool sawConst = false;
1339 bool staticOnly = false;
Cary Clark0d225392018-06-07 09:59:07 -04001340 bool sawTypedef = false;
Cary Clark27dddae2018-06-08 15:57:37 -04001341 Definition* deferredTypedefComment = nullptr;
Cary Clark154beea2017-10-26 07:58:48 -04001342 const Definition* requireDense = nullptr;
1343 const Definition* startDef = nullptr;
Cary Clark8032b982017-07-28 11:04:54 -04001344 for (auto& child : def->fTokens) {
Cary Clark0d225392018-06-07 09:59:07 -04001345 if (KeyWord::kInline == child.fKeyWord) {
1346 continue;
1347 }
Cary Clark154beea2017-10-26 07:58:48 -04001348 if (KeyWord::kOperator == child.fKeyWord && method &&
1349 Definition::MethodType::kOperator == method->fMethodType) {
1350 eatOperator = true;
1351 continue;
1352 }
1353 if (eatOperator) {
1354 if (Bracket::kSquare == child.fBracket || Bracket::kParen == child.fBracket) {
1355 continue;
1356 }
1357 eatOperator = false;
1358 fContinuation = nullptr;
1359 if (KeyWord::kConst == child.fKeyWord) {
1360 continue;
1361 }
1362 }
Cary Clarkbad5ad72017-08-03 17:14:08 -04001363 if (memberEnd) {
1364 if (memberEnd != &child) {
1365 continue;
1366 }
Cary Clark154beea2017-10-26 07:58:48 -04001367 startDef = &child;
Cary Clark0d225392018-06-07 09:59:07 -04001368 this->setStart(child.fContentStart + 1, &child);
Cary Clarkbad5ad72017-08-03 17:14:08 -04001369 memberEnd = nullptr;
1370 }
Cary Clark8032b982017-07-28 11:04:54 -04001371 if (child.fPrivate) {
Cary Clark884dd7d2017-10-11 10:37:52 -04001372 if (MarkType::kMethod == child.fMarkType) {
1373 inInline = true;
1374 }
Cary Clark8032b982017-07-28 11:04:54 -04001375 continue;
1376 }
Cary Clark884dd7d2017-10-11 10:37:52 -04001377 if (inInline) {
1378 if (Definition::Type::kKeyWord == child.fType) {
1379 SkASSERT(MarkType::kMethod != child.fMarkType);
1380 continue;
1381 }
1382 if (Definition::Type::kPunctuation == child.fType) {
1383 if (Punctuation::kLeftBrace == child.fPunctuation) {
1384 inInline = false;
1385 } else {
1386 SkASSERT(Punctuation::kAsterisk == child.fPunctuation);
1387 }
1388 continue;
1389 }
1390 if (Definition::Type::kWord == child.fType) {
1391 string name(child.fContentStart, child.fContentEnd - child.fContentStart);
1392 SkASSERT(string::npos != name.find("::"));
1393 continue;
1394 }
1395 if (Definition::Type::kBracket == child.fType) {
1396 SkASSERT(Bracket::kParen == child.fBracket);
1397 continue;
1398 }
1399 }
Cary Clark8032b982017-07-28 11:04:54 -04001400 if (fContinuation) {
1401 if (Definition::Type::kKeyWord == child.fType) {
Cary Clarkd0530ba2017-09-14 11:25:39 -04001402 if (KeyWord::kFriend == child.fKeyWord ||
Cary Clark73fa9722017-08-29 17:36:51 -04001403 KeyWord::kSK_API == child.fKeyWord) {
Cary Clark8032b982017-07-28 11:04:54 -04001404 continue;
1405 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001406 const IncludeKey& includeKey = kKeyWords[(int) child.fKeyWord];
1407 if (KeyProperty::kNumber == includeKey.fProperty) {
1408 continue;
1409 }
Cary Clark8032b982017-07-28 11:04:54 -04001410 }
Cary Clark3cd22cc2017-12-01 11:49:58 -05001411 if (Definition::Type::kBracket == child.fType) {
1412 if (Bracket::kAngle == child.fBracket) {
Cary Clark8032b982017-07-28 11:04:54 -04001413 continue;
1414 }
Cary Clark3cd22cc2017-12-01 11:49:58 -05001415 if (Bracket::kParen == child.fBracket) {
1416 if (!clonedMethod) {
1417 if (inConstructor) {
1418 fContinuation = child.fContentStart;
1419 }
1420 continue;
Cary Clark8032b982017-07-28 11:04:54 -04001421 }
Cary Clark3cd22cc2017-12-01 11:49:58 -05001422 int alternate = 1;
1423 ptrdiff_t childLen = child.fContentEnd - child.fContentStart;
1424 SkASSERT(')' == child.fContentStart[childLen]);
1425 ++childLen;
1426 do {
1427 TextParser params(clonedMethod->fFileName, clonedMethod->fStart,
1428 clonedMethod->fContentStart, clonedMethod->fLineCount);
1429 params.skipToEndBracket('(');
1430 if (params.startsWith(child.fContentStart, childLen)) {
1431 this->methodOut(clonedMethod, child);
Cary Clark137b8742018-05-30 09:21:49 -04001432 sawConst = false;
Cary Clark3cd22cc2017-12-01 11:49:58 -05001433 break;
1434 }
1435 ++alternate;
1436 string alternateMethod = methodName + '_' + to_string(alternate);
Cary Clark224c7002018-06-27 11:00:21 -04001437 clonedMethod = this->findMethod(alternateMethod, root);
Cary Clark3cd22cc2017-12-01 11:49:58 -05001438 } while (clonedMethod);
1439 if (!clonedMethod) {
Cary Clark0d225392018-06-07 09:59:07 -04001440 return child.reportError<bool>("cloned method not found");
Cary Clark3cd22cc2017-12-01 11:49:58 -05001441 }
1442 clonedMethod = nullptr;
1443 continue;
Cary Clark8032b982017-07-28 11:04:54 -04001444 }
Cary Clark8032b982017-07-28 11:04:54 -04001445 }
1446 if (Definition::Type::kWord == child.fType) {
1447 if (clonedMethod) {
1448 continue;
1449 }
1450 size_t len = (size_t) (child.fContentEnd - child.fContentStart);
1451 const char operatorStr[] = "operator";
1452 size_t operatorLen = sizeof(operatorStr) - 1;
1453 if (len >= operatorLen && !strncmp(child.fContentStart, operatorStr, operatorLen)) {
1454 fContinuation = child.fContentEnd;
1455 continue;
1456 }
1457 }
1458 if (Definition::Type::kPunctuation == child.fType &&
1459 (Punctuation::kSemicolon == child.fPunctuation ||
Cary Clark6fc50412017-09-21 12:31:06 -04001460 Punctuation::kLeftBrace == child.fPunctuation ||
1461 (Punctuation::kColon == child.fPunctuation && inConstructor))) {
Cary Clark8032b982017-07-28 11:04:54 -04001462 SkASSERT(fContinuation[0] == '(');
1463 const char* continueEnd = child.fContentStart;
1464 while (continueEnd > fContinuation && isspace(continueEnd[-1])) {
1465 --continueEnd;
1466 }
1467 methodName += string(fContinuation, continueEnd - fContinuation);
Cary Clark82f1f742018-06-28 08:50:35 -04001468 if (string::npos != methodName.find('\n')) {
1469 methodName.erase(std::remove(methodName.begin(), methodName.end(), '\n'),
1470 methodName.end());
1471 }
Cary Clark0d225392018-06-07 09:59:07 -04001472 method = this->findMethod(methodName, root);
Cary Clark8032b982017-07-28 11:04:54 -04001473 if (!method) {
Cary Clark56356312018-02-08 14:45:18 -05001474 if (fBmhStructDef && fBmhStructDef->fDeprecated) {
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001475 fContinuation = nullptr;
Cary Clark56356312018-02-08 14:45:18 -05001476 continue;
1477 }
Cary Clark06c20f32018-03-20 15:53:27 -04001478 return child.reportError<bool>("method not found");
Cary Clark8032b982017-07-28 11:04:54 -04001479 }
Cary Clark579985c2017-07-31 11:48:27 -04001480 this->methodOut(method, child);
Cary Clark137b8742018-05-30 09:21:49 -04001481 sawConst = false;
Cary Clark8032b982017-07-28 11:04:54 -04001482 continue;
1483 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001484 if (Definition::Type::kPunctuation == child.fType &&
1485 Punctuation::kAsterisk == child.fPunctuation &&
1486 clonedMethod) {
1487 continue;
1488 }
Cary Clarkbad5ad72017-08-03 17:14:08 -04001489 if (inConstructor) {
1490 continue;
1491 }
Cary Clark0d225392018-06-07 09:59:07 -04001492 method = this->findMethod(methodName + "()", root);
Cary Clark8032b982017-07-28 11:04:54 -04001493 if (method) {
Cary Clark6fc50412017-09-21 12:31:06 -04001494 if (method->fCloned) {
1495 clonedMethod = method;
1496 continue;
1497 }
Cary Clark579985c2017-07-31 11:48:27 -04001498 this->methodOut(method, child);
Cary Clark137b8742018-05-30 09:21:49 -04001499 sawConst = false;
Cary Clark8032b982017-07-28 11:04:54 -04001500 continue;
Cary Clark56356312018-02-08 14:45:18 -05001501 } else if (fBmhStructDef && fBmhStructDef->fDeprecated) {
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001502 fContinuation = nullptr;
1503 continue;
Cary Clark8032b982017-07-28 11:04:54 -04001504 }
Cary Clark06c20f32018-03-20 15:53:27 -04001505 return child.reportError<bool>("method not found");
Cary Clark8032b982017-07-28 11:04:54 -04001506 }
1507 if (Bracket::kSlashSlash == child.fBracket || Bracket::kSlashStar == child.fBracket) {
1508 if (!fDeferComment) {
1509 fDeferComment = &child;
1510 }
1511 continue;
Ben Wagner63fd7602017-10-09 15:45:33 -04001512 }
Cary Clark8032b982017-07-28 11:04:54 -04001513 if (MarkType::kMethod == child.fMarkType) {
Cary Clark2d4bf5f2018-04-16 08:37:38 -04001514 if (this->isInternalName(child)) {
Cary Clarkbad5ad72017-08-03 17:14:08 -04001515 continue;
1516 }
Cary Clark8032b982017-07-28 11:04:54 -04001517 const char* bodyEnd = fDeferComment ? fDeferComment->fContentStart - 1 :
Cary Clark154beea2017-10-26 07:58:48 -04001518 fAttrDeprecated ? fAttrDeprecated->fContentStart - 1 :
Cary Clark8032b982017-07-28 11:04:54 -04001519 child.fContentStart;
Cary Clark7cfcbca2018-01-04 16:11:51 -05001520 if (Definition::Type::kBracket == def->fType && Bracket::kDebugCode == def->fBracket) {
1521 auto tokenIter = def->fParent->fTokens.begin();
1522 std::advance(tokenIter, def->fParentIndex - 1);
1523 Definition* prior = &*tokenIter;
1524 if (Definition::Type::kBracket == def->fType &&
1525 Bracket::kSlashStar == prior->fBracket) {
1526 bodyEnd = prior->fContentStart - 1;
1527 }
1528 }
Cary Clark8032b982017-07-28 11:04:54 -04001529 // FIXME: roll end-trimming into writeBlockTrim call
1530 while (fStart < bodyEnd && ' ' >= bodyEnd[-1]) {
1531 --bodyEnd;
1532 }
1533 int blockSize = (int) (bodyEnd - fStart);
Cary Clark2d4bf5f2018-04-16 08:37:38 -04001534 SkASSERT(blockSize >= 0);
Cary Clark8032b982017-07-28 11:04:54 -04001535 if (blockSize) {
Cary Clark7cfcbca2018-01-04 16:11:51 -05001536 string debugstr(fStart, blockSize);
Cary Clark8032b982017-07-28 11:04:54 -04001537 this->writeBlock(blockSize, fStart);
1538 }
Cary Clark154beea2017-10-26 07:58:48 -04001539 startDef = &child;
Cary Clark0d225392018-06-07 09:59:07 -04001540 this->setStart(child.fContentStart, &child);
Cary Clark06c20f32018-03-20 15:53:27 -04001541 auto mapFind = fBmhParser->fMethodMap.find(child.fName);
1542 if (fBmhParser->fMethodMap.end() != mapFind) {
1543 inConstructor = false;
1544 method = &mapFind->second;
Cary Clark0d225392018-06-07 09:59:07 -04001545 methodName = child.fName;
Cary Clark06c20f32018-03-20 15:53:27 -04001546 } else {
1547 methodName = root->fName + "::" + child.fName;
Cary Clark224c7002018-06-27 11:00:21 -04001548 size_t lastName = root->fName.rfind(':');
1549 lastName = string::npos == lastName ? 0 : lastName + 1;
1550 inConstructor = root->fName.substr(lastName) == child.fName;
Cary Clark06c20f32018-03-20 15:53:27 -04001551 method = root->find(methodName, RootDefinition::AllowParens::kNo);
1552 }
Cary Clark8032b982017-07-28 11:04:54 -04001553 fContinuation = child.fContentEnd;
Cary Clark8032b982017-07-28 11:04:54 -04001554 if (!method) {
1555 continue;
1556 }
1557 if (method->fCloned) {
1558 clonedMethod = method;
1559 continue;
1560 }
Cary Clark579985c2017-07-31 11:48:27 -04001561 this->methodOut(method, child);
Cary Clark137b8742018-05-30 09:21:49 -04001562 sawConst = false;
Cary Clark73fa9722017-08-29 17:36:51 -04001563 if (fAttrDeprecated) {
Cary Clark154beea2017-10-26 07:58:48 -04001564 startDef = fAttrDeprecated;
Cary Clark0d225392018-06-07 09:59:07 -04001565 this->setStartBack(fAttrDeprecated->fContentStart, fAttrDeprecated);
Cary Clark73fa9722017-08-29 17:36:51 -04001566 fAttrDeprecated = nullptr;
1567 }
Cary Clark8032b982017-07-28 11:04:54 -04001568 continue;
Ben Wagner63fd7602017-10-09 15:45:33 -04001569 }
Cary Clark8032b982017-07-28 11:04:54 -04001570 if (Definition::Type::kKeyWord == child.fType) {
Cary Clark8032b982017-07-28 11:04:54 -04001571 switch (child.fKeyWord) {
1572 case KeyWord::kStruct:
Cary Clark73fa9722017-08-29 17:36:51 -04001573 case KeyWord::kClass:
Cary Clark0d225392018-06-07 09:59:07 -04001574 fICSStack.push_back(&child);
1575 fStructEnded = false;
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001576 fStructMemberTab = 0;
Cary Clark8032b982017-07-28 11:04:54 -04001577 // if struct contains members, compute their name and comment tabs
Cary Clark73fa9722017-08-29 17:36:51 -04001578 if (child.fChildren.size() > 0) {
1579 const ParentPair* testPair = &pair;
1580 while ((testPair = testPair->fPrev)) {
1581 if (KeyWord::kClass == testPair->fParent->fKeyWord) {
1582 inStruct = fInStruct = true;
1583 break;
1584 }
1585 }
1586 }
Cary Clark8032b982017-07-28 11:04:54 -04001587 if (fInStruct) {
Cary Clark154beea2017-10-26 07:58:48 -04001588 // try child; root+child; root->parent+child; etc.
1589 int trial = 0;
Cary Clark682c58d2018-05-16 07:07:07 -04001590 RootDefinition* search = root;
1591 Definition* parent = search->fParent;
Cary Clark154beea2017-10-26 07:58:48 -04001592 do {
1593 string name;
1594 if (0 == trial) {
1595 name = child.fName;
1596 } else if (1 == trial) {
1597 name = root->fName + "::" + child.fName;
Cary Clark137b8742018-05-30 09:21:49 -04001598 } else if (2 == trial) {
1599 name = root->fName;
Cary Clark154beea2017-10-26 07:58:48 -04001600 } else {
1601 SkASSERT(parent);
1602 name = parent->fName + "::" + child.fName;
1603 search = parent->asRoot();
1604 parent = search->fParent;
1605 }
1606 fBmhStructDef = search->find(name, RootDefinition::AllowParens::kNo);
1607 } while (!fBmhStructDef && ++trial);
Cary Clark682c58d2018-05-16 07:07:07 -04001608 root = fBmhStructDef->asRoot();
Cary Clark154beea2017-10-26 07:58:48 -04001609 SkASSERT(root);
Cary Clark8032b982017-07-28 11:04:54 -04001610 fIndent += 4;
Cary Clark8032b982017-07-28 11:04:54 -04001611 this->structSizeMembers(child);
1612 fIndent -= 4;
Cary Clark154beea2017-10-26 07:58:48 -04001613 SkASSERT(!fIndentNext);
1614 fIndentNext = true;
Cary Clark8032b982017-07-28 11:04:54 -04001615 }
Cary Clark8032b982017-07-28 11:04:54 -04001616 if (child.fChildren.size() > 0) {
1617 const char* bodyEnd = fDeferComment ? fDeferComment->fContentStart - 1 :
1618 child.fContentStart;
Cary Clark73fa9722017-08-29 17:36:51 -04001619 this->writeBlockTrim((int) (bodyEnd - fStart), fStart);
Cary Clark154beea2017-10-26 07:58:48 -04001620 if (fPendingMethod) {
Cary Clark27dddae2018-06-08 15:57:37 -04001621 if (fIndent >= 4) {
1622 this->indentOut();
1623 }
Cary Clark154beea2017-10-26 07:58:48 -04001624 fPendingMethod = false;
1625 }
1626 startDef = requireDense ? requireDense : &child;
Cary Clark0d225392018-06-07 09:59:07 -04001627 if (requireDense) {
1628 startDef = requireDense;
1629 this->setStart(requireDense->fContentStart, requireDense);
1630 } else {
1631 startDef = &child;
1632 this->setStart(child.fContentStart, &child);
1633 }
Cary Clark154beea2017-10-26 07:58:48 -04001634 requireDense = nullptr;
Cary Clark0d225392018-06-07 09:59:07 -04001635 if (!fInStruct && (!root || child.fName != root->fName)) {
Cary Clark154beea2017-10-26 07:58:48 -04001636 root = &fBmhParser->fClassMap[child.fName];
1637 fRootTopic = root->fParent;
1638 SkASSERT(!root->fVisited);
1639 root->clearVisited();
Cary Clark0d225392018-06-07 09:59:07 -04001640#if 0
1641 // this seems better balanced; but real problem is probably fInStruct
1642 if (fIndentStack.size() > 0) {
1643 this->indentOut();
1644 }
1645 SkASSERT(!fIndent);
1646#else
Cary Clark154beea2017-10-26 07:58:48 -04001647 fIndent = 0;
Cary Clark0d225392018-06-07 09:59:07 -04001648#endif
Cary Clark154beea2017-10-26 07:58:48 -04001649 fBmhStructDef = root;
1650 }
Cary Clark8032b982017-07-28 11:04:54 -04001651 if (child.fName == root->fName) {
1652 if (Definition* parent = root->fParent) {
1653 if (MarkType::kTopic == parent->fMarkType ||
1654 MarkType::kSubtopic == parent->fMarkType) {
Cary Clarke4aa3712017-09-15 02:56:12 -04001655 const char* commentStart = root->fContentStart;
1656 const char* commentEnd = root->fChildren[0]->fStart;
Cary Clark8032b982017-07-28 11:04:54 -04001657 this->structOut(root, *root, commentStart, commentEnd);
1658 } else {
1659 SkASSERT(0); // incomplete
1660 }
1661 } else {
1662 SkASSERT(0); // incomplete
1663 }
1664 } else {
Cary Clark154beea2017-10-26 07:58:48 -04001665 SkASSERT(fInStruct);
Cary Clark137b8742018-05-30 09:21:49 -04001666 Definition* priorBlock = fBmhStructDef;
Cary Clark8032b982017-07-28 11:04:54 -04001667 Definition* codeBlock = nullptr;
Cary Clark154beea2017-10-26 07:58:48 -04001668 Definition* nextBlock = nullptr;
1669 for (auto test : fBmhStructDef->fChildren) {
Cary Clark8032b982017-07-28 11:04:54 -04001670 if (MarkType::kCode == test->fMarkType) {
Cary Clark137b8742018-05-30 09:21:49 -04001671 SkASSERT(!codeBlock); // FIXME: check enum earlier
Cary Clark8032b982017-07-28 11:04:54 -04001672 codeBlock = test;
1673 continue;
1674 }
1675 if (codeBlock) {
Cary Clark154beea2017-10-26 07:58:48 -04001676 nextBlock = test;
Cary Clark8032b982017-07-28 11:04:54 -04001677 break;
1678 }
Cary Clark137b8742018-05-30 09:21:49 -04001679 priorBlock = test;
Cary Clark8032b982017-07-28 11:04:54 -04001680 }
Cary Clark137b8742018-05-30 09:21:49 -04001681 // FIXME: trigger error earlier if inner #Struct or #Class is missing #Code
Cary Clark56356312018-02-08 14:45:18 -05001682 if (!fBmhStructDef->fDeprecated) {
1683 SkASSERT(codeBlock);
1684 SkASSERT(nextBlock); // FIXME: check enum for correct order earlier
Cary Clark78c110e2018-02-09 16:49:09 -05001685 const char* commentStart = codeBlock->fTerminator;
1686 const char* commentEnd = nextBlock->fStart;
Cary Clark137b8742018-05-30 09:21:49 -04001687 // FIXME: trigger error if #Code is present but comment is before it earlier
1688 SkASSERT(priorBlock); // code always preceded by #Line (I think)
1689 TextParser priorComment(priorBlock->fFileName,
1690 priorBlock->fTerminator, codeBlock->fStart,
1691 priorBlock->fLineCount);
1692 priorComment.trimEnd();
1693 if (!priorComment.eof()) {
1694 return priorBlock->reportError<bool>(
1695 "expect no comment before #Code");
1696 }
1697 TextParser nextComment(codeBlock->fFileName, commentStart,
1698 commentEnd, codeBlock->fLineCount);
1699 nextComment.trimEnd();
1700 if (!priorComment.eof()) {
1701 return priorBlock->reportError<bool>(
1702 "expect comment after #Code");
1703 }
1704 if (!nextComment.eof()) {
1705
1706 }
Cary Clark78c110e2018-02-09 16:49:09 -05001707 fIndentNext = true;
1708 this->structOut(root, *fBmhStructDef, commentStart, commentEnd);
Cary Clark56356312018-02-08 14:45:18 -05001709 }
Cary Clark8032b982017-07-28 11:04:54 -04001710 }
1711 fDeferComment = nullptr;
1712 } else {
Cary Clark0d225392018-06-07 09:59:07 -04001713 // empty forward reference
1714 bool writeTwo = '\n' == child.fContentStart[-1]
1715 && '\n' == child.fContentStart[-2];
1716 if (writeTwo) {
1717 const char* bodyEnd = fDeferComment ? fDeferComment->fContentStart - 1 :
1718 child.fContentStart;
1719 this->writeBlockTrim((int) (bodyEnd - fStart), fStart);
1720 this->lf(writeTwo ? 2 : 1);
1721 fIndent = 0;
1722 this->writeBlockTrim(child.length() + 1, child.fContentStart);
1723 writeTwo = '\n' == child.fContentEnd[1]
1724 && '\n' == child.fContentStart[2];
1725 this->lf(writeTwo ? 2 : 1);
1726 fStart = child.fContentEnd + 1;
1727 fDeferComment = nullptr;
1728 }
Cary Clark8032b982017-07-28 11:04:54 -04001729 }
1730 break;
1731 case KeyWord::kEnum: {
Cary Clarkbad5ad72017-08-03 17:14:08 -04001732 fInEnum = true;
Cary Clark8032b982017-07-28 11:04:54 -04001733 this->enumHeaderOut(root, child);
1734 this->enumSizeItems(child);
1735 } break;
1736 case KeyWord::kConst:
1737 case KeyWord::kConstExpr:
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001738 sawConst = !memberStart || staticOnly;
1739 if (!memberStart) {
1740 memberStart = &child;
1741 staticOnly = true;
1742 }
Cary Clark0d225392018-06-07 09:59:07 -04001743 if (MarkType::kConst == child.fMarkType) {
1744 auto constIter = fBmhParser->fConstMap.find(child.fName);
1745 if (fBmhParser->fConstMap.end() != constIter) {
1746 const RootDefinition& bmhConst = constIter->second;
1747 this->constOut(&child, &bmhConst);
1748 fDeferComment = nullptr;
1749 }
1750 }
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001751 break;
Cary Clark8032b982017-07-28 11:04:54 -04001752 case KeyWord::kStatic:
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001753 if (!memberStart) {
1754 memberStart = &child;
1755 staticOnly = true;
1756 }
1757 break;
Cary Clark8032b982017-07-28 11:04:54 -04001758 case KeyWord::kInt:
Cary Clarkd0530ba2017-09-14 11:25:39 -04001759 case KeyWord::kUint8_t:
1760 case KeyWord::kUint16_t:
Cary Clark8032b982017-07-28 11:04:54 -04001761 case KeyWord::kUint32_t:
Cary Clarkd0530ba2017-09-14 11:25:39 -04001762 case KeyWord::kUint64_t:
Cary Clark4dc5a452018-05-21 11:56:57 -04001763 case KeyWord::kUintPtr_t:
Cary Clark73fa9722017-08-29 17:36:51 -04001764 case KeyWord::kUnsigned:
Cary Clark8032b982017-07-28 11:04:54 -04001765 case KeyWord::kSize_t:
1766 case KeyWord::kFloat:
1767 case KeyWord::kBool:
Cary Clark56356312018-02-08 14:45:18 -05001768 case KeyWord::kChar:
Cary Clark8032b982017-07-28 11:04:54 -04001769 case KeyWord::kVoid:
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001770 staticOnly = false;
Cary Clark8032b982017-07-28 11:04:54 -04001771 if (!memberStart) {
1772 memberStart = &child;
1773 }
1774 break;
1775 case KeyWord::kPublic:
1776 case KeyWord::kPrivate:
1777 case KeyWord::kProtected:
1778 case KeyWord::kFriend:
Cary Clark73fa9722017-08-29 17:36:51 -04001779 case KeyWord::kInline:
1780 case KeyWord::kSK_API:
Cary Clarkbbfda252018-03-09 15:32:01 -05001781 case KeyWord::kTemplate:
Cary Clark0d225392018-06-07 09:59:07 -04001782 break;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001783 case KeyWord::kTypedef:
Cary Clark0d225392018-06-07 09:59:07 -04001784 SkASSERT(!memberStart);
1785 memberStart = &child;
Cary Clark27dddae2018-06-08 15:57:37 -04001786 deferredTypedefComment = fDeferComment;
Cary Clark0d225392018-06-07 09:59:07 -04001787 sawTypedef = true;
Cary Clark8032b982017-07-28 11:04:54 -04001788 break;
Cary Clark154beea2017-10-26 07:58:48 -04001789 case KeyWord::kSK_BEGIN_REQUIRE_DENSE:
1790 requireDense = &child;
1791 break;
Cary Clark8032b982017-07-28 11:04:54 -04001792 default:
1793 SkASSERT(0);
1794 }
Cary Clarkd98f78c2018-04-26 08:32:37 -04001795 if (KeyWord::kUint8_t == child.fKeyWord || KeyWord::kUint32_t == child.fKeyWord) {
Cary Clarkd0530ba2017-09-14 11:25:39 -04001796 continue;
Cary Clark8032b982017-07-28 11:04:54 -04001797 } else {
Cary Clarkbad5ad72017-08-03 17:14:08 -04001798 if (fInEnum && KeyWord::kClass == child.fChildren[0]->fKeyWord) {
Cary Clark73fa9722017-08-29 17:36:51 -04001799 if (!this->populate(child.fChildren[0], &pair, root)) {
Cary Clarkbad5ad72017-08-03 17:14:08 -04001800 return false;
1801 }
Cary Clark154beea2017-10-26 07:58:48 -04001802 } else {
1803 if (!this->populate(&child, &pair, root)) {
1804 return false;
1805 }
1806 if (KeyWord::kClass == child.fKeyWord || KeyWord::kStruct == child.fKeyWord) {
Cary Clark0d225392018-06-07 09:59:07 -04001807 fICSStack.pop_back();
1808 fStructEnded = true;
Cary Clark154beea2017-10-26 07:58:48 -04001809 if (fInStruct) {
1810 fInStruct = false;
1811 do {
1812 SkASSERT(root);
1813 root = const_cast<RootDefinition*>(root->fParent->asRoot());
1814 } while (MarkType::kTopic == root->fMarkType ||
1815 MarkType::kSubtopic == root->fMarkType);
Cary Clark0d225392018-06-07 09:59:07 -04001816#if 0
1817 }
1818 if (MarkType::kStruct == root->fMarkType ||
1819 MarkType::kClass == root->fMarkType) {
1820#else
Cary Clark154beea2017-10-26 07:58:48 -04001821 SkASSERT(MarkType::kStruct == root->fMarkType ||
Cary Clark0d225392018-06-07 09:59:07 -04001822 MarkType::kClass == root->fMarkType);
1823#endif
Cary Clark154beea2017-10-26 07:58:48 -04001824 fPendingMethod = false;
1825 if (startDef) {
1826 fPendingMethod = find_start(startDef, fStart);
1827 }
1828 fOutdentNext = !fPendingMethod;
1829 }
1830 }
Cary Clark8032b982017-07-28 11:04:54 -04001831 }
1832 }
1833 continue;
Ben Wagner63fd7602017-10-09 15:45:33 -04001834 }
Cary Clark8032b982017-07-28 11:04:54 -04001835 if (Definition::Type::kBracket == child.fType) {
Ben Wagner63fd7602017-10-09 15:45:33 -04001836 if (KeyWord::kEnum == child.fParent->fKeyWord ||
Cary Clarkbad5ad72017-08-03 17:14:08 -04001837 (KeyWord::kClass == child.fParent->fKeyWord && child.fParent->fParent &&
1838 KeyWord::kEnum == child.fParent->fParent->fKeyWord)) {
Cary Clarkd0530ba2017-09-14 11:25:39 -04001839 SkASSERT(Bracket::kBrace == child.fBracket);
Cary Clark682c58d2018-05-16 07:07:07 -04001840 this->enumMembersOut(*child.fParent);
Cary Clark8032b982017-07-28 11:04:54 -04001841 this->writeString("};");
1842 this->lf(2);
Cary Clark154beea2017-10-26 07:58:48 -04001843 startDef = child.fParent;
Cary Clark0d225392018-06-07 09:59:07 -04001844 this->setStart(child.fParent->fContentEnd, child.fParent);
Cary Clark8032b982017-07-28 11:04:54 -04001845 SkASSERT(';' == fStart[0]);
1846 ++fStart;
1847 fDeferComment = nullptr;
1848 fInEnum = false;
Cary Clark154beea2017-10-26 07:58:48 -04001849 if (fIndentNext) {
1850// fIndent -= 4;
1851 fIndentNext = false;
1852 }
Cary Clark8032b982017-07-28 11:04:54 -04001853 continue;
Cary Clark73fa9722017-08-29 17:36:51 -04001854 }
1855 if (fAttrDeprecated) {
1856 continue;
1857 }
Cary Clarkb94f6da2018-06-08 11:54:32 -04001858 if (KeyWord::kDefine == child.fKeyWord && this->defineOut(child)) {
1859 fDeferComment = nullptr;
1860 continue;
1861 }
Cary Clark8032b982017-07-28 11:04:54 -04001862 fDeferComment = nullptr;
Cary Clark154beea2017-10-26 07:58:48 -04001863 if (KeyWord::kClass == def->fKeyWord || KeyWord::kStruct == def->fKeyWord) {
1864 fIndentNext = true;
1865 }
Cary Clark73fa9722017-08-29 17:36:51 -04001866 if (!this->populate(&child, &pair, root)) {
Cary Clark8032b982017-07-28 11:04:54 -04001867 return false;
1868 }
Cary Clark0d225392018-06-07 09:59:07 -04001869 if (KeyWord::kClass == def->fKeyWord || KeyWord::kStruct == def->fKeyWord) {
1870 if (def->iRootParent() && (!fStartSetter
1871 || MarkType::kMethod != fStartSetter->fMarkType)) {
1872 this->setStart(child.fContentEnd, &child);
1873 fDeferComment = nullptr;
1874 }
1875 }
Cary Clark8032b982017-07-28 11:04:54 -04001876 continue;
1877 }
1878 if (Definition::Type::kWord == child.fType) {
1879 if (MarkType::kMember == child.fMarkType) {
Cary Clark884dd7d2017-10-11 10:37:52 -04001880 if (!memberStart) {
1881 auto iter = def->fTokens.begin();
1882 std::advance(iter, child.fParentIndex - 1);
1883 memberStart = &*iter;
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001884 staticOnly = false;
Cary Clark0d225392018-06-07 09:59:07 -04001885 }
1886 if (!fStructMemberTab) {
1887 SkASSERT(KeyWord::kStruct == def->fParent->fKeyWord);
1888 fIndent += 4;
1889 this->structSizeMembers(*def->fParent);
1890 fIndent -= 4;
1891 fIndentNext = true;
Cary Clark884dd7d2017-10-11 10:37:52 -04001892 }
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001893 SkASSERT(fBmhStructDef);
1894 if (!fBmhStructDef->fDeprecated) {
1895 memberEnd = this->structMemberOut(memberStart, child);
1896 startDef = &child;
Cary Clark0d225392018-06-07 09:59:07 -04001897 this->setStart(child.fContentEnd + 1, &child);
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001898 fDeferComment = nullptr;
1899 }
Cary Clark56356312018-02-08 14:45:18 -05001900 } else if (MarkType::kNone == child.fMarkType && sawConst
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001901 && fEnumDef && !fEnumDef->fDeprecated) {
1902 const Definition* bmhConst = nullptr;
1903 string match;
1904 if (root) {
1905 match = root->fName + "::";
1906 }
1907 match += string(child.fContentStart, child.fContentEnd - child.fContentStart);
1908 for (auto enumChild : fEnumDef->fChildren) {
1909 if (MarkType::kConst == enumChild->fMarkType && enumChild->fName == match) {
1910 bmhConst = enumChild;
1911 break;
1912 }
1913 }
1914 if (bmhConst) {
Cary Clark137b8742018-05-30 09:21:49 -04001915 this->constOut(memberStart, bmhConst);
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001916 fDeferComment = nullptr;
1917 sawConst = false;
1918 }
Cary Clark137b8742018-05-30 09:21:49 -04001919 } else if (MarkType::kNone == child.fMarkType && sawConst && !fEnumDef) {
1920 string match;
1921 if (root) {
1922 match = root->fName + "::";
1923 match += string(child.fContentStart, child.fContentEnd - child.fContentStart);
1924 auto bmhClassIter = fBmhParser->fClassMap.find(root->fName);
1925 if (fBmhParser->fClassMap.end() != bmhClassIter) {
1926 RootDefinition& bmhClass = bmhClassIter->second;
1927 auto constIter = std::find_if(bmhClass.fLeaves.begin(), bmhClass.fLeaves.end(),
1928 [match](std::pair<const string, Definition>& leaf){ return match == leaf.second.fName; } );
1929 if (bmhClass.fLeaves.end() != constIter) {
1930 const Definition& bmhConst = constIter->second;
1931 if (MarkType::kConst == bmhConst.fMarkType
1932 && MarkType::kSubtopic == bmhConst.fParent->fMarkType) {
1933 fBmhConst = &bmhConst;
1934 fConstDef = &child;
1935 }
1936 }
1937 }
Cary Clark137b8742018-05-30 09:21:49 -04001938 }
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001939 }
Cary Clark8032b982017-07-28 11:04:54 -04001940 if (child.fMemberStart) {
1941 memberStart = &child;
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001942 staticOnly = false;
Cary Clark8032b982017-07-28 11:04:54 -04001943 }
Cary Clark89b14562018-03-19 09:04:10 -04001944 if (kAttrDeprecatedLen == (size_t) (child.fContentEnd - child.fContentStart) &&
1945 !strncmp(gAttrDeprecated, child.fStart, kAttrDeprecatedLen)) {
Cary Clark73fa9722017-08-29 17:36:51 -04001946 fAttrDeprecated = &child;
1947 }
Cary Clark8032b982017-07-28 11:04:54 -04001948 continue;
1949 }
1950 if (Definition::Type::kPunctuation == child.fType) {
1951 if (Punctuation::kSemicolon == child.fPunctuation) {
Cary Clark137b8742018-05-30 09:21:49 -04001952 if (sawConst && fBmhConst) { // find bmh documentation. Parent must be subtopic.
1953 const Definition* subtopic = fBmhConst->fParent;
1954 SkASSERT(subtopic);
1955 SkASSERT(MarkType::kSubtopic == subtopic->fMarkType);
1956 auto firstConst = std::find_if(subtopic->fChildren.begin(),
1957 subtopic->fChildren.end(),
1958 [](const Definition* def){ return MarkType::kConst == def->fMarkType;});
1959 SkASSERT(firstConst != subtopic->fChildren.end());
1960 bool constIsFirst = *firstConst == fBmhConst;
1961 if (constIsFirst) { // If first #Const child, output subtopic description.
1962 this->constOut(memberStart, subtopic);
1963 // find member / value / comment tabs
1964 // look for a one-to-one correspondence between bmh and include
1965 this->constSizeMembers(root);
Cary Clark0d225392018-06-07 09:59:07 -04001966 fDeferComment = nullptr;
Cary Clark137b8742018-05-30 09:21:49 -04001967 }
Cary Clark137b8742018-05-30 09:21:49 -04001968 // after const code, output #Line description as short comment
1969 auto lineIter = std::find_if(fBmhConst->fChildren.begin(),
1970 fBmhConst->fChildren.end(),
1971 [](const Definition* def){ return MarkType::kLine == def->fMarkType; });
1972 SkASSERT(fBmhConst->fChildren.end() != lineIter);
1973 const Definition* lineDef = *lineIter;
1974 if (fConstLength > 100) {
1975 this->writeCommentHeader();
1976 this->writeSpace();
1977 this->rewriteBlock(lineDef->length(), lineDef->fContentStart, Phrase::kYes);
1978 this->writeCommentTrailer(OneLine::kYes);
1979 }
1980 this->lfcr();
1981 TextParser constText(memberStart);
1982 const char* nameEnd = constText.trimmedBracketEnd('=');
1983 SkAssertResult(constText.skipToEndBracket('='));
1984 const char* valueEnd = constText.trimmedBracketEnd(';');
1985 this->writeBlock((int) (nameEnd - memberStart->fContentStart),
1986 memberStart->fContentStart);
1987 this->indentToColumn(fConstValueTab);
1988 this->writeBlock((int) (valueEnd - constText.fChar), constText.fChar);
1989 this->writeString(";");
1990 if (fConstLength <= 100) {
1991 this->indentToColumn(fConstCommentTab);
1992 this->writeString("//!<");
1993 this->writeSpace();
1994 this->rewriteBlock(lineDef->length(), lineDef->fContentStart, Phrase::kYes);
1995 }
Cary Clark0d225392018-06-07 09:59:07 -04001996 this->setStart(child.fContentStart + 1, &child);
Cary Clark137b8742018-05-30 09:21:49 -04001997 fDeferComment = nullptr;
1998 fBmhConst = nullptr;
1999 sawConst = false;
Cary Clark0d225392018-06-07 09:59:07 -04002000 } else if (sawTypedef) {
2001 const Definition* bmhTypedef = nullptr;
2002 if (root) {
2003 SkDEBUGCODE(auto classIter = fBmhParser->fClassMap.find(root->fName));
2004 SkASSERT(fBmhParser->fClassMap.end() != classIter);
2005 RootDefinition& classDef = fBmhParser->fClassMap[root->fName];
2006 auto leafIter = classDef.fLeaves.find(memberStart->fName);
2007 if (classDef.fLeaves.end() != leafIter) {
2008 bmhTypedef = &leafIter->second;
2009 }
2010 }
2011 if (!bmhTypedef) {
2012 auto typedefIter = fBmhParser->fTypedefMap.find(memberStart->fName);
2013 SkASSERT(fBmhParser->fTypedefMap.end() != typedefIter);
2014 bmhTypedef = &typedefIter->second;
2015 }
Cary Clark27dddae2018-06-08 15:57:37 -04002016 fDeferComment = deferredTypedefComment;
Cary Clark0d225392018-06-07 09:59:07 -04002017 this->constOut(memberStart, bmhTypedef);
2018 fDeferComment = nullptr;
2019 sawTypedef = false;
Cary Clark137b8742018-05-30 09:21:49 -04002020 }
Cary Clark8032b982017-07-28 11:04:54 -04002021 memberStart = nullptr;
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002022 staticOnly = false;
Cary Clark8032b982017-07-28 11:04:54 -04002023 if (inStruct) {
2024 fInStruct = false;
2025 }
2026 continue;
2027 }
2028 if (Punctuation::kLeftBrace == child.fPunctuation ||
2029 Punctuation::kColon == child.fPunctuation ||
2030 Punctuation::kAsterisk == child.fPunctuation
2031 ) {
2032 continue;
2033 }
2034 }
2035 }
2036 return true;
2037}
2038
2039bool IncludeWriter::populate(BmhParser& bmhParser) {
2040 bool allPassed = true;
2041 for (auto& includeMapper : fIncludeMap) {
2042 size_t lastSlash = includeMapper.first.rfind('/');
Cary Clarkd0530ba2017-09-14 11:25:39 -04002043 if (string::npos == lastSlash) {
2044 lastSlash = includeMapper.first.rfind('\\');
2045 }
Cary Clark8032b982017-07-28 11:04:54 -04002046 if (string::npos == lastSlash || lastSlash >= includeMapper.first.length() - 1) {
2047 return this->reportError<bool>("malformed include name");
2048 }
2049 string fileName = includeMapper.first.substr(lastSlash + 1);
2050 if (".h" != fileName.substr(fileName.length() - 2)) {
2051 return this->reportError<bool>("expected fileName.h");
2052 }
2053 string skClassName = fileName.substr(0, fileName.length() - 2);
2054 fOut = fopen(fileName.c_str(), "wb");
2055 if (!fOut) {
2056 SkDebugf("could not open output file %s\n", fileName.c_str());
2057 return false;
2058 }
Cary Clark0d225392018-06-07 09:59:07 -04002059 RootDefinition* root =
2060 bmhParser.fClassMap.end() == bmhParser.fClassMap.find(skClassName) ?
2061 nullptr : &bmhParser.fClassMap[skClassName];
Cary Clark8032b982017-07-28 11:04:54 -04002062 fBmhParser = &bmhParser;
Cary Clark0d225392018-06-07 09:59:07 -04002063 if (root) {
2064 fRootTopic = root->fParent;
2065 root->clearVisited();
2066 } else {
2067 SkASSERT("Sk" == skClassName.substr(0, 2));
2068 string topicName = skClassName.substr(2);
2069 auto topicIter = bmhParser.fTopicMap.find(topicName);
2070 SkASSERT(bmhParser.fTopicMap.end() != topicIter);
2071 fRootTopic = topicIter->second->asRoot();
2072 fFirstWrite = true; // write file information after includes
2073 }
Cary Clark2d4bf5f2018-04-16 08:37:38 -04002074 fFileName = includeMapper.second.fFileName;
Cary Clark0d225392018-06-07 09:59:07 -04002075 this->setStartBack(includeMapper.second.fContentStart, &includeMapper.second);
Cary Clark8032b982017-07-28 11:04:54 -04002076 fEnd = includeMapper.second.fContentEnd;
Cary Clark2f466242017-12-11 16:03:17 -05002077 fAnonymousEnumCount = 1;
Cary Clarkb94f6da2018-06-08 11:54:32 -04002078 this->writeHeader(includeMapper);
Cary Clark73fa9722017-08-29 17:36:51 -04002079 allPassed &= this->populate(&includeMapper.second, nullptr, root);
Cary Clark8032b982017-07-28 11:04:54 -04002080 this->writeBlock((int) (fEnd - fStart), fStart);
Cary Clark0d225392018-06-07 09:59:07 -04002081#if 0
2082 if (fIndentStack.size() > 0) {
2083 this->indentOut();
2084 }
2085 SkASSERT(!fIndent);
2086#else
Cary Clark8032b982017-07-28 11:04:54 -04002087 fIndent = 0;
Cary Clark0d225392018-06-07 09:59:07 -04002088#endif
Cary Clark8032b982017-07-28 11:04:54 -04002089 this->lfcr();
2090 this->writePending();
2091 fclose(fOut);
Cary Clark7cfcbca2018-01-04 16:11:51 -05002092 fflush(fOut);
2093 size_t slash = fFileName.find_last_of('/');
2094 if (string::npos == slash) {
2095 slash = 0;
2096 }
2097 size_t back = fFileName.find_last_of('\\');
2098 if (string::npos == back) {
2099 back = 0;
2100 }
2101 string dir = fFileName.substr(0, SkTMax(slash, back) + 1);
2102 string readname = dir + fileName;
2103 if (this->writtenFileDiffers(fileName, readname)) {
2104 SkDebugf("wrote updated %s\n", fileName.c_str());
2105 } else {
2106 remove(fileName.c_str());
2107 }
Cary Clark8032b982017-07-28 11:04:54 -04002108 }
2109 return allPassed;
2110}
2111
2112// change Xxx_Xxx to xxx xxx
2113static string ConvertRef(const string str, bool first) {
2114 string substitute;
2115 for (char c : str) {
2116 if ('_' == c) {
2117 c = ' '; // change Xxx_Xxx to xxx xxx
2118 } else if (isupper(c) && !first) {
2119 c = tolower(c);
2120 }
2121 substitute += c;
2122 first = false;
2123 }
2124 return substitute;
2125}
2126
Cary Clark8032b982017-07-28 11:04:54 -04002127string IncludeWriter::resolveMethod(const char* start, const char* end, bool first) {
2128 string methodname(start, end - start);
Cary Clark579985c2017-07-31 11:48:27 -04002129 if (string::npos != methodname.find("()")) {
2130 return "";
2131 }
Cary Clark8032b982017-07-28 11:04:54 -04002132 string substitute;
2133 auto rootDefIter = fBmhParser->fMethodMap.find(methodname);
2134 if (fBmhParser->fMethodMap.end() != rootDefIter) {
2135 substitute = methodname + "()";
2136 } else {
Cary Clark1eace2d2017-07-31 07:52:43 -04002137 RootDefinition* parent = nullptr;
2138 for (auto candidate : fRootTopic->fChildren) {
2139 if (MarkType::kClass == candidate->fMarkType
2140 || MarkType::kStruct == candidate->fMarkType) {
2141 parent = candidate->asRoot();
2142 break;
2143 }
2144 }
Cary Clark0d225392018-06-07 09:59:07 -04002145 if (parent) {
2146 auto defRef = parent->find(parent->fName + "::" + methodname,
2147 RootDefinition::AllowParens::kNo);
2148 if (defRef && MarkType::kMethod == defRef->fMarkType) {
2149 substitute = methodname + "()";
2150 } else {
2151 auto defineIter = fBmhParser->fDefineMap.find(methodname);
2152 if (fBmhParser->fDefineMap.end() != defineIter) {
2153 const RootDefinition& defineDef = defineIter->second;
2154 auto codeIter = std::find_if(defineDef.fChildren.begin(),
2155 defineDef.fChildren.end(),
2156 [](Definition* child){ return MarkType::kCode == child->fMarkType; } );
2157 if (defineDef.fChildren.end() != codeIter) {
2158 const Definition* codeDef = *codeIter;
2159 string codeContents(codeDef->fContentStart, codeDef->length());
2160 size_t namePos = codeContents.find(methodname);
2161 if (string::npos != namePos) {
2162 size_t parenPos = namePos + methodname.length();
2163 if (parenPos < codeContents.length() && '(' == codeContents[parenPos]) {
2164 substitute = methodname + "()";
2165 }
Cary Clark137b8742018-05-30 09:21:49 -04002166 }
2167 }
2168 }
2169 }
Cary Clark8032b982017-07-28 11:04:54 -04002170 }
2171 }
Cary Clark579985c2017-07-31 11:48:27 -04002172 if (fMethodDef && methodname == fMethodDef->fName) {
2173 TextParser report(fBmhMethod);
2174 report.reportError("method should not include references to itself");
2175 return "";
2176 }
Cary Clark6fc50412017-09-21 12:31:06 -04002177 if (fBmhMethod) {
2178 for (auto child : fBmhMethod->fChildren) {
2179 if (MarkType::kParam != child->fMarkType) {
2180 continue;
2181 }
2182 if (methodname == child->fName) {
2183 return "";
2184 }
2185 }
2186 }
Cary Clark8032b982017-07-28 11:04:54 -04002187 return substitute;
2188}
2189
Cary Clark137b8742018-05-30 09:21:49 -04002190string IncludeWriter::resolveAlias(const Definition* def) {
2191 for (auto child : def->fChildren) {
2192 if (MarkType::kSubstitute == child->fMarkType) {
2193 return string(child->fContentStart, (int) (child->fContentEnd - child->fContentStart));
2194 }
2195 if (MarkType::kAlias == child->fMarkType && def->fName == child->fName) {
2196 return this->resolveAlias(child);
2197 }
2198 }
2199 return "";
2200}
2201
Cary Clarkce101242017-09-01 15:51:02 -04002202string IncludeWriter::resolveRef(const char* start, const char* end, bool first,
2203 RefType* refType) {
Ben Wagner63fd7602017-10-09 15:45:33 -04002204 // look up Xxx_Xxx
Cary Clark8032b982017-07-28 11:04:54 -04002205 string undername(start, end - start);
Cary Clarkce101242017-09-01 15:51:02 -04002206 for (const auto& external : fBmhParser->fExternals) {
2207 if (external.fName == undername) {
2208 *refType = RefType::kExternal;
2209 return external.fName;
2210 }
2211 }
2212 *refType = RefType::kNormal;
Cary Clark8032b982017-07-28 11:04:54 -04002213 SkASSERT(string::npos == undername.find(' '));
2214 const Definition* rootDef = nullptr;
Cary Clark78c110e2018-02-09 16:49:09 -05002215 string substitute;
Cary Clark8032b982017-07-28 11:04:54 -04002216 {
2217 auto rootDefIter = fBmhParser->fTopicMap.find(undername);
2218 if (fBmhParser->fTopicMap.end() != rootDefIter) {
2219 rootDef = rootDefIter->second;
2220 } else {
2221 string prefixedName = fRootTopic->fName + '_' + undername;
2222 rootDefIter = fBmhParser->fTopicMap.find(prefixedName);
2223 if (fBmhParser->fTopicMap.end() != rootDefIter) {
2224 rootDef = rootDefIter->second;
Cary Clark154beea2017-10-26 07:58:48 -04002225 } else if (fBmhStructDef) {
2226 string localPrefix = fBmhStructDef->fFiddle + '_' + undername;
Cary Clarkbad5ad72017-08-03 17:14:08 -04002227 rootDefIter = fBmhParser->fTopicMap.find(localPrefix);
2228 if (fBmhParser->fTopicMap.end() != rootDefIter) {
2229 rootDef = rootDefIter->second;
2230 }
Cary Clark78c110e2018-02-09 16:49:09 -05002231 if (!rootDef) {
2232 size_t doubleColon = fBmhStructDef->fName.rfind("::");
2233 if (string::npos != doubleColon && undername
2234 == fBmhStructDef->fName.substr(doubleColon + 2)) {
2235 substitute = fBmhStructDef->fName;
2236 }
2237 }
2238 }
Cary Clark137b8742018-05-30 09:21:49 -04002239 if (!rootDef && fEnumDef && "Sk" + prefixedName == fEnumDef->fFiddle) {
2240 rootDef = fEnumDef;
2241 }
Cary Clark78c110e2018-02-09 16:49:09 -05002242 if (!rootDef && !substitute.length()) {
Cary Clark8032b982017-07-28 11:04:54 -04002243 auto aliasIter = fBmhParser->fAliasMap.find(undername);
2244 if (fBmhParser->fAliasMap.end() != aliasIter) {
Cary Clark78c110e2018-02-09 16:49:09 -05002245 rootDef = aliasIter->second;
Cary Clark186d08f2018-04-03 08:43:27 -04002246 } else if (fInEnum && fEnumDef && this->findEnumSubtopic(undername, &rootDef)) {
2247 ;
Cary Clark8032b982017-07-28 11:04:54 -04002248 } else if (!first) {
Cary Clark186d08f2018-04-03 08:43:27 -04002249 this->fChar = start;
Cary Clarkffb3d682018-05-17 12:17:28 -04002250 this->fLine = start;
Cary Clark579985c2017-07-31 11:48:27 -04002251 this->reportError("reference unfound");
2252 return "";
Cary Clark8032b982017-07-28 11:04:54 -04002253 }
2254 }
2255 }
2256 }
Cary Clark8032b982017-07-28 11:04:54 -04002257 if (rootDef) {
Cary Clark78c110e2018-02-09 16:49:09 -05002258 MarkType rootType = rootDef->fMarkType;
Cary Clark137b8742018-05-30 09:21:49 -04002259 if (MarkType::kSubtopic == rootType || MarkType::kTopic == rootType
2260 || MarkType::kAlias == rootType) {
2261 substitute = this->resolveAlias(rootDef);
Cary Clark8032b982017-07-28 11:04:54 -04002262 }
2263 if (!substitute.length()) {
Cary Clark78c110e2018-02-09 16:49:09 -05002264 string match = rootDef->fName;
2265 size_t index;
2266 while (string::npos != (index = match.find('_'))) {
2267 match.erase(index, 1);
2268 }
2269 string skmatch = "Sk" + match;
Cary Clark137b8742018-05-30 09:21:49 -04002270 auto parent = MarkType::kAlias == rootType ? rootDef->fParent : rootDef;
Cary Clark78c110e2018-02-09 16:49:09 -05002271 for (auto child : parent->fChildren) {
2272 // there may be more than one
2273 // prefer the one mostly closely matching in text
2274 if ((MarkType::kClass == child->fMarkType ||
2275 MarkType::kStruct == child->fMarkType ||
2276 (MarkType::kEnum == child->fMarkType && !child->fAnonymous) ||
2277 MarkType::kEnumClass == child->fMarkType) && (match == child->fName ||
2278 skmatch == child->fName)) {
2279 substitute = child->fName;
2280 break;
2281 }
2282 }
2283 }
2284 if (!substitute.length()) {
2285 for (auto child : rootDef->fChildren) {
2286 // there may be more than one
2287 // if so, it's a bug since it's unknown which is the right one
2288 if (MarkType::kClass == child->fMarkType ||
2289 MarkType::kStruct == child->fMarkType ||
2290 (MarkType::kEnum == child->fMarkType && !child->fAnonymous) ||
2291 MarkType::kEnumClass == child->fMarkType) {
2292 SkASSERT("" == substitute);
2293 substitute = child->fName;
2294 if (MarkType::kEnum == child->fMarkType) {
2295 size_t parentClassEnd = substitute.find("::");
2296 SkASSERT(string::npos != parentClassEnd);
2297 string subEnd = substitute.substr(parentClassEnd + 2);
2298 if (fInEnum) {
2299 substitute = subEnd;
2300 }
2301 if (subEnd == undername) {
2302 break;
2303 }
2304 }
2305 }
2306 }
2307 }
2308 if (!substitute.length()) {
2309 const Definition* parent = rootDef;
2310 do {
2311 parent = parent->fParent;
2312 } while (parent && (MarkType::kSubtopic == parent->fMarkType
2313 || MarkType::kTopic == parent->fMarkType));
Cary Clark8032b982017-07-28 11:04:54 -04002314 if (parent) {
2315 if (MarkType::kClass == parent->fMarkType ||
2316 MarkType::kStruct == parent->fMarkType ||
Cary Clarkd0530ba2017-09-14 11:25:39 -04002317 (MarkType::kEnum == parent->fMarkType && !parent->fAnonymous) ||
Cary Clark8032b982017-07-28 11:04:54 -04002318 MarkType::kEnumClass == parent->fMarkType) {
2319 if (parent->fParent != fRootTopic) {
2320 substitute = parent->fName;
Cary Clark2a8c48b2018-02-15 17:31:24 -05002321 substitute += ' ';
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002322 substitute += ConvertRef(rootDef->fName, false);
Cary Clark8032b982017-07-28 11:04:54 -04002323 } else {
Cary Clark137b8742018-05-30 09:21:49 -04002324 size_t underpos = undername.find('_');
2325 if (string::npos != underpos) {
2326 string parentName = undername.substr(0, underpos);
2327 string skName = "Sk" + parentName;
2328 if (skName == parent->fName) {
2329 SkASSERT(start >= fLastDescription->fContentStart);
2330 string lastDescription = string(fLastDescription->fContentStart,
2331 (int) (start - fLastDescription->fContentStart));
2332 size_t lineStart = lastDescription.rfind('\n');
2333 SkASSERT(string::npos != lineStart);
2334 fLine = fLastDescription->fContentStart + lineStart + 1;
2335 fChar = start;
2336 fEnd = end;
2337 return this->reportError<string>("remove underline");
2338 }
2339 }
Cary Clark8032b982017-07-28 11:04:54 -04002340 substitute += ConvertRef(undername, first);
2341 }
2342 }
2343 }
2344 }
2345 }
Cary Clark1eace2d2017-07-31 07:52:43 -04002346 // Ensure first word after period is capitalized if substitute is lower cased.
2347 if (first && isupper(start[0]) && substitute.length() > 0 && islower(substitute[0])) {
2348 substitute[0] = start[0];
2349 }
Cary Clark8032b982017-07-28 11:04:54 -04002350 return substitute;
2351}
Cary Clark579985c2017-07-31 11:48:27 -04002352
Cary Clark8032b982017-07-28 11:04:54 -04002353int IncludeWriter::lookupMethod(const PunctuationState punctuation, const Word word,
Cary Clark6fc50412017-09-21 12:31:06 -04002354 const int lastSpace, const int run, int lastWrite, const char* data,
2355 bool hasIndirection) {
Cary Clark579985c2017-07-31 11:48:27 -04002356 int wordStart = lastSpace;
2357 while (' ' >= data[wordStart]) {
2358 ++wordStart;
2359 }
2360 const int wordEnd = PunctuationState::kDelimiter == punctuation ||
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002361 PunctuationState::kParen == punctuation ||
Cary Clark8032b982017-07-28 11:04:54 -04002362 PunctuationState::kPeriod == punctuation ? run - 1 : run;
Cary Clark6fc50412017-09-21 12:31:06 -04002363 string temp;
2364 if (hasIndirection && '(' != data[wordEnd - 1] && ')' != data[wordEnd - 1]) {
2365 // FIXME: hard-coded to assume a.b or a->b is a.b() or a->b().
2366 // need to check class a for member b to see if this is so
2367 TextParser parser(fFileName, &data[wordStart], &data[wordEnd], fLineCount);
2368 const char* indirection = parser.anyOf(".>");
2369 if (&data[wordEnd] <= &indirection[2] || 'f' != indirection[1] ||
2370 !isupper(indirection[2])) {
2371 temp = string(&data[wordStart], wordEnd - wordStart) + "()";
2372 }
2373 } else {
2374 temp = this->resolveMethod(&data[wordStart], &data[wordEnd], Word::kFirst == word);
2375 }
Cary Clark8032b982017-07-28 11:04:54 -04002376 if (temp.length()) {
Cary Clark579985c2017-07-31 11:48:27 -04002377 if (wordStart > lastWrite) {
2378 SkASSERT(data[wordStart - 1] >= ' ');
Cary Clark8032b982017-07-28 11:04:54 -04002379 if (' ' == data[lastWrite]) {
2380 this->writeSpace();
2381 }
Cary Clark0d225392018-06-07 09:59:07 -04002382 this->firstBlockTrim(wordStart - lastWrite, &data[lastWrite]);
Cary Clark579985c2017-07-31 11:48:27 -04002383 if (' ' == data[wordStart - 1]) {
Cary Clark8032b982017-07-28 11:04:54 -04002384 this->writeSpace();
2385 }
2386 }
2387 SkASSERT(temp[temp.length() - 1] > ' ');
2388 this->writeString(temp.c_str());
Cary Clark579985c2017-07-31 11:48:27 -04002389 lastWrite = wordEnd;
Cary Clark8032b982017-07-28 11:04:54 -04002390 }
2391 return lastWrite;
2392}
2393
2394int IncludeWriter::lookupReference(const PunctuationState punctuation, const Word word,
2395 const int start, const int run, int lastWrite, const char last, const char* data) {
2396 const int end = PunctuationState::kDelimiter == punctuation ||
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002397 PunctuationState::kParen == punctuation ||
Cary Clark8032b982017-07-28 11:04:54 -04002398 PunctuationState::kPeriod == punctuation ? run - 1 : run;
Cary Clarkce101242017-09-01 15:51:02 -04002399 RefType refType = RefType::kUndefined;
2400 string resolved = string(&data[start], (size_t) (end - start));
2401 string temp = this->resolveRef(&data[start], &data[end], Word::kFirst == word, &refType);
Cary Clark8032b982017-07-28 11:04:54 -04002402 if (!temp.length()) {
2403 if (Word::kFirst != word && '_' != last) {
Cary Clarkce101242017-09-01 15:51:02 -04002404 temp = ConvertRef(resolved, false);
Cary Clark8032b982017-07-28 11:04:54 -04002405 }
Ben Wagner63fd7602017-10-09 15:45:33 -04002406 }
Cary Clark8032b982017-07-28 11:04:54 -04002407 if (temp.length()) {
2408 if (start > lastWrite) {
2409 SkASSERT(data[start - 1] >= ' ');
2410 if (' ' == data[lastWrite]) {
2411 this->writeSpace();
2412 }
Cary Clark0d225392018-06-07 09:59:07 -04002413 this->firstBlockTrim(start - lastWrite, &data[lastWrite]);
Cary Clark8032b982017-07-28 11:04:54 -04002414 if (' ' == data[start - 1]) {
2415 this->writeSpace();
2416 }
2417 }
2418 SkASSERT(temp[temp.length() - 1] > ' ');
2419 this->writeString(temp.c_str());
2420 lastWrite = end;
2421 }
2422 return lastWrite;
2423}
2424
2425/* returns true if rewriteBlock wrote linefeeds */
Cary Clarkd0530ba2017-09-14 11:25:39 -04002426IncludeWriter::Wrote IncludeWriter::rewriteBlock(int size, const char* data, Phrase phrase) {
Cary Clark8032b982017-07-28 11:04:54 -04002427 bool wroteLineFeeds = false;
2428 while (size > 0 && data[0] <= ' ') {
2429 --size;
2430 ++data;
2431 }
2432 while (size > 0 && data[size - 1] <= ' ') {
2433 --size;
2434 }
2435 if (0 == size) {
2436 return Wrote::kNone;
2437 }
Cary Clark0d225392018-06-07 09:59:07 -04002438 if (fReturnOnWrite) {
2439 return Wrote::kChars;
2440 }
Cary Clark8032b982017-07-28 11:04:54 -04002441 int run = 0;
2442 Word word = Word::kStart;
Cary Clarkd0530ba2017-09-14 11:25:39 -04002443 PunctuationState punctuation = Phrase::kNo == phrase ?
2444 PunctuationState::kStart : PunctuationState::kSpace;
Cary Clark8032b982017-07-28 11:04:54 -04002445 int start = 0;
2446 int lastWrite = 0;
2447 int lineFeeds = 0;
2448 int lastPrintable = 0;
Cary Clark579985c2017-07-31 11:48:27 -04002449 int lastSpace = -1;
Cary Clark8032b982017-07-28 11:04:54 -04002450 char c = 0;
Kevin Lubick42846132018-01-05 10:11:11 -05002451 char last = 0;
Cary Clark6fc50412017-09-21 12:31:06 -04002452 bool embeddedIndirection = false;
Cary Clark579985c2017-07-31 11:48:27 -04002453 bool embeddedSymbol = false;
Cary Clark8032b982017-07-28 11:04:54 -04002454 bool hasLower = false;
2455 bool hasUpper = false;
Cary Clark6fc50412017-09-21 12:31:06 -04002456 bool hasIndirection = false;
Cary Clark8032b982017-07-28 11:04:54 -04002457 bool hasSymbol = false;
2458 while (run < size) {
2459 last = c;
2460 c = data[run];
2461 SkASSERT(' ' <= c || '\n' == c);
2462 if (lineFeeds && ' ' < c) {
2463 if (lastPrintable >= lastWrite) {
2464 if (' ' == data[lastWrite]) {
2465 this->writeSpace();
Cary Clarka560c472017-11-27 10:44:06 -05002466 lastWrite++;
Cary Clark8032b982017-07-28 11:04:54 -04002467 }
2468 this->writeBlock(lastPrintable - lastWrite + 1, &data[lastWrite]);
2469 }
2470 if (lineFeeds > 1) {
2471 this->lf(2);
2472 }
2473 this->lfcr(); // defer the indent until non-whitespace is seen
2474 lastWrite = run;
2475 lineFeeds = 0;
2476 }
2477 if (' ' < c) {
2478 lastPrintable = run;
2479 }
2480 switch (c) {
2481 case '\n':
2482 ++lineFeeds;
2483 wroteLineFeeds = true;
2484 case ' ':
2485 switch (word) {
2486 case Word::kStart:
2487 break;
2488 case Word::kUnderline:
2489 case Word::kCap:
2490 case Word::kFirst:
2491 if (!hasLower) {
2492 break;
2493 }
2494 lastWrite = this->lookupReference(punctuation, word, start, run,
2495 lastWrite, last, data);
2496 break;
2497 case Word::kMixed:
Cary Clark579985c2017-07-31 11:48:27 -04002498 if (hasUpper && hasLower && !hasSymbol && lastSpace > 0) {
2499 lastWrite = this->lookupMethod(punctuation, word, lastSpace, run,
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002500 lastWrite, data, hasIndirection);
Cary Clark8032b982017-07-28 11:04:54 -04002501 }
2502 break;
2503 default:
2504 SkASSERT(0);
2505 }
Cary Clark1eace2d2017-07-31 07:52:43 -04002506 punctuation = PunctuationState::kPeriod == punctuation ||
Ben Wagner63fd7602017-10-09 15:45:33 -04002507 (PunctuationState::kStart == punctuation && ' ' >= last) ?
Cary Clark1eace2d2017-07-31 07:52:43 -04002508 PunctuationState::kStart : PunctuationState::kSpace;
Cary Clark8032b982017-07-28 11:04:54 -04002509 word = Word::kStart;
Cary Clark6fc50412017-09-21 12:31:06 -04002510 embeddedIndirection = false;
Cary Clark579985c2017-07-31 11:48:27 -04002511 embeddedSymbol = false;
Cary Clark8032b982017-07-28 11:04:54 -04002512 hasLower = false;
2513 hasUpper = false;
Cary Clark6fc50412017-09-21 12:31:06 -04002514 hasIndirection = false;
Cary Clark8032b982017-07-28 11:04:54 -04002515 hasSymbol = false;
Cary Clark579985c2017-07-31 11:48:27 -04002516 lastSpace = run;
Cary Clark8032b982017-07-28 11:04:54 -04002517 break;
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002518 case '.': case ',': case ';': case ':': case ')':
Cary Clark8032b982017-07-28 11:04:54 -04002519 switch (word) {
2520 case Word::kStart:
2521 punctuation = PunctuationState::kDelimiter;
2522 case Word::kCap:
2523 case Word::kFirst:
2524 case Word::kUnderline:
2525 case Word::kMixed:
2526 if (PunctuationState::kDelimiter == punctuation ||
2527 PunctuationState::kPeriod == punctuation) {
2528 word = Word::kMixed;
2529 }
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002530 punctuation = '.' == c ? PunctuationState::kPeriod :
2531 PunctuationState::kDelimiter;
Cary Clark8032b982017-07-28 11:04:54 -04002532 break;
2533 default:
2534 SkASSERT(0);
2535 }
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002536 ('.' == c ? embeddedIndirection : embeddedSymbol) = true;
Cary Clark8032b982017-07-28 11:04:54 -04002537 break;
Cary Clark6fc50412017-09-21 12:31:06 -04002538 case '>':
2539 if ('-' == last) {
2540 embeddedIndirection = true;
2541 break;
2542 }
Cary Clark8032b982017-07-28 11:04:54 -04002543 case '\'': // possessive apostrophe isn't treated as delimiting punctation
Cary Clarkce101242017-09-01 15:51:02 -04002544 case '\"': // quote is passed straight through
Cary Clark8032b982017-07-28 11:04:54 -04002545 case '=':
2546 case '!': // assumed not to be punctuation, but a programming symbol
Cary Clark6fc50412017-09-21 12:31:06 -04002547 case '&': case '<': case '{': case '}': case '/': case '*': case '[': case ']':
Cary Clark8032b982017-07-28 11:04:54 -04002548 word = Word::kMixed;
Cary Clark579985c2017-07-31 11:48:27 -04002549 embeddedSymbol = true;
Cary Clark8032b982017-07-28 11:04:54 -04002550 break;
2551 case '(':
2552 if (' ' == last) {
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002553 punctuation = PunctuationState::kParen;
Cary Clark8032b982017-07-28 11:04:54 -04002554 } else {
2555 word = Word::kMixed;
2556 }
Cary Clark579985c2017-07-31 11:48:27 -04002557 embeddedSymbol = true;
Cary Clark8032b982017-07-28 11:04:54 -04002558 break;
Cary Clark8032b982017-07-28 11:04:54 -04002559 case '_':
2560 switch (word) {
2561 case Word::kStart:
2562 word = Word::kMixed;
2563 break;
2564 case Word::kCap:
2565 case Word::kFirst:
2566 case Word::kUnderline:
2567 word = Word::kUnderline;
2568 break;
2569 case Word::kMixed:
2570 break;
2571 default:
2572 SkASSERT(0);
2573 }
Cary Clark579985c2017-07-31 11:48:27 -04002574 hasSymbol |= embeddedSymbol;
Cary Clark8032b982017-07-28 11:04:54 -04002575 break;
Cary Clark73fa9722017-08-29 17:36:51 -04002576 case '+':
2577 // hackery to allow C++
2578 SkASSERT('C' == last || '+' == last); // FIXME: don't allow + outside of #Formula
2579 break;
Cary Clark8032b982017-07-28 11:04:54 -04002580 case 'A': case 'B': case 'C': case 'D': case 'E':
2581 case 'F': case 'G': case 'H': case 'I': case 'J':
2582 case 'K': case 'L': case 'M': case 'N': case 'O':
2583 case 'P': case 'Q': case 'R': case 'S': case 'T':
2584 case 'U': case 'V': case 'W': case 'X': case 'Y':
2585 case 'Z':
2586 switch (word) {
2587 case Word::kStart:
2588 word = PunctuationState::kStart == punctuation ? Word::kFirst : Word::kCap;
2589 start = run;
2590 break;
2591 case Word::kCap:
2592 case Word::kFirst:
Cary Clark154beea2017-10-26 07:58:48 -04002593 if (!isupper(last) && '~' != last) {
Cary Clark8032b982017-07-28 11:04:54 -04002594 word = Word::kMixed;
2595 }
2596 break;
2597 case Word::kUnderline:
2598 // some word in Xxx_XXX_Xxx can be all upper, but all can't: XXX_XXX
2599 if ('_' != last && !isupper(last)) {
2600 word = Word::kMixed;
2601 }
2602 break;
2603 case Word::kMixed:
2604 break;
2605 default:
2606 SkASSERT(0);
2607 }
2608 hasUpper = true;
2609 if (PunctuationState::kPeriod == punctuation ||
2610 PunctuationState::kDelimiter == punctuation) {
2611 word = Word::kMixed;
Cary Clark8032b982017-07-28 11:04:54 -04002612 }
Cary Clark6fc50412017-09-21 12:31:06 -04002613 hasIndirection |= embeddedIndirection;
Cary Clark579985c2017-07-31 11:48:27 -04002614 hasSymbol |= embeddedSymbol;
Cary Clark8032b982017-07-28 11:04:54 -04002615 break;
2616 case 'a': case 'b': case 'c': case 'd': case 'e':
2617 case 'f': case 'g': case 'h': case 'i': case 'j':
2618 case 'k': case 'l': case 'm': case 'n': case 'o':
2619 case 'p': case 'q': case 'r': case 's': case 't':
2620 case 'u': case 'v': case 'w': case 'x': case 'y':
Ben Wagner63fd7602017-10-09 15:45:33 -04002621 case 'z':
Cary Clark8032b982017-07-28 11:04:54 -04002622 case '0': case '1': case '2': case '3': case '4':
2623 case '5': case '6': case '7': case '8': case '9':
Cary Clark682c58d2018-05-16 07:07:07 -04002624 case '%': // to do : ensure that preceding is a number
Cary Clark8032b982017-07-28 11:04:54 -04002625 case '-':
2626 switch (word) {
2627 case Word::kStart:
2628 word = Word::kMixed;
2629 break;
2630 case Word::kMixed:
2631 case Word::kCap:
2632 case Word::kFirst:
2633 case Word::kUnderline:
2634 break;
2635 default:
2636 SkASSERT(0);
2637 }
2638 hasLower = true;
2639 punctuation = PunctuationState::kStart;
Cary Clark6fc50412017-09-21 12:31:06 -04002640 hasIndirection |= embeddedIndirection;
Cary Clark579985c2017-07-31 11:48:27 -04002641 hasSymbol |= embeddedSymbol;
Cary Clark8032b982017-07-28 11:04:54 -04002642 break;
Cary Clark154beea2017-10-26 07:58:48 -04002643 case '~':
2644 SkASSERT(Word::kStart == word);
2645 word = PunctuationState::kStart == punctuation ? Word::kFirst : Word::kCap;
2646 start = run;
2647 hasUpper = true;
2648 hasIndirection |= embeddedIndirection;
2649 hasSymbol |= embeddedSymbol;
2650 break;
Cary Clark8032b982017-07-28 11:04:54 -04002651 default:
2652 SkASSERT(0);
2653 }
2654 ++run;
2655 }
2656 if ((word == Word::kCap || word == Word::kFirst || word == Word::kUnderline) && hasLower) {
2657 lastWrite = this->lookupReference(punctuation, word, start, run, lastWrite, last, data);
Cary Clark579985c2017-07-31 11:48:27 -04002658 } else if (word == Word::kMixed && hasUpper && hasLower && !hasSymbol && lastSpace > 0) {
Cary Clark6fc50412017-09-21 12:31:06 -04002659 lastWrite = this->lookupMethod(punctuation, word, lastSpace, run, lastWrite, data,
2660 hasIndirection && !hasSymbol);
Cary Clark8032b982017-07-28 11:04:54 -04002661 }
2662 if (run > lastWrite) {
2663 if (' ' == data[lastWrite]) {
2664 this->writeSpace();
2665 }
2666 this->writeBlock(run - lastWrite, &data[lastWrite]);
2667 }
2668 return wroteLineFeeds ? Wrote::kLF : Wrote::kChars;
2669}
Cary Clarkb94f6da2018-06-08 11:54:32 -04002670
2671static string paddedString(int num) {
2672 auto padded = std::to_string(num);
2673 padded.insert(0, 2U - std::min(string::size_type(2), padded.length()), '0');
2674 return padded;
2675}
2676
2677bool IncludeWriter::writeHeader(std::pair<const string, Definition>& include) {
2678 std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
2679 time_t tt = std::chrono::system_clock::to_time_t(now);
2680 tm local_tm = *localtime(&tt);
2681
2682 // find end of copyright header
2683 fChar = fStart;
2684 if (!this->skipExact(
2685 "/*\n"
2686 " * Copyright ")) {
2687 return this->reportError<bool>("copyright mismatch 1");
2688 }
2689 const char* date = fChar;
2690 this->skipToSpace();
2691 string yearStr(date, fChar - date);
2692 int year = stoi(yearStr);
2693 if (year < 2005 || year > local_tm.tm_year + 1900) {
2694 return this->reportError<bool>("copyright year out of range");
2695 }
2696 this->skipSpace();
2697 const char android[] = "The Android Open Source Project";
2698 const char google[] = "Google Inc.";
2699 if (this->startsWith(android)) {
2700 this->skipExact(android);
2701 } else if (!this->skipExact(google)) {
2702 return this->reportError<bool>("copyright mismatch 2");
2703 }
2704 if (!this->skipExact(
2705 "\n"
2706 " *\n"
2707 " * Use of this source code is governed by a BSD-style license that can be\n"
2708 " * found in the LICENSE file.\n"
2709 " */\n"
2710 "\n"
2711 )) {
2712 return this->reportError<bool>("copyright mismatch 2");
2713 }
2714 this->writeBlock(fChar - fStart, fStart);
2715 this->lf(2);
2716 this->writeString("/* Generated by tools/bookmaker from");
2717 this->writeSpace();
2718 string includeName = include.first;
2719 std::replace(includeName.begin(), includeName.end(), '\\', '/');
2720 this->writeString(includeName);
2721 this->writeSpace();
2722 this->writeString("and");
2723 this->writeSpace();
2724 string bmhName = fRootTopic->fFileName;
2725 std::replace(bmhName.begin(), bmhName.end(), '\\', '/');
2726 this->writeString(bmhName);
2727 this->lfcr();
2728 fIndent = 3;
2729 string dateTimeStr = std::to_string(local_tm.tm_year + 1900) + "-"
2730 + paddedString(local_tm.tm_mon + 1) + "-"
2731 + paddedString(local_tm.tm_mday) + " "
2732 + paddedString(local_tm.tm_hour) + ":"
2733 + paddedString(local_tm.tm_min) + ":"
2734 + paddedString(local_tm.tm_sec);
2735 this->writeString("on");
2736 this->writeSpace();
2737 this->writeString(dateTimeStr);
2738 this->writeString(". Additional documentation and examples can be found at:");
2739 this->lfcr();
2740 this->writeString("https://skia.org/user/api/");
2741 size_t bmhPageStart = bmhName.rfind('/');
2742 size_t bmhPageEnd = bmhName.rfind('.');
2743 if (string::npos == bmhPageStart || string::npos == bmhPageEnd) {
2744 return this->reportError<bool>("badly formed bmh page name");
2745 }
2746 ++bmhPageStart;
2747 string bmhPage = bmhName.substr(bmhPageStart, bmhPageEnd - bmhPageStart);
2748 this->writeString(bmhPage);
2749 this->lf(2);
2750 this->writeString("You may edit either file directly. Structural changes to public interfaces require");
2751 this->lfcr();
2752 this->writeString("editing both files. After editing");
2753 this->writeSpace();
2754 this->writeString(bmhName);
2755 this->writeSpace();
2756 this->writeString(", run:");
2757 this->lfcr();
2758 fIndent += 4;
2759 this->writeString("bookmaker -b docs -i");
2760 this->writeSpace();
2761 this->writeString(includeName);
2762 this->writeSpace();
2763 this->writeString("-p");
2764 this->lfcr();
2765 fIndent -= 4;
2766 this->writeString("to create an updated version of this file.");
2767 this->lfcr();
2768 fIndent = 1;
2769 this->writeString("*/");
2770 this->lf(2);
2771 fIndent = 0;
Cary Clark27dddae2018-06-08 15:57:37 -04002772 if (this->startsWith("/* Generated by tools/bookmaker from")) {
2773 this->skipToEndBracket("*/");
2774 if (!this->skipExact("*/\n\n")) {
2775 return this->reportError<bool>("malformed generated comment");
2776 }
2777 }
Cary Clarkb94f6da2018-06-08 11:54:32 -04002778 fStart = fChar;
Cary Clark27dddae2018-06-08 15:57:37 -04002779
Cary Clarkb94f6da2018-06-08 11:54:32 -04002780 return true;
2781}