blob: e9ab8b94c477cc09cbc24fe49c9067c3bb4dd419 [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 Clarkab5c9af2018-07-12 16:24:53 -0400439 if (!enumDef && enumName == root->fName) {
440 enumDef = root;
441 }
Cary Clark8032b982017-07-28 11:04:54 -0400442 SkASSERT(enumDef);
443 // child[0] should be #Code comment starts at child[0].fTerminator
444 // though skip until #Code is found (in case there's a #ToDo, etc)
445 // child[1] should be #Const comment ends at child[1].fStart
446 // comment becomes enum header (if any)
447 } else {
448 string enumName(root->fName);
449 enumName += "::_anonymous";
450 if (fAnonymousEnumCount > 1) {
451 enumName += '_' + to_string(fAnonymousEnumCount);
452 }
Cary Clarkce101242017-09-01 15:51:02 -0400453 enumDef = root->find(enumName, RootDefinition::AllowParens::kNo);
Cary Clark8032b982017-07-28 11:04:54 -0400454 SkASSERT(enumDef);
455 ++fAnonymousEnumCount;
456 }
457 Definition* codeBlock = nullptr;
458 const char* commentStart = nullptr;
459 bool wroteHeader = false;
Cary Clark6fc50412017-09-21 12:31:06 -0400460 bool lastAnchor = false;
Cary Clark8032b982017-07-28 11:04:54 -0400461 SkDEBUGCODE(bool foundConst = false);
462 for (auto test : enumDef->fChildren) {
Cary Clark682c58d2018-05-16 07:07:07 -0400463 if (MarkType::kCode == test->fMarkType && !codeBlock) {
Cary Clark8032b982017-07-28 11:04:54 -0400464 codeBlock = test;
465 commentStart = codeBlock->fTerminator;
466 continue;
467 }
468 if (!codeBlock) {
469 continue;
470 }
471 const char* commentEnd = test->fStart;
472 if (!wroteHeader &&
473 !this->contentFree((int) (commentEnd - commentStart), commentStart)) {
Cary Clark154beea2017-10-26 07:58:48 -0400474 if (fIndentNext) {
Cary Clark0d225392018-06-07 09:59:07 -0400475 // FIXME: how can I tell where fIdentNext gets cleared?
476 this->indentIn(IndentKind::kEnumChild);
Cary Clark154beea2017-10-26 07:58:48 -0400477 }
Cary Clark8032b982017-07-28 11:04:54 -0400478 this->writeCommentHeader();
479 this->writeString("\\enum");
Cary Clarkbad5ad72017-08-03 17:14:08 -0400480 if (fullName.length() > 0) {
481 this->writeSpace();
482 this->writeString(fullName.c_str());
483 }
Cary Clark0d225392018-06-07 09:59:07 -0400484 this->indentIn(IndentKind::kEnumChild2);
Cary Clark8032b982017-07-28 11:04:54 -0400485 this->lfcr();
486 wroteHeader = true;
487 }
Cary Clark6fc50412017-09-21 12:31:06 -0400488 if (lastAnchor) {
489 if (commentEnd - commentStart > 1) {
490 SkASSERT('\n' == commentStart[0]);
491 if (' ' == commentStart[1]) {
492 this->writeSpace();
493 }
494 }
495 lastAnchor = false;
496 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400497 this->rewriteBlock((int) (commentEnd - commentStart), commentStart, Phrase::kNo);
Cary Clark682c58d2018-05-16 07:07:07 -0400498 if (MarkType::kAnchor == test->fMarkType || MarkType::kCode == test->fMarkType) {
Cary Clark154beea2017-10-26 07:58:48 -0400499 bool newLine = commentEnd - commentStart > 1 &&
500 '\n' == commentEnd[-1] && '\n' == commentEnd[-2];
Cary Clark8032b982017-07-28 11:04:54 -0400501 commentStart = test->fContentStart;
Cary Clark682c58d2018-05-16 07:07:07 -0400502 commentEnd = MarkType::kAnchor == test->fMarkType ? test->fChildren[0]->fStart :
503 test->fContentEnd;
Cary Clark154beea2017-10-26 07:58:48 -0400504 if (newLine) {
505 this->lf(2);
506 } else {
507 this->writeSpace();
508 }
Cary Clark682c58d2018-05-16 07:07:07 -0400509 if (MarkType::kAnchor == test->fMarkType) {
510 this->rewriteBlock((int) (commentEnd - commentStart), commentStart, Phrase::kNo);
511 } else {
512 this->writeBlock((int) (commentEnd - commentStart), commentStart);
513 this->lf(2);
514 }
Cary Clark6fc50412017-09-21 12:31:06 -0400515 lastAnchor = true; // this->writeSpace();
Cary Clark8032b982017-07-28 11:04:54 -0400516 }
517 commentStart = test->fTerminator;
518 if (MarkType::kConst == test->fMarkType) {
519 SkASSERT(codeBlock); // FIXME: check enum for correct order earlier
520 SkDEBUGCODE(foundConst = true);
521 break;
522 }
523 }
524 SkASSERT(codeBlock);
525 SkASSERT(foundConst);
526 if (wroteHeader) {
Cary Clark0d225392018-06-07 09:59:07 -0400527 this->indentOut();
Cary Clark8032b982017-07-28 11:04:54 -0400528 this->lfcr();
Cary Clark137b8742018-05-30 09:21:49 -0400529 this->writeCommentTrailer(OneLine::kNo);
Cary Clark8032b982017-07-28 11:04:54 -0400530 }
Cary Clarkbad5ad72017-08-03 17:14:08 -0400531 Definition* braceHolder = child.fChildren[0];
532 if (KeyWord::kClass == braceHolder->fKeyWord) {
533 braceHolder = braceHolder->fChildren[0];
534 }
535 bodyEnd = braceHolder->fContentStart;
Cary Clark8032b982017-07-28 11:04:54 -0400536 SkASSERT('{' == bodyEnd[0]);
537 ++bodyEnd;
538 this->lfcr();
539 this->writeBlock((int) (bodyEnd - fStart), fStart); // write include "enum Name {"
Cary Clark0d225392018-06-07 09:59:07 -0400540 this->indentIn(IndentKind::kEnumHeader2);
Cary Clark8032b982017-07-28 11:04:54 -0400541 this->singleLF();
Cary Clark0d225392018-06-07 09:59:07 -0400542 this->setStart(bodyEnd, braceHolder);
Cary Clark8032b982017-07-28 11:04:54 -0400543 fEnumDef = enumDef;
544}
545
Cary Clark682c58d2018-05-16 07:07:07 -0400546const Definition* IncludeWriter::enumMemberForComment(const Definition* currentEnumItem) const {
547 for (auto constItem : currentEnumItem->fChildren) {
548 if (MarkType::kLine == constItem->fMarkType
549 || MarkType::kExperimental == constItem->fMarkType
550 || MarkType::kDeprecated == constItem->fMarkType) {
551 return constItem;
552 }
553 }
554 SkASSERT(0);
555 return nullptr;
556}
557
558string IncludeWriter::enumMemberComment(const Definition* currentEnumItem,
559 const Definition& child) const {
560 // #Const should always be followed by #Line, so description follows that
561 string shortComment;
562 for (auto constItem : currentEnumItem->fChildren) {
563 if (MarkType::kLine == constItem->fMarkType) {
564 shortComment = string(constItem->fContentStart, constItem->length());
565 break;
566 }
567 if (IncompleteAllowed(constItem->fMarkType)) {
Cary Clark137b8742018-05-30 09:21:49 -0400568 shortComment = constItem->fParent->incompleteMessage(Definition::DetailsType::kPhrase);
Cary Clark682c58d2018-05-16 07:07:07 -0400569 }
570 }
571 if (!shortComment.length()) {
572 currentEnumItem->reportError<void>("missing #Line or #Deprecated or #Experimental");
573 }
574 return shortComment;
575}
576
577IncludeWriter::ItemState IncludeWriter::enumMemberName(
578 const Definition& child, const Definition* token, Item* item, LastItem* last,
579 const Definition** currentEnumItem) {
580 TextParser parser(fFileName, last->fStart, last->fEnd, fLineCount);
581 parser.skipWhiteSpace();
582 item->fName = string(parser.fChar, (int) (last->fEnd - parser.fChar));
583 *currentEnumItem = this->matchMemberName(item->fName, child);
584 if (token) {
Cary Clark0d225392018-06-07 09:59:07 -0400585 this->setStart(token->fContentEnd, token);
Cary Clark682c58d2018-05-16 07:07:07 -0400586 TextParser enumLine(token->fFileName, last->fEnd, token->fContentStart, token->fLineCount);
587 const char* end = enumLine.anyOf(",}=");
588 SkASSERT(end);
589 if ('=' == *end) { // write enum value
590 last->fEnd = token->fContentEnd;
591 item->fValue = string(token->fContentStart, (int) (last->fEnd - token->fContentStart));
592 return ItemState::kValue;
593 }
594 }
595 return ItemState::kComment;
596}
597
598void IncludeWriter::enumMemberOut(const Definition* currentEnumItem, const Definition& child,
599 const Item& item, Preprocessor& preprocessor) {
600 SkASSERT(currentEnumItem);
601 string shortComment = this->enumMemberComment(currentEnumItem, child);
602 int enumItemValueTab =
603 SkTMax((int) item.fName.length() + fIndent + 1, fEnumItemValueTab); // 1: ,
604 int valueLength = item.fValue.length();
605 int assignLength = valueLength ? valueLength + 3 : 0; // 3: space = space
606 int enumItemCommentTab = SkTMax(enumItemValueTab + assignLength, fEnumItemCommentTab);
607 int trimNeeded = enumItemCommentTab + shortComment.length() - (100 - (sizeof("//!< ") - 1));
608 bool crAfterName = false;
609 if (trimNeeded > 0) {
610 if (item.fValue.length()) {
611 int valueSpare = SkTMin(trimNeeded, // 3 below: space = space
612 (int) (enumItemCommentTab - enumItemValueTab - item.fValue.length() - 3));
613 SkASSERT(valueSpare >= 0);
614 trimNeeded -= valueSpare;
615 enumItemCommentTab -= valueSpare;
616 }
617 if (trimNeeded > 0) {
618 int nameSpare = SkTMin(trimNeeded, (int) (enumItemValueTab - item.fName.length()
619 - fIndent - 1)); // 1: ,
620 SkASSERT(nameSpare >= 0);
621 trimNeeded -= nameSpare;
622 enumItemValueTab -= nameSpare;
623 enumItemCommentTab -= nameSpare;
624 }
625 if (trimNeeded > 0) {
626 crAfterName = true;
627 if (!valueLength) {
628 this->enumMemberForComment(currentEnumItem)->reportError<void>("comment too long");
629 } else if (valueLength + fIndent + 8 + shortComment.length() > // 8: addtional indent
630 100 - (sizeof(", //!< ") - 1)) { // -1: zero-terminated string
631 this->enumMemberForComment(currentEnumItem)->reportError<void>("comment 2 long");
632 } // 2: = space
633 enumItemValueTab = fEnumItemValueTab + 2 // 2: , space
634 - SkTMax(0, fEnumItemValueTab + 2 + valueLength + 2 - fEnumItemCommentTab);
635 enumItemCommentTab = SkTMax(enumItemValueTab + valueLength + 2, fEnumItemCommentTab);
636 }
637 }
638 this->lfcr();
639 this->writeString(item.fName);
640 int saveIndent = fIndent;
641 if (item.fValue.length()) {
642 if (!crAfterName) {
643 this->indentToColumn(enumItemValueTab);
644 } else {
645 this->writeSpace();
646 }
647 this->writeString("=");
648 if (crAfterName) {
649 this->lfcr();
650 fIndent = enumItemValueTab;
651 } else {
652 this->writeSpace();
653 }
654 this->writeString(item.fValue);
655 }
656 this->writeString(",");
657 this->indentToColumn(enumItemCommentTab);
658 this->writeString("//!<");
659 this->writeSpace();
660 this->rewriteBlock(shortComment.length(), shortComment.c_str(), Phrase::kYes);
661 this->lfcr();
662 fIndent = saveIndent;
663 if (preprocessor.fStart) {
664 SkASSERT(preprocessor.fEnd);
665 int saveIndent = fIndent;
666 fIndent = SkTMax(0, fIndent - 8);
667 this->lf(2);
668 this->writeBlock(
669 (int) (preprocessor.fEnd - preprocessor.fStart), preprocessor.fStart);
670 this->lfcr();
671 fIndent = saveIndent;
672 preprocessor.reset();
673 }
674}
675
676// iterate through include tokens and find how much remains for 1 line comments
677// put ones that fit on same line, ones that are too big wrap
678void IncludeWriter::enumMembersOut(Definition& child) {
679 ItemState state = ItemState::kNone;
680 const Definition* currentEnumItem;
681 LastItem last = { nullptr, nullptr };
682 auto brace = child.fChildren[0];
683 if (KeyWord::kClass == brace->fKeyWord) {
684 brace = brace->fChildren[0];
685 }
686 SkASSERT(Bracket::kBrace == brace->fBracket);
Cary Clarkbad5ad72017-08-03 17:14:08 -0400687 vector<IterState> iterStack;
Cary Clark682c58d2018-05-16 07:07:07 -0400688 iterStack.emplace_back(brace->fTokens.begin(), brace->fTokens.end());
Cary Clarkbad5ad72017-08-03 17:14:08 -0400689 IterState* iterState = &iterStack[0];
Cary Clark186d08f2018-04-03 08:43:27 -0400690 Preprocessor preprocessor;
Cary Clark682c58d2018-05-16 07:07:07 -0400691 Item item;
692 while (iterState->fDefIter != iterState->fDefEnd) {
693 auto& token = *iterState->fDefIter++;
694 if (this->enumPreprocessor(&token, MemberPass::kOut, iterStack, &iterState,
Cary Clark186d08f2018-04-03 08:43:27 -0400695 &preprocessor)) {
Cary Clarkbad5ad72017-08-03 17:14:08 -0400696 continue;
697 }
Cary Clark682c58d2018-05-16 07:07:07 -0400698 if (ItemState::kName == state) {
699 state = this->enumMemberName(child, &token, &item, &last, &currentEnumItem);
Cary Clark8032b982017-07-28 11:04:54 -0400700 }
Cary Clark682c58d2018-05-16 07:07:07 -0400701 if (ItemState::kValue == state) {
702 TextParser valueEnd(token.fFileName, last.fEnd, token.fContentStart, token.fLineCount);
Cary Clark8032b982017-07-28 11:04:54 -0400703 const char* end = valueEnd.anyOf(",}");
704 if (!end) { // write expression continuation
Cary Clark682c58d2018-05-16 07:07:07 -0400705 item.fValue += string(last.fEnd, (int) (token.fContentEnd - last.fEnd));
Cary Clark8032b982017-07-28 11:04:54 -0400706 continue;
707 }
708 }
Cary Clark682c58d2018-05-16 07:07:07 -0400709 if (ItemState::kNone != state) {
710 this->enumMemberOut(currentEnumItem, child, item, preprocessor);
Cary Clark137b8742018-05-30 09:21:49 -0400711 item.reset();
Cary Clark0d225392018-06-07 09:59:07 -0400712 this->setStartBack(token.fContentStart, &token);
Cary Clark682c58d2018-05-16 07:07:07 -0400713 state = ItemState::kNone;
714 last.fStart = nullptr;
Cary Clark8032b982017-07-28 11:04:54 -0400715 }
Cary Clark682c58d2018-05-16 07:07:07 -0400716 SkASSERT(ItemState::kNone == state);
717 if (!last.fStart) {
718 last.fStart = fStart;
Cary Clark8032b982017-07-28 11:04:54 -0400719 }
Cary Clark682c58d2018-05-16 07:07:07 -0400720 last.fEnd = token.fContentEnd;
721 state = ItemState::kName;
Cary Clark8032b982017-07-28 11:04:54 -0400722 }
Cary Clark682c58d2018-05-16 07:07:07 -0400723 if (ItemState::kName == state) {
724 state = this->enumMemberName(child, nullptr, &item, &last, &currentEnumItem);
725 }
726 if (ItemState::kValue == state || ItemState::kComment == state) {
727 this->enumMemberOut(currentEnumItem, child, item, preprocessor);
728 }
Cary Clark0d225392018-06-07 09:59:07 -0400729 this->indentOut();
Cary Clark8032b982017-07-28 11:04:54 -0400730}
731
Cary Clark186d08f2018-04-03 08:43:27 -0400732bool IncludeWriter::enumPreprocessor(Definition* token, MemberPass pass,
733 vector<IterState>& iterStack, IterState** iterState, Preprocessor* preprocessor) {
734 if (token && Definition::Type::kBracket == token->fType) {
735 if (Bracket::kSlashSlash == token->fBracket) {
736 if (MemberPass::kOut == pass) {
Cary Clark0d225392018-06-07 09:59:07 -0400737 this->setStart(token->fContentEnd, token);
Cary Clark186d08f2018-04-03 08:43:27 -0400738 }
739 return true; // ignore old inline comments
740 }
741 if (Bracket::kSlashStar == token->fBracket) {
742 if (MemberPass::kOut == pass) {
Cary Clark0d225392018-06-07 09:59:07 -0400743 this->setStart(token->fContentEnd + 1, token);
Cary Clark186d08f2018-04-03 08:43:27 -0400744 }
745 return true; // ignore old inline comments
746 }
747 if (Bracket::kPound == token->fBracket) { // preprocessor wraps member
Cary Clarka4f581a2018-04-03 15:31:59 -0400748 preprocessor->fDefinition = token;
Cary Clark186d08f2018-04-03 08:43:27 -0400749 preprocessor->fStart = token->fContentStart;
750 if (KeyWord::kIf == token->fKeyWord || KeyWord::kIfdef == token->fKeyWord) {
751 iterStack.emplace_back(token->fTokens.begin(), token->fTokens.end());
752 *iterState = &iterStack.back();
753 preprocessor->fWord = true;
754 } else if (KeyWord::kEndif == token->fKeyWord || KeyWord::kElif == token->fKeyWord
755 || KeyWord::kElse == token->fKeyWord) {
756 iterStack.pop_back();
757 *iterState = &iterStack.back();
758 preprocessor->fEnd = token->fContentEnd;
759 if (KeyWord::kElif == token->fKeyWord) {
760 iterStack.emplace_back(token->fTokens.begin(), token->fTokens.end());
761 *iterState = &iterStack.back();
762 preprocessor->fWord = true;
763 }
764 } else {
765 SkASSERT(0); // incomplete
766 }
767 return true;
768 }
Cary Clarka4f581a2018-04-03 15:31:59 -0400769 if (preprocessor->fDefinition) {
770 if (Bracket::kParen == token->fBracket) {
771 preprocessor->fEnd = token->fContentEnd;
772 SkASSERT(')' == *preprocessor->fEnd);
773 ++preprocessor->fEnd;
774 return true;
775 }
776 SkASSERT(0); // incomplete
777 }
Cary Clark186d08f2018-04-03 08:43:27 -0400778 return true;
779 }
780 if (token && Definition::Type::kWord != token->fType) {
781 SkASSERT(0); // incomplete
782 }
783 if (preprocessor->fWord) {
784 preprocessor->fWord = false;
785 preprocessor->fEnd = token->fContentEnd;
786 return true;
787 }
788 return false;
789}
790
Cary Clark8032b982017-07-28 11:04:54 -0400791void IncludeWriter::enumSizeItems(const Definition& child) {
Cary Clark682c58d2018-05-16 07:07:07 -0400792 ItemState state = ItemState::kNone;
793 ItemLength lengths = { 0, 0, 0, 0 };
Cary Clark8032b982017-07-28 11:04:54 -0400794 const char* lastEnd = nullptr;
Cary Clark8032b982017-07-28 11:04:54 -0400795 auto brace = child.fChildren[0];
Cary Clarkbad5ad72017-08-03 17:14:08 -0400796 if (KeyWord::kClass == brace->fKeyWord) {
797 brace = brace->fChildren[0];
798 }
Cary Clark8032b982017-07-28 11:04:54 -0400799 SkASSERT(Bracket::kBrace == brace->fBracket);
Cary Clarkbad5ad72017-08-03 17:14:08 -0400800 vector<IterState> iterStack;
801 iterStack.emplace_back(brace->fTokens.begin(), brace->fTokens.end());
802 IterState* iterState = &iterStack[0];
Cary Clark186d08f2018-04-03 08:43:27 -0400803 Preprocessor preprocessor;
Cary Clark682c58d2018-05-16 07:07:07 -0400804 string enumName;
Cary Clarkbad5ad72017-08-03 17:14:08 -0400805 while (iterState->fDefIter != iterState->fDefEnd) {
806 auto& token = *iterState->fDefIter++;
Cary Clark186d08f2018-04-03 08:43:27 -0400807 if (this->enumPreprocessor(&token, MemberPass::kCount, iterStack, &iterState,
808 &preprocessor)) {
Cary Clarkbad5ad72017-08-03 17:14:08 -0400809 continue;
810 }
Cary Clark682c58d2018-05-16 07:07:07 -0400811 if (ItemState::kName == state) {
812 TextParser enumLine(token.fFileName, lastEnd, token.fContentStart, token.fLineCount);
Cary Clark8032b982017-07-28 11:04:54 -0400813 const char* end = enumLine.anyOf(",}=");
814 SkASSERT(end);
Cary Clark682c58d2018-05-16 07:07:07 -0400815 state = '=' == *end ? ItemState::kValue : ItemState::kComment;
816 if (ItemState::kValue == state) {
Cary Clark8032b982017-07-28 11:04:54 -0400817 lastEnd = token.fContentEnd;
Cary Clark682c58d2018-05-16 07:07:07 -0400818 lengths.fCurValue = (int) (lastEnd - token.fContentStart);
Cary Clark8032b982017-07-28 11:04:54 -0400819 continue;
820 }
821 }
Cary Clark682c58d2018-05-16 07:07:07 -0400822 if (ItemState::kValue == state) {
823 TextParser valueEnd(token.fFileName, lastEnd, token.fContentStart, token.fLineCount);
Cary Clark8032b982017-07-28 11:04:54 -0400824 const char* end = valueEnd.anyOf(",}");
825 if (!end) { // write expression continuation
Cary Clark682c58d2018-05-16 07:07:07 -0400826 lengths.fCurValue += (int) (token.fContentEnd - lastEnd);
Cary Clark8032b982017-07-28 11:04:54 -0400827 continue;
828 }
829 }
Cary Clark682c58d2018-05-16 07:07:07 -0400830 if (ItemState::kNone != state) {
831 this->checkEnumLengths(child, enumName, &lengths);
832 lengths.fCurValue = 0;
833 state = ItemState::kNone;
Cary Clark8032b982017-07-28 11:04:54 -0400834 }
Cary Clark682c58d2018-05-16 07:07:07 -0400835 SkASSERT(ItemState::kNone == state);
Cary Clark8032b982017-07-28 11:04:54 -0400836 lastEnd = token.fContentEnd;
Cary Clark682c58d2018-05-16 07:07:07 -0400837 lengths.fCurName = (int) (lastEnd - token.fContentStart);
838 enumName = string(token.fContentStart, lengths.fCurName);
839 state = ItemState::kName;
Cary Clark8032b982017-07-28 11:04:54 -0400840 }
Cary Clark682c58d2018-05-16 07:07:07 -0400841 if (ItemState::kNone != state) {
842 this->checkEnumLengths(child, enumName, &lengths);
Cary Clark8032b982017-07-28 11:04:54 -0400843 }
Cary Clark682c58d2018-05-16 07:07:07 -0400844 fEnumItemValueTab = lengths.fLongestName + fIndent + 1 /* 1: , */ ;
845 if (lengths.fLongestValue) {
846 lengths.fLongestValue += 3; // 3: space = space
Cary Clark8032b982017-07-28 11:04:54 -0400847 }
Cary Clark682c58d2018-05-16 07:07:07 -0400848 fEnumItemCommentTab = fEnumItemValueTab + lengths.fLongestValue + 1 ; // 1: space before //!<
Cary Clark8032b982017-07-28 11:04:54 -0400849 // iterate through bmh children and see which comments fit on include lines
Cary Clark682c58d2018-05-16 07:07:07 -0400850 if (!this->checkChildCommentLength(fEnumDef, MarkType::kConst)) {
851 fEnumDef->reportError<void>("expected at least one #Const in #Enum");
Cary Clark8032b982017-07-28 11:04:54 -0400852 }
853}
854
Cary Clark682c58d2018-05-16 07:07:07 -0400855const Definition* IncludeWriter::matchMemberName(string matchName, const Definition& child) const {
856 const Definition* parent = &child;
857 if (KeyWord::kEnum == child.fKeyWord && child.fChildren.size() > 0
858 && KeyWord::kClass == child.fChildren[0]->fKeyWord) {
859 matchName = child.fChildren[0]->fName + "::" + matchName;
860 }
861 do {
862 if (KeyWord::kStruct == parent->fKeyWord || KeyWord::kClass == parent->fKeyWord) {
863 matchName = parent->fName + "::" + matchName;
864 }
865 } while ((parent = parent->fParent));
866 const Definition* enumItem = nullptr;
867 for (auto testItem : fEnumDef->fChildren) {
868 if (MarkType::kConst != testItem->fMarkType) {
869 continue;
870 }
871 if (matchName != testItem->fName) {
872 continue;
873 }
874 enumItem = testItem;
875 break;
876 }
877 SkASSERT(enumItem);
878 return enumItem;
879}
880
Cary Clark8032b982017-07-28 11:04:54 -0400881// walk children and output complete method doxygen description
Cary Clark682c58d2018-05-16 07:07:07 -0400882void IncludeWriter::methodOut(Definition* method, const Definition& child) {
Cary Clark154beea2017-10-26 07:58:48 -0400883 if (fPendingMethod) {
Cary Clark0d225392018-06-07 09:59:07 -0400884 this->indentOut();
Cary Clark154beea2017-10-26 07:58:48 -0400885 fPendingMethod = false;
886 }
Cary Clark579985c2017-07-31 11:48:27 -0400887 fBmhMethod = method;
888 fMethodDef = &child;
Cary Clark8032b982017-07-28 11:04:54 -0400889 fContinuation = nullptr;
890 fDeferComment = nullptr;
Cary Clark682c58d2018-05-16 07:07:07 -0400891 Definition* csParent = method->csParent();
Cary Clarka4f581a2018-04-03 15:31:59 -0400892 if (csParent && (0 == fIndent || fIndentNext)) {
Cary Clark0d225392018-06-07 09:59:07 -0400893 this->indentIn(IndentKind::kMethodOut);
Cary Clark154beea2017-10-26 07:58:48 -0400894 fIndentNext = false;
Cary Clark8032b982017-07-28 11:04:54 -0400895 }
896 this->writeCommentHeader();
897 fIndent += 4;
Cary Clark1a8d7622018-03-05 13:26:16 -0500898 this->descriptionOut(method, SkipFirstLine::kNo, Phrase::kNo);
Cary Clark8032b982017-07-28 11:04:54 -0400899 // compute indention column
900 size_t column = 0;
901 bool hasParmReturn = false;
902 for (auto methodPart : method->fChildren) {
903 if (MarkType::kParam == methodPart->fMarkType) {
904 column = SkTMax(column, methodPart->fName.length());
905 hasParmReturn = true;
906 } else if (MarkType::kReturn == methodPart->fMarkType) {
907 hasParmReturn = true;
908 }
909 }
910 if (hasParmReturn) {
911 this->lf(2);
912 column += fIndent + sizeof("@return ");
913 int saveIndent = fIndent;
914 for (auto methodPart : method->fChildren) {
Cary Clark8032b982017-07-28 11:04:54 -0400915 if (MarkType::kParam == methodPart->fMarkType) {
916 this->writeString("@param");
917 this->writeSpace();
918 this->writeString(methodPart->fName.c_str());
919 } else if (MarkType::kReturn == methodPart->fMarkType) {
920 this->writeString("@return");
921 } else {
922 continue;
923 }
Cary Clark1a8d7622018-03-05 13:26:16 -0500924 this->indentToColumn(column);
925 fIndent = column;
Cary Clark1a8d7622018-03-05 13:26:16 -0500926 this->descriptionOut(methodPart, SkipFirstLine::kNo, Phrase::kYes);
Cary Clark8032b982017-07-28 11:04:54 -0400927 fIndent = saveIndent;
928 this->lfcr();
929 }
930 } else {
931 this->lfcr();
932 }
933 fIndent -= 4;
934 this->lfcr();
Cary Clark137b8742018-05-30 09:21:49 -0400935 this->writeCommentTrailer(OneLine::kNo);
Cary Clark579985c2017-07-31 11:48:27 -0400936 fBmhMethod = nullptr;
937 fMethodDef = nullptr;
Cary Clark78c110e2018-02-09 16:49:09 -0500938 fEnumDef = nullptr;
Cary Clarkd0530ba2017-09-14 11:25:39 -0400939 fWroteMethod = true;
Cary Clark8032b982017-07-28 11:04:54 -0400940}
941
942void IncludeWriter::structOut(const Definition* root, const Definition& child,
943 const char* commentStart, const char* commentEnd) {
944 this->writeCommentHeader();
945 this->writeString("\\");
946 SkASSERT(MarkType::kClass == child.fMarkType || MarkType::kStruct == child.fMarkType);
947 this->writeString(MarkType::kClass == child.fMarkType ? "class" : "struct");
948 this->writeSpace();
949 this->writeString(child.fName.c_str());
950 fIndent += 4;
951 this->lfcr();
Cary Clarkc68ba1d2018-02-20 10:35:29 -0500952 if (child.fDeprecated) {
Cary Clark682c58d2018-05-16 07:07:07 -0400953 this->writeString(child.incompleteMessage(Definition::DetailsType::kSentence));
Cary Clarkc68ba1d2018-02-20 10:35:29 -0500954 } else {
955 this->rewriteBlock((int)(commentEnd - commentStart), commentStart, Phrase::kNo);
956 }
Cary Clark8032b982017-07-28 11:04:54 -0400957 fIndent -= 4;
958 this->lfcr();
Cary Clark137b8742018-05-30 09:21:49 -0400959 this->writeCommentTrailer(OneLine::kNo);
Cary Clark8032b982017-07-28 11:04:54 -0400960}
961
Cary Clark186d08f2018-04-03 08:43:27 -0400962bool IncludeWriter::findEnumSubtopic(string undername, const Definition** rootDefPtr) const {
963 const Definition* subtopic = fEnumDef->fParent;
964 string subcheck = subtopic->fFiddle + '_' + undername;
965 auto iter = fBmhParser->fTopicMap.find(subcheck);
966 if (iter == fBmhParser->fTopicMap.end()) {
967 return false;
968 }
969 *rootDefPtr = iter->second;
970 return true;
971}
972
Cary Clark2dc84ad2018-01-26 12:56:22 -0500973Definition* IncludeWriter::findMemberCommentBlock(const vector<Definition*>& bmhChildren,
Cary Clark2d4bf5f2018-04-16 08:37:38 -0400974 string name) const {
Cary Clarkc68ba1d2018-02-20 10:35:29 -0500975 for (auto memberDef : bmhChildren) {
976 if (MarkType::kMember != memberDef->fMarkType) {
977 continue;
978 }
979 string match = memberDef->fName;
980 // if match.endsWith(name) ...
Cary Clark300cc5b2018-02-20 12:50:35 -0500981 if (match.length() >= name.length() &&
Cary Clarkc68ba1d2018-02-20 10:35:29 -0500982 0 == match.compare(match.length() - name.length(), name.length(), name)) {
983 return memberDef;
984 }
985 }
986 for (auto memberDef : bmhChildren) {
987 if (MarkType::kSubtopic != memberDef->fMarkType && MarkType::kTopic != memberDef->fMarkType) {
988 continue;
989 }
990 Definition* result = this->findMemberCommentBlock(memberDef->fChildren, name);
991 if (result) {
992 return result;
993 }
994 }
995 return nullptr;
Cary Clark2dc84ad2018-01-26 12:56:22 -0500996}
997
Cary Clark0d225392018-06-07 09:59:07 -0400998Definition* IncludeWriter::findMethod(string name, RootDefinition* root) const {
999 if (root) {
1000 return root->find(name, RootDefinition::AllowParens::kNo);
1001 }
1002 auto methodIter = fBmhParser->fMethodMap.find(name);
1003 if (fBmhParser->fMethodMap.end() == methodIter) {
1004 return nullptr;
1005 }
1006 return &methodIter->second;
1007}
1008
1009
1010void IncludeWriter::firstBlock(int size, const char* data) {
1011 SkAssertResult(this->firstBlockTrim(size, data));
1012}
1013
1014bool IncludeWriter::firstBlockTrim(int size, const char* data) {
1015 bool result = this->writeBlockTrim(size, data);
1016 if (fFirstWrite) {
1017 auto fileInfo = std::find_if(fRootTopic->fChildren.begin(), fRootTopic->fChildren.end(),
1018 [](const Definition* def){ return MarkType::kFile == def->fMarkType; } );
1019 if (fRootTopic->fChildren.end() != fileInfo) {
1020 this->writeCommentHeader();
1021 this->writeString("\\file");
1022 this->writeSpace();
1023 size_t lastSlash = fFileName.rfind('/');
1024 if (string::npos == lastSlash) {
1025 lastSlash = fFileName.rfind('\\');
1026 }
1027 string fileName = fFileName.substr(lastSlash + 1);
1028 this->writeString(fileName);
1029 this->lf(2);
1030 fIndent += 4;
1031 this->descriptionOut(*fileInfo, SkipFirstLine::kNo, Phrase::kNo);
1032 fIndent -= 4;
1033 this->writeCommentTrailer(OneLine::kNo);
1034 }
1035 fFirstWrite = false;
1036 }
1037 return result;
1038}
1039
1040void IncludeWriter::setStart(const char* start, const Definition* def) {
1041 SkASSERT(start >= fStart);
1042 this->setStartBack(start, def);
1043}
1044
1045void IncludeWriter::setStartBack(const char* start, const Definition* def) {
1046 fStartSetter = def;
1047 fStart = start;
1048}
1049
Cary Clarkbad5ad72017-08-03 17:14:08 -04001050Definition* IncludeWriter::structMemberOut(const Definition* memberStart, const Definition& child) {
Cary Clark0d225392018-06-07 09:59:07 -04001051 const char* blockStart = !fWroteMethod && fDeferComment ? fDeferComment->fContentEnd : fStart;
Cary Clarkd0530ba2017-09-14 11:25:39 -04001052 const char* blockEnd = fWroteMethod && fDeferComment ? fDeferComment->fStart - 1 :
1053 memberStart->fStart;
Cary Clark0d225392018-06-07 09:59:07 -04001054 this->firstBlockTrim((int) (blockEnd - blockStart), blockStart);
1055 this->indentDeferred(IndentKind::kStructMember);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001056 fWroteMethod = false;
Cary Clark8032b982017-07-28 11:04:54 -04001057 string name(child.fContentStart, (int) (child.fContentEnd - child.fContentStart));
Cary Clark2dc84ad2018-01-26 12:56:22 -05001058 Definition* commentBlock = this->findMemberCommentBlock(fBmhStructDef->fChildren, name);
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001059 if (!commentBlock) {
Cary Clark137b8742018-05-30 09:21:49 -04001060 return memberStart->reportError<Definition*>("member missing comment block 2");
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001061 }
Cary Clark137b8742018-05-30 09:21:49 -04001062 auto lineIter = std::find_if(commentBlock->fChildren.begin(), commentBlock->fChildren.end(),
1063 [](const Definition* def){ return MarkType::kLine == def->fMarkType; } );
1064 SkASSERT(commentBlock->fChildren.end() != lineIter);
1065 const Definition* lineDef = *lineIter;
1066 if (fStructMemberLength > 100) {
Cary Clark8032b982017-07-28 11:04:54 -04001067 this->writeCommentHeader();
Cary Clark137b8742018-05-30 09:21:49 -04001068 this->writeSpace();
1069 this->rewriteBlock(lineDef->length(), lineDef->fContentStart, Phrase::kYes);
1070 this->writeCommentTrailer(OneLine::kYes);
Cary Clark8032b982017-07-28 11:04:54 -04001071 }
1072 this->lfcr();
Cary Clarkbad5ad72017-08-03 17:14:08 -04001073 this->writeBlock((int) (child.fStart - memberStart->fContentStart),
Cary Clark8032b982017-07-28 11:04:54 -04001074 memberStart->fContentStart);
1075 this->indentToColumn(fStructMemberTab);
1076 this->writeString(name.c_str());
Cary Clark6fc50412017-09-21 12:31:06 -04001077 auto tokenIter = child.fParent->fTokens.begin();
Cary Clarkbad5ad72017-08-03 17:14:08 -04001078 std::advance(tokenIter, child.fParentIndex + 1);
1079 Definition* valueStart = &*tokenIter;
1080 while (Definition::Type::kPunctuation != tokenIter->fType) {
1081 std::advance(tokenIter, 1);
1082 SkASSERT(child.fParent->fTokens.end() != tokenIter);
1083 }
1084 Definition* valueEnd = &*tokenIter;
1085 if (valueStart != valueEnd) {
1086 this->indentToColumn(fStructValueTab);
1087 this->writeString("=");
1088 this->writeSpace();
1089 this->writeBlock((int) (valueEnd->fStart - valueStart->fContentStart),
Ben Wagner63fd7602017-10-09 15:45:33 -04001090 valueStart->fContentStart);
Cary Clarkbad5ad72017-08-03 17:14:08 -04001091 }
Cary Clark8032b982017-07-28 11:04:54 -04001092 this->writeString(";");
Cary Clark137b8742018-05-30 09:21:49 -04001093 if (fStructMemberLength <= 100) {
Cary Clark8032b982017-07-28 11:04:54 -04001094 this->indentToColumn(fStructCommentTab);
1095 this->writeString("//!<");
1096 this->writeSpace();
Cary Clark137b8742018-05-30 09:21:49 -04001097 this->rewriteBlock(lineDef->length(), lineDef->fContentStart, Phrase::kYes);
Cary Clark8032b982017-07-28 11:04:54 -04001098 }
Cary Clark137b8742018-05-30 09:21:49 -04001099 this->lf(1);
Cary Clarkbad5ad72017-08-03 17:14:08 -04001100 return valueEnd;
Cary Clark8032b982017-07-28 11:04:54 -04001101}
1102
Cary Clark137b8742018-05-30 09:21:49 -04001103// const and constexpr and #define aren't contained in a braces like struct and enum.
1104// use a bmh subtopic to group like ones together, then measure them in the include as if
1105// they were formally linked together
1106void IncludeWriter::constSizeMembers(const RootDefinition* root) {
1107 // fBmhConst->fParent is subtopic containing all grouped const expressions
1108 // fConstDef is token of const include name, hopefully on same line as const start
1109 string rootPrefix = root ? root->fName + "::" : "";
1110 const Definition* test = fConstDef;
1111 int tokenIndex = test->fParentIndex;
1112 int longestName = 0;
1113 int longestValue = 0;
1114 int longestComment = 0;
1115 const Definition* subtopic = fBmhConst->fParent;
1116 SkASSERT(subtopic);
1117 SkASSERT(MarkType::kSubtopic == subtopic->fMarkType);
1118 // back up to first token on line
1119 size_t lineCount = test->fLineCount;
1120 const Definition* last;
1121 auto tokenIter = test->fParent->fTokens.begin();
1122 std::advance(tokenIter, tokenIndex);
1123 do {
1124 last = test;
1125 std::advance(tokenIter, -1);
1126 test = &*tokenIter;
1127 SkASSERT(test->fParentIndex == --tokenIndex);
1128 } while (lineCount == test->fLineCount);
1129 test = last;
1130 for (auto child : subtopic->fChildren) {
1131 if (MarkType::kConst != child->fMarkType) {
1132 continue;
1133 }
1134 // expect found name to be on the left of assign
1135 // expect assign
1136 // expect semicolon
1137 // no parens, no braces
1138 while (rootPrefix + test->fName != child->fName) {
1139 std::advance(tokenIter, 1);
1140 test = &*tokenIter;
1141 SkASSERT(lineCount >= test->fLineCount);
1142 }
1143 ++lineCount;
1144 TextParser constText(test);
1145 const char* nameEnd = constText.trimmedBracketEnd('=');
1146 SkAssertResult(constText.skipToEndBracket('='));
1147 const char* valueEnd = constText.trimmedBracketEnd(';');
1148 auto lineIter = std::find_if(child->fChildren.begin(), child->fChildren.end(),
1149 [](const Definition* def){ return MarkType::kLine == def->fMarkType; });
1150 SkASSERT(child->fChildren.end() != lineIter);
1151 longestName = SkTMax(longestName, (int) (nameEnd - constText.fStart));
1152 longestValue = SkTMax(longestValue, (int) (valueEnd - constText.fChar));
1153 longestComment = SkTMax(longestComment, (*lineIter)->length());
1154 }
1155 // write fStructValueTab, fStructCommentTab
1156 fConstValueTab = longestName + fIndent + 1;
1157 fConstCommentTab = fConstValueTab + longestValue + 2;
1158 fConstLength = fConstCommentTab + longestComment + (int) sizeof("//!<");
1159}
1160
Cary Clarkb94f6da2018-06-08 11:54:32 -04001161bool IncludeWriter::defineOut(const Definition& def) {
1162 if (def.fTokens.size() < 1) {
1163 return false;
1164 }
1165 auto& child = def.fTokens.front();
1166 string name(child.fContentStart, child.length());
1167 auto defIter = fBmhParser->fDefineMap.find(name);
1168 if (fBmhParser->fDefineMap.end() == defIter) {
1169 return false;
1170 }
1171 const Definition& bmhDef = defIter->second;
1172 this->constOut(&def, &bmhDef);
Cary Clarkb94f6da2018-06-08 11:54:32 -04001173 return true;
1174}
1175
Cary Clark884dd7d2017-10-11 10:37:52 -04001176void IncludeWriter::structSizeMembers(const Definition& child) {
Cary Clark8032b982017-07-28 11:04:54 -04001177 int longestType = 0;
1178 Definition* typeStart = nullptr;
1179 int longestName = 0;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001180 int longestValue = 0;
Cary Clark137b8742018-05-30 09:21:49 -04001181 int longestComment = 0;
Cary Clark8032b982017-07-28 11:04:54 -04001182 SkASSERT(child.fChildren.size() == 1 || child.fChildren.size() == 2);
1183 bool inEnum = false;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001184 bool inMethod = false;
1185 bool inMember = false;
Cary Clark8032b982017-07-28 11:04:54 -04001186 auto brace = child.fChildren[0];
1187 SkASSERT(Bracket::kBrace == brace->fBracket);
1188 for (auto& token : brace->fTokens) {
1189 if (Definition::Type::kBracket == token.fType) {
1190 if (Bracket::kSlashSlash == token.fBracket) {
1191 continue; // ignore old inline comments
1192 }
1193 if (Bracket::kSlashStar == token.fBracket) {
1194 continue; // ignore old inline comments
1195 }
1196 if (Bracket::kParen == token.fBracket) {
Cary Clarkbad5ad72017-08-03 17:14:08 -04001197 if (inMethod) {
1198 continue;
1199 }
Cary Clark8032b982017-07-28 11:04:54 -04001200 break;
1201 }
1202 SkASSERT(0); // incomplete
1203 }
1204 if (Definition::Type::kKeyWord == token.fType) {
1205 switch (token.fKeyWord) {
1206 case KeyWord::kEnum:
1207 inEnum = true;
1208 break;
1209 case KeyWord::kConst:
1210 case KeyWord::kConstExpr:
1211 case KeyWord::kStatic:
1212 case KeyWord::kInt:
Cary Clarkd0530ba2017-09-14 11:25:39 -04001213 case KeyWord::kUint8_t:
1214 case KeyWord::kUint16_t:
Cary Clark8032b982017-07-28 11:04:54 -04001215 case KeyWord::kUint32_t:
Cary Clarkd0530ba2017-09-14 11:25:39 -04001216 case KeyWord::kUint64_t:
Cary Clark4dc5a452018-05-21 11:56:57 -04001217 case KeyWord::kUintPtr_t:
Cary Clark8032b982017-07-28 11:04:54 -04001218 case KeyWord::kSize_t:
1219 case KeyWord::kFloat:
1220 case KeyWord::kBool:
Cary Clarkd2ca79c2018-08-10 13:09:13 -04001221 case KeyWord::kChar:
Cary Clark8032b982017-07-28 11:04:54 -04001222 case KeyWord::kVoid:
1223 if (!typeStart) {
1224 typeStart = &token;
1225 }
1226 break;
1227 default:
1228 break;
1229 }
1230 continue;
1231 }
1232 if (Definition::Type::kPunctuation == token.fType) {
1233 if (inEnum) {
1234 SkASSERT(Punctuation::kSemicolon == token.fPunctuation);
1235 inEnum = false;
1236 }
Cary Clarkbad5ad72017-08-03 17:14:08 -04001237 if (inMethod) {
1238 if (Punctuation::kColon == token.fPunctuation) {
1239 inMethod = false;
1240 } else if (Punctuation::kLeftBrace == token.fPunctuation) {
1241 inMethod = false;
Cary Clark73fa9722017-08-29 17:36:51 -04001242 } else if (Punctuation::kSemicolon == token.fPunctuation) {
1243 inMethod = false;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001244 } else {
1245 SkASSERT(0); // incomplete
1246 }
1247 }
1248 if (inMember) {
1249 SkASSERT(Punctuation::kSemicolon == token.fPunctuation);
1250 typeStart = nullptr;
1251 inMember = false;
1252 }
Cary Clark8032b982017-07-28 11:04:54 -04001253 continue;
1254 }
1255 if (Definition::Type::kWord != token.fType) {
1256 SkASSERT(0); // incomplete
1257 }
1258 if (MarkType::kMember == token.fMarkType) {
1259 TextParser typeStr(token.fFileName, typeStart->fContentStart, token.fContentStart,
1260 token.fLineCount);
1261 typeStr.trimEnd();
Cary Clarkbad5ad72017-08-03 17:14:08 -04001262 longestType = SkTMax(longestType, (int) (typeStr.fEnd - typeStr.fStart));
Cary Clark8032b982017-07-28 11:04:54 -04001263 longestName = SkTMax(longestName, (int) (token.fContentEnd - token.fContentStart));
1264 typeStart->fMemberStart = true;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001265 inMember = true;
Cary Clark137b8742018-05-30 09:21:49 -04001266 string tokenName(token.fContentStart, (int) (token.fContentEnd - token.fContentStart));
1267 Definition* commentBlock = this->findMemberCommentBlock(fBmhStructDef->fChildren,
1268 tokenName);
1269 if (!commentBlock) {
1270 return token.reportError<void>("member missing comment block 1");
1271 }
1272 auto lineIter = std::find_if(commentBlock->fChildren.begin(),
1273 commentBlock->fChildren.end(),
1274 [](const Definition* def){ return MarkType::kLine == def->fMarkType; } );
1275 SkASSERT(commentBlock->fChildren.end() != lineIter);
1276 const Definition* lineDef = *lineIter;
1277 longestComment = SkTMax(longestComment, lineDef->length());
Cary Clarkbad5ad72017-08-03 17:14:08 -04001278 continue;
1279 }
1280 if (MarkType::kMethod == token.fMarkType) {
1281 inMethod = true;
Cary Clark8032b982017-07-28 11:04:54 -04001282 continue;
1283 }
1284 SkASSERT(MarkType::kNone == token.fMarkType);
Cary Clarkbad5ad72017-08-03 17:14:08 -04001285 if (typeStart) {
1286 if (inMember) {
1287 longestValue =
1288 SkTMax(longestValue, (int) (token.fContentEnd - token.fContentStart));
1289 }
1290 } else {
Cary Clark8032b982017-07-28 11:04:54 -04001291 typeStart = &token;
1292 }
1293 }
1294 fStructMemberTab = longestType + fIndent + 1 /* space before name */ ;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001295 fStructValueTab = fStructMemberTab + longestName + 2 /* space ; */ ;
1296 fStructCommentTab = fStructValueTab;
1297 if (longestValue) {
1298 fStructCommentTab += longestValue + 3 /* space = space */ ;
1299 fStructValueTab -= 1 /* ; */ ;
1300 }
Cary Clark137b8742018-05-30 09:21:49 -04001301 fStructMemberLength = fStructCommentTab + longestComment;
Cary Clark682c58d2018-05-16 07:07:07 -04001302 // iterate through struct to ensure that members' comments fit on line
1303 // struct or class may not have any members
1304 (void) this->checkChildCommentLength(fBmhStructDef, MarkType::kMember);
Cary Clark8032b982017-07-28 11:04:54 -04001305}
1306
Cary Clark154beea2017-10-26 07:58:48 -04001307static bool find_start(const Definition* startDef, const char* start) {
1308 for (const auto& child : startDef->fTokens) {
1309 if (child.fContentStart == start) {
1310 return MarkType::kMethod == child.fMarkType;
1311 }
1312 if (child.fContentStart >= start) {
1313 break;
1314 }
1315 if (find_start(&child, start)) {
1316 return true;
1317 }
1318 }
1319 return false;
1320}
1321
Cary Clark73fa9722017-08-29 17:36:51 -04001322bool IncludeWriter::populate(Definition* def, ParentPair* prevPair, RootDefinition* root) {
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001323 if (!def->fTokens.size()) {
1324 return true;
1325 }
Cary Clark73fa9722017-08-29 17:36:51 -04001326 ParentPair pair = { def, prevPair };
Cary Clark8032b982017-07-28 11:04:54 -04001327 // write bulk of original include up to class, method, enum, etc., excepting preceding comment
1328 // find associated bmh object
1329 // write any associated comments in Doxygen form
1330 // skip include comment
1331 // if there is a series of same named methods, write one set of comments, then write all methods
1332 string methodName;
Cary Clark682c58d2018-05-16 07:07:07 -04001333 Definition* method = nullptr;
1334 Definition* clonedMethod = nullptr;
Cary Clark8032b982017-07-28 11:04:54 -04001335 const Definition* memberStart = nullptr;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001336 const Definition* memberEnd = nullptr;
Cary Clark8032b982017-07-28 11:04:54 -04001337 fContinuation = nullptr;
1338 bool inStruct = false;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001339 bool inConstructor = false;
Cary Clark884dd7d2017-10-11 10:37:52 -04001340 bool inInline = false;
Cary Clark154beea2017-10-26 07:58:48 -04001341 bool eatOperator = false;
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001342 bool sawConst = false;
1343 bool staticOnly = false;
Cary Clark0d225392018-06-07 09:59:07 -04001344 bool sawTypedef = false;
Cary Clark27dddae2018-06-08 15:57:37 -04001345 Definition* deferredTypedefComment = nullptr;
Cary Clark154beea2017-10-26 07:58:48 -04001346 const Definition* requireDense = nullptr;
1347 const Definition* startDef = nullptr;
Cary Clark8032b982017-07-28 11:04:54 -04001348 for (auto& child : def->fTokens) {
Cary Clark0d225392018-06-07 09:59:07 -04001349 if (KeyWord::kInline == child.fKeyWord) {
1350 continue;
1351 }
Cary Clark154beea2017-10-26 07:58:48 -04001352 if (KeyWord::kOperator == child.fKeyWord && method &&
1353 Definition::MethodType::kOperator == method->fMethodType) {
1354 eatOperator = true;
1355 continue;
1356 }
1357 if (eatOperator) {
1358 if (Bracket::kSquare == child.fBracket || Bracket::kParen == child.fBracket) {
1359 continue;
1360 }
1361 eatOperator = false;
1362 fContinuation = nullptr;
1363 if (KeyWord::kConst == child.fKeyWord) {
1364 continue;
1365 }
1366 }
Cary Clarkbad5ad72017-08-03 17:14:08 -04001367 if (memberEnd) {
1368 if (memberEnd != &child) {
1369 continue;
1370 }
Cary Clark154beea2017-10-26 07:58:48 -04001371 startDef = &child;
Cary Clark0d225392018-06-07 09:59:07 -04001372 this->setStart(child.fContentStart + 1, &child);
Cary Clarkbad5ad72017-08-03 17:14:08 -04001373 memberEnd = nullptr;
1374 }
Cary Clark8032b982017-07-28 11:04:54 -04001375 if (child.fPrivate) {
Cary Clark884dd7d2017-10-11 10:37:52 -04001376 if (MarkType::kMethod == child.fMarkType) {
1377 inInline = true;
1378 }
Cary Clark8032b982017-07-28 11:04:54 -04001379 continue;
1380 }
Cary Clark884dd7d2017-10-11 10:37:52 -04001381 if (inInline) {
1382 if (Definition::Type::kKeyWord == child.fType) {
1383 SkASSERT(MarkType::kMethod != child.fMarkType);
1384 continue;
1385 }
1386 if (Definition::Type::kPunctuation == child.fType) {
1387 if (Punctuation::kLeftBrace == child.fPunctuation) {
1388 inInline = false;
1389 } else {
1390 SkASSERT(Punctuation::kAsterisk == child.fPunctuation);
1391 }
1392 continue;
1393 }
1394 if (Definition::Type::kWord == child.fType) {
1395 string name(child.fContentStart, child.fContentEnd - child.fContentStart);
1396 SkASSERT(string::npos != name.find("::"));
1397 continue;
1398 }
1399 if (Definition::Type::kBracket == child.fType) {
1400 SkASSERT(Bracket::kParen == child.fBracket);
1401 continue;
1402 }
1403 }
Cary Clark8032b982017-07-28 11:04:54 -04001404 if (fContinuation) {
1405 if (Definition::Type::kKeyWord == child.fType) {
Cary Clarkd0530ba2017-09-14 11:25:39 -04001406 if (KeyWord::kFriend == child.fKeyWord ||
Cary Clark73fa9722017-08-29 17:36:51 -04001407 KeyWord::kSK_API == child.fKeyWord) {
Cary Clark8032b982017-07-28 11:04:54 -04001408 continue;
1409 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001410 const IncludeKey& includeKey = kKeyWords[(int) child.fKeyWord];
1411 if (KeyProperty::kNumber == includeKey.fProperty) {
1412 continue;
1413 }
Cary Clark8032b982017-07-28 11:04:54 -04001414 }
Cary Clark3cd22cc2017-12-01 11:49:58 -05001415 if (Definition::Type::kBracket == child.fType) {
1416 if (Bracket::kAngle == child.fBracket) {
Cary Clark8032b982017-07-28 11:04:54 -04001417 continue;
1418 }
Cary Clark3cd22cc2017-12-01 11:49:58 -05001419 if (Bracket::kParen == child.fBracket) {
1420 if (!clonedMethod) {
1421 if (inConstructor) {
1422 fContinuation = child.fContentStart;
1423 }
1424 continue;
Cary Clark8032b982017-07-28 11:04:54 -04001425 }
Cary Clark3cd22cc2017-12-01 11:49:58 -05001426 int alternate = 1;
1427 ptrdiff_t childLen = child.fContentEnd - child.fContentStart;
1428 SkASSERT(')' == child.fContentStart[childLen]);
1429 ++childLen;
1430 do {
1431 TextParser params(clonedMethod->fFileName, clonedMethod->fStart,
1432 clonedMethod->fContentStart, clonedMethod->fLineCount);
1433 params.skipToEndBracket('(');
1434 if (params.startsWith(child.fContentStart, childLen)) {
1435 this->methodOut(clonedMethod, child);
Cary Clark137b8742018-05-30 09:21:49 -04001436 sawConst = false;
Cary Clark3cd22cc2017-12-01 11:49:58 -05001437 break;
1438 }
1439 ++alternate;
1440 string alternateMethod = methodName + '_' + to_string(alternate);
Cary Clark224c7002018-06-27 11:00:21 -04001441 clonedMethod = this->findMethod(alternateMethod, root);
Cary Clark3cd22cc2017-12-01 11:49:58 -05001442 } while (clonedMethod);
1443 if (!clonedMethod) {
Cary Clark0d225392018-06-07 09:59:07 -04001444 return child.reportError<bool>("cloned method not found");
Cary Clark3cd22cc2017-12-01 11:49:58 -05001445 }
1446 clonedMethod = nullptr;
1447 continue;
Cary Clark8032b982017-07-28 11:04:54 -04001448 }
Cary Clark8032b982017-07-28 11:04:54 -04001449 }
1450 if (Definition::Type::kWord == child.fType) {
1451 if (clonedMethod) {
1452 continue;
1453 }
1454 size_t len = (size_t) (child.fContentEnd - child.fContentStart);
1455 const char operatorStr[] = "operator";
1456 size_t operatorLen = sizeof(operatorStr) - 1;
1457 if (len >= operatorLen && !strncmp(child.fContentStart, operatorStr, operatorLen)) {
1458 fContinuation = child.fContentEnd;
1459 continue;
1460 }
1461 }
1462 if (Definition::Type::kPunctuation == child.fType &&
1463 (Punctuation::kSemicolon == child.fPunctuation ||
Cary Clark6fc50412017-09-21 12:31:06 -04001464 Punctuation::kLeftBrace == child.fPunctuation ||
1465 (Punctuation::kColon == child.fPunctuation && inConstructor))) {
Cary Clark8032b982017-07-28 11:04:54 -04001466 SkASSERT(fContinuation[0] == '(');
1467 const char* continueEnd = child.fContentStart;
1468 while (continueEnd > fContinuation && isspace(continueEnd[-1])) {
1469 --continueEnd;
1470 }
Cary Clarkab5c9af2018-07-12 16:24:53 -04001471 const char defaultTag[] = " = default";
1472 size_t tagSize = sizeof(defaultTag) - 1;
1473 const char* tokenEnd = continueEnd - tagSize;
1474 if (tokenEnd <= fContinuation || strncmp(tokenEnd, defaultTag, tagSize)) {
1475 tokenEnd = continueEnd;
1476 }
1477 methodName += string(fContinuation, tokenEnd - fContinuation);
Cary Clark82f1f742018-06-28 08:50:35 -04001478 if (string::npos != methodName.find('\n')) {
1479 methodName.erase(std::remove(methodName.begin(), methodName.end(), '\n'),
1480 methodName.end());
1481 }
Cary Clark0d225392018-06-07 09:59:07 -04001482 method = this->findMethod(methodName, root);
Cary Clark8032b982017-07-28 11:04:54 -04001483 if (!method) {
Cary Clark56356312018-02-08 14:45:18 -05001484 if (fBmhStructDef && fBmhStructDef->fDeprecated) {
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001485 fContinuation = nullptr;
Cary Clark56356312018-02-08 14:45:18 -05001486 continue;
1487 }
Cary Clark06c20f32018-03-20 15:53:27 -04001488 return child.reportError<bool>("method not found");
Cary Clark8032b982017-07-28 11:04:54 -04001489 }
Cary Clark579985c2017-07-31 11:48:27 -04001490 this->methodOut(method, child);
Cary Clark137b8742018-05-30 09:21:49 -04001491 sawConst = false;
Cary Clark8032b982017-07-28 11:04:54 -04001492 continue;
1493 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001494 if (Definition::Type::kPunctuation == child.fType &&
1495 Punctuation::kAsterisk == child.fPunctuation &&
1496 clonedMethod) {
1497 continue;
1498 }
Cary Clarkbad5ad72017-08-03 17:14:08 -04001499 if (inConstructor) {
1500 continue;
1501 }
Cary Clark0d225392018-06-07 09:59:07 -04001502 method = this->findMethod(methodName + "()", root);
Cary Clark8032b982017-07-28 11:04:54 -04001503 if (method) {
Cary Clark6fc50412017-09-21 12:31:06 -04001504 if (method->fCloned) {
1505 clonedMethod = method;
1506 continue;
1507 }
Cary Clark579985c2017-07-31 11:48:27 -04001508 this->methodOut(method, child);
Cary Clark137b8742018-05-30 09:21:49 -04001509 sawConst = false;
Cary Clark8032b982017-07-28 11:04:54 -04001510 continue;
Cary Clark56356312018-02-08 14:45:18 -05001511 } else if (fBmhStructDef && fBmhStructDef->fDeprecated) {
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001512 fContinuation = nullptr;
1513 continue;
Cary Clark8032b982017-07-28 11:04:54 -04001514 }
Cary Clark06c20f32018-03-20 15:53:27 -04001515 return child.reportError<bool>("method not found");
Cary Clark8032b982017-07-28 11:04:54 -04001516 }
1517 if (Bracket::kSlashSlash == child.fBracket || Bracket::kSlashStar == child.fBracket) {
1518 if (!fDeferComment) {
1519 fDeferComment = &child;
1520 }
1521 continue;
Ben Wagner63fd7602017-10-09 15:45:33 -04001522 }
Cary Clark8032b982017-07-28 11:04:54 -04001523 if (MarkType::kMethod == child.fMarkType) {
Cary Clark2d4bf5f2018-04-16 08:37:38 -04001524 if (this->isInternalName(child)) {
Cary Clarkbad5ad72017-08-03 17:14:08 -04001525 continue;
1526 }
Cary Clark8032b982017-07-28 11:04:54 -04001527 const char* bodyEnd = fDeferComment ? fDeferComment->fContentStart - 1 :
Cary Clark154beea2017-10-26 07:58:48 -04001528 fAttrDeprecated ? fAttrDeprecated->fContentStart - 1 :
Cary Clark8032b982017-07-28 11:04:54 -04001529 child.fContentStart;
Cary Clark7cfcbca2018-01-04 16:11:51 -05001530 if (Definition::Type::kBracket == def->fType && Bracket::kDebugCode == def->fBracket) {
1531 auto tokenIter = def->fParent->fTokens.begin();
1532 std::advance(tokenIter, def->fParentIndex - 1);
1533 Definition* prior = &*tokenIter;
1534 if (Definition::Type::kBracket == def->fType &&
1535 Bracket::kSlashStar == prior->fBracket) {
1536 bodyEnd = prior->fContentStart - 1;
1537 }
1538 }
Cary Clark8032b982017-07-28 11:04:54 -04001539 // FIXME: roll end-trimming into writeBlockTrim call
1540 while (fStart < bodyEnd && ' ' >= bodyEnd[-1]) {
1541 --bodyEnd;
1542 }
1543 int blockSize = (int) (bodyEnd - fStart);
Cary Clark2d4bf5f2018-04-16 08:37:38 -04001544 SkASSERT(blockSize >= 0);
Cary Clark8032b982017-07-28 11:04:54 -04001545 if (blockSize) {
Cary Clark7cfcbca2018-01-04 16:11:51 -05001546 string debugstr(fStart, blockSize);
Cary Clark8032b982017-07-28 11:04:54 -04001547 this->writeBlock(blockSize, fStart);
1548 }
Cary Clark154beea2017-10-26 07:58:48 -04001549 startDef = &child;
Cary Clark0d225392018-06-07 09:59:07 -04001550 this->setStart(child.fContentStart, &child);
Cary Clark06c20f32018-03-20 15:53:27 -04001551 auto mapFind = fBmhParser->fMethodMap.find(child.fName);
1552 if (fBmhParser->fMethodMap.end() != mapFind) {
1553 inConstructor = false;
1554 method = &mapFind->second;
Cary Clark0d225392018-06-07 09:59:07 -04001555 methodName = child.fName;
Cary Clark06c20f32018-03-20 15:53:27 -04001556 } else {
1557 methodName = root->fName + "::" + child.fName;
Cary Clark224c7002018-06-27 11:00:21 -04001558 size_t lastName = root->fName.rfind(':');
1559 lastName = string::npos == lastName ? 0 : lastName + 1;
1560 inConstructor = root->fName.substr(lastName) == child.fName;
Cary Clark06c20f32018-03-20 15:53:27 -04001561 method = root->find(methodName, RootDefinition::AllowParens::kNo);
1562 }
Cary Clark8032b982017-07-28 11:04:54 -04001563 fContinuation = child.fContentEnd;
Cary Clark8032b982017-07-28 11:04:54 -04001564 if (!method) {
1565 continue;
1566 }
1567 if (method->fCloned) {
1568 clonedMethod = method;
1569 continue;
1570 }
Cary Clark579985c2017-07-31 11:48:27 -04001571 this->methodOut(method, child);
Cary Clark137b8742018-05-30 09:21:49 -04001572 sawConst = false;
Cary Clark73fa9722017-08-29 17:36:51 -04001573 if (fAttrDeprecated) {
Cary Clark154beea2017-10-26 07:58:48 -04001574 startDef = fAttrDeprecated;
Cary Clark0d225392018-06-07 09:59:07 -04001575 this->setStartBack(fAttrDeprecated->fContentStart, fAttrDeprecated);
Cary Clark73fa9722017-08-29 17:36:51 -04001576 fAttrDeprecated = nullptr;
1577 }
Cary Clark8032b982017-07-28 11:04:54 -04001578 continue;
Ben Wagner63fd7602017-10-09 15:45:33 -04001579 }
Cary Clark8032b982017-07-28 11:04:54 -04001580 if (Definition::Type::kKeyWord == child.fType) {
Cary Clark8032b982017-07-28 11:04:54 -04001581 switch (child.fKeyWord) {
1582 case KeyWord::kStruct:
Cary Clark73fa9722017-08-29 17:36:51 -04001583 case KeyWord::kClass:
Cary Clark0d225392018-06-07 09:59:07 -04001584 fICSStack.push_back(&child);
1585 fStructEnded = false;
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001586 fStructMemberTab = 0;
Cary Clark8032b982017-07-28 11:04:54 -04001587 // if struct contains members, compute their name and comment tabs
Cary Clark73fa9722017-08-29 17:36:51 -04001588 if (child.fChildren.size() > 0) {
1589 const ParentPair* testPair = &pair;
1590 while ((testPair = testPair->fPrev)) {
1591 if (KeyWord::kClass == testPair->fParent->fKeyWord) {
1592 inStruct = fInStruct = true;
1593 break;
1594 }
1595 }
1596 }
Cary Clark8032b982017-07-28 11:04:54 -04001597 if (fInStruct) {
Cary Clark154beea2017-10-26 07:58:48 -04001598 // try child; root+child; root->parent+child; etc.
1599 int trial = 0;
Cary Clark682c58d2018-05-16 07:07:07 -04001600 RootDefinition* search = root;
1601 Definition* parent = search->fParent;
Cary Clark154beea2017-10-26 07:58:48 -04001602 do {
1603 string name;
1604 if (0 == trial) {
1605 name = child.fName;
1606 } else if (1 == trial) {
1607 name = root->fName + "::" + child.fName;
Cary Clark137b8742018-05-30 09:21:49 -04001608 } else if (2 == trial) {
1609 name = root->fName;
Cary Clark154beea2017-10-26 07:58:48 -04001610 } else {
1611 SkASSERT(parent);
1612 name = parent->fName + "::" + child.fName;
1613 search = parent->asRoot();
1614 parent = search->fParent;
1615 }
1616 fBmhStructDef = search->find(name, RootDefinition::AllowParens::kNo);
1617 } while (!fBmhStructDef && ++trial);
Cary Clark682c58d2018-05-16 07:07:07 -04001618 root = fBmhStructDef->asRoot();
Cary Clark154beea2017-10-26 07:58:48 -04001619 SkASSERT(root);
Cary Clark8032b982017-07-28 11:04:54 -04001620 fIndent += 4;
Cary Clark8032b982017-07-28 11:04:54 -04001621 this->structSizeMembers(child);
1622 fIndent -= 4;
Cary Clark154beea2017-10-26 07:58:48 -04001623 SkASSERT(!fIndentNext);
1624 fIndentNext = true;
Cary Clark8032b982017-07-28 11:04:54 -04001625 }
Cary Clark8032b982017-07-28 11:04:54 -04001626 if (child.fChildren.size() > 0) {
1627 const char* bodyEnd = fDeferComment ? fDeferComment->fContentStart - 1 :
1628 child.fContentStart;
Cary Clark73fa9722017-08-29 17:36:51 -04001629 this->writeBlockTrim((int) (bodyEnd - fStart), fStart);
Cary Clark154beea2017-10-26 07:58:48 -04001630 if (fPendingMethod) {
Cary Clark27dddae2018-06-08 15:57:37 -04001631 if (fIndent >= 4) {
1632 this->indentOut();
1633 }
Cary Clark154beea2017-10-26 07:58:48 -04001634 fPendingMethod = false;
1635 }
1636 startDef = requireDense ? requireDense : &child;
Cary Clark0d225392018-06-07 09:59:07 -04001637 if (requireDense) {
1638 startDef = requireDense;
1639 this->setStart(requireDense->fContentStart, requireDense);
1640 } else {
1641 startDef = &child;
1642 this->setStart(child.fContentStart, &child);
1643 }
Cary Clark154beea2017-10-26 07:58:48 -04001644 requireDense = nullptr;
Cary Clark0d225392018-06-07 09:59:07 -04001645 if (!fInStruct && (!root || child.fName != root->fName)) {
Cary Clark154beea2017-10-26 07:58:48 -04001646 root = &fBmhParser->fClassMap[child.fName];
1647 fRootTopic = root->fParent;
1648 SkASSERT(!root->fVisited);
1649 root->clearVisited();
Cary Clark0d225392018-06-07 09:59:07 -04001650#if 0
1651 // this seems better balanced; but real problem is probably fInStruct
1652 if (fIndentStack.size() > 0) {
1653 this->indentOut();
1654 }
1655 SkASSERT(!fIndent);
1656#else
Cary Clark154beea2017-10-26 07:58:48 -04001657 fIndent = 0;
Cary Clark0d225392018-06-07 09:59:07 -04001658#endif
Cary Clark154beea2017-10-26 07:58:48 -04001659 fBmhStructDef = root;
1660 }
Cary Clark8032b982017-07-28 11:04:54 -04001661 if (child.fName == root->fName) {
1662 if (Definition* parent = root->fParent) {
1663 if (MarkType::kTopic == parent->fMarkType ||
1664 MarkType::kSubtopic == parent->fMarkType) {
Cary Clarke4aa3712017-09-15 02:56:12 -04001665 const char* commentStart = root->fContentStart;
1666 const char* commentEnd = root->fChildren[0]->fStart;
Cary Clark8032b982017-07-28 11:04:54 -04001667 this->structOut(root, *root, commentStart, commentEnd);
1668 } else {
1669 SkASSERT(0); // incomplete
1670 }
1671 } else {
1672 SkASSERT(0); // incomplete
1673 }
1674 } else {
Cary Clark154beea2017-10-26 07:58:48 -04001675 SkASSERT(fInStruct);
Cary Clark137b8742018-05-30 09:21:49 -04001676 Definition* priorBlock = fBmhStructDef;
Cary Clark8032b982017-07-28 11:04:54 -04001677 Definition* codeBlock = nullptr;
Cary Clark154beea2017-10-26 07:58:48 -04001678 Definition* nextBlock = nullptr;
1679 for (auto test : fBmhStructDef->fChildren) {
Cary Clark8032b982017-07-28 11:04:54 -04001680 if (MarkType::kCode == test->fMarkType) {
Cary Clark137b8742018-05-30 09:21:49 -04001681 SkASSERT(!codeBlock); // FIXME: check enum earlier
Cary Clark8032b982017-07-28 11:04:54 -04001682 codeBlock = test;
1683 continue;
1684 }
1685 if (codeBlock) {
Cary Clark154beea2017-10-26 07:58:48 -04001686 nextBlock = test;
Cary Clark8032b982017-07-28 11:04:54 -04001687 break;
1688 }
Cary Clark137b8742018-05-30 09:21:49 -04001689 priorBlock = test;
Cary Clark8032b982017-07-28 11:04:54 -04001690 }
Cary Clark137b8742018-05-30 09:21:49 -04001691 // FIXME: trigger error earlier if inner #Struct or #Class is missing #Code
Cary Clark56356312018-02-08 14:45:18 -05001692 if (!fBmhStructDef->fDeprecated) {
1693 SkASSERT(codeBlock);
1694 SkASSERT(nextBlock); // FIXME: check enum for correct order earlier
Cary Clark78c110e2018-02-09 16:49:09 -05001695 const char* commentStart = codeBlock->fTerminator;
1696 const char* commentEnd = nextBlock->fStart;
Cary Clark137b8742018-05-30 09:21:49 -04001697 // FIXME: trigger error if #Code is present but comment is before it earlier
1698 SkASSERT(priorBlock); // code always preceded by #Line (I think)
1699 TextParser priorComment(priorBlock->fFileName,
1700 priorBlock->fTerminator, codeBlock->fStart,
1701 priorBlock->fLineCount);
1702 priorComment.trimEnd();
1703 if (!priorComment.eof()) {
1704 return priorBlock->reportError<bool>(
1705 "expect no comment before #Code");
1706 }
1707 TextParser nextComment(codeBlock->fFileName, commentStart,
1708 commentEnd, codeBlock->fLineCount);
1709 nextComment.trimEnd();
1710 if (!priorComment.eof()) {
1711 return priorBlock->reportError<bool>(
1712 "expect comment after #Code");
1713 }
1714 if (!nextComment.eof()) {
1715
1716 }
Cary Clark78c110e2018-02-09 16:49:09 -05001717 fIndentNext = true;
1718 this->structOut(root, *fBmhStructDef, commentStart, commentEnd);
Cary Clark56356312018-02-08 14:45:18 -05001719 }
Cary Clark8032b982017-07-28 11:04:54 -04001720 }
1721 fDeferComment = nullptr;
1722 } else {
Cary Clark0d225392018-06-07 09:59:07 -04001723 // empty forward reference
1724 bool writeTwo = '\n' == child.fContentStart[-1]
1725 && '\n' == child.fContentStart[-2];
1726 if (writeTwo) {
1727 const char* bodyEnd = fDeferComment ? fDeferComment->fContentStart - 1 :
1728 child.fContentStart;
1729 this->writeBlockTrim((int) (bodyEnd - fStart), fStart);
1730 this->lf(writeTwo ? 2 : 1);
1731 fIndent = 0;
1732 this->writeBlockTrim(child.length() + 1, child.fContentStart);
1733 writeTwo = '\n' == child.fContentEnd[1]
1734 && '\n' == child.fContentStart[2];
1735 this->lf(writeTwo ? 2 : 1);
1736 fStart = child.fContentEnd + 1;
1737 fDeferComment = nullptr;
1738 }
Cary Clark8032b982017-07-28 11:04:54 -04001739 }
1740 break;
1741 case KeyWord::kEnum: {
Cary Clarkbad5ad72017-08-03 17:14:08 -04001742 fInEnum = true;
Cary Clark8032b982017-07-28 11:04:54 -04001743 this->enumHeaderOut(root, child);
1744 this->enumSizeItems(child);
1745 } break;
1746 case KeyWord::kConst:
1747 case KeyWord::kConstExpr:
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001748 sawConst = !memberStart || staticOnly;
1749 if (!memberStart) {
1750 memberStart = &child;
1751 staticOnly = true;
1752 }
Cary Clark0d225392018-06-07 09:59:07 -04001753 if (MarkType::kConst == child.fMarkType) {
1754 auto constIter = fBmhParser->fConstMap.find(child.fName);
1755 if (fBmhParser->fConstMap.end() != constIter) {
1756 const RootDefinition& bmhConst = constIter->second;
1757 this->constOut(&child, &bmhConst);
1758 fDeferComment = nullptr;
1759 }
1760 }
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001761 break;
Cary Clark8032b982017-07-28 11:04:54 -04001762 case KeyWord::kStatic:
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001763 if (!memberStart) {
1764 memberStart = &child;
1765 staticOnly = true;
1766 }
1767 break;
Cary Clark8032b982017-07-28 11:04:54 -04001768 case KeyWord::kInt:
Cary Clarkd0530ba2017-09-14 11:25:39 -04001769 case KeyWord::kUint8_t:
1770 case KeyWord::kUint16_t:
Cary Clark8032b982017-07-28 11:04:54 -04001771 case KeyWord::kUint32_t:
Cary Clarkd0530ba2017-09-14 11:25:39 -04001772 case KeyWord::kUint64_t:
Cary Clark4dc5a452018-05-21 11:56:57 -04001773 case KeyWord::kUintPtr_t:
Cary Clark73fa9722017-08-29 17:36:51 -04001774 case KeyWord::kUnsigned:
Cary Clark8032b982017-07-28 11:04:54 -04001775 case KeyWord::kSize_t:
1776 case KeyWord::kFloat:
1777 case KeyWord::kBool:
Cary Clark56356312018-02-08 14:45:18 -05001778 case KeyWord::kChar:
Cary Clark8032b982017-07-28 11:04:54 -04001779 case KeyWord::kVoid:
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001780 staticOnly = false;
Cary Clark8032b982017-07-28 11:04:54 -04001781 if (!memberStart) {
1782 memberStart = &child;
1783 }
1784 break;
Cary Clarkd2ca79c2018-08-10 13:09:13 -04001785 case KeyWord::kAlignAs:
Cary Clark8032b982017-07-28 11:04:54 -04001786 case KeyWord::kPublic:
1787 case KeyWord::kPrivate:
1788 case KeyWord::kProtected:
1789 case KeyWord::kFriend:
Cary Clark73fa9722017-08-29 17:36:51 -04001790 case KeyWord::kInline:
1791 case KeyWord::kSK_API:
Cary Clarkbbfda252018-03-09 15:32:01 -05001792 case KeyWord::kTemplate:
Cary Clark0d225392018-06-07 09:59:07 -04001793 break;
Cary Clarkbad5ad72017-08-03 17:14:08 -04001794 case KeyWord::kTypedef:
Cary Clark0d225392018-06-07 09:59:07 -04001795 SkASSERT(!memberStart);
1796 memberStart = &child;
Cary Clark27dddae2018-06-08 15:57:37 -04001797 deferredTypedefComment = fDeferComment;
Cary Clark0d225392018-06-07 09:59:07 -04001798 sawTypedef = true;
Cary Clark8032b982017-07-28 11:04:54 -04001799 break;
Cary Clark154beea2017-10-26 07:58:48 -04001800 case KeyWord::kSK_BEGIN_REQUIRE_DENSE:
1801 requireDense = &child;
1802 break;
Cary Clark8032b982017-07-28 11:04:54 -04001803 default:
1804 SkASSERT(0);
1805 }
Cary Clarkd98f78c2018-04-26 08:32:37 -04001806 if (KeyWord::kUint8_t == child.fKeyWord || KeyWord::kUint32_t == child.fKeyWord) {
Cary Clarkd0530ba2017-09-14 11:25:39 -04001807 continue;
Cary Clark8032b982017-07-28 11:04:54 -04001808 } else {
Cary Clark87f71c02018-08-10 14:33:14 -04001809 if (fInEnum && child.fChildren.size() > 0
1810 && KeyWord::kClass == child.fChildren[0]->fKeyWord) {
Cary Clark73fa9722017-08-29 17:36:51 -04001811 if (!this->populate(child.fChildren[0], &pair, root)) {
Cary Clarkbad5ad72017-08-03 17:14:08 -04001812 return false;
1813 }
Cary Clark154beea2017-10-26 07:58:48 -04001814 } else {
1815 if (!this->populate(&child, &pair, root)) {
1816 return false;
1817 }
1818 if (KeyWord::kClass == child.fKeyWord || KeyWord::kStruct == child.fKeyWord) {
Cary Clark0d225392018-06-07 09:59:07 -04001819 fICSStack.pop_back();
1820 fStructEnded = true;
Cary Clark154beea2017-10-26 07:58:48 -04001821 if (fInStruct) {
1822 fInStruct = false;
1823 do {
1824 SkASSERT(root);
1825 root = const_cast<RootDefinition*>(root->fParent->asRoot());
1826 } while (MarkType::kTopic == root->fMarkType ||
1827 MarkType::kSubtopic == root->fMarkType);
Cary Clark0d225392018-06-07 09:59:07 -04001828#if 0
1829 }
1830 if (MarkType::kStruct == root->fMarkType ||
1831 MarkType::kClass == root->fMarkType) {
1832#else
Cary Clark154beea2017-10-26 07:58:48 -04001833 SkASSERT(MarkType::kStruct == root->fMarkType ||
Cary Clark0d225392018-06-07 09:59:07 -04001834 MarkType::kClass == root->fMarkType);
1835#endif
Cary Clark154beea2017-10-26 07:58:48 -04001836 fPendingMethod = false;
1837 if (startDef) {
1838 fPendingMethod = find_start(startDef, fStart);
1839 }
1840 fOutdentNext = !fPendingMethod;
1841 }
1842 }
Cary Clark8032b982017-07-28 11:04:54 -04001843 }
1844 }
1845 continue;
Ben Wagner63fd7602017-10-09 15:45:33 -04001846 }
Cary Clark8032b982017-07-28 11:04:54 -04001847 if (Definition::Type::kBracket == child.fType) {
Ben Wagner63fd7602017-10-09 15:45:33 -04001848 if (KeyWord::kEnum == child.fParent->fKeyWord ||
Cary Clarkbad5ad72017-08-03 17:14:08 -04001849 (KeyWord::kClass == child.fParent->fKeyWord && child.fParent->fParent &&
1850 KeyWord::kEnum == child.fParent->fParent->fKeyWord)) {
Cary Clarkd0530ba2017-09-14 11:25:39 -04001851 SkASSERT(Bracket::kBrace == child.fBracket);
Cary Clark682c58d2018-05-16 07:07:07 -04001852 this->enumMembersOut(*child.fParent);
Cary Clark8032b982017-07-28 11:04:54 -04001853 this->writeString("};");
1854 this->lf(2);
Cary Clark154beea2017-10-26 07:58:48 -04001855 startDef = child.fParent;
Cary Clark0d225392018-06-07 09:59:07 -04001856 this->setStart(child.fParent->fContentEnd, child.fParent);
Cary Clark8032b982017-07-28 11:04:54 -04001857 SkASSERT(';' == fStart[0]);
1858 ++fStart;
1859 fDeferComment = nullptr;
1860 fInEnum = false;
Cary Clark154beea2017-10-26 07:58:48 -04001861 if (fIndentNext) {
1862// fIndent -= 4;
1863 fIndentNext = false;
1864 }
Cary Clark8032b982017-07-28 11:04:54 -04001865 continue;
Cary Clark73fa9722017-08-29 17:36:51 -04001866 }
1867 if (fAttrDeprecated) {
1868 continue;
1869 }
Cary Clarkb94f6da2018-06-08 11:54:32 -04001870 if (KeyWord::kDefine == child.fKeyWord && this->defineOut(child)) {
1871 fDeferComment = nullptr;
1872 continue;
1873 }
Cary Clark8032b982017-07-28 11:04:54 -04001874 fDeferComment = nullptr;
Cary Clark154beea2017-10-26 07:58:48 -04001875 if (KeyWord::kClass == def->fKeyWord || KeyWord::kStruct == def->fKeyWord) {
1876 fIndentNext = true;
1877 }
Cary Clark73fa9722017-08-29 17:36:51 -04001878 if (!this->populate(&child, &pair, root)) {
Cary Clark8032b982017-07-28 11:04:54 -04001879 return false;
1880 }
Cary Clark0d225392018-06-07 09:59:07 -04001881 if (KeyWord::kClass == def->fKeyWord || KeyWord::kStruct == def->fKeyWord) {
1882 if (def->iRootParent() && (!fStartSetter
1883 || MarkType::kMethod != fStartSetter->fMarkType)) {
1884 this->setStart(child.fContentEnd, &child);
1885 fDeferComment = nullptr;
1886 }
1887 }
Cary Clark8032b982017-07-28 11:04:54 -04001888 continue;
1889 }
1890 if (Definition::Type::kWord == child.fType) {
1891 if (MarkType::kMember == child.fMarkType) {
Cary Clark884dd7d2017-10-11 10:37:52 -04001892 if (!memberStart) {
1893 auto iter = def->fTokens.begin();
1894 std::advance(iter, child.fParentIndex - 1);
1895 memberStart = &*iter;
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001896 staticOnly = false;
Cary Clark0d225392018-06-07 09:59:07 -04001897 }
1898 if (!fStructMemberTab) {
1899 SkASSERT(KeyWord::kStruct == def->fParent->fKeyWord);
1900 fIndent += 4;
1901 this->structSizeMembers(*def->fParent);
1902 fIndent -= 4;
1903 fIndentNext = true;
Cary Clark884dd7d2017-10-11 10:37:52 -04001904 }
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001905 SkASSERT(fBmhStructDef);
1906 if (!fBmhStructDef->fDeprecated) {
1907 memberEnd = this->structMemberOut(memberStart, child);
1908 startDef = &child;
Cary Clark0d225392018-06-07 09:59:07 -04001909 this->setStart(child.fContentEnd + 1, &child);
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001910 fDeferComment = nullptr;
1911 }
Cary Clark56356312018-02-08 14:45:18 -05001912 } else if (MarkType::kNone == child.fMarkType && sawConst
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001913 && fEnumDef && !fEnumDef->fDeprecated) {
1914 const Definition* bmhConst = nullptr;
1915 string match;
1916 if (root) {
1917 match = root->fName + "::";
1918 }
1919 match += string(child.fContentStart, child.fContentEnd - child.fContentStart);
1920 for (auto enumChild : fEnumDef->fChildren) {
1921 if (MarkType::kConst == enumChild->fMarkType && enumChild->fName == match) {
1922 bmhConst = enumChild;
1923 break;
1924 }
1925 }
1926 if (bmhConst) {
Cary Clark137b8742018-05-30 09:21:49 -04001927 this->constOut(memberStart, bmhConst);
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001928 fDeferComment = nullptr;
1929 sawConst = false;
1930 }
Cary Clark137b8742018-05-30 09:21:49 -04001931 } else if (MarkType::kNone == child.fMarkType && sawConst && !fEnumDef) {
1932 string match;
1933 if (root) {
1934 match = root->fName + "::";
1935 match += string(child.fContentStart, child.fContentEnd - child.fContentStart);
1936 auto bmhClassIter = fBmhParser->fClassMap.find(root->fName);
1937 if (fBmhParser->fClassMap.end() != bmhClassIter) {
1938 RootDefinition& bmhClass = bmhClassIter->second;
1939 auto constIter = std::find_if(bmhClass.fLeaves.begin(), bmhClass.fLeaves.end(),
1940 [match](std::pair<const string, Definition>& leaf){ return match == leaf.second.fName; } );
1941 if (bmhClass.fLeaves.end() != constIter) {
1942 const Definition& bmhConst = constIter->second;
1943 if (MarkType::kConst == bmhConst.fMarkType
1944 && MarkType::kSubtopic == bmhConst.fParent->fMarkType) {
1945 fBmhConst = &bmhConst;
1946 fConstDef = &child;
1947 }
1948 }
1949 }
Cary Clark137b8742018-05-30 09:21:49 -04001950 }
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001951 }
Cary Clark8032b982017-07-28 11:04:54 -04001952 if (child.fMemberStart) {
1953 memberStart = &child;
Cary Clarkc68ba1d2018-02-20 10:35:29 -05001954 staticOnly = false;
Cary Clark8032b982017-07-28 11:04:54 -04001955 }
Cary Clark89b14562018-03-19 09:04:10 -04001956 if (kAttrDeprecatedLen == (size_t) (child.fContentEnd - child.fContentStart) &&
1957 !strncmp(gAttrDeprecated, child.fStart, kAttrDeprecatedLen)) {
Cary Clark73fa9722017-08-29 17:36:51 -04001958 fAttrDeprecated = &child;
1959 }
Cary Clark8032b982017-07-28 11:04:54 -04001960 continue;
1961 }
1962 if (Definition::Type::kPunctuation == child.fType) {
1963 if (Punctuation::kSemicolon == child.fPunctuation) {
Cary Clark137b8742018-05-30 09:21:49 -04001964 if (sawConst && fBmhConst) { // find bmh documentation. Parent must be subtopic.
1965 const Definition* subtopic = fBmhConst->fParent;
1966 SkASSERT(subtopic);
1967 SkASSERT(MarkType::kSubtopic == subtopic->fMarkType);
1968 auto firstConst = std::find_if(subtopic->fChildren.begin(),
1969 subtopic->fChildren.end(),
1970 [](const Definition* def){ return MarkType::kConst == def->fMarkType;});
1971 SkASSERT(firstConst != subtopic->fChildren.end());
1972 bool constIsFirst = *firstConst == fBmhConst;
1973 if (constIsFirst) { // If first #Const child, output subtopic description.
1974 this->constOut(memberStart, subtopic);
1975 // find member / value / comment tabs
1976 // look for a one-to-one correspondence between bmh and include
1977 this->constSizeMembers(root);
Cary Clark0d225392018-06-07 09:59:07 -04001978 fDeferComment = nullptr;
Cary Clark137b8742018-05-30 09:21:49 -04001979 }
Cary Clark137b8742018-05-30 09:21:49 -04001980 // after const code, output #Line description as short comment
1981 auto lineIter = std::find_if(fBmhConst->fChildren.begin(),
1982 fBmhConst->fChildren.end(),
1983 [](const Definition* def){ return MarkType::kLine == def->fMarkType; });
1984 SkASSERT(fBmhConst->fChildren.end() != lineIter);
1985 const Definition* lineDef = *lineIter;
1986 if (fConstLength > 100) {
1987 this->writeCommentHeader();
1988 this->writeSpace();
1989 this->rewriteBlock(lineDef->length(), lineDef->fContentStart, Phrase::kYes);
1990 this->writeCommentTrailer(OneLine::kYes);
1991 }
1992 this->lfcr();
1993 TextParser constText(memberStart);
1994 const char* nameEnd = constText.trimmedBracketEnd('=');
1995 SkAssertResult(constText.skipToEndBracket('='));
1996 const char* valueEnd = constText.trimmedBracketEnd(';');
1997 this->writeBlock((int) (nameEnd - memberStart->fContentStart),
1998 memberStart->fContentStart);
1999 this->indentToColumn(fConstValueTab);
2000 this->writeBlock((int) (valueEnd - constText.fChar), constText.fChar);
2001 this->writeString(";");
2002 if (fConstLength <= 100) {
2003 this->indentToColumn(fConstCommentTab);
2004 this->writeString("//!<");
2005 this->writeSpace();
2006 this->rewriteBlock(lineDef->length(), lineDef->fContentStart, Phrase::kYes);
2007 }
Cary Clark0d225392018-06-07 09:59:07 -04002008 this->setStart(child.fContentStart + 1, &child);
Cary Clark137b8742018-05-30 09:21:49 -04002009 fDeferComment = nullptr;
2010 fBmhConst = nullptr;
2011 sawConst = false;
Cary Clark0d225392018-06-07 09:59:07 -04002012 } else if (sawTypedef) {
2013 const Definition* bmhTypedef = nullptr;
2014 if (root) {
2015 SkDEBUGCODE(auto classIter = fBmhParser->fClassMap.find(root->fName));
2016 SkASSERT(fBmhParser->fClassMap.end() != classIter);
2017 RootDefinition& classDef = fBmhParser->fClassMap[root->fName];
2018 auto leafIter = classDef.fLeaves.find(memberStart->fName);
2019 if (classDef.fLeaves.end() != leafIter) {
2020 bmhTypedef = &leafIter->second;
2021 }
2022 }
2023 if (!bmhTypedef) {
2024 auto typedefIter = fBmhParser->fTypedefMap.find(memberStart->fName);
2025 SkASSERT(fBmhParser->fTypedefMap.end() != typedefIter);
2026 bmhTypedef = &typedefIter->second;
2027 }
Cary Clark27dddae2018-06-08 15:57:37 -04002028 fDeferComment = deferredTypedefComment;
Cary Clark0d225392018-06-07 09:59:07 -04002029 this->constOut(memberStart, bmhTypedef);
2030 fDeferComment = nullptr;
2031 sawTypedef = false;
Cary Clark137b8742018-05-30 09:21:49 -04002032 }
Cary Clark8032b982017-07-28 11:04:54 -04002033 memberStart = nullptr;
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002034 staticOnly = false;
Cary Clark8032b982017-07-28 11:04:54 -04002035 if (inStruct) {
2036 fInStruct = false;
2037 }
2038 continue;
2039 }
2040 if (Punctuation::kLeftBrace == child.fPunctuation ||
2041 Punctuation::kColon == child.fPunctuation ||
2042 Punctuation::kAsterisk == child.fPunctuation
2043 ) {
2044 continue;
2045 }
2046 }
2047 }
2048 return true;
2049}
2050
2051bool IncludeWriter::populate(BmhParser& bmhParser) {
2052 bool allPassed = true;
2053 for (auto& includeMapper : fIncludeMap) {
2054 size_t lastSlash = includeMapper.first.rfind('/');
Cary Clarkd0530ba2017-09-14 11:25:39 -04002055 if (string::npos == lastSlash) {
2056 lastSlash = includeMapper.first.rfind('\\');
2057 }
Cary Clark8032b982017-07-28 11:04:54 -04002058 if (string::npos == lastSlash || lastSlash >= includeMapper.first.length() - 1) {
2059 return this->reportError<bool>("malformed include name");
2060 }
2061 string fileName = includeMapper.first.substr(lastSlash + 1);
2062 if (".h" != fileName.substr(fileName.length() - 2)) {
2063 return this->reportError<bool>("expected fileName.h");
2064 }
2065 string skClassName = fileName.substr(0, fileName.length() - 2);
Cary Clark80247e52018-07-11 16:18:41 -04002066 this->reset();
Cary Clark8032b982017-07-28 11:04:54 -04002067 fOut = fopen(fileName.c_str(), "wb");
2068 if (!fOut) {
2069 SkDebugf("could not open output file %s\n", fileName.c_str());
2070 return false;
2071 }
Cary Clark0d225392018-06-07 09:59:07 -04002072 RootDefinition* root =
2073 bmhParser.fClassMap.end() == bmhParser.fClassMap.find(skClassName) ?
2074 nullptr : &bmhParser.fClassMap[skClassName];
Cary Clark8032b982017-07-28 11:04:54 -04002075 fBmhParser = &bmhParser;
Cary Clark0d225392018-06-07 09:59:07 -04002076 if (root) {
2077 fRootTopic = root->fParent;
2078 root->clearVisited();
2079 } else {
2080 SkASSERT("Sk" == skClassName.substr(0, 2));
2081 string topicName = skClassName.substr(2);
2082 auto topicIter = bmhParser.fTopicMap.find(topicName);
2083 SkASSERT(bmhParser.fTopicMap.end() != topicIter);
2084 fRootTopic = topicIter->second->asRoot();
2085 fFirstWrite = true; // write file information after includes
2086 }
Cary Clark2d4bf5f2018-04-16 08:37:38 -04002087 fFileName = includeMapper.second.fFileName;
Cary Clark0d225392018-06-07 09:59:07 -04002088 this->setStartBack(includeMapper.second.fContentStart, &includeMapper.second);
Cary Clark8032b982017-07-28 11:04:54 -04002089 fEnd = includeMapper.second.fContentEnd;
Cary Clark2f466242017-12-11 16:03:17 -05002090 fAnonymousEnumCount = 1;
Cary Clarkb94f6da2018-06-08 11:54:32 -04002091 this->writeHeader(includeMapper);
Cary Clark73fa9722017-08-29 17:36:51 -04002092 allPassed &= this->populate(&includeMapper.second, nullptr, root);
Cary Clark8032b982017-07-28 11:04:54 -04002093 this->writeBlock((int) (fEnd - fStart), fStart);
Cary Clark0d225392018-06-07 09:59:07 -04002094#if 0
2095 if (fIndentStack.size() > 0) {
2096 this->indentOut();
2097 }
2098 SkASSERT(!fIndent);
2099#else
Cary Clark8032b982017-07-28 11:04:54 -04002100 fIndent = 0;
Cary Clark0d225392018-06-07 09:59:07 -04002101#endif
Cary Clark8032b982017-07-28 11:04:54 -04002102 this->lfcr();
2103 this->writePending();
2104 fclose(fOut);
Cary Clark7cfcbca2018-01-04 16:11:51 -05002105 fflush(fOut);
2106 size_t slash = fFileName.find_last_of('/');
2107 if (string::npos == slash) {
2108 slash = 0;
2109 }
2110 size_t back = fFileName.find_last_of('\\');
2111 if (string::npos == back) {
2112 back = 0;
2113 }
2114 string dir = fFileName.substr(0, SkTMax(slash, back) + 1);
2115 string readname = dir + fileName;
Cary Clark5b1f9532018-08-28 14:53:37 -04002116 if (ParserCommon::WrittenFileDiffers(fileName, readname)) {
Cary Clark7cfcbca2018-01-04 16:11:51 -05002117 SkDebugf("wrote updated %s\n", fileName.c_str());
2118 } else {
2119 remove(fileName.c_str());
2120 }
Cary Clark8032b982017-07-28 11:04:54 -04002121 }
2122 return allPassed;
2123}
2124
2125// change Xxx_Xxx to xxx xxx
2126static string ConvertRef(const string str, bool first) {
2127 string substitute;
2128 for (char c : str) {
2129 if ('_' == c) {
2130 c = ' '; // change Xxx_Xxx to xxx xxx
2131 } else if (isupper(c) && !first) {
2132 c = tolower(c);
2133 }
2134 substitute += c;
2135 first = false;
2136 }
2137 return substitute;
2138}
2139
Cary Clark8032b982017-07-28 11:04:54 -04002140string IncludeWriter::resolveMethod(const char* start, const char* end, bool first) {
2141 string methodname(start, end - start);
Cary Clark579985c2017-07-31 11:48:27 -04002142 if (string::npos != methodname.find("()")) {
2143 return "";
2144 }
Cary Clark8032b982017-07-28 11:04:54 -04002145 string substitute;
2146 auto rootDefIter = fBmhParser->fMethodMap.find(methodname);
2147 if (fBmhParser->fMethodMap.end() != rootDefIter) {
2148 substitute = methodname + "()";
2149 } else {
Cary Clark1eace2d2017-07-31 07:52:43 -04002150 RootDefinition* parent = nullptr;
2151 for (auto candidate : fRootTopic->fChildren) {
2152 if (MarkType::kClass == candidate->fMarkType
2153 || MarkType::kStruct == candidate->fMarkType) {
2154 parent = candidate->asRoot();
2155 break;
2156 }
2157 }
Cary Clark0d225392018-06-07 09:59:07 -04002158 if (parent) {
2159 auto defRef = parent->find(parent->fName + "::" + methodname,
2160 RootDefinition::AllowParens::kNo);
2161 if (defRef && MarkType::kMethod == defRef->fMarkType) {
2162 substitute = methodname + "()";
2163 } else {
2164 auto defineIter = fBmhParser->fDefineMap.find(methodname);
2165 if (fBmhParser->fDefineMap.end() != defineIter) {
2166 const RootDefinition& defineDef = defineIter->second;
2167 auto codeIter = std::find_if(defineDef.fChildren.begin(),
2168 defineDef.fChildren.end(),
2169 [](Definition* child){ return MarkType::kCode == child->fMarkType; } );
2170 if (defineDef.fChildren.end() != codeIter) {
2171 const Definition* codeDef = *codeIter;
2172 string codeContents(codeDef->fContentStart, codeDef->length());
2173 size_t namePos = codeContents.find(methodname);
2174 if (string::npos != namePos) {
2175 size_t parenPos = namePos + methodname.length();
2176 if (parenPos < codeContents.length() && '(' == codeContents[parenPos]) {
2177 substitute = methodname + "()";
2178 }
Cary Clark137b8742018-05-30 09:21:49 -04002179 }
2180 }
2181 }
2182 }
Cary Clark8032b982017-07-28 11:04:54 -04002183 }
2184 }
Cary Clark579985c2017-07-31 11:48:27 -04002185 if (fMethodDef && methodname == fMethodDef->fName) {
2186 TextParser report(fBmhMethod);
2187 report.reportError("method should not include references to itself");
2188 return "";
2189 }
Cary Clark6fc50412017-09-21 12:31:06 -04002190 if (fBmhMethod) {
2191 for (auto child : fBmhMethod->fChildren) {
2192 if (MarkType::kParam != child->fMarkType) {
2193 continue;
2194 }
2195 if (methodname == child->fName) {
2196 return "";
2197 }
2198 }
2199 }
Cary Clark8032b982017-07-28 11:04:54 -04002200 return substitute;
2201}
2202
Cary Clark137b8742018-05-30 09:21:49 -04002203string IncludeWriter::resolveAlias(const Definition* def) {
2204 for (auto child : def->fChildren) {
2205 if (MarkType::kSubstitute == child->fMarkType) {
2206 return string(child->fContentStart, (int) (child->fContentEnd - child->fContentStart));
2207 }
2208 if (MarkType::kAlias == child->fMarkType && def->fName == child->fName) {
2209 return this->resolveAlias(child);
2210 }
2211 }
2212 return "";
2213}
2214
Cary Clarkce101242017-09-01 15:51:02 -04002215string IncludeWriter::resolveRef(const char* start, const char* end, bool first,
2216 RefType* refType) {
Ben Wagner63fd7602017-10-09 15:45:33 -04002217 // look up Xxx_Xxx
Cary Clark8032b982017-07-28 11:04:54 -04002218 string undername(start, end - start);
Cary Clarkce101242017-09-01 15:51:02 -04002219 for (const auto& external : fBmhParser->fExternals) {
2220 if (external.fName == undername) {
2221 *refType = RefType::kExternal;
2222 return external.fName;
2223 }
2224 }
2225 *refType = RefType::kNormal;
Cary Clark8032b982017-07-28 11:04:54 -04002226 SkASSERT(string::npos == undername.find(' '));
2227 const Definition* rootDef = nullptr;
Cary Clark78c110e2018-02-09 16:49:09 -05002228 string substitute;
Cary Clark8032b982017-07-28 11:04:54 -04002229 {
2230 auto rootDefIter = fBmhParser->fTopicMap.find(undername);
2231 if (fBmhParser->fTopicMap.end() != rootDefIter) {
2232 rootDef = rootDefIter->second;
2233 } else {
2234 string prefixedName = fRootTopic->fName + '_' + undername;
2235 rootDefIter = fBmhParser->fTopicMap.find(prefixedName);
2236 if (fBmhParser->fTopicMap.end() != rootDefIter) {
2237 rootDef = rootDefIter->second;
Cary Clark154beea2017-10-26 07:58:48 -04002238 } else if (fBmhStructDef) {
2239 string localPrefix = fBmhStructDef->fFiddle + '_' + undername;
Cary Clarkbad5ad72017-08-03 17:14:08 -04002240 rootDefIter = fBmhParser->fTopicMap.find(localPrefix);
2241 if (fBmhParser->fTopicMap.end() != rootDefIter) {
2242 rootDef = rootDefIter->second;
2243 }
Cary Clark78c110e2018-02-09 16:49:09 -05002244 if (!rootDef) {
2245 size_t doubleColon = fBmhStructDef->fName.rfind("::");
2246 if (string::npos != doubleColon && undername
2247 == fBmhStructDef->fName.substr(doubleColon + 2)) {
2248 substitute = fBmhStructDef->fName;
2249 }
2250 }
2251 }
Cary Clark137b8742018-05-30 09:21:49 -04002252 if (!rootDef && fEnumDef && "Sk" + prefixedName == fEnumDef->fFiddle) {
2253 rootDef = fEnumDef;
2254 }
Cary Clark78c110e2018-02-09 16:49:09 -05002255 if (!rootDef && !substitute.length()) {
Cary Clark8032b982017-07-28 11:04:54 -04002256 auto aliasIter = fBmhParser->fAliasMap.find(undername);
2257 if (fBmhParser->fAliasMap.end() != aliasIter) {
Cary Clark78c110e2018-02-09 16:49:09 -05002258 rootDef = aliasIter->second;
Cary Clark186d08f2018-04-03 08:43:27 -04002259 } else if (fInEnum && fEnumDef && this->findEnumSubtopic(undername, &rootDef)) {
2260 ;
Cary Clark8032b982017-07-28 11:04:54 -04002261 } else if (!first) {
Cary Clark186d08f2018-04-03 08:43:27 -04002262 this->fChar = start;
Cary Clarkffb3d682018-05-17 12:17:28 -04002263 this->fLine = start;
Cary Clark579985c2017-07-31 11:48:27 -04002264 this->reportError("reference unfound");
2265 return "";
Cary Clark8032b982017-07-28 11:04:54 -04002266 }
2267 }
2268 }
2269 }
Cary Clark8032b982017-07-28 11:04:54 -04002270 if (rootDef) {
Cary Clark78c110e2018-02-09 16:49:09 -05002271 MarkType rootType = rootDef->fMarkType;
Cary Clark137b8742018-05-30 09:21:49 -04002272 if (MarkType::kSubtopic == rootType || MarkType::kTopic == rootType
2273 || MarkType::kAlias == rootType) {
2274 substitute = this->resolveAlias(rootDef);
Cary Clark8032b982017-07-28 11:04:54 -04002275 }
2276 if (!substitute.length()) {
Cary Clark78c110e2018-02-09 16:49:09 -05002277 string match = rootDef->fName;
2278 size_t index;
2279 while (string::npos != (index = match.find('_'))) {
2280 match.erase(index, 1);
2281 }
2282 string skmatch = "Sk" + match;
Cary Clark137b8742018-05-30 09:21:49 -04002283 auto parent = MarkType::kAlias == rootType ? rootDef->fParent : rootDef;
Cary Clark78c110e2018-02-09 16:49:09 -05002284 for (auto child : parent->fChildren) {
2285 // there may be more than one
2286 // prefer the one mostly closely matching in text
2287 if ((MarkType::kClass == child->fMarkType ||
2288 MarkType::kStruct == child->fMarkType ||
2289 (MarkType::kEnum == child->fMarkType && !child->fAnonymous) ||
2290 MarkType::kEnumClass == child->fMarkType) && (match == child->fName ||
2291 skmatch == child->fName)) {
2292 substitute = child->fName;
2293 break;
2294 }
2295 }
2296 }
2297 if (!substitute.length()) {
2298 for (auto child : rootDef->fChildren) {
2299 // there may be more than one
2300 // if so, it's a bug since it's unknown which is the right one
2301 if (MarkType::kClass == child->fMarkType ||
2302 MarkType::kStruct == child->fMarkType ||
2303 (MarkType::kEnum == child->fMarkType && !child->fAnonymous) ||
2304 MarkType::kEnumClass == child->fMarkType) {
2305 SkASSERT("" == substitute);
2306 substitute = child->fName;
2307 if (MarkType::kEnum == child->fMarkType) {
2308 size_t parentClassEnd = substitute.find("::");
2309 SkASSERT(string::npos != parentClassEnd);
2310 string subEnd = substitute.substr(parentClassEnd + 2);
2311 if (fInEnum) {
2312 substitute = subEnd;
2313 }
2314 if (subEnd == undername) {
2315 break;
2316 }
2317 }
2318 }
2319 }
2320 }
2321 if (!substitute.length()) {
2322 const Definition* parent = rootDef;
2323 do {
2324 parent = parent->fParent;
2325 } while (parent && (MarkType::kSubtopic == parent->fMarkType
2326 || MarkType::kTopic == parent->fMarkType));
Cary Clark8032b982017-07-28 11:04:54 -04002327 if (parent) {
2328 if (MarkType::kClass == parent->fMarkType ||
2329 MarkType::kStruct == parent->fMarkType ||
Cary Clarkd0530ba2017-09-14 11:25:39 -04002330 (MarkType::kEnum == parent->fMarkType && !parent->fAnonymous) ||
Cary Clark8032b982017-07-28 11:04:54 -04002331 MarkType::kEnumClass == parent->fMarkType) {
2332 if (parent->fParent != fRootTopic) {
2333 substitute = parent->fName;
Cary Clark2a8c48b2018-02-15 17:31:24 -05002334 substitute += ' ';
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002335 substitute += ConvertRef(rootDef->fName, false);
Cary Clark8032b982017-07-28 11:04:54 -04002336 } else {
Cary Clark137b8742018-05-30 09:21:49 -04002337 size_t underpos = undername.find('_');
2338 if (string::npos != underpos) {
2339 string parentName = undername.substr(0, underpos);
2340 string skName = "Sk" + parentName;
2341 if (skName == parent->fName) {
2342 SkASSERT(start >= fLastDescription->fContentStart);
2343 string lastDescription = string(fLastDescription->fContentStart,
2344 (int) (start - fLastDescription->fContentStart));
2345 size_t lineStart = lastDescription.rfind('\n');
2346 SkASSERT(string::npos != lineStart);
2347 fLine = fLastDescription->fContentStart + lineStart + 1;
2348 fChar = start;
2349 fEnd = end;
2350 return this->reportError<string>("remove underline");
2351 }
2352 }
Cary Clark8032b982017-07-28 11:04:54 -04002353 substitute += ConvertRef(undername, first);
2354 }
2355 }
2356 }
2357 }
2358 }
Cary Clark1eace2d2017-07-31 07:52:43 -04002359 // Ensure first word after period is capitalized if substitute is lower cased.
2360 if (first && isupper(start[0]) && substitute.length() > 0 && islower(substitute[0])) {
2361 substitute[0] = start[0];
2362 }
Cary Clark8032b982017-07-28 11:04:54 -04002363 return substitute;
2364}
Cary Clark579985c2017-07-31 11:48:27 -04002365
Cary Clark8032b982017-07-28 11:04:54 -04002366int IncludeWriter::lookupMethod(const PunctuationState punctuation, const Word word,
Cary Clark6fc50412017-09-21 12:31:06 -04002367 const int lastSpace, const int run, int lastWrite, const char* data,
2368 bool hasIndirection) {
Cary Clark579985c2017-07-31 11:48:27 -04002369 int wordStart = lastSpace;
2370 while (' ' >= data[wordStart]) {
2371 ++wordStart;
2372 }
2373 const int wordEnd = PunctuationState::kDelimiter == punctuation ||
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002374 PunctuationState::kParen == punctuation ||
Cary Clark8032b982017-07-28 11:04:54 -04002375 PunctuationState::kPeriod == punctuation ? run - 1 : run;
Cary Clark6fc50412017-09-21 12:31:06 -04002376 string temp;
2377 if (hasIndirection && '(' != data[wordEnd - 1] && ')' != data[wordEnd - 1]) {
2378 // FIXME: hard-coded to assume a.b or a->b is a.b() or a->b().
2379 // need to check class a for member b to see if this is so
2380 TextParser parser(fFileName, &data[wordStart], &data[wordEnd], fLineCount);
2381 const char* indirection = parser.anyOf(".>");
2382 if (&data[wordEnd] <= &indirection[2] || 'f' != indirection[1] ||
2383 !isupper(indirection[2])) {
2384 temp = string(&data[wordStart], wordEnd - wordStart) + "()";
2385 }
2386 } else {
2387 temp = this->resolveMethod(&data[wordStart], &data[wordEnd], Word::kFirst == word);
2388 }
Cary Clark8032b982017-07-28 11:04:54 -04002389 if (temp.length()) {
Cary Clark579985c2017-07-31 11:48:27 -04002390 if (wordStart > lastWrite) {
2391 SkASSERT(data[wordStart - 1] >= ' ');
Cary Clark8032b982017-07-28 11:04:54 -04002392 if (' ' == data[lastWrite]) {
2393 this->writeSpace();
2394 }
Cary Clark0d225392018-06-07 09:59:07 -04002395 this->firstBlockTrim(wordStart - lastWrite, &data[lastWrite]);
Cary Clark579985c2017-07-31 11:48:27 -04002396 if (' ' == data[wordStart - 1]) {
Cary Clark8032b982017-07-28 11:04:54 -04002397 this->writeSpace();
2398 }
2399 }
2400 SkASSERT(temp[temp.length() - 1] > ' ');
2401 this->writeString(temp.c_str());
Cary Clark579985c2017-07-31 11:48:27 -04002402 lastWrite = wordEnd;
Cary Clark8032b982017-07-28 11:04:54 -04002403 }
2404 return lastWrite;
2405}
2406
2407int IncludeWriter::lookupReference(const PunctuationState punctuation, const Word word,
2408 const int start, const int run, int lastWrite, const char last, const char* data) {
2409 const int end = PunctuationState::kDelimiter == punctuation ||
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002410 PunctuationState::kParen == punctuation ||
Cary Clark8032b982017-07-28 11:04:54 -04002411 PunctuationState::kPeriod == punctuation ? run - 1 : run;
Cary Clarkce101242017-09-01 15:51:02 -04002412 RefType refType = RefType::kUndefined;
2413 string resolved = string(&data[start], (size_t) (end - start));
2414 string temp = this->resolveRef(&data[start], &data[end], Word::kFirst == word, &refType);
Cary Clark8032b982017-07-28 11:04:54 -04002415 if (!temp.length()) {
2416 if (Word::kFirst != word && '_' != last) {
Cary Clarkce101242017-09-01 15:51:02 -04002417 temp = ConvertRef(resolved, false);
Cary Clark8032b982017-07-28 11:04:54 -04002418 }
Ben Wagner63fd7602017-10-09 15:45:33 -04002419 }
Cary Clark8032b982017-07-28 11:04:54 -04002420 if (temp.length()) {
2421 if (start > lastWrite) {
2422 SkASSERT(data[start - 1] >= ' ');
2423 if (' ' == data[lastWrite]) {
2424 this->writeSpace();
2425 }
Cary Clark0d225392018-06-07 09:59:07 -04002426 this->firstBlockTrim(start - lastWrite, &data[lastWrite]);
Cary Clark8032b982017-07-28 11:04:54 -04002427 if (' ' == data[start - 1]) {
2428 this->writeSpace();
2429 }
2430 }
2431 SkASSERT(temp[temp.length() - 1] > ' ');
2432 this->writeString(temp.c_str());
2433 lastWrite = end;
2434 }
2435 return lastWrite;
2436}
2437
2438/* returns true if rewriteBlock wrote linefeeds */
Cary Clarkd0530ba2017-09-14 11:25:39 -04002439IncludeWriter::Wrote IncludeWriter::rewriteBlock(int size, const char* data, Phrase phrase) {
Cary Clark8032b982017-07-28 11:04:54 -04002440 bool wroteLineFeeds = false;
2441 while (size > 0 && data[0] <= ' ') {
2442 --size;
2443 ++data;
2444 }
2445 while (size > 0 && data[size - 1] <= ' ') {
2446 --size;
2447 }
2448 if (0 == size) {
2449 return Wrote::kNone;
2450 }
Cary Clark0d225392018-06-07 09:59:07 -04002451 if (fReturnOnWrite) {
2452 return Wrote::kChars;
2453 }
Cary Clark8032b982017-07-28 11:04:54 -04002454 int run = 0;
2455 Word word = Word::kStart;
Cary Clarkd0530ba2017-09-14 11:25:39 -04002456 PunctuationState punctuation = Phrase::kNo == phrase ?
2457 PunctuationState::kStart : PunctuationState::kSpace;
Cary Clark8032b982017-07-28 11:04:54 -04002458 int start = 0;
2459 int lastWrite = 0;
2460 int lineFeeds = 0;
2461 int lastPrintable = 0;
Cary Clark579985c2017-07-31 11:48:27 -04002462 int lastSpace = -1;
Cary Clark8032b982017-07-28 11:04:54 -04002463 char c = 0;
Kevin Lubick42846132018-01-05 10:11:11 -05002464 char last = 0;
Cary Clark6fc50412017-09-21 12:31:06 -04002465 bool embeddedIndirection = false;
Cary Clark579985c2017-07-31 11:48:27 -04002466 bool embeddedSymbol = false;
Cary Clark8032b982017-07-28 11:04:54 -04002467 bool hasLower = false;
2468 bool hasUpper = false;
Cary Clark6fc50412017-09-21 12:31:06 -04002469 bool hasIndirection = false;
Cary Clark8032b982017-07-28 11:04:54 -04002470 bool hasSymbol = false;
2471 while (run < size) {
2472 last = c;
2473 c = data[run];
2474 SkASSERT(' ' <= c || '\n' == c);
2475 if (lineFeeds && ' ' < c) {
2476 if (lastPrintable >= lastWrite) {
2477 if (' ' == data[lastWrite]) {
2478 this->writeSpace();
Cary Clarka560c472017-11-27 10:44:06 -05002479 lastWrite++;
Cary Clark8032b982017-07-28 11:04:54 -04002480 }
2481 this->writeBlock(lastPrintable - lastWrite + 1, &data[lastWrite]);
2482 }
2483 if (lineFeeds > 1) {
2484 this->lf(2);
2485 }
2486 this->lfcr(); // defer the indent until non-whitespace is seen
2487 lastWrite = run;
2488 lineFeeds = 0;
2489 }
2490 if (' ' < c) {
2491 lastPrintable = run;
2492 }
2493 switch (c) {
2494 case '\n':
2495 ++lineFeeds;
2496 wroteLineFeeds = true;
2497 case ' ':
2498 switch (word) {
2499 case Word::kStart:
2500 break;
2501 case Word::kUnderline:
2502 case Word::kCap:
2503 case Word::kFirst:
2504 if (!hasLower) {
2505 break;
2506 }
2507 lastWrite = this->lookupReference(punctuation, word, start, run,
2508 lastWrite, last, data);
2509 break;
2510 case Word::kMixed:
Cary Clark579985c2017-07-31 11:48:27 -04002511 if (hasUpper && hasLower && !hasSymbol && lastSpace > 0) {
2512 lastWrite = this->lookupMethod(punctuation, word, lastSpace, run,
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002513 lastWrite, data, hasIndirection);
Cary Clark8032b982017-07-28 11:04:54 -04002514 }
2515 break;
2516 default:
2517 SkASSERT(0);
2518 }
Cary Clark1eace2d2017-07-31 07:52:43 -04002519 punctuation = PunctuationState::kPeriod == punctuation ||
Ben Wagner63fd7602017-10-09 15:45:33 -04002520 (PunctuationState::kStart == punctuation && ' ' >= last) ?
Cary Clark1eace2d2017-07-31 07:52:43 -04002521 PunctuationState::kStart : PunctuationState::kSpace;
Cary Clark8032b982017-07-28 11:04:54 -04002522 word = Word::kStart;
Cary Clark6fc50412017-09-21 12:31:06 -04002523 embeddedIndirection = false;
Cary Clark579985c2017-07-31 11:48:27 -04002524 embeddedSymbol = false;
Cary Clark8032b982017-07-28 11:04:54 -04002525 hasLower = false;
2526 hasUpper = false;
Cary Clark6fc50412017-09-21 12:31:06 -04002527 hasIndirection = false;
Cary Clark8032b982017-07-28 11:04:54 -04002528 hasSymbol = false;
Cary Clark579985c2017-07-31 11:48:27 -04002529 lastSpace = run;
Cary Clark8032b982017-07-28 11:04:54 -04002530 break;
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002531 case '.': case ',': case ';': case ':': case ')':
Cary Clark8032b982017-07-28 11:04:54 -04002532 switch (word) {
2533 case Word::kStart:
2534 punctuation = PunctuationState::kDelimiter;
2535 case Word::kCap:
2536 case Word::kFirst:
2537 case Word::kUnderline:
2538 case Word::kMixed:
2539 if (PunctuationState::kDelimiter == punctuation ||
2540 PunctuationState::kPeriod == punctuation) {
2541 word = Word::kMixed;
2542 }
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002543 punctuation = '.' == c ? PunctuationState::kPeriod :
2544 PunctuationState::kDelimiter;
Cary Clark8032b982017-07-28 11:04:54 -04002545 break;
2546 default:
2547 SkASSERT(0);
2548 }
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002549 ('.' == c ? embeddedIndirection : embeddedSymbol) = true;
Cary Clark8032b982017-07-28 11:04:54 -04002550 break;
Cary Clark6fc50412017-09-21 12:31:06 -04002551 case '>':
2552 if ('-' == last) {
2553 embeddedIndirection = true;
2554 break;
2555 }
Cary Clark8032b982017-07-28 11:04:54 -04002556 case '\'': // possessive apostrophe isn't treated as delimiting punctation
Cary Clarkce101242017-09-01 15:51:02 -04002557 case '\"': // quote is passed straight through
Cary Clark8032b982017-07-28 11:04:54 -04002558 case '=':
2559 case '!': // assumed not to be punctuation, but a programming symbol
Cary Clark6fc50412017-09-21 12:31:06 -04002560 case '&': case '<': case '{': case '}': case '/': case '*': case '[': case ']':
Cary Clark8032b982017-07-28 11:04:54 -04002561 word = Word::kMixed;
Cary Clark579985c2017-07-31 11:48:27 -04002562 embeddedSymbol = true;
Cary Clark8032b982017-07-28 11:04:54 -04002563 break;
2564 case '(':
2565 if (' ' == last) {
Cary Clarkc68ba1d2018-02-20 10:35:29 -05002566 punctuation = PunctuationState::kParen;
Cary Clark8032b982017-07-28 11:04:54 -04002567 } else {
2568 word = Word::kMixed;
2569 }
Cary Clark579985c2017-07-31 11:48:27 -04002570 embeddedSymbol = true;
Cary Clark8032b982017-07-28 11:04:54 -04002571 break;
Cary Clark8032b982017-07-28 11:04:54 -04002572 case '_':
2573 switch (word) {
2574 case Word::kStart:
2575 word = Word::kMixed;
2576 break;
2577 case Word::kCap:
2578 case Word::kFirst:
2579 case Word::kUnderline:
2580 word = Word::kUnderline;
2581 break;
2582 case Word::kMixed:
2583 break;
2584 default:
2585 SkASSERT(0);
2586 }
Cary Clark579985c2017-07-31 11:48:27 -04002587 hasSymbol |= embeddedSymbol;
Cary Clark8032b982017-07-28 11:04:54 -04002588 break;
Cary Clark73fa9722017-08-29 17:36:51 -04002589 case '+':
2590 // hackery to allow C++
2591 SkASSERT('C' == last || '+' == last); // FIXME: don't allow + outside of #Formula
2592 break;
Cary Clark8032b982017-07-28 11:04:54 -04002593 case 'A': case 'B': case 'C': case 'D': case 'E':
2594 case 'F': case 'G': case 'H': case 'I': case 'J':
2595 case 'K': case 'L': case 'M': case 'N': case 'O':
2596 case 'P': case 'Q': case 'R': case 'S': case 'T':
2597 case 'U': case 'V': case 'W': case 'X': case 'Y':
2598 case 'Z':
2599 switch (word) {
2600 case Word::kStart:
2601 word = PunctuationState::kStart == punctuation ? Word::kFirst : Word::kCap;
2602 start = run;
2603 break;
2604 case Word::kCap:
2605 case Word::kFirst:
Cary Clark154beea2017-10-26 07:58:48 -04002606 if (!isupper(last) && '~' != last) {
Cary Clark8032b982017-07-28 11:04:54 -04002607 word = Word::kMixed;
2608 }
2609 break;
2610 case Word::kUnderline:
2611 // some word in Xxx_XXX_Xxx can be all upper, but all can't: XXX_XXX
2612 if ('_' != last && !isupper(last)) {
2613 word = Word::kMixed;
2614 }
2615 break;
2616 case Word::kMixed:
2617 break;
2618 default:
2619 SkASSERT(0);
2620 }
2621 hasUpper = true;
2622 if (PunctuationState::kPeriod == punctuation ||
2623 PunctuationState::kDelimiter == punctuation) {
2624 word = Word::kMixed;
Cary Clark8032b982017-07-28 11:04:54 -04002625 }
Cary Clark6fc50412017-09-21 12:31:06 -04002626 hasIndirection |= embeddedIndirection;
Cary Clark579985c2017-07-31 11:48:27 -04002627 hasSymbol |= embeddedSymbol;
Cary Clark8032b982017-07-28 11:04:54 -04002628 break;
2629 case 'a': case 'b': case 'c': case 'd': case 'e':
2630 case 'f': case 'g': case 'h': case 'i': case 'j':
2631 case 'k': case 'l': case 'm': case 'n': case 'o':
2632 case 'p': case 'q': case 'r': case 's': case 't':
2633 case 'u': case 'v': case 'w': case 'x': case 'y':
Ben Wagner63fd7602017-10-09 15:45:33 -04002634 case 'z':
Cary Clark8032b982017-07-28 11:04:54 -04002635 case '0': case '1': case '2': case '3': case '4':
2636 case '5': case '6': case '7': case '8': case '9':
Cary Clark682c58d2018-05-16 07:07:07 -04002637 case '%': // to do : ensure that preceding is a number
Cary Clark8032b982017-07-28 11:04:54 -04002638 case '-':
2639 switch (word) {
2640 case Word::kStart:
2641 word = Word::kMixed;
2642 break;
2643 case Word::kMixed:
2644 case Word::kCap:
2645 case Word::kFirst:
2646 case Word::kUnderline:
2647 break;
2648 default:
2649 SkASSERT(0);
2650 }
2651 hasLower = true;
2652 punctuation = PunctuationState::kStart;
Cary Clark6fc50412017-09-21 12:31:06 -04002653 hasIndirection |= embeddedIndirection;
Cary Clark579985c2017-07-31 11:48:27 -04002654 hasSymbol |= embeddedSymbol;
Cary Clark8032b982017-07-28 11:04:54 -04002655 break;
Cary Clark154beea2017-10-26 07:58:48 -04002656 case '~':
2657 SkASSERT(Word::kStart == word);
2658 word = PunctuationState::kStart == punctuation ? Word::kFirst : Word::kCap;
2659 start = run;
2660 hasUpper = true;
2661 hasIndirection |= embeddedIndirection;
2662 hasSymbol |= embeddedSymbol;
2663 break;
Cary Clark8032b982017-07-28 11:04:54 -04002664 default:
2665 SkASSERT(0);
2666 }
2667 ++run;
2668 }
2669 if ((word == Word::kCap || word == Word::kFirst || word == Word::kUnderline) && hasLower) {
2670 lastWrite = this->lookupReference(punctuation, word, start, run, lastWrite, last, data);
Cary Clark579985c2017-07-31 11:48:27 -04002671 } else if (word == Word::kMixed && hasUpper && hasLower && !hasSymbol && lastSpace > 0) {
Cary Clark6fc50412017-09-21 12:31:06 -04002672 lastWrite = this->lookupMethod(punctuation, word, lastSpace, run, lastWrite, data,
2673 hasIndirection && !hasSymbol);
Cary Clark8032b982017-07-28 11:04:54 -04002674 }
2675 if (run > lastWrite) {
2676 if (' ' == data[lastWrite]) {
2677 this->writeSpace();
2678 }
2679 this->writeBlock(run - lastWrite, &data[lastWrite]);
2680 }
2681 return wroteLineFeeds ? Wrote::kLF : Wrote::kChars;
2682}
Cary Clarkb94f6da2018-06-08 11:54:32 -04002683
2684static string paddedString(int num) {
2685 auto padded = std::to_string(num);
2686 padded.insert(0, 2U - std::min(string::size_type(2), padded.length()), '0');
2687 return padded;
2688}
2689
2690bool IncludeWriter::writeHeader(std::pair<const string, Definition>& include) {
2691 std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
2692 time_t tt = std::chrono::system_clock::to_time_t(now);
2693 tm local_tm = *localtime(&tt);
2694
2695 // find end of copyright header
2696 fChar = fStart;
Cary Clarkd2ca79c2018-08-10 13:09:13 -04002697 this->skipWhiteSpace();
Cary Clarkb94f6da2018-06-08 11:54:32 -04002698 if (!this->skipExact(
2699 "/*\n"
2700 " * Copyright ")) {
2701 return this->reportError<bool>("copyright mismatch 1");
2702 }
2703 const char* date = fChar;
2704 this->skipToSpace();
2705 string yearStr(date, fChar - date);
2706 int year = stoi(yearStr);
2707 if (year < 2005 || year > local_tm.tm_year + 1900) {
2708 return this->reportError<bool>("copyright year out of range");
2709 }
2710 this->skipSpace();
2711 const char android[] = "The Android Open Source Project";
2712 const char google[] = "Google Inc.";
2713 if (this->startsWith(android)) {
2714 this->skipExact(android);
2715 } else if (!this->skipExact(google)) {
2716 return this->reportError<bool>("copyright mismatch 2");
2717 }
2718 if (!this->skipExact(
2719 "\n"
2720 " *\n"
2721 " * Use of this source code is governed by a BSD-style license that can be\n"
2722 " * found in the LICENSE file.\n"
2723 " */\n"
2724 "\n"
2725 )) {
2726 return this->reportError<bool>("copyright mismatch 2");
2727 }
2728 this->writeBlock(fChar - fStart, fStart);
2729 this->lf(2);
2730 this->writeString("/* Generated by tools/bookmaker from");
2731 this->writeSpace();
2732 string includeName = include.first;
2733 std::replace(includeName.begin(), includeName.end(), '\\', '/');
2734 this->writeString(includeName);
2735 this->writeSpace();
2736 this->writeString("and");
2737 this->writeSpace();
2738 string bmhName = fRootTopic->fFileName;
2739 std::replace(bmhName.begin(), bmhName.end(), '\\', '/');
2740 this->writeString(bmhName);
2741 this->lfcr();
2742 fIndent = 3;
2743 string dateTimeStr = std::to_string(local_tm.tm_year + 1900) + "-"
2744 + paddedString(local_tm.tm_mon + 1) + "-"
2745 + paddedString(local_tm.tm_mday) + " "
2746 + paddedString(local_tm.tm_hour) + ":"
2747 + paddedString(local_tm.tm_min) + ":"
2748 + paddedString(local_tm.tm_sec);
2749 this->writeString("on");
2750 this->writeSpace();
2751 this->writeString(dateTimeStr);
2752 this->writeString(". Additional documentation and examples can be found at:");
2753 this->lfcr();
2754 this->writeString("https://skia.org/user/api/");
2755 size_t bmhPageStart = bmhName.rfind('/');
2756 size_t bmhPageEnd = bmhName.rfind('.');
2757 if (string::npos == bmhPageStart || string::npos == bmhPageEnd) {
2758 return this->reportError<bool>("badly formed bmh page name");
2759 }
2760 ++bmhPageStart;
2761 string bmhPage = bmhName.substr(bmhPageStart, bmhPageEnd - bmhPageStart);
2762 this->writeString(bmhPage);
2763 this->lf(2);
2764 this->writeString("You may edit either file directly. Structural changes to public interfaces require");
2765 this->lfcr();
2766 this->writeString("editing both files. After editing");
2767 this->writeSpace();
2768 this->writeString(bmhName);
2769 this->writeSpace();
2770 this->writeString(", run:");
2771 this->lfcr();
2772 fIndent += 4;
2773 this->writeString("bookmaker -b docs -i");
2774 this->writeSpace();
2775 this->writeString(includeName);
2776 this->writeSpace();
2777 this->writeString("-p");
2778 this->lfcr();
2779 fIndent -= 4;
2780 this->writeString("to create an updated version of this file.");
2781 this->lfcr();
2782 fIndent = 1;
2783 this->writeString("*/");
2784 this->lf(2);
2785 fIndent = 0;
Cary Clark27dddae2018-06-08 15:57:37 -04002786 if (this->startsWith("/* Generated by tools/bookmaker from")) {
2787 this->skipToEndBracket("*/");
2788 if (!this->skipExact("*/\n\n")) {
2789 return this->reportError<bool>("malformed generated comment");
2790 }
2791 }
Cary Clarkb94f6da2018-06-08 11:54:32 -04002792 fStart = fChar;
Cary Clark27dddae2018-06-08 15:57:37 -04002793
Cary Clarkb94f6da2018-06-08 11:54:32 -04002794 return true;
2795}