Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1 | /* |
| 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 Clark | b94f6da | 2018-06-08 11:54:32 -0400 | [diff] [blame] | 9 | #include <chrono> |
| 10 | #include <ctime> |
| 11 | #include <string> |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 12 | |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 13 | bool 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 | |
| 47 | void 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 Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 64 | void IncludeWriter::constOut(const Definition* memberStart, const Definition* bmhConst) { |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 65 | const char* bodyEnd = fDeferComment ? fDeferComment->fContentStart - 1 : |
| 66 | memberStart->fContentStart; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 67 | this->firstBlockTrim((int) (bodyEnd - fStart), fStart); // may write nothing |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 68 | this->lf(2); |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 69 | this->indentDeferred(IndentKind::kConstOut); |
| 70 | if (fStructEnded) { |
| 71 | fIndent = fICSStack.size() * 4; |
| 72 | fStructEnded = false; |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 73 | } |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 74 | // 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 Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 90 | bool IncludeWriter::descriptionOut(const Definition* def, SkipFirstLine skipFirstLine, |
Cary Clark | 1a8d762 | 2018-03-05 13:26:16 -0500 | [diff] [blame] | 91 | Phrase phrase) { |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 92 | bool wroteSomething = false; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 93 | const char* commentStart = def->fContentStart; |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 94 | 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 Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 101 | SkDEBUGCODE(bool wroteCode = false); |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 102 | if (def->fDeprecated) { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 103 | if (fReturnOnWrite) { |
| 104 | return true; |
| 105 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 106 | string message = def->incompleteMessage(Definition::DetailsType::kSentence); |
| 107 | this->writeString(message); |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 108 | this->lfcr(); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 109 | wroteSomething = true; |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 110 | } |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 111 | const Definition* lastDescription = def; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 112 | for (auto prop : def->fChildren) { |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 113 | fLastDescription = lastDescription; |
| 114 | lastDescription = prop; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 115 | switch (prop->fMarkType) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 116 | 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 Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 123 | if (fReturnOnWrite) { |
| 124 | return true; |
| 125 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 126 | this->lf(2); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 127 | wroteSomething = true; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | size_t childSize = prop->fChildren.size(); |
| 131 | if (childSize) { |
Cary Clark | b94f6da | 2018-06-08 11:54:32 -0400 | [diff] [blame] | 132 | 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 Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 140 | } |
| 141 | commentLen = (int) (prop->fContentEnd - commentStart); |
| 142 | SkASSERT(commentLen > 0); |
| 143 | if (literal) { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 144 | if (!fReturnOnWrite && !literalOutdent) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 145 | fIndent += 4; |
| 146 | } |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 147 | wroteSomething |= this->writeBlockIndent(commentLen, commentStart); |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 148 | if (fReturnOnWrite) { |
| 149 | return true; |
| 150 | } |
| 151 | if (!fReturnOnWrite) { |
| 152 | this->lf(2); |
| 153 | if (!literalOutdent) { |
| 154 | fIndent -= 4; |
| 155 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 156 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 157 | SkDEBUGCODE(wroteCode = true); |
| 158 | } |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 159 | commentStart = prop->fTerminator; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 160 | } break; |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 161 | case MarkType::kBug: { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 162 | if (fReturnOnWrite) { |
| 163 | return true; |
| 164 | } |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 165 | string bugstr("(see skbug.com/" + string(prop->fContentStart, |
| 166 | prop->fContentEnd - prop->fContentStart) + ')'); |
| 167 | this->writeString(bugstr); |
| 168 | this->lfcr(); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 169 | wroteSomething = true; |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 170 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 171 | case MarkType::kDeprecated: |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 172 | 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 Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 177 | if (fReturnOnWrite) { |
| 178 | return true; |
| 179 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 180 | this->lfcr(); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 181 | wroteSomething = true; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | commentStart = prop->fContentStart; |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 185 | if (MarkType::kPrivate != prop->fMarkType && ' ' < commentStart[0]) { |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 186 | commentStart = strchr(commentStart, '\n'); |
| 187 | } |
| 188 | if (MarkType::kBug == prop->fMarkType) { |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 189 | commentStart = prop->fContentEnd; |
| 190 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 191 | commentLen = (int) (prop->fContentEnd - commentStart); |
| 192 | if (commentLen > 0) { |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 193 | wroteSomething |= this->writeBlockIndent(commentLen, commentStart); |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 194 | if (wroteSomething && fReturnOnWrite) { |
| 195 | return true; |
| 196 | } |
Cary Clark | 1a8d762 | 2018-03-05 13:26:16 -0500 | [diff] [blame] | 197 | const char* end = commentStart + commentLen; |
| 198 | while (end > commentStart && ' ' == end[-1]) { |
| 199 | --end; |
| 200 | } |
| 201 | if (end > commentStart && '\n' == end[-1]) { |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 202 | this->lfcr(); |
| 203 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 204 | } |
| 205 | commentStart = prop->fTerminator; |
| 206 | commentLen = (int) (def->fContentEnd - commentStart); |
| 207 | break; |
| 208 | case MarkType::kExperimental: |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 209 | commentStart = prop->fContentStart; |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 210 | if (' ' < commentStart[0]) { |
| 211 | commentStart = strchr(commentStart, '\n'); |
| 212 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 213 | commentLen = (int) (prop->fContentEnd - commentStart); |
| 214 | if (commentLen > 0) { |
| 215 | if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart, Phrase::kNo)) { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 216 | if (fReturnOnWrite) { |
| 217 | return true; |
| 218 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 219 | this->lfcr(); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 220 | wroteSomething = true; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | commentStart = prop->fTerminator; |
| 224 | commentLen = (int) (def->fContentEnd - commentStart); |
| 225 | break; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 226 | case MarkType::kFormula: { |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 227 | commentLen = prop->fStart - commentStart; |
| 228 | if (commentLen > 0) { |
| 229 | if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart, Phrase::kNo)) { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 230 | if (fReturnOnWrite) { |
| 231 | return true; |
| 232 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 233 | if (commentLen > 1 && '\n' == prop->fStart[-1] && |
| 234 | '\n' == prop->fStart[-2]) { |
| 235 | this->lf(1); |
| 236 | } else { |
| 237 | this->writeSpace(); |
| 238 | } |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 239 | wroteSomething = true; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 240 | } |
| 241 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 242 | int saveIndent = fIndent; |
| 243 | if (fIndent < fColumn + 1) { |
| 244 | fIndent = fColumn + 1; |
| 245 | } |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 246 | wroteSomething |= this->writeBlockIndent(prop->length(), prop->fContentStart); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 247 | fIndent = saveIndent; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 248 | if (wroteSomething && fReturnOnWrite) { |
| 249 | return true; |
| 250 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 251 | commentStart = prop->fTerminator; |
| 252 | commentLen = (int) (def->fContentEnd - commentStart); |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 253 | 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 Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 261 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 262 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 263 | } break; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 264 | case MarkType::kDetails: |
Cary Clark | ab2621d | 2018-01-30 10:08:57 -0500 | [diff] [blame] | 265 | case MarkType::kIn: |
| 266 | case MarkType::kLine: |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 267 | 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 Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 272 | if (fReturnOnWrite) { |
| 273 | return true; |
| 274 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 275 | this->lfcr(); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 276 | wroteSomething = true; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 277 | } |
| 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 Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 287 | if (fReturnOnWrite) { |
| 288 | return true; |
| 289 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 290 | this->lfcr(); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 291 | wroteSomething = true; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 292 | } |
| 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 Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 298 | if (fReturnOnWrite) { |
| 299 | return true; |
| 300 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 301 | this->writeString("-"); |
| 302 | this->writeSpace(); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 303 | wroteSomething |= this->descriptionOut(column, SkipFirstLine::kNo, Phrase::kNo); |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 304 | 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 Clark | 1a8d762 | 2018-03-05 13:26:16 -0500 | [diff] [blame] | 313 | case MarkType::kPhraseRef: { |
| 314 | commentLen = prop->fStart - commentStart; |
| 315 | if (commentLen > 0) { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 316 | if (fReturnOnWrite) { |
| 317 | return true; |
| 318 | } |
Cary Clark | 1a8d762 | 2018-03-05 13:26:16 -0500 | [diff] [blame] | 319 | 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 Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 322 | wroteSomething = true; |
Cary Clark | 1a8d762 | 2018-03-05 13:26:16 -0500 | [diff] [blame] | 323 | } |
| 324 | auto iter = fBmhParser->fPhraseMap.find(prop->fName); |
| 325 | if (fBmhParser->fPhraseMap.end() == iter) { |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 326 | return this->reportError<bool>("missing phrase definition"); |
Cary Clark | 1a8d762 | 2018-03-05 13:26:16 -0500 | [diff] [blame] | 327 | } |
| 328 | Definition* phraseDef = iter->second; |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 329 | // 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 Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 345 | if (fReturnOnWrite) { |
| 346 | return true; |
| 347 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 348 | 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 Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 363 | wroteSomething = true; |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 364 | } |
| 365 | if (length > 0) { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 366 | if (fReturnOnWrite) { |
| 367 | return true; |
| 368 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 369 | this->rewriteBlock(length, start, defIsPhrase); |
| 370 | } |
Cary Clark | 1a8d762 | 2018-03-05 13:26:16 -0500 | [diff] [blame] | 371 | commentStart = prop->fContentStart; |
| 372 | commentLen = (int) (def->fContentEnd - commentStart); |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 373 | if (!fReturnOnWrite) { |
| 374 | if ('\n' == commentStart[0] && '\n' == commentStart[1]) { |
| 375 | this->lf(2); |
| 376 | } |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 377 | } |
Cary Clark | 1a8d762 | 2018-03-05 13:26:16 -0500 | [diff] [blame] | 378 | } break; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 379 | default: |
| 380 | commentLen = (int) (prop->fStart - commentStart); |
| 381 | breakOut = true; |
| 382 | } |
| 383 | if (breakOut) { |
| 384 | break; |
| 385 | } |
| 386 | } |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 387 | if (!breakOut) { |
| 388 | commentLen = (int) (def->fContentEnd - commentStart); |
| 389 | } |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 390 | SkASSERT(wroteCode || (commentLen > 0 && commentLen < 1500) || def->fDeprecated); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 391 | if (commentLen > 0) { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 392 | if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart, phrase)) { |
| 393 | if (fReturnOnWrite) { |
| 394 | return true; |
| 395 | } |
| 396 | wroteSomething = true; |
| 397 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 398 | } |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 399 | SkASSERT(!fReturnOnWrite || !wroteSomething); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 400 | return wroteSomething; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 401 | } |
| 402 | |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 403 | void IncludeWriter::enumHeaderOut(RootDefinition* root, const Definition& child) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 404 | const Definition* enumDef = nullptr; |
| 405 | const char* bodyEnd = fDeferComment ? fDeferComment->fContentStart - 1 : |
| 406 | child.fContentStart; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 407 | this->firstBlockTrim((int) (bodyEnd - fStart), fStart); // may write nothing |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 408 | this->lf(2); |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 409 | this->indentDeferred(IndentKind::kEnumHeader); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 410 | fDeferComment = nullptr; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 411 | this->setStart(child.fContentStart, &child); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 412 | const auto& nameDef = child.fTokens.front(); |
| 413 | string fullName; |
| 414 | if (nullptr != nameDef.fContentEnd) { |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 415 | 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 428 | fullName = root->fName + "::" + enumName; |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 429 | enumDef = root->find(enumName, RootDefinition::AllowParens::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 430 | if (!enumDef) { |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 431 | enumDef = root->find(fullName, RootDefinition::AllowParens::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 432 | } |
Cary Clark | 06c20f3 | 2018-03-20 15:53:27 -0400 | [diff] [blame] | 433 | if (!enumDef) { |
| 434 | auto mapEntry = fBmhParser->fEnumMap.find(enumName); |
| 435 | if (fBmhParser->fEnumMap.end() != mapEntry) { |
| 436 | enumDef = &mapEntry->second; |
| 437 | } |
| 438 | } |
Cary Clark | ab5c9af | 2018-07-12 16:24:53 -0400 | [diff] [blame] | 439 | if (!enumDef && enumName == root->fName) { |
| 440 | enumDef = root; |
| 441 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 442 | 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 Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 453 | enumDef = root->find(enumName, RootDefinition::AllowParens::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 454 | SkASSERT(enumDef); |
| 455 | ++fAnonymousEnumCount; |
| 456 | } |
| 457 | Definition* codeBlock = nullptr; |
| 458 | const char* commentStart = nullptr; |
| 459 | bool wroteHeader = false; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 460 | bool lastAnchor = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 461 | SkDEBUGCODE(bool foundConst = false); |
| 462 | for (auto test : enumDef->fChildren) { |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 463 | if (MarkType::kCode == test->fMarkType && !codeBlock) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 464 | 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 Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 474 | if (fIndentNext) { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 475 | // FIXME: how can I tell where fIdentNext gets cleared? |
| 476 | this->indentIn(IndentKind::kEnumChild); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 477 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 478 | this->writeCommentHeader(); |
| 479 | this->writeString("\\enum"); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 480 | if (fullName.length() > 0) { |
| 481 | this->writeSpace(); |
| 482 | this->writeString(fullName.c_str()); |
| 483 | } |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 484 | this->indentIn(IndentKind::kEnumChild2); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 485 | this->lfcr(); |
| 486 | wroteHeader = true; |
| 487 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 488 | 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 Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 497 | this->rewriteBlock((int) (commentEnd - commentStart), commentStart, Phrase::kNo); |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 498 | if (MarkType::kAnchor == test->fMarkType || MarkType::kCode == test->fMarkType) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 499 | bool newLine = commentEnd - commentStart > 1 && |
| 500 | '\n' == commentEnd[-1] && '\n' == commentEnd[-2]; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 501 | commentStart = test->fContentStart; |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 502 | commentEnd = MarkType::kAnchor == test->fMarkType ? test->fChildren[0]->fStart : |
| 503 | test->fContentEnd; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 504 | if (newLine) { |
| 505 | this->lf(2); |
| 506 | } else { |
| 507 | this->writeSpace(); |
| 508 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 509 | 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 Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 515 | lastAnchor = true; // this->writeSpace(); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 516 | } |
| 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 Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 527 | this->indentOut(); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 528 | this->lfcr(); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 529 | this->writeCommentTrailer(OneLine::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 530 | } |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 531 | Definition* braceHolder = child.fChildren[0]; |
| 532 | if (KeyWord::kClass == braceHolder->fKeyWord) { |
| 533 | braceHolder = braceHolder->fChildren[0]; |
| 534 | } |
| 535 | bodyEnd = braceHolder->fContentStart; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 536 | SkASSERT('{' == bodyEnd[0]); |
| 537 | ++bodyEnd; |
| 538 | this->lfcr(); |
| 539 | this->writeBlock((int) (bodyEnd - fStart), fStart); // write include "enum Name {" |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 540 | this->indentIn(IndentKind::kEnumHeader2); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 541 | this->singleLF(); |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 542 | this->setStart(bodyEnd, braceHolder); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 543 | fEnumDef = enumDef; |
| 544 | } |
| 545 | |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 546 | const 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 | |
| 558 | string 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 Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 568 | shortComment = constItem->fParent->incompleteMessage(Definition::DetailsType::kPhrase); |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | if (!shortComment.length()) { |
| 572 | currentEnumItem->reportError<void>("missing #Line or #Deprecated or #Experimental"); |
| 573 | } |
| 574 | return shortComment; |
| 575 | } |
| 576 | |
| 577 | IncludeWriter::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 Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 585 | this->setStart(token->fContentEnd, token); |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 586 | 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 | |
| 598 | void 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 |
| 678 | void 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 Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 687 | vector<IterState> iterStack; |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 688 | iterStack.emplace_back(brace->fTokens.begin(), brace->fTokens.end()); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 689 | IterState* iterState = &iterStack[0]; |
Cary Clark | 186d08f | 2018-04-03 08:43:27 -0400 | [diff] [blame] | 690 | Preprocessor preprocessor; |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 691 | Item item; |
| 692 | while (iterState->fDefIter != iterState->fDefEnd) { |
| 693 | auto& token = *iterState->fDefIter++; |
| 694 | if (this->enumPreprocessor(&token, MemberPass::kOut, iterStack, &iterState, |
Cary Clark | 186d08f | 2018-04-03 08:43:27 -0400 | [diff] [blame] | 695 | &preprocessor)) { |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 696 | continue; |
| 697 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 698 | if (ItemState::kName == state) { |
| 699 | state = this->enumMemberName(child, &token, &item, &last, ¤tEnumItem); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 700 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 701 | if (ItemState::kValue == state) { |
| 702 | TextParser valueEnd(token.fFileName, last.fEnd, token.fContentStart, token.fLineCount); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 703 | const char* end = valueEnd.anyOf(",}"); |
| 704 | if (!end) { // write expression continuation |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 705 | item.fValue += string(last.fEnd, (int) (token.fContentEnd - last.fEnd)); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 706 | continue; |
| 707 | } |
| 708 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 709 | if (ItemState::kNone != state) { |
| 710 | this->enumMemberOut(currentEnumItem, child, item, preprocessor); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 711 | item.reset(); |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 712 | this->setStartBack(token.fContentStart, &token); |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 713 | state = ItemState::kNone; |
| 714 | last.fStart = nullptr; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 715 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 716 | SkASSERT(ItemState::kNone == state); |
| 717 | if (!last.fStart) { |
| 718 | last.fStart = fStart; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 719 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 720 | last.fEnd = token.fContentEnd; |
| 721 | state = ItemState::kName; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 722 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 723 | if (ItemState::kName == state) { |
| 724 | state = this->enumMemberName(child, nullptr, &item, &last, ¤tEnumItem); |
| 725 | } |
| 726 | if (ItemState::kValue == state || ItemState::kComment == state) { |
| 727 | this->enumMemberOut(currentEnumItem, child, item, preprocessor); |
| 728 | } |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 729 | this->indentOut(); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 730 | } |
| 731 | |
Cary Clark | 186d08f | 2018-04-03 08:43:27 -0400 | [diff] [blame] | 732 | bool 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 Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 737 | this->setStart(token->fContentEnd, token); |
Cary Clark | 186d08f | 2018-04-03 08:43:27 -0400 | [diff] [blame] | 738 | } |
| 739 | return true; // ignore old inline comments |
| 740 | } |
| 741 | if (Bracket::kSlashStar == token->fBracket) { |
| 742 | if (MemberPass::kOut == pass) { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 743 | this->setStart(token->fContentEnd + 1, token); |
Cary Clark | 186d08f | 2018-04-03 08:43:27 -0400 | [diff] [blame] | 744 | } |
| 745 | return true; // ignore old inline comments |
| 746 | } |
| 747 | if (Bracket::kPound == token->fBracket) { // preprocessor wraps member |
Cary Clark | a4f581a | 2018-04-03 15:31:59 -0400 | [diff] [blame] | 748 | preprocessor->fDefinition = token; |
Cary Clark | 186d08f | 2018-04-03 08:43:27 -0400 | [diff] [blame] | 749 | 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 Clark | a4f581a | 2018-04-03 15:31:59 -0400 | [diff] [blame] | 769 | 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 Clark | 186d08f | 2018-04-03 08:43:27 -0400 | [diff] [blame] | 778 | 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 791 | void IncludeWriter::enumSizeItems(const Definition& child) { |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 792 | ItemState state = ItemState::kNone; |
| 793 | ItemLength lengths = { 0, 0, 0, 0 }; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 794 | const char* lastEnd = nullptr; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 795 | auto brace = child.fChildren[0]; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 796 | if (KeyWord::kClass == brace->fKeyWord) { |
| 797 | brace = brace->fChildren[0]; |
| 798 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 799 | SkASSERT(Bracket::kBrace == brace->fBracket); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 800 | vector<IterState> iterStack; |
| 801 | iterStack.emplace_back(brace->fTokens.begin(), brace->fTokens.end()); |
| 802 | IterState* iterState = &iterStack[0]; |
Cary Clark | 186d08f | 2018-04-03 08:43:27 -0400 | [diff] [blame] | 803 | Preprocessor preprocessor; |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 804 | string enumName; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 805 | while (iterState->fDefIter != iterState->fDefEnd) { |
| 806 | auto& token = *iterState->fDefIter++; |
Cary Clark | 186d08f | 2018-04-03 08:43:27 -0400 | [diff] [blame] | 807 | if (this->enumPreprocessor(&token, MemberPass::kCount, iterStack, &iterState, |
| 808 | &preprocessor)) { |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 809 | continue; |
| 810 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 811 | if (ItemState::kName == state) { |
| 812 | TextParser enumLine(token.fFileName, lastEnd, token.fContentStart, token.fLineCount); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 813 | const char* end = enumLine.anyOf(",}="); |
| 814 | SkASSERT(end); |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 815 | state = '=' == *end ? ItemState::kValue : ItemState::kComment; |
| 816 | if (ItemState::kValue == state) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 817 | lastEnd = token.fContentEnd; |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 818 | lengths.fCurValue = (int) (lastEnd - token.fContentStart); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 819 | continue; |
| 820 | } |
| 821 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 822 | if (ItemState::kValue == state) { |
| 823 | TextParser valueEnd(token.fFileName, lastEnd, token.fContentStart, token.fLineCount); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 824 | const char* end = valueEnd.anyOf(",}"); |
| 825 | if (!end) { // write expression continuation |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 826 | lengths.fCurValue += (int) (token.fContentEnd - lastEnd); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 827 | continue; |
| 828 | } |
| 829 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 830 | if (ItemState::kNone != state) { |
| 831 | this->checkEnumLengths(child, enumName, &lengths); |
| 832 | lengths.fCurValue = 0; |
| 833 | state = ItemState::kNone; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 834 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 835 | SkASSERT(ItemState::kNone == state); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 836 | lastEnd = token.fContentEnd; |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 837 | lengths.fCurName = (int) (lastEnd - token.fContentStart); |
| 838 | enumName = string(token.fContentStart, lengths.fCurName); |
| 839 | state = ItemState::kName; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 840 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 841 | if (ItemState::kNone != state) { |
| 842 | this->checkEnumLengths(child, enumName, &lengths); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 843 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 844 | fEnumItemValueTab = lengths.fLongestName + fIndent + 1 /* 1: , */ ; |
| 845 | if (lengths.fLongestValue) { |
| 846 | lengths.fLongestValue += 3; // 3: space = space |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 847 | } |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 848 | fEnumItemCommentTab = fEnumItemValueTab + lengths.fLongestValue + 1 ; // 1: space before //!< |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 849 | // iterate through bmh children and see which comments fit on include lines |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 850 | if (!this->checkChildCommentLength(fEnumDef, MarkType::kConst)) { |
| 851 | fEnumDef->reportError<void>("expected at least one #Const in #Enum"); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 855 | const 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 881 | // walk children and output complete method doxygen description |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 882 | void IncludeWriter::methodOut(Definition* method, const Definition& child) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 883 | if (fPendingMethod) { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 884 | this->indentOut(); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 885 | fPendingMethod = false; |
| 886 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 887 | fBmhMethod = method; |
| 888 | fMethodDef = &child; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 889 | fContinuation = nullptr; |
| 890 | fDeferComment = nullptr; |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 891 | Definition* csParent = method->csParent(); |
Cary Clark | a4f581a | 2018-04-03 15:31:59 -0400 | [diff] [blame] | 892 | if (csParent && (0 == fIndent || fIndentNext)) { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 893 | this->indentIn(IndentKind::kMethodOut); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 894 | fIndentNext = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 895 | } |
| 896 | this->writeCommentHeader(); |
| 897 | fIndent += 4; |
Cary Clark | 1a8d762 | 2018-03-05 13:26:16 -0500 | [diff] [blame] | 898 | this->descriptionOut(method, SkipFirstLine::kNo, Phrase::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 899 | // 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 915 | 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 Clark | 1a8d762 | 2018-03-05 13:26:16 -0500 | [diff] [blame] | 924 | this->indentToColumn(column); |
| 925 | fIndent = column; |
Cary Clark | 1a8d762 | 2018-03-05 13:26:16 -0500 | [diff] [blame] | 926 | this->descriptionOut(methodPart, SkipFirstLine::kNo, Phrase::kYes); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 927 | fIndent = saveIndent; |
| 928 | this->lfcr(); |
| 929 | } |
| 930 | } else { |
| 931 | this->lfcr(); |
| 932 | } |
| 933 | fIndent -= 4; |
| 934 | this->lfcr(); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 935 | this->writeCommentTrailer(OneLine::kNo); |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 936 | fBmhMethod = nullptr; |
| 937 | fMethodDef = nullptr; |
Cary Clark | 78c110e | 2018-02-09 16:49:09 -0500 | [diff] [blame] | 938 | fEnumDef = nullptr; |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 939 | fWroteMethod = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | void 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 Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 952 | if (child.fDeprecated) { |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 953 | this->writeString(child.incompleteMessage(Definition::DetailsType::kSentence)); |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 954 | } else { |
| 955 | this->rewriteBlock((int)(commentEnd - commentStart), commentStart, Phrase::kNo); |
| 956 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 957 | fIndent -= 4; |
| 958 | this->lfcr(); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 959 | this->writeCommentTrailer(OneLine::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 960 | } |
| 961 | |
Cary Clark | 186d08f | 2018-04-03 08:43:27 -0400 | [diff] [blame] | 962 | bool 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 Clark | 2dc84ad | 2018-01-26 12:56:22 -0500 | [diff] [blame] | 973 | Definition* IncludeWriter::findMemberCommentBlock(const vector<Definition*>& bmhChildren, |
Cary Clark | 2d4bf5f | 2018-04-16 08:37:38 -0400 | [diff] [blame] | 974 | string name) const { |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 975 | for (auto memberDef : bmhChildren) { |
| 976 | if (MarkType::kMember != memberDef->fMarkType) { |
| 977 | continue; |
| 978 | } |
| 979 | string match = memberDef->fName; |
| 980 | // if match.endsWith(name) ... |
Cary Clark | 300cc5b | 2018-02-20 12:50:35 -0500 | [diff] [blame] | 981 | if (match.length() >= name.length() && |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 982 | 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 Clark | 2dc84ad | 2018-01-26 12:56:22 -0500 | [diff] [blame] | 996 | } |
| 997 | |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 998 | Definition* 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 | |
| 1010 | void IncludeWriter::firstBlock(int size, const char* data) { |
| 1011 | SkAssertResult(this->firstBlockTrim(size, data)); |
| 1012 | } |
| 1013 | |
| 1014 | bool 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 | |
| 1040 | void IncludeWriter::setStart(const char* start, const Definition* def) { |
| 1041 | SkASSERT(start >= fStart); |
| 1042 | this->setStartBack(start, def); |
| 1043 | } |
| 1044 | |
| 1045 | void IncludeWriter::setStartBack(const char* start, const Definition* def) { |
| 1046 | fStartSetter = def; |
| 1047 | fStart = start; |
| 1048 | } |
| 1049 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1050 | Definition* IncludeWriter::structMemberOut(const Definition* memberStart, const Definition& child) { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1051 | const char* blockStart = !fWroteMethod && fDeferComment ? fDeferComment->fContentEnd : fStart; |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1052 | const char* blockEnd = fWroteMethod && fDeferComment ? fDeferComment->fStart - 1 : |
| 1053 | memberStart->fStart; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1054 | this->firstBlockTrim((int) (blockEnd - blockStart), blockStart); |
| 1055 | this->indentDeferred(IndentKind::kStructMember); |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1056 | fWroteMethod = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1057 | string name(child.fContentStart, (int) (child.fContentEnd - child.fContentStart)); |
Cary Clark | 2dc84ad | 2018-01-26 12:56:22 -0500 | [diff] [blame] | 1058 | Definition* commentBlock = this->findMemberCommentBlock(fBmhStructDef->fChildren, name); |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1059 | if (!commentBlock) { |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1060 | return memberStart->reportError<Definition*>("member missing comment block 2"); |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1061 | } |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1062 | 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1067 | this->writeCommentHeader(); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1068 | this->writeSpace(); |
| 1069 | this->rewriteBlock(lineDef->length(), lineDef->fContentStart, Phrase::kYes); |
| 1070 | this->writeCommentTrailer(OneLine::kYes); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1071 | } |
| 1072 | this->lfcr(); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1073 | this->writeBlock((int) (child.fStart - memberStart->fContentStart), |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1074 | memberStart->fContentStart); |
| 1075 | this->indentToColumn(fStructMemberTab); |
| 1076 | this->writeString(name.c_str()); |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1077 | auto tokenIter = child.fParent->fTokens.begin(); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1078 | 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 Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1090 | valueStart->fContentStart); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1091 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1092 | this->writeString(";"); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1093 | if (fStructMemberLength <= 100) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1094 | this->indentToColumn(fStructCommentTab); |
| 1095 | this->writeString("//!<"); |
| 1096 | this->writeSpace(); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1097 | this->rewriteBlock(lineDef->length(), lineDef->fContentStart, Phrase::kYes); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1098 | } |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1099 | this->lf(1); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1100 | return valueEnd; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1101 | } |
| 1102 | |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1103 | // 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 |
| 1106 | void 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 Clark | b94f6da | 2018-06-08 11:54:32 -0400 | [diff] [blame] | 1161 | bool 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 Clark | b94f6da | 2018-06-08 11:54:32 -0400 | [diff] [blame] | 1173 | return true; |
| 1174 | } |
| 1175 | |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 1176 | void IncludeWriter::structSizeMembers(const Definition& child) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1177 | int longestType = 0; |
| 1178 | Definition* typeStart = nullptr; |
| 1179 | int longestName = 0; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1180 | int longestValue = 0; |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1181 | int longestComment = 0; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1182 | SkASSERT(child.fChildren.size() == 1 || child.fChildren.size() == 2); |
| 1183 | bool inEnum = false; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1184 | bool inMethod = false; |
| 1185 | bool inMember = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1186 | 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 Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1197 | if (inMethod) { |
| 1198 | continue; |
| 1199 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1200 | 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 Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1213 | case KeyWord::kUint8_t: |
| 1214 | case KeyWord::kUint16_t: |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1215 | case KeyWord::kUint32_t: |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1216 | case KeyWord::kUint64_t: |
Cary Clark | 4dc5a45 | 2018-05-21 11:56:57 -0400 | [diff] [blame] | 1217 | case KeyWord::kUintPtr_t: |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1218 | case KeyWord::kSize_t: |
| 1219 | case KeyWord::kFloat: |
| 1220 | case KeyWord::kBool: |
Cary Clark | d2ca79c | 2018-08-10 13:09:13 -0400 | [diff] [blame] | 1221 | case KeyWord::kChar: |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1222 | 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 Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1237 | if (inMethod) { |
| 1238 | if (Punctuation::kColon == token.fPunctuation) { |
| 1239 | inMethod = false; |
| 1240 | } else if (Punctuation::kLeftBrace == token.fPunctuation) { |
| 1241 | inMethod = false; |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1242 | } else if (Punctuation::kSemicolon == token.fPunctuation) { |
| 1243 | inMethod = false; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1244 | } else { |
| 1245 | SkASSERT(0); // incomplete |
| 1246 | } |
| 1247 | } |
| 1248 | if (inMember) { |
| 1249 | SkASSERT(Punctuation::kSemicolon == token.fPunctuation); |
| 1250 | typeStart = nullptr; |
| 1251 | inMember = false; |
| 1252 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1253 | 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 Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1262 | longestType = SkTMax(longestType, (int) (typeStr.fEnd - typeStr.fStart)); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1263 | longestName = SkTMax(longestName, (int) (token.fContentEnd - token.fContentStart)); |
| 1264 | typeStart->fMemberStart = true; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1265 | inMember = true; |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1266 | 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 Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1278 | continue; |
| 1279 | } |
| 1280 | if (MarkType::kMethod == token.fMarkType) { |
| 1281 | inMethod = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1282 | continue; |
| 1283 | } |
| 1284 | SkASSERT(MarkType::kNone == token.fMarkType); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1285 | if (typeStart) { |
| 1286 | if (inMember) { |
| 1287 | longestValue = |
| 1288 | SkTMax(longestValue, (int) (token.fContentEnd - token.fContentStart)); |
| 1289 | } |
| 1290 | } else { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1291 | typeStart = &token; |
| 1292 | } |
| 1293 | } |
| 1294 | fStructMemberTab = longestType + fIndent + 1 /* space before name */ ; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1295 | fStructValueTab = fStructMemberTab + longestName + 2 /* space ; */ ; |
| 1296 | fStructCommentTab = fStructValueTab; |
| 1297 | if (longestValue) { |
| 1298 | fStructCommentTab += longestValue + 3 /* space = space */ ; |
| 1299 | fStructValueTab -= 1 /* ; */ ; |
| 1300 | } |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1301 | fStructMemberLength = fStructCommentTab + longestComment; |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1302 | // 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1305 | } |
| 1306 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1307 | static 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 Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1322 | bool IncludeWriter::populate(Definition* def, ParentPair* prevPair, RootDefinition* root) { |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1323 | if (!def->fTokens.size()) { |
| 1324 | return true; |
| 1325 | } |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1326 | ParentPair pair = { def, prevPair }; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1327 | // 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 Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1333 | Definition* method = nullptr; |
| 1334 | Definition* clonedMethod = nullptr; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1335 | const Definition* memberStart = nullptr; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1336 | const Definition* memberEnd = nullptr; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1337 | fContinuation = nullptr; |
| 1338 | bool inStruct = false; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1339 | bool inConstructor = false; |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 1340 | bool inInline = false; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1341 | bool eatOperator = false; |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1342 | bool sawConst = false; |
| 1343 | bool staticOnly = false; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1344 | bool sawTypedef = false; |
Cary Clark | 27dddae | 2018-06-08 15:57:37 -0400 | [diff] [blame] | 1345 | Definition* deferredTypedefComment = nullptr; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1346 | const Definition* requireDense = nullptr; |
| 1347 | const Definition* startDef = nullptr; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1348 | for (auto& child : def->fTokens) { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1349 | if (KeyWord::kInline == child.fKeyWord) { |
| 1350 | continue; |
| 1351 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1352 | 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 Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1367 | if (memberEnd) { |
| 1368 | if (memberEnd != &child) { |
| 1369 | continue; |
| 1370 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1371 | startDef = &child; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1372 | this->setStart(child.fContentStart + 1, &child); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1373 | memberEnd = nullptr; |
| 1374 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1375 | if (child.fPrivate) { |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 1376 | if (MarkType::kMethod == child.fMarkType) { |
| 1377 | inInline = true; |
| 1378 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1379 | continue; |
| 1380 | } |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 1381 | 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1404 | if (fContinuation) { |
| 1405 | if (Definition::Type::kKeyWord == child.fType) { |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1406 | if (KeyWord::kFriend == child.fKeyWord || |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1407 | KeyWord::kSK_API == child.fKeyWord) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1408 | continue; |
| 1409 | } |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1410 | const IncludeKey& includeKey = kKeyWords[(int) child.fKeyWord]; |
| 1411 | if (KeyProperty::kNumber == includeKey.fProperty) { |
| 1412 | continue; |
| 1413 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1414 | } |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 1415 | if (Definition::Type::kBracket == child.fType) { |
| 1416 | if (Bracket::kAngle == child.fBracket) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1417 | continue; |
| 1418 | } |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 1419 | if (Bracket::kParen == child.fBracket) { |
| 1420 | if (!clonedMethod) { |
| 1421 | if (inConstructor) { |
| 1422 | fContinuation = child.fContentStart; |
| 1423 | } |
| 1424 | continue; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1425 | } |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 1426 | 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 Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1436 | sawConst = false; |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 1437 | break; |
| 1438 | } |
| 1439 | ++alternate; |
| 1440 | string alternateMethod = methodName + '_' + to_string(alternate); |
Cary Clark | 224c700 | 2018-06-27 11:00:21 -0400 | [diff] [blame] | 1441 | clonedMethod = this->findMethod(alternateMethod, root); |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 1442 | } while (clonedMethod); |
| 1443 | if (!clonedMethod) { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1444 | return child.reportError<bool>("cloned method not found"); |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 1445 | } |
| 1446 | clonedMethod = nullptr; |
| 1447 | continue; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1448 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1449 | } |
| 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 Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1464 | Punctuation::kLeftBrace == child.fPunctuation || |
| 1465 | (Punctuation::kColon == child.fPunctuation && inConstructor))) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1466 | SkASSERT(fContinuation[0] == '('); |
| 1467 | const char* continueEnd = child.fContentStart; |
| 1468 | while (continueEnd > fContinuation && isspace(continueEnd[-1])) { |
| 1469 | --continueEnd; |
| 1470 | } |
Cary Clark | ab5c9af | 2018-07-12 16:24:53 -0400 | [diff] [blame] | 1471 | 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 Clark | 82f1f74 | 2018-06-28 08:50:35 -0400 | [diff] [blame] | 1478 | if (string::npos != methodName.find('\n')) { |
| 1479 | methodName.erase(std::remove(methodName.begin(), methodName.end(), '\n'), |
| 1480 | methodName.end()); |
| 1481 | } |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1482 | method = this->findMethod(methodName, root); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1483 | if (!method) { |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 1484 | if (fBmhStructDef && fBmhStructDef->fDeprecated) { |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1485 | fContinuation = nullptr; |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 1486 | continue; |
| 1487 | } |
Cary Clark | 06c20f3 | 2018-03-20 15:53:27 -0400 | [diff] [blame] | 1488 | return child.reportError<bool>("method not found"); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1489 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1490 | this->methodOut(method, child); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1491 | sawConst = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1492 | continue; |
| 1493 | } |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1494 | if (Definition::Type::kPunctuation == child.fType && |
| 1495 | Punctuation::kAsterisk == child.fPunctuation && |
| 1496 | clonedMethod) { |
| 1497 | continue; |
| 1498 | } |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1499 | if (inConstructor) { |
| 1500 | continue; |
| 1501 | } |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1502 | method = this->findMethod(methodName + "()", root); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1503 | if (method) { |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1504 | if (method->fCloned) { |
| 1505 | clonedMethod = method; |
| 1506 | continue; |
| 1507 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1508 | this->methodOut(method, child); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1509 | sawConst = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1510 | continue; |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 1511 | } else if (fBmhStructDef && fBmhStructDef->fDeprecated) { |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1512 | fContinuation = nullptr; |
| 1513 | continue; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1514 | } |
Cary Clark | 06c20f3 | 2018-03-20 15:53:27 -0400 | [diff] [blame] | 1515 | return child.reportError<bool>("method not found"); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1516 | } |
| 1517 | if (Bracket::kSlashSlash == child.fBracket || Bracket::kSlashStar == child.fBracket) { |
| 1518 | if (!fDeferComment) { |
| 1519 | fDeferComment = &child; |
| 1520 | } |
| 1521 | continue; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1522 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1523 | if (MarkType::kMethod == child.fMarkType) { |
Cary Clark | 2d4bf5f | 2018-04-16 08:37:38 -0400 | [diff] [blame] | 1524 | if (this->isInternalName(child)) { |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1525 | continue; |
| 1526 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1527 | const char* bodyEnd = fDeferComment ? fDeferComment->fContentStart - 1 : |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1528 | fAttrDeprecated ? fAttrDeprecated->fContentStart - 1 : |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1529 | child.fContentStart; |
Cary Clark | 7cfcbca | 2018-01-04 16:11:51 -0500 | [diff] [blame] | 1530 | 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1539 | // FIXME: roll end-trimming into writeBlockTrim call |
| 1540 | while (fStart < bodyEnd && ' ' >= bodyEnd[-1]) { |
| 1541 | --bodyEnd; |
| 1542 | } |
| 1543 | int blockSize = (int) (bodyEnd - fStart); |
Cary Clark | 2d4bf5f | 2018-04-16 08:37:38 -0400 | [diff] [blame] | 1544 | SkASSERT(blockSize >= 0); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1545 | if (blockSize) { |
Cary Clark | 7cfcbca | 2018-01-04 16:11:51 -0500 | [diff] [blame] | 1546 | string debugstr(fStart, blockSize); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1547 | this->writeBlock(blockSize, fStart); |
| 1548 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1549 | startDef = &child; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1550 | this->setStart(child.fContentStart, &child); |
Cary Clark | 06c20f3 | 2018-03-20 15:53:27 -0400 | [diff] [blame] | 1551 | auto mapFind = fBmhParser->fMethodMap.find(child.fName); |
| 1552 | if (fBmhParser->fMethodMap.end() != mapFind) { |
| 1553 | inConstructor = false; |
| 1554 | method = &mapFind->second; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1555 | methodName = child.fName; |
Cary Clark | 06c20f3 | 2018-03-20 15:53:27 -0400 | [diff] [blame] | 1556 | } else { |
| 1557 | methodName = root->fName + "::" + child.fName; |
Cary Clark | 224c700 | 2018-06-27 11:00:21 -0400 | [diff] [blame] | 1558 | size_t lastName = root->fName.rfind(':'); |
| 1559 | lastName = string::npos == lastName ? 0 : lastName + 1; |
| 1560 | inConstructor = root->fName.substr(lastName) == child.fName; |
Cary Clark | 06c20f3 | 2018-03-20 15:53:27 -0400 | [diff] [blame] | 1561 | method = root->find(methodName, RootDefinition::AllowParens::kNo); |
| 1562 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1563 | fContinuation = child.fContentEnd; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1564 | if (!method) { |
| 1565 | continue; |
| 1566 | } |
| 1567 | if (method->fCloned) { |
| 1568 | clonedMethod = method; |
| 1569 | continue; |
| 1570 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1571 | this->methodOut(method, child); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1572 | sawConst = false; |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1573 | if (fAttrDeprecated) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1574 | startDef = fAttrDeprecated; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1575 | this->setStartBack(fAttrDeprecated->fContentStart, fAttrDeprecated); |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1576 | fAttrDeprecated = nullptr; |
| 1577 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1578 | continue; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1579 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1580 | if (Definition::Type::kKeyWord == child.fType) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1581 | switch (child.fKeyWord) { |
| 1582 | case KeyWord::kStruct: |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1583 | case KeyWord::kClass: |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1584 | fICSStack.push_back(&child); |
| 1585 | fStructEnded = false; |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1586 | fStructMemberTab = 0; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1587 | // if struct contains members, compute their name and comment tabs |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1588 | 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1597 | if (fInStruct) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1598 | // try child; root+child; root->parent+child; etc. |
| 1599 | int trial = 0; |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1600 | RootDefinition* search = root; |
| 1601 | Definition* parent = search->fParent; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1602 | do { |
| 1603 | string name; |
| 1604 | if (0 == trial) { |
| 1605 | name = child.fName; |
| 1606 | } else if (1 == trial) { |
| 1607 | name = root->fName + "::" + child.fName; |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1608 | } else if (2 == trial) { |
| 1609 | name = root->fName; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1610 | } 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 Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1618 | root = fBmhStructDef->asRoot(); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1619 | SkASSERT(root); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1620 | fIndent += 4; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1621 | this->structSizeMembers(child); |
| 1622 | fIndent -= 4; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1623 | SkASSERT(!fIndentNext); |
| 1624 | fIndentNext = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1625 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1626 | if (child.fChildren.size() > 0) { |
| 1627 | const char* bodyEnd = fDeferComment ? fDeferComment->fContentStart - 1 : |
| 1628 | child.fContentStart; |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1629 | this->writeBlockTrim((int) (bodyEnd - fStart), fStart); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1630 | if (fPendingMethod) { |
Cary Clark | 27dddae | 2018-06-08 15:57:37 -0400 | [diff] [blame] | 1631 | if (fIndent >= 4) { |
| 1632 | this->indentOut(); |
| 1633 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1634 | fPendingMethod = false; |
| 1635 | } |
| 1636 | startDef = requireDense ? requireDense : &child; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1637 | if (requireDense) { |
| 1638 | startDef = requireDense; |
| 1639 | this->setStart(requireDense->fContentStart, requireDense); |
| 1640 | } else { |
| 1641 | startDef = &child; |
| 1642 | this->setStart(child.fContentStart, &child); |
| 1643 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1644 | requireDense = nullptr; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1645 | if (!fInStruct && (!root || child.fName != root->fName)) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1646 | root = &fBmhParser->fClassMap[child.fName]; |
| 1647 | fRootTopic = root->fParent; |
| 1648 | SkASSERT(!root->fVisited); |
| 1649 | root->clearVisited(); |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1650 | #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 Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1657 | fIndent = 0; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1658 | #endif |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1659 | fBmhStructDef = root; |
| 1660 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1661 | if (child.fName == root->fName) { |
| 1662 | if (Definition* parent = root->fParent) { |
| 1663 | if (MarkType::kTopic == parent->fMarkType || |
| 1664 | MarkType::kSubtopic == parent->fMarkType) { |
Cary Clark | e4aa371 | 2017-09-15 02:56:12 -0400 | [diff] [blame] | 1665 | const char* commentStart = root->fContentStart; |
| 1666 | const char* commentEnd = root->fChildren[0]->fStart; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1667 | this->structOut(root, *root, commentStart, commentEnd); |
| 1668 | } else { |
| 1669 | SkASSERT(0); // incomplete |
| 1670 | } |
| 1671 | } else { |
| 1672 | SkASSERT(0); // incomplete |
| 1673 | } |
| 1674 | } else { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1675 | SkASSERT(fInStruct); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1676 | Definition* priorBlock = fBmhStructDef; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1677 | Definition* codeBlock = nullptr; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1678 | Definition* nextBlock = nullptr; |
| 1679 | for (auto test : fBmhStructDef->fChildren) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1680 | if (MarkType::kCode == test->fMarkType) { |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1681 | SkASSERT(!codeBlock); // FIXME: check enum earlier |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1682 | codeBlock = test; |
| 1683 | continue; |
| 1684 | } |
| 1685 | if (codeBlock) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1686 | nextBlock = test; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1687 | break; |
| 1688 | } |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1689 | priorBlock = test; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1690 | } |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1691 | // FIXME: trigger error earlier if inner #Struct or #Class is missing #Code |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 1692 | if (!fBmhStructDef->fDeprecated) { |
| 1693 | SkASSERT(codeBlock); |
| 1694 | SkASSERT(nextBlock); // FIXME: check enum for correct order earlier |
Cary Clark | 78c110e | 2018-02-09 16:49:09 -0500 | [diff] [blame] | 1695 | const char* commentStart = codeBlock->fTerminator; |
| 1696 | const char* commentEnd = nextBlock->fStart; |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1697 | // 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 Clark | 78c110e | 2018-02-09 16:49:09 -0500 | [diff] [blame] | 1717 | fIndentNext = true; |
| 1718 | this->structOut(root, *fBmhStructDef, commentStart, commentEnd); |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 1719 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1720 | } |
| 1721 | fDeferComment = nullptr; |
| 1722 | } else { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1723 | // 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1739 | } |
| 1740 | break; |
| 1741 | case KeyWord::kEnum: { |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1742 | fInEnum = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1743 | this->enumHeaderOut(root, child); |
| 1744 | this->enumSizeItems(child); |
| 1745 | } break; |
| 1746 | case KeyWord::kConst: |
| 1747 | case KeyWord::kConstExpr: |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1748 | sawConst = !memberStart || staticOnly; |
| 1749 | if (!memberStart) { |
| 1750 | memberStart = &child; |
| 1751 | staticOnly = true; |
| 1752 | } |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1753 | 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 Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1761 | break; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1762 | case KeyWord::kStatic: |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1763 | if (!memberStart) { |
| 1764 | memberStart = &child; |
| 1765 | staticOnly = true; |
| 1766 | } |
| 1767 | break; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1768 | case KeyWord::kInt: |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1769 | case KeyWord::kUint8_t: |
| 1770 | case KeyWord::kUint16_t: |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1771 | case KeyWord::kUint32_t: |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1772 | case KeyWord::kUint64_t: |
Cary Clark | 4dc5a45 | 2018-05-21 11:56:57 -0400 | [diff] [blame] | 1773 | case KeyWord::kUintPtr_t: |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1774 | case KeyWord::kUnsigned: |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1775 | case KeyWord::kSize_t: |
| 1776 | case KeyWord::kFloat: |
| 1777 | case KeyWord::kBool: |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 1778 | case KeyWord::kChar: |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1779 | case KeyWord::kVoid: |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1780 | staticOnly = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1781 | if (!memberStart) { |
| 1782 | memberStart = &child; |
| 1783 | } |
| 1784 | break; |
Cary Clark | d2ca79c | 2018-08-10 13:09:13 -0400 | [diff] [blame] | 1785 | case KeyWord::kAlignAs: |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1786 | case KeyWord::kPublic: |
| 1787 | case KeyWord::kPrivate: |
| 1788 | case KeyWord::kProtected: |
| 1789 | case KeyWord::kFriend: |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1790 | case KeyWord::kInline: |
| 1791 | case KeyWord::kSK_API: |
Cary Clark | bbfda25 | 2018-03-09 15:32:01 -0500 | [diff] [blame] | 1792 | case KeyWord::kTemplate: |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1793 | break; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1794 | case KeyWord::kTypedef: |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1795 | SkASSERT(!memberStart); |
| 1796 | memberStart = &child; |
Cary Clark | 27dddae | 2018-06-08 15:57:37 -0400 | [diff] [blame] | 1797 | deferredTypedefComment = fDeferComment; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1798 | sawTypedef = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1799 | break; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1800 | case KeyWord::kSK_BEGIN_REQUIRE_DENSE: |
| 1801 | requireDense = &child; |
| 1802 | break; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1803 | default: |
| 1804 | SkASSERT(0); |
| 1805 | } |
Cary Clark | d98f78c | 2018-04-26 08:32:37 -0400 | [diff] [blame] | 1806 | if (KeyWord::kUint8_t == child.fKeyWord || KeyWord::kUint32_t == child.fKeyWord) { |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1807 | continue; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1808 | } else { |
Cary Clark | 87f71c0 | 2018-08-10 14:33:14 -0400 | [diff] [blame] | 1809 | if (fInEnum && child.fChildren.size() > 0 |
| 1810 | && KeyWord::kClass == child.fChildren[0]->fKeyWord) { |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1811 | if (!this->populate(child.fChildren[0], &pair, root)) { |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1812 | return false; |
| 1813 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1814 | } else { |
| 1815 | if (!this->populate(&child, &pair, root)) { |
| 1816 | return false; |
| 1817 | } |
| 1818 | if (KeyWord::kClass == child.fKeyWord || KeyWord::kStruct == child.fKeyWord) { |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1819 | fICSStack.pop_back(); |
| 1820 | fStructEnded = true; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1821 | 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 Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1828 | #if 0 |
| 1829 | } |
| 1830 | if (MarkType::kStruct == root->fMarkType || |
| 1831 | MarkType::kClass == root->fMarkType) { |
| 1832 | #else |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1833 | SkASSERT(MarkType::kStruct == root->fMarkType || |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1834 | MarkType::kClass == root->fMarkType); |
| 1835 | #endif |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1836 | fPendingMethod = false; |
| 1837 | if (startDef) { |
| 1838 | fPendingMethod = find_start(startDef, fStart); |
| 1839 | } |
| 1840 | fOutdentNext = !fPendingMethod; |
| 1841 | } |
| 1842 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | continue; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1846 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1847 | if (Definition::Type::kBracket == child.fType) { |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1848 | if (KeyWord::kEnum == child.fParent->fKeyWord || |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1849 | (KeyWord::kClass == child.fParent->fKeyWord && child.fParent->fParent && |
| 1850 | KeyWord::kEnum == child.fParent->fParent->fKeyWord)) { |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1851 | SkASSERT(Bracket::kBrace == child.fBracket); |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1852 | this->enumMembersOut(*child.fParent); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1853 | this->writeString("};"); |
| 1854 | this->lf(2); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1855 | startDef = child.fParent; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1856 | this->setStart(child.fParent->fContentEnd, child.fParent); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1857 | SkASSERT(';' == fStart[0]); |
| 1858 | ++fStart; |
| 1859 | fDeferComment = nullptr; |
| 1860 | fInEnum = false; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1861 | if (fIndentNext) { |
| 1862 | // fIndent -= 4; |
| 1863 | fIndentNext = false; |
| 1864 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1865 | continue; |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1866 | } |
| 1867 | if (fAttrDeprecated) { |
| 1868 | continue; |
| 1869 | } |
Cary Clark | b94f6da | 2018-06-08 11:54:32 -0400 | [diff] [blame] | 1870 | if (KeyWord::kDefine == child.fKeyWord && this->defineOut(child)) { |
| 1871 | fDeferComment = nullptr; |
| 1872 | continue; |
| 1873 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1874 | fDeferComment = nullptr; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1875 | if (KeyWord::kClass == def->fKeyWord || KeyWord::kStruct == def->fKeyWord) { |
| 1876 | fIndentNext = true; |
| 1877 | } |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1878 | if (!this->populate(&child, &pair, root)) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1879 | return false; |
| 1880 | } |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1881 | 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1888 | continue; |
| 1889 | } |
| 1890 | if (Definition::Type::kWord == child.fType) { |
| 1891 | if (MarkType::kMember == child.fMarkType) { |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 1892 | if (!memberStart) { |
| 1893 | auto iter = def->fTokens.begin(); |
| 1894 | std::advance(iter, child.fParentIndex - 1); |
| 1895 | memberStart = &*iter; |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1896 | staticOnly = false; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1897 | } |
| 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 Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 1904 | } |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1905 | SkASSERT(fBmhStructDef); |
| 1906 | if (!fBmhStructDef->fDeprecated) { |
| 1907 | memberEnd = this->structMemberOut(memberStart, child); |
| 1908 | startDef = &child; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1909 | this->setStart(child.fContentEnd + 1, &child); |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1910 | fDeferComment = nullptr; |
| 1911 | } |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 1912 | } else if (MarkType::kNone == child.fMarkType && sawConst |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1913 | && 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 Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1927 | this->constOut(memberStart, bmhConst); |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1928 | fDeferComment = nullptr; |
| 1929 | sawConst = false; |
| 1930 | } |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1931 | } 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 Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1950 | } |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1951 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1952 | if (child.fMemberStart) { |
| 1953 | memberStart = &child; |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 1954 | staticOnly = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1955 | } |
Cary Clark | 89b1456 | 2018-03-19 09:04:10 -0400 | [diff] [blame] | 1956 | if (kAttrDeprecatedLen == (size_t) (child.fContentEnd - child.fContentStart) && |
| 1957 | !strncmp(gAttrDeprecated, child.fStart, kAttrDeprecatedLen)) { |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1958 | fAttrDeprecated = &child; |
| 1959 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1960 | continue; |
| 1961 | } |
| 1962 | if (Definition::Type::kPunctuation == child.fType) { |
| 1963 | if (Punctuation::kSemicolon == child.fPunctuation) { |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1964 | 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 Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1978 | fDeferComment = nullptr; |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1979 | } |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 1980 | // 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 Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 2008 | this->setStart(child.fContentStart + 1, &child); |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 2009 | fDeferComment = nullptr; |
| 2010 | fBmhConst = nullptr; |
| 2011 | sawConst = false; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 2012 | } 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 Clark | 27dddae | 2018-06-08 15:57:37 -0400 | [diff] [blame] | 2028 | fDeferComment = deferredTypedefComment; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 2029 | this->constOut(memberStart, bmhTypedef); |
| 2030 | fDeferComment = nullptr; |
| 2031 | sawTypedef = false; |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 2032 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2033 | memberStart = nullptr; |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 2034 | staticOnly = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2035 | 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 | |
| 2051 | bool IncludeWriter::populate(BmhParser& bmhParser) { |
| 2052 | bool allPassed = true; |
| 2053 | for (auto& includeMapper : fIncludeMap) { |
| 2054 | size_t lastSlash = includeMapper.first.rfind('/'); |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 2055 | if (string::npos == lastSlash) { |
| 2056 | lastSlash = includeMapper.first.rfind('\\'); |
| 2057 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2058 | 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 Clark | 80247e5 | 2018-07-11 16:18:41 -0400 | [diff] [blame] | 2066 | this->reset(); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2067 | 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 Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 2072 | RootDefinition* root = |
| 2073 | bmhParser.fClassMap.end() == bmhParser.fClassMap.find(skClassName) ? |
| 2074 | nullptr : &bmhParser.fClassMap[skClassName]; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2075 | fBmhParser = &bmhParser; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 2076 | 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 Clark | 2d4bf5f | 2018-04-16 08:37:38 -0400 | [diff] [blame] | 2087 | fFileName = includeMapper.second.fFileName; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 2088 | this->setStartBack(includeMapper.second.fContentStart, &includeMapper.second); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2089 | fEnd = includeMapper.second.fContentEnd; |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 2090 | fAnonymousEnumCount = 1; |
Cary Clark | b94f6da | 2018-06-08 11:54:32 -0400 | [diff] [blame] | 2091 | this->writeHeader(includeMapper); |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 2092 | allPassed &= this->populate(&includeMapper.second, nullptr, root); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2093 | this->writeBlock((int) (fEnd - fStart), fStart); |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 2094 | #if 0 |
| 2095 | if (fIndentStack.size() > 0) { |
| 2096 | this->indentOut(); |
| 2097 | } |
| 2098 | SkASSERT(!fIndent); |
| 2099 | #else |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2100 | fIndent = 0; |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 2101 | #endif |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2102 | this->lfcr(); |
| 2103 | this->writePending(); |
| 2104 | fclose(fOut); |
Cary Clark | 7cfcbca | 2018-01-04 16:11:51 -0500 | [diff] [blame] | 2105 | 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 Clark | 5b1f953 | 2018-08-28 14:53:37 -0400 | [diff] [blame] | 2116 | if (ParserCommon::WrittenFileDiffers(fileName, readname)) { |
Cary Clark | 7cfcbca | 2018-01-04 16:11:51 -0500 | [diff] [blame] | 2117 | SkDebugf("wrote updated %s\n", fileName.c_str()); |
| 2118 | } else { |
| 2119 | remove(fileName.c_str()); |
| 2120 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2121 | } |
| 2122 | return allPassed; |
| 2123 | } |
| 2124 | |
| 2125 | // change Xxx_Xxx to xxx xxx |
| 2126 | static 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2140 | string IncludeWriter::resolveMethod(const char* start, const char* end, bool first) { |
| 2141 | string methodname(start, end - start); |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2142 | if (string::npos != methodname.find("()")) { |
| 2143 | return ""; |
| 2144 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2145 | string substitute; |
| 2146 | auto rootDefIter = fBmhParser->fMethodMap.find(methodname); |
| 2147 | if (fBmhParser->fMethodMap.end() != rootDefIter) { |
| 2148 | substitute = methodname + "()"; |
| 2149 | } else { |
Cary Clark | 1eace2d | 2017-07-31 07:52:43 -0400 | [diff] [blame] | 2150 | 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 Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 2158 | 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 Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 2179 | } |
| 2180 | } |
| 2181 | } |
| 2182 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2183 | } |
| 2184 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2185 | if (fMethodDef && methodname == fMethodDef->fName) { |
| 2186 | TextParser report(fBmhMethod); |
| 2187 | report.reportError("method should not include references to itself"); |
| 2188 | return ""; |
| 2189 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 2190 | 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2200 | return substitute; |
| 2201 | } |
| 2202 | |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 2203 | string 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 Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2215 | string IncludeWriter::resolveRef(const char* start, const char* end, bool first, |
| 2216 | RefType* refType) { |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 2217 | // look up Xxx_Xxx |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2218 | string undername(start, end - start); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2219 | 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2226 | SkASSERT(string::npos == undername.find(' ')); |
| 2227 | const Definition* rootDef = nullptr; |
Cary Clark | 78c110e | 2018-02-09 16:49:09 -0500 | [diff] [blame] | 2228 | string substitute; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2229 | { |
| 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 Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 2238 | } else if (fBmhStructDef) { |
| 2239 | string localPrefix = fBmhStructDef->fFiddle + '_' + undername; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2240 | rootDefIter = fBmhParser->fTopicMap.find(localPrefix); |
| 2241 | if (fBmhParser->fTopicMap.end() != rootDefIter) { |
| 2242 | rootDef = rootDefIter->second; |
| 2243 | } |
Cary Clark | 78c110e | 2018-02-09 16:49:09 -0500 | [diff] [blame] | 2244 | 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 Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 2252 | if (!rootDef && fEnumDef && "Sk" + prefixedName == fEnumDef->fFiddle) { |
| 2253 | rootDef = fEnumDef; |
| 2254 | } |
Cary Clark | 78c110e | 2018-02-09 16:49:09 -0500 | [diff] [blame] | 2255 | if (!rootDef && !substitute.length()) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2256 | auto aliasIter = fBmhParser->fAliasMap.find(undername); |
| 2257 | if (fBmhParser->fAliasMap.end() != aliasIter) { |
Cary Clark | 78c110e | 2018-02-09 16:49:09 -0500 | [diff] [blame] | 2258 | rootDef = aliasIter->second; |
Cary Clark | 186d08f | 2018-04-03 08:43:27 -0400 | [diff] [blame] | 2259 | } else if (fInEnum && fEnumDef && this->findEnumSubtopic(undername, &rootDef)) { |
| 2260 | ; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2261 | } else if (!first) { |
Cary Clark | 186d08f | 2018-04-03 08:43:27 -0400 | [diff] [blame] | 2262 | this->fChar = start; |
Cary Clark | ffb3d68 | 2018-05-17 12:17:28 -0400 | [diff] [blame] | 2263 | this->fLine = start; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2264 | this->reportError("reference unfound"); |
| 2265 | return ""; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2266 | } |
| 2267 | } |
| 2268 | } |
| 2269 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2270 | if (rootDef) { |
Cary Clark | 78c110e | 2018-02-09 16:49:09 -0500 | [diff] [blame] | 2271 | MarkType rootType = rootDef->fMarkType; |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 2272 | if (MarkType::kSubtopic == rootType || MarkType::kTopic == rootType |
| 2273 | || MarkType::kAlias == rootType) { |
| 2274 | substitute = this->resolveAlias(rootDef); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2275 | } |
| 2276 | if (!substitute.length()) { |
Cary Clark | 78c110e | 2018-02-09 16:49:09 -0500 | [diff] [blame] | 2277 | 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 Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 2283 | auto parent = MarkType::kAlias == rootType ? rootDef->fParent : rootDef; |
Cary Clark | 78c110e | 2018-02-09 16:49:09 -0500 | [diff] [blame] | 2284 | 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2327 | if (parent) { |
| 2328 | if (MarkType::kClass == parent->fMarkType || |
| 2329 | MarkType::kStruct == parent->fMarkType || |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 2330 | (MarkType::kEnum == parent->fMarkType && !parent->fAnonymous) || |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2331 | MarkType::kEnumClass == parent->fMarkType) { |
| 2332 | if (parent->fParent != fRootTopic) { |
| 2333 | substitute = parent->fName; |
Cary Clark | 2a8c48b | 2018-02-15 17:31:24 -0500 | [diff] [blame] | 2334 | substitute += ' '; |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 2335 | substitute += ConvertRef(rootDef->fName, false); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2336 | } else { |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 2337 | 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2353 | substitute += ConvertRef(undername, first); |
| 2354 | } |
| 2355 | } |
| 2356 | } |
| 2357 | } |
| 2358 | } |
Cary Clark | 1eace2d | 2017-07-31 07:52:43 -0400 | [diff] [blame] | 2359 | // 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2363 | return substitute; |
| 2364 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2365 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2366 | int IncludeWriter::lookupMethod(const PunctuationState punctuation, const Word word, |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 2367 | const int lastSpace, const int run, int lastWrite, const char* data, |
| 2368 | bool hasIndirection) { |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2369 | int wordStart = lastSpace; |
| 2370 | while (' ' >= data[wordStart]) { |
| 2371 | ++wordStart; |
| 2372 | } |
| 2373 | const int wordEnd = PunctuationState::kDelimiter == punctuation || |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 2374 | PunctuationState::kParen == punctuation || |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2375 | PunctuationState::kPeriod == punctuation ? run - 1 : run; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 2376 | 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2389 | if (temp.length()) { |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2390 | if (wordStart > lastWrite) { |
| 2391 | SkASSERT(data[wordStart - 1] >= ' '); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2392 | if (' ' == data[lastWrite]) { |
| 2393 | this->writeSpace(); |
| 2394 | } |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 2395 | this->firstBlockTrim(wordStart - lastWrite, &data[lastWrite]); |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2396 | if (' ' == data[wordStart - 1]) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2397 | this->writeSpace(); |
| 2398 | } |
| 2399 | } |
| 2400 | SkASSERT(temp[temp.length() - 1] > ' '); |
| 2401 | this->writeString(temp.c_str()); |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2402 | lastWrite = wordEnd; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2403 | } |
| 2404 | return lastWrite; |
| 2405 | } |
| 2406 | |
| 2407 | int 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 Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 2410 | PunctuationState::kParen == punctuation || |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2411 | PunctuationState::kPeriod == punctuation ? run - 1 : run; |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2412 | 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2415 | if (!temp.length()) { |
| 2416 | if (Word::kFirst != word && '_' != last) { |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2417 | temp = ConvertRef(resolved, false); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2418 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 2419 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2420 | if (temp.length()) { |
| 2421 | if (start > lastWrite) { |
| 2422 | SkASSERT(data[start - 1] >= ' '); |
| 2423 | if (' ' == data[lastWrite]) { |
| 2424 | this->writeSpace(); |
| 2425 | } |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 2426 | this->firstBlockTrim(start - lastWrite, &data[lastWrite]); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2427 | 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 Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 2439 | IncludeWriter::Wrote IncludeWriter::rewriteBlock(int size, const char* data, Phrase phrase) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2440 | 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 Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 2451 | if (fReturnOnWrite) { |
| 2452 | return Wrote::kChars; |
| 2453 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2454 | int run = 0; |
| 2455 | Word word = Word::kStart; |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 2456 | PunctuationState punctuation = Phrase::kNo == phrase ? |
| 2457 | PunctuationState::kStart : PunctuationState::kSpace; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2458 | int start = 0; |
| 2459 | int lastWrite = 0; |
| 2460 | int lineFeeds = 0; |
| 2461 | int lastPrintable = 0; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2462 | int lastSpace = -1; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2463 | char c = 0; |
Kevin Lubick | 4284613 | 2018-01-05 10:11:11 -0500 | [diff] [blame] | 2464 | char last = 0; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 2465 | bool embeddedIndirection = false; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2466 | bool embeddedSymbol = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2467 | bool hasLower = false; |
| 2468 | bool hasUpper = false; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 2469 | bool hasIndirection = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2470 | 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 Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2479 | lastWrite++; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2480 | } |
| 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 Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2511 | if (hasUpper && hasLower && !hasSymbol && lastSpace > 0) { |
| 2512 | lastWrite = this->lookupMethod(punctuation, word, lastSpace, run, |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 2513 | lastWrite, data, hasIndirection); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2514 | } |
| 2515 | break; |
| 2516 | default: |
| 2517 | SkASSERT(0); |
| 2518 | } |
Cary Clark | 1eace2d | 2017-07-31 07:52:43 -0400 | [diff] [blame] | 2519 | punctuation = PunctuationState::kPeriod == punctuation || |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 2520 | (PunctuationState::kStart == punctuation && ' ' >= last) ? |
Cary Clark | 1eace2d | 2017-07-31 07:52:43 -0400 | [diff] [blame] | 2521 | PunctuationState::kStart : PunctuationState::kSpace; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2522 | word = Word::kStart; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 2523 | embeddedIndirection = false; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2524 | embeddedSymbol = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2525 | hasLower = false; |
| 2526 | hasUpper = false; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 2527 | hasIndirection = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2528 | hasSymbol = false; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2529 | lastSpace = run; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2530 | break; |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 2531 | case '.': case ',': case ';': case ':': case ')': |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2532 | 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 Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 2543 | punctuation = '.' == c ? PunctuationState::kPeriod : |
| 2544 | PunctuationState::kDelimiter; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2545 | break; |
| 2546 | default: |
| 2547 | SkASSERT(0); |
| 2548 | } |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 2549 | ('.' == c ? embeddedIndirection : embeddedSymbol) = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2550 | break; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 2551 | case '>': |
| 2552 | if ('-' == last) { |
| 2553 | embeddedIndirection = true; |
| 2554 | break; |
| 2555 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2556 | case '\'': // possessive apostrophe isn't treated as delimiting punctation |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2557 | case '\"': // quote is passed straight through |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2558 | case '=': |
| 2559 | case '!': // assumed not to be punctuation, but a programming symbol |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 2560 | case '&': case '<': case '{': case '}': case '/': case '*': case '[': case ']': |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2561 | word = Word::kMixed; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2562 | embeddedSymbol = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2563 | break; |
| 2564 | case '(': |
| 2565 | if (' ' == last) { |
Cary Clark | c68ba1d | 2018-02-20 10:35:29 -0500 | [diff] [blame] | 2566 | punctuation = PunctuationState::kParen; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2567 | } else { |
| 2568 | word = Word::kMixed; |
| 2569 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2570 | embeddedSymbol = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2571 | break; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2572 | 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 Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2587 | hasSymbol |= embeddedSymbol; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2588 | break; |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 2589 | case '+': |
| 2590 | // hackery to allow C++ |
| 2591 | SkASSERT('C' == last || '+' == last); // FIXME: don't allow + outside of #Formula |
| 2592 | break; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2593 | 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 Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 2606 | if (!isupper(last) && '~' != last) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2607 | 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2625 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 2626 | hasIndirection |= embeddedIndirection; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2627 | hasSymbol |= embeddedSymbol; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2628 | 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 Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 2634 | case 'z': |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2635 | case '0': case '1': case '2': case '3': case '4': |
| 2636 | case '5': case '6': case '7': case '8': case '9': |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 2637 | case '%': // to do : ensure that preceding is a number |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2638 | 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 Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 2653 | hasIndirection |= embeddedIndirection; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2654 | hasSymbol |= embeddedSymbol; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2655 | break; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 2656 | 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 Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2664 | 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 Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2671 | } else if (word == Word::kMixed && hasUpper && hasLower && !hasSymbol && lastSpace > 0) { |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 2672 | lastWrite = this->lookupMethod(punctuation, word, lastSpace, run, lastWrite, data, |
| 2673 | hasIndirection && !hasSymbol); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2674 | } |
| 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 Clark | b94f6da | 2018-06-08 11:54:32 -0400 | [diff] [blame] | 2683 | |
| 2684 | static 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 | |
| 2690 | bool 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 Clark | d2ca79c | 2018-08-10 13:09:13 -0400 | [diff] [blame] | 2697 | this->skipWhiteSpace(); |
Cary Clark | b94f6da | 2018-06-08 11:54:32 -0400 | [diff] [blame] | 2698 | 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 Clark | 27dddae | 2018-06-08 15:57:37 -0400 | [diff] [blame] | 2786 | 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 Clark | b94f6da | 2018-06-08 11:54:32 -0400 | [diff] [blame] | 2792 | fStart = fChar; |
Cary Clark | 27dddae | 2018-06-08 15:57:37 -0400 | [diff] [blame] | 2793 | |
Cary Clark | b94f6da | 2018-06-08 11:54:32 -0400 | [diff] [blame] | 2794 | return true; |
| 2795 | } |