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" |
| 9 | |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 10 | void IncludeWriter::descriptionOut(const Definition* def) { |
| 11 | const char* commentStart = def->fContentStart; |
| 12 | int commentLen = (int) (def->fContentEnd - commentStart); |
| 13 | bool breakOut = false; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 14 | SkDEBUGCODE(bool wroteCode = false); |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 15 | for (auto prop : def->fChildren) { |
| 16 | switch (prop->fMarkType) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 17 | case MarkType::kCode: { |
| 18 | bool literal = false; |
| 19 | bool literalOutdent = false; |
| 20 | commentLen = (int) (prop->fStart - commentStart); |
| 21 | if (commentLen > 0) { |
| 22 | SkASSERT(commentLen < 1000); |
| 23 | if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart, Phrase::kNo)) { |
| 24 | this->lf(2); |
| 25 | } |
| 26 | } |
| 27 | size_t childSize = prop->fChildren.size(); |
| 28 | if (childSize) { |
| 29 | SkASSERT(1 == childSize || 2 == childSize); // incomplete |
| 30 | SkASSERT(MarkType::kLiteral == prop->fChildren[0]->fMarkType); |
| 31 | SkASSERT(1 == childSize || MarkType::kOutdent == prop->fChildren[1]->fMarkType); |
| 32 | commentStart = prop->fChildren[childSize - 1]->fContentStart; |
| 33 | literal = true; |
| 34 | literalOutdent = 2 == childSize && |
| 35 | MarkType::kOutdent == prop->fChildren[1]->fMarkType; |
| 36 | } |
| 37 | commentLen = (int) (prop->fContentEnd - commentStart); |
| 38 | SkASSERT(commentLen > 0); |
| 39 | if (literal) { |
| 40 | if (!literalOutdent) { |
| 41 | fIndent += 4; |
| 42 | } |
| 43 | this->writeBlockIndent(commentLen, commentStart); |
| 44 | this->lf(2); |
| 45 | if (!literalOutdent) { |
| 46 | fIndent -= 4; |
| 47 | } |
| 48 | commentStart = prop->fTerminator; |
| 49 | SkDEBUGCODE(wroteCode = true); |
| 50 | } |
| 51 | } break; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 52 | case MarkType::kDefinedBy: |
| 53 | commentStart = prop->fTerminator; |
| 54 | break; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 55 | case MarkType::kDeprecated: |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 56 | case MarkType::kPrivate: |
| 57 | commentLen = (int) (prop->fStart - commentStart); |
| 58 | if (commentLen > 0) { |
| 59 | SkASSERT(commentLen < 1000); |
| 60 | if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart, Phrase::kNo)) { |
| 61 | this->lfcr(); |
| 62 | } |
| 63 | } |
| 64 | commentStart = prop->fContentStart; |
| 65 | commentLen = (int) (prop->fContentEnd - commentStart); |
| 66 | if (commentLen > 0) { |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 67 | this->writeBlockIndent(commentLen, commentStart); |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 68 | if ('\n' != commentStart[commentLen - 1] && '\n' == commentStart[commentLen]) { |
| 69 | this->lfcr(); |
| 70 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 71 | } |
| 72 | commentStart = prop->fTerminator; |
| 73 | commentLen = (int) (def->fContentEnd - commentStart); |
| 74 | break; |
| 75 | case MarkType::kExperimental: |
| 76 | this->writeString("EXPERIMENTAL:"); |
| 77 | this->writeSpace(); |
| 78 | commentStart = prop->fContentStart; |
| 79 | commentLen = (int) (prop->fContentEnd - commentStart); |
| 80 | if (commentLen > 0) { |
| 81 | if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart, Phrase::kNo)) { |
| 82 | this->lfcr(); |
| 83 | } |
| 84 | } |
| 85 | commentStart = prop->fTerminator; |
| 86 | commentLen = (int) (def->fContentEnd - commentStart); |
| 87 | break; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 88 | case MarkType::kFormula: { |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 89 | commentLen = prop->fStart - commentStart; |
| 90 | if (commentLen > 0) { |
| 91 | if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart, Phrase::kNo)) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 92 | if (commentLen > 1 && '\n' == prop->fStart[-1] && |
| 93 | '\n' == prop->fStart[-2]) { |
| 94 | this->lf(1); |
| 95 | } else { |
| 96 | this->writeSpace(); |
| 97 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 98 | } |
| 99 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 100 | int saveIndent = fIndent; |
| 101 | if (fIndent < fColumn + 1) { |
| 102 | fIndent = fColumn + 1; |
| 103 | } |
| 104 | this->writeBlockIndent(prop->length(), prop->fContentStart); |
| 105 | fIndent = saveIndent; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 106 | commentStart = prop->fTerminator; |
| 107 | commentLen = (int) (def->fContentEnd - commentStart); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 108 | if (commentLen > 1 && '\n' == commentStart[0] && '\n' == commentStart[1]) { |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 109 | this->lf(2); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 110 | } else { |
| 111 | SkASSERT('\n' == prop->fTerminator[0]); |
| 112 | if ('.' != prop->fTerminator[1] && !fLinefeeds) { |
| 113 | this->writeSpace(); |
| 114 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 115 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 116 | } break; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 117 | case MarkType::kToDo: |
| 118 | commentLen = (int) (prop->fStart - commentStart); |
| 119 | if (commentLen > 0) { |
| 120 | SkASSERT(commentLen < 1000); |
| 121 | if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart, Phrase::kNo)) { |
| 122 | this->lfcr(); |
| 123 | } |
| 124 | } |
| 125 | commentStart = prop->fTerminator; |
| 126 | commentLen = (int) (def->fContentEnd - commentStart); |
| 127 | break; |
| 128 | case MarkType::kList: |
| 129 | commentLen = prop->fStart - commentStart; |
| 130 | if (commentLen > 0) { |
| 131 | if (Wrote::kNone != this->rewriteBlock(commentLen, commentStart, |
| 132 | Phrase::kNo)) { |
| 133 | this->lfcr(); |
| 134 | } |
| 135 | } |
| 136 | for (auto row : prop->fChildren) { |
| 137 | SkASSERT(MarkType::kRow == row->fMarkType); |
| 138 | for (auto column : row->fChildren) { |
| 139 | SkASSERT(MarkType::kColumn == column->fMarkType); |
| 140 | this->writeString("-"); |
| 141 | this->writeSpace(); |
| 142 | this->descriptionOut(column); |
| 143 | this->lf(1); |
| 144 | } |
| 145 | } |
| 146 | commentStart = prop->fTerminator; |
| 147 | commentLen = (int) (def->fContentEnd - commentStart); |
| 148 | if ('\n' == commentStart[0] && '\n' == commentStart[1]) { |
| 149 | this->lf(2); |
| 150 | } |
| 151 | break; |
| 152 | default: |
| 153 | commentLen = (int) (prop->fStart - commentStart); |
| 154 | breakOut = true; |
| 155 | } |
| 156 | if (breakOut) { |
| 157 | break; |
| 158 | } |
| 159 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 160 | SkASSERT(wroteCode || (commentLen > 0 && commentLen < 1500)); |
| 161 | if (commentLen > 0) { |
| 162 | this->rewriteBlock(commentLen, commentStart, Phrase::kNo); |
| 163 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 166 | void IncludeWriter::enumHeaderOut(const RootDefinition* root, |
| 167 | const Definition& child) { |
| 168 | const Definition* enumDef = nullptr; |
| 169 | const char* bodyEnd = fDeferComment ? fDeferComment->fContentStart - 1 : |
| 170 | child.fContentStart; |
| 171 | this->writeBlockTrim((int) (bodyEnd - fStart), fStart); // may write nothing |
| 172 | this->lf(2); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 173 | if (fIndentNext) { |
| 174 | fIndent += 4; |
| 175 | fIndentNext = false; |
| 176 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 177 | fDeferComment = nullptr; |
| 178 | fStart = child.fContentStart; |
| 179 | const auto& nameDef = child.fTokens.front(); |
| 180 | string fullName; |
| 181 | if (nullptr != nameDef.fContentEnd) { |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 182 | TextParser enumClassCheck(&nameDef); |
| 183 | const char* start = enumClassCheck.fStart; |
| 184 | size_t len = (size_t) (enumClassCheck.fEnd - start); |
| 185 | bool enumClass = enumClassCheck.skipExact("class "); |
| 186 | if (enumClass) { |
| 187 | start = enumClassCheck.fChar; |
| 188 | const char* end = enumClassCheck.anyOf(" \n;{"); |
| 189 | len = (size_t) (end - start); |
| 190 | } |
| 191 | string enumName(start, len); |
| 192 | if (enumClass) { |
| 193 | child.fChildren[0]->fName = enumName; |
| 194 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 195 | fullName = root->fName + "::" + enumName; |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 196 | enumDef = root->find(enumName, RootDefinition::AllowParens::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 197 | if (!enumDef) { |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 198 | enumDef = root->find(fullName, RootDefinition::AllowParens::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 199 | } |
| 200 | SkASSERT(enumDef); |
| 201 | // child[0] should be #Code comment starts at child[0].fTerminator |
| 202 | // though skip until #Code is found (in case there's a #ToDo, etc) |
| 203 | // child[1] should be #Const comment ends at child[1].fStart |
| 204 | // comment becomes enum header (if any) |
| 205 | } else { |
| 206 | string enumName(root->fName); |
| 207 | enumName += "::_anonymous"; |
| 208 | if (fAnonymousEnumCount > 1) { |
| 209 | enumName += '_' + to_string(fAnonymousEnumCount); |
| 210 | } |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 211 | enumDef = root->find(enumName, RootDefinition::AllowParens::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 212 | SkASSERT(enumDef); |
| 213 | ++fAnonymousEnumCount; |
| 214 | } |
| 215 | Definition* codeBlock = nullptr; |
| 216 | const char* commentStart = nullptr; |
| 217 | bool wroteHeader = false; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 218 | bool lastAnchor = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 219 | SkDEBUGCODE(bool foundConst = false); |
| 220 | for (auto test : enumDef->fChildren) { |
| 221 | if (MarkType::kCode == test->fMarkType) { |
| 222 | SkASSERT(!codeBlock); // FIXME: check enum for correct order earlier |
| 223 | codeBlock = test; |
| 224 | commentStart = codeBlock->fTerminator; |
| 225 | continue; |
| 226 | } |
| 227 | if (!codeBlock) { |
| 228 | continue; |
| 229 | } |
| 230 | const char* commentEnd = test->fStart; |
| 231 | if (!wroteHeader && |
| 232 | !this->contentFree((int) (commentEnd - commentStart), commentStart)) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 233 | if (fIndentNext) { |
| 234 | fIndent += 4; |
| 235 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 236 | this->writeCommentHeader(); |
| 237 | this->writeString("\\enum"); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 238 | if (fullName.length() > 0) { |
| 239 | this->writeSpace(); |
| 240 | this->writeString(fullName.c_str()); |
| 241 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 242 | fIndent += 4; |
| 243 | this->lfcr(); |
| 244 | wroteHeader = true; |
| 245 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 246 | if (lastAnchor) { |
| 247 | if (commentEnd - commentStart > 1) { |
| 248 | SkASSERT('\n' == commentStart[0]); |
| 249 | if (' ' == commentStart[1]) { |
| 250 | this->writeSpace(); |
| 251 | } |
| 252 | } |
| 253 | lastAnchor = false; |
| 254 | } |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 255 | this->rewriteBlock((int) (commentEnd - commentStart), commentStart, Phrase::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 256 | if (MarkType::kAnchor == test->fMarkType) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 257 | bool newLine = commentEnd - commentStart > 1 && |
| 258 | '\n' == commentEnd[-1] && '\n' == commentEnd[-2]; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 259 | commentStart = test->fContentStart; |
| 260 | commentEnd = test->fChildren[0]->fStart; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 261 | if (newLine) { |
| 262 | this->lf(2); |
| 263 | } else { |
| 264 | this->writeSpace(); |
| 265 | } |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 266 | this->rewriteBlock((int) (commentEnd - commentStart), commentStart, Phrase::kNo); |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 267 | lastAnchor = true; // this->writeSpace(); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 268 | } |
| 269 | commentStart = test->fTerminator; |
| 270 | if (MarkType::kConst == test->fMarkType) { |
| 271 | SkASSERT(codeBlock); // FIXME: check enum for correct order earlier |
| 272 | SkDEBUGCODE(foundConst = true); |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | SkASSERT(codeBlock); |
| 277 | SkASSERT(foundConst); |
| 278 | if (wroteHeader) { |
| 279 | fIndent -= 4; |
| 280 | this->lfcr(); |
| 281 | this->writeCommentTrailer(); |
| 282 | } |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 283 | Definition* braceHolder = child.fChildren[0]; |
| 284 | if (KeyWord::kClass == braceHolder->fKeyWord) { |
| 285 | braceHolder = braceHolder->fChildren[0]; |
| 286 | } |
| 287 | bodyEnd = braceHolder->fContentStart; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 288 | SkASSERT('{' == bodyEnd[0]); |
| 289 | ++bodyEnd; |
| 290 | this->lfcr(); |
| 291 | this->writeBlock((int) (bodyEnd - fStart), fStart); // write include "enum Name {" |
| 292 | fIndent += 4; |
| 293 | this->singleLF(); |
| 294 | fStart = bodyEnd; |
| 295 | fEnumDef = enumDef; |
| 296 | } |
| 297 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 298 | void IncludeWriter::enumMembersOut(const RootDefinition* root, Definition& child) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 299 | // iterate through include tokens and find how much remains for 1 line comments |
| 300 | // put ones that fit on same line, ones that are too big on preceding line? |
| 301 | const Definition* currentEnumItem = nullptr; |
| 302 | const char* commentStart = nullptr; |
| 303 | const char* lastEnd = nullptr; |
| 304 | int commentLen = 0; |
| 305 | enum class State { |
| 306 | kNoItem, |
| 307 | kItemName, |
| 308 | kItemValue, |
| 309 | kItemComment, |
| 310 | }; |
| 311 | State state = State::kNoItem; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 312 | vector<IterState> iterStack; |
| 313 | iterStack.emplace_back(child.fTokens.begin(), child.fTokens.end()); |
| 314 | IterState* iterState = &iterStack[0]; |
| 315 | bool preprocessorWord = false; |
| 316 | const char* preprocessStart = nullptr; |
| 317 | const char* preprocessEnd = nullptr; |
| 318 | for (int onePast = 0; onePast < 2; onePast += iterState->fDefIter == iterState->fDefEnd) { |
| 319 | Definition* token = onePast ? nullptr : &*iterState->fDefIter++; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 320 | if (token && Definition::Type::kBracket == token->fType) { |
| 321 | if (Bracket::kSlashSlash == token->fBracket) { |
| 322 | fStart = token->fContentEnd; |
| 323 | continue; // ignore old inline comments |
| 324 | } |
| 325 | if (Bracket::kSlashStar == token->fBracket) { |
| 326 | fStart = token->fContentEnd + 1; |
| 327 | continue; // ignore old inline comments |
| 328 | } |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 329 | if (Bracket::kPound == token->fBracket) { // preprocessor wraps member |
| 330 | preprocessStart = token->fContentStart; |
| 331 | if (KeyWord::kIf == token->fKeyWord || KeyWord::kIfdef == token->fKeyWord) { |
| 332 | iterStack.emplace_back(token->fTokens.begin(), token->fTokens.end()); |
| 333 | iterState = &iterStack.back(); |
| 334 | preprocessorWord = true; |
| 335 | } else if (KeyWord::kEndif == token->fKeyWord) { |
| 336 | iterStack.pop_back(); |
| 337 | iterState = &iterStack.back(); |
| 338 | preprocessEnd = token->fContentEnd; |
| 339 | } else { |
| 340 | SkASSERT(0); // incomplete |
| 341 | } |
| 342 | continue; |
| 343 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 344 | SkASSERT(0); // incomplete |
| 345 | } |
| 346 | if (token && Definition::Type::kWord != token->fType) { |
| 347 | SkASSERT(0); // incomplete |
| 348 | } |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 349 | if (preprocessorWord) { |
| 350 | preprocessorWord = false; |
| 351 | preprocessEnd = token->fContentEnd; |
| 352 | continue; |
| 353 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 354 | if (token && State::kItemName == state) { |
| 355 | TextParser enumLine(token->fFileName, lastEnd, |
| 356 | token->fContentStart, token->fLineCount); |
| 357 | const char* end = enumLine.anyOf(",}="); |
| 358 | SkASSERT(end); |
| 359 | state = '=' == *end ? State::kItemValue : State::kItemComment; |
| 360 | if (State::kItemValue == state) { // write enum value |
| 361 | this->indentToColumn(fEnumItemValueTab); |
| 362 | this->writeString("="); |
| 363 | this->writeSpace(); |
| 364 | lastEnd = token->fContentEnd; |
| 365 | this->writeBlock((int) (lastEnd - token->fContentStart), |
| 366 | token->fContentStart); // write const value if any |
| 367 | continue; |
| 368 | } |
| 369 | } |
| 370 | if (token && State::kItemValue == state) { |
| 371 | TextParser valueEnd(token->fFileName, lastEnd, |
| 372 | token->fContentStart, token->fLineCount); |
| 373 | const char* end = valueEnd.anyOf(",}"); |
| 374 | if (!end) { // write expression continuation |
| 375 | if (' ' == lastEnd[0]) { |
| 376 | this->writeSpace(); |
| 377 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 378 | this->writeBlock((int) (token->fContentEnd - lastEnd), lastEnd); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 379 | continue; |
| 380 | } |
| 381 | } |
| 382 | if (State::kNoItem != state) { |
| 383 | this->writeString(","); |
| 384 | SkASSERT(currentEnumItem); |
| 385 | if (currentEnumItem->fShort) { |
| 386 | this->indentToColumn(fEnumItemCommentTab); |
| 387 | this->writeString("//!<"); |
| 388 | this->writeSpace(); |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 389 | this->rewriteBlock(commentLen, commentStart, Phrase::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 390 | } |
| 391 | if (onePast) { |
| 392 | fIndent -= 4; |
| 393 | } |
| 394 | this->lfcr(); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 395 | if (preprocessStart) { |
| 396 | SkASSERT(preprocessEnd); |
| 397 | int saveIndent = fIndent; |
| 398 | fIndent = SkTMax(0, fIndent - 8); |
| 399 | this->lf(2); |
| 400 | this->writeBlock((int) (preprocessEnd - preprocessStart), preprocessStart); |
| 401 | this->lfcr(); |
| 402 | fIndent = saveIndent; |
| 403 | preprocessStart = nullptr; |
| 404 | preprocessEnd = nullptr; |
| 405 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 406 | if (token && State::kItemValue == state) { |
| 407 | fStart = token->fContentStart; |
| 408 | } |
| 409 | state = State::kNoItem; |
| 410 | } |
| 411 | SkASSERT(State::kNoItem == state); |
| 412 | if (onePast) { |
| 413 | break; |
| 414 | } |
| 415 | SkASSERT(token); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 416 | string itemName = root->fName + "::"; |
| 417 | if (KeyWord::kClass == child.fParent->fKeyWord) { |
| 418 | itemName += child.fParent->fName + "::"; |
| 419 | } |
| 420 | itemName += string(token->fContentStart, (int) (token->fContentEnd - token->fContentStart)); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 421 | for (auto& enumItem : fEnumDef->fChildren) { |
| 422 | if (MarkType::kConst != enumItem->fMarkType) { |
| 423 | continue; |
| 424 | } |
| 425 | if (itemName != enumItem->fName) { |
| 426 | continue; |
| 427 | } |
| 428 | currentEnumItem = enumItem; |
| 429 | break; |
| 430 | } |
| 431 | SkASSERT(currentEnumItem); |
| 432 | // if description fits, it goes after item |
| 433 | commentStart = currentEnumItem->fContentStart; |
| 434 | const char* commentEnd; |
| 435 | if (currentEnumItem->fChildren.size() > 0) { |
| 436 | commentEnd = currentEnumItem->fChildren[0]->fStart; |
| 437 | } else { |
| 438 | commentEnd = currentEnumItem->fContentEnd; |
| 439 | } |
| 440 | TextParser enumComment(fFileName, commentStart, commentEnd, currentEnumItem->fLineCount); |
| 441 | if (enumComment.skipToLineStart()) { // skip const value |
| 442 | commentStart = enumComment.fChar; |
| 443 | commentLen = (int) (commentEnd - commentStart); |
| 444 | } else { |
| 445 | const Definition* privateDef = currentEnumItem->fChildren[0]; |
| 446 | SkASSERT(MarkType::kPrivate == privateDef->fMarkType); |
| 447 | commentStart = privateDef->fContentStart; |
| 448 | commentLen = (int) (privateDef->fContentEnd - privateDef->fContentStart); |
| 449 | } |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 450 | // FIXME: may assert here if there's no const value |
| 451 | // should have detected and errored on that earlier when enum fContentStart was set |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 452 | SkASSERT(commentLen > 0 && commentLen < 1000); |
| 453 | if (!currentEnumItem->fShort) { |
| 454 | this->writeCommentHeader(); |
| 455 | fIndent += 4; |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 456 | bool wroteLineFeed = Wrote::kLF == |
| 457 | this->rewriteBlock(commentLen, commentStart, Phrase::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 458 | fIndent -= 4; |
| 459 | if (wroteLineFeed || fColumn > 100 - 3 /* space * / */ ) { |
| 460 | this->lfcr(); |
| 461 | } else { |
| 462 | this->writeSpace(); |
| 463 | } |
| 464 | this->writeCommentTrailer(); |
| 465 | } |
| 466 | lastEnd = token->fContentEnd; |
| 467 | this->lfcr(); |
| 468 | if (',' == fStart[0]) { |
| 469 | ++fStart; |
| 470 | } |
| 471 | this->writeBlock((int) (lastEnd - fStart), fStart); // enum item name |
| 472 | fStart = token->fContentEnd; |
| 473 | state = State::kItemName; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | void IncludeWriter::enumSizeItems(const Definition& child) { |
| 478 | enum class State { |
| 479 | kNoItem, |
| 480 | kItemName, |
| 481 | kItemValue, |
| 482 | kItemComment, |
| 483 | }; |
| 484 | State state = State::kNoItem; |
| 485 | int longestName = 0; |
| 486 | int longestValue = 0; |
| 487 | int valueLen = 0; |
| 488 | const char* lastEnd = nullptr; |
Cary Clark | 7cfcbca | 2018-01-04 16:11:51 -0500 | [diff] [blame] | 489 | // SkASSERT(child.fChildren.size() == 1 || child.fChildren.size() == 2); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 490 | auto brace = child.fChildren[0]; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 491 | if (KeyWord::kClass == brace->fKeyWord) { |
| 492 | brace = brace->fChildren[0]; |
| 493 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 494 | SkASSERT(Bracket::kBrace == brace->fBracket); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 495 | vector<IterState> iterStack; |
| 496 | iterStack.emplace_back(brace->fTokens.begin(), brace->fTokens.end()); |
| 497 | IterState* iterState = &iterStack[0]; |
| 498 | bool preprocessorWord = false; |
| 499 | while (iterState->fDefIter != iterState->fDefEnd) { |
| 500 | auto& token = *iterState->fDefIter++; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 501 | if (Definition::Type::kBracket == token.fType) { |
| 502 | if (Bracket::kSlashSlash == token.fBracket) { |
| 503 | continue; // ignore old inline comments |
| 504 | } |
| 505 | if (Bracket::kSlashStar == token.fBracket) { |
| 506 | continue; // ignore old inline comments |
| 507 | } |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 508 | if (Bracket::kPound == token.fBracket) { // preprocessor wraps member |
| 509 | if (KeyWord::kIf == token.fKeyWord || KeyWord::kIfdef == token.fKeyWord) { |
| 510 | iterStack.emplace_back(token.fTokens.begin(), token.fTokens.end()); |
| 511 | iterState = &iterStack.back(); |
| 512 | preprocessorWord = true; |
| 513 | } else if (KeyWord::kEndif == token.fKeyWord) { |
| 514 | iterStack.pop_back(); |
| 515 | iterState = &iterStack.back(); |
| 516 | } else { |
| 517 | SkASSERT(0); // incomplete |
| 518 | } |
| 519 | continue; |
| 520 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 521 | SkASSERT(0); // incomplete |
| 522 | } |
| 523 | if (Definition::Type::kWord != token.fType) { |
| 524 | SkASSERT(0); // incomplete |
| 525 | } |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 526 | if (preprocessorWord) { |
| 527 | preprocessorWord = false; |
| 528 | continue; |
| 529 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 530 | if (State::kItemName == state) { |
| 531 | TextParser enumLine(token.fFileName, lastEnd, |
| 532 | token.fContentStart, token.fLineCount); |
| 533 | const char* end = enumLine.anyOf(",}="); |
| 534 | SkASSERT(end); |
| 535 | state = '=' == *end ? State::kItemValue : State::kItemComment; |
| 536 | if (State::kItemValue == state) { |
| 537 | valueLen = (int) (token.fContentEnd - token.fContentStart); |
| 538 | lastEnd = token.fContentEnd; |
| 539 | continue; |
| 540 | } |
| 541 | } |
| 542 | if (State::kItemValue == state) { |
| 543 | TextParser valueEnd(token.fFileName, lastEnd, |
| 544 | token.fContentStart, token.fLineCount); |
| 545 | const char* end = valueEnd.anyOf(",}"); |
| 546 | if (!end) { // write expression continuation |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 547 | valueLen += (int) (token.fContentEnd - lastEnd); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 548 | continue; |
| 549 | } |
| 550 | } |
| 551 | if (State::kNoItem != state) { |
| 552 | longestValue = SkTMax(longestValue, valueLen); |
| 553 | state = State::kNoItem; |
| 554 | } |
| 555 | SkASSERT(State::kNoItem == state); |
| 556 | lastEnd = token.fContentEnd; |
| 557 | longestName = SkTMax(longestName, (int) (lastEnd - token.fContentStart)); |
| 558 | state = State::kItemName; |
| 559 | } |
| 560 | if (State::kItemValue == state) { |
| 561 | longestValue = SkTMax(longestValue, valueLen); |
| 562 | } |
| 563 | fEnumItemValueTab = longestName + fIndent + 1 /* space before = */ ; |
| 564 | if (longestValue) { |
| 565 | longestValue += 3; /* = space , */ |
| 566 | } |
| 567 | fEnumItemCommentTab = fEnumItemValueTab + longestValue + 1 /* space before //!< */ ; |
| 568 | // iterate through bmh children and see which comments fit on include lines |
| 569 | for (auto& enumItem : fEnumDef->fChildren) { |
| 570 | if (MarkType::kConst != enumItem->fMarkType) { |
| 571 | continue; |
| 572 | } |
| 573 | TextParser enumLine(enumItem); |
| 574 | enumLine.trimEnd(); |
| 575 | enumLine.skipToLineStart(); // skip const value |
| 576 | const char* commentStart = enumLine.fChar; |
| 577 | enumLine.skipLine(); |
| 578 | ptrdiff_t lineLen = enumLine.fChar - commentStart + 5 /* //!< space */ ; |
| 579 | if (!enumLine.eof()) { |
| 580 | enumLine.skipWhiteSpace(); |
| 581 | } |
| 582 | enumItem->fShort = enumLine.eof() && fEnumItemCommentTab + lineLen < 100; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // walk children and output complete method doxygen description |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 587 | void IncludeWriter::methodOut(const Definition* method, const Definition& child) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 588 | if (fPendingMethod) { |
| 589 | fIndent -= 4; |
| 590 | fPendingMethod = false; |
| 591 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 592 | fBmhMethod = method; |
| 593 | fMethodDef = &child; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 594 | fContinuation = nullptr; |
| 595 | fDeferComment = nullptr; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 596 | if (0 == fIndent || fIndentNext) { |
| 597 | fIndent += 4; |
| 598 | fIndentNext = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 599 | } |
| 600 | this->writeCommentHeader(); |
| 601 | fIndent += 4; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 602 | this->descriptionOut(method); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 603 | // compute indention column |
| 604 | size_t column = 0; |
| 605 | bool hasParmReturn = false; |
| 606 | for (auto methodPart : method->fChildren) { |
| 607 | if (MarkType::kParam == methodPart->fMarkType) { |
| 608 | column = SkTMax(column, methodPart->fName.length()); |
| 609 | hasParmReturn = true; |
| 610 | } else if (MarkType::kReturn == methodPart->fMarkType) { |
| 611 | hasParmReturn = true; |
| 612 | } |
| 613 | } |
| 614 | if (hasParmReturn) { |
| 615 | this->lf(2); |
| 616 | column += fIndent + sizeof("@return "); |
| 617 | int saveIndent = fIndent; |
| 618 | for (auto methodPart : method->fChildren) { |
| 619 | const char* partStart = methodPart->fContentStart; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 620 | const char* partEnd = methodPart->fContentEnd; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 621 | if (MarkType::kParam == methodPart->fMarkType) { |
| 622 | this->writeString("@param"); |
| 623 | this->writeSpace(); |
| 624 | this->writeString(methodPart->fName.c_str()); |
| 625 | } else if (MarkType::kReturn == methodPart->fMarkType) { |
| 626 | this->writeString("@return"); |
| 627 | } else { |
| 628 | continue; |
| 629 | } |
| 630 | while ('\n' == partEnd[-1]) { |
| 631 | --partEnd; |
| 632 | } |
| 633 | while ('#' == partEnd[-1]) { // FIXME: so wrong; should not be before fContentEnd |
| 634 | --partEnd; |
| 635 | } |
| 636 | this->indentToColumn(column); |
| 637 | int partLen = (int) (partEnd - partStart); |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 638 | // FIXME : detect this earlier; assert if #Return is empty |
Cary Clark | f059e7c | 2017-12-20 14:53:21 -0500 | [diff] [blame] | 639 | SkASSERT(partLen > 0 && partLen < 300); // may assert if param desc is especially long |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 640 | fIndent = column; |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 641 | this->rewriteBlock(partLen, partStart, Phrase::kYes); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 642 | fIndent = saveIndent; |
| 643 | this->lfcr(); |
| 644 | } |
| 645 | } else { |
| 646 | this->lfcr(); |
| 647 | } |
| 648 | fIndent -= 4; |
| 649 | this->lfcr(); |
| 650 | this->writeCommentTrailer(); |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 651 | fBmhMethod = nullptr; |
| 652 | fMethodDef = nullptr; |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 653 | fWroteMethod = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | void IncludeWriter::structOut(const Definition* root, const Definition& child, |
| 657 | const char* commentStart, const char* commentEnd) { |
| 658 | this->writeCommentHeader(); |
| 659 | this->writeString("\\"); |
| 660 | SkASSERT(MarkType::kClass == child.fMarkType || MarkType::kStruct == child.fMarkType); |
| 661 | this->writeString(MarkType::kClass == child.fMarkType ? "class" : "struct"); |
| 662 | this->writeSpace(); |
| 663 | this->writeString(child.fName.c_str()); |
| 664 | fIndent += 4; |
| 665 | this->lfcr(); |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 666 | this->rewriteBlock((int) (commentEnd - commentStart), commentStart, Phrase::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 667 | fIndent -= 4; |
| 668 | this->lfcr(); |
| 669 | this->writeCommentTrailer(); |
| 670 | } |
| 671 | |
Cary Clark | 2dc84ad | 2018-01-26 12:56:22 -0500 | [diff] [blame^] | 672 | Definition* IncludeWriter::findMemberCommentBlock(const vector<Definition*>& bmhChildren, |
| 673 | const string& name) const { |
| 674 | for (auto memberDef : bmhChildren) { |
| 675 | if (memberDef->fName.length() - name.length() == memberDef->fName.find(name)) { |
| 676 | return memberDef; |
| 677 | } |
| 678 | } |
| 679 | for (auto memberDef : bmhChildren) { |
| 680 | if (MarkType::kSubtopic == memberDef->fMarkType || MarkType::kTopic == memberDef->fMarkType) { |
| 681 | Definition* result = this->findMemberCommentBlock(memberDef->fChildren, name); |
| 682 | if (result) { |
| 683 | return result; |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | return nullptr; |
| 688 | } |
| 689 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 690 | Definition* IncludeWriter::structMemberOut(const Definition* memberStart, const Definition& child) { |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 691 | const char* blockStart = !fWroteMethod && fDeferComment ? fLastComment->fContentEnd : fStart; |
| 692 | const char* blockEnd = fWroteMethod && fDeferComment ? fDeferComment->fStart - 1 : |
| 693 | memberStart->fStart; |
| 694 | this->writeBlockTrim((int) (blockEnd - blockStart), blockStart); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 695 | if (fIndentNext) { |
| 696 | fIndent += 4; |
| 697 | fIndentNext = false; |
| 698 | } |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 699 | fWroteMethod = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 700 | string name(child.fContentStart, (int) (child.fContentEnd - child.fContentStart)); |
Cary Clark | 2dc84ad | 2018-01-26 12:56:22 -0500 | [diff] [blame^] | 701 | Definition* commentBlock = this->findMemberCommentBlock(fBmhStructDef->fChildren, name); |
| 702 | if (!commentBlock) { |
| 703 | return memberStart->reportError<Definition*>("member missing comment block"); |
| 704 | } |
| 705 | const char* commentStart = commentBlock->fContentStart; |
| 706 | ptrdiff_t commentLen = commentBlock->fContentEnd - commentStart; |
| 707 | bool isShort = commentBlock->fShort; |
| 708 | SkASSERT(!isShort || commentBlock->fChildren.size() == 0); |
| 709 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 710 | if (!isShort) { |
| 711 | this->writeCommentHeader(); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 712 | bool wroteLineFeed = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 713 | fIndent += 4; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 714 | for (auto child : commentBlock->fChildren) { |
| 715 | commentLen = child->fStart - commentStart; |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 716 | wroteLineFeed |= Wrote::kLF == this->rewriteBlock(commentLen, commentStart, Phrase::kNo); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 717 | if (MarkType::kFormula == child->fMarkType) { |
| 718 | this->writeSpace(); |
| 719 | this->writeBlock((int) (child->fContentEnd - child->fContentStart), |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 720 | child->fContentStart); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 721 | } |
| 722 | commentStart = child->fTerminator; |
| 723 | } |
| 724 | commentLen = commentBlock->fContentEnd - commentStart; |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 725 | wroteLineFeed |= Wrote::kLF == this->rewriteBlock(commentLen, commentStart, Phrase::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 726 | fIndent -= 4; |
| 727 | if (wroteLineFeed || fColumn > 100 - 3 /* space * / */ ) { |
| 728 | this->lfcr(); |
| 729 | } else { |
| 730 | this->writeSpace(); |
| 731 | } |
| 732 | this->writeCommentTrailer(); |
| 733 | } |
| 734 | this->lfcr(); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 735 | this->writeBlock((int) (child.fStart - memberStart->fContentStart), |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 736 | memberStart->fContentStart); |
| 737 | this->indentToColumn(fStructMemberTab); |
| 738 | this->writeString(name.c_str()); |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 739 | auto tokenIter = child.fParent->fTokens.begin(); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 740 | std::advance(tokenIter, child.fParentIndex + 1); |
| 741 | Definition* valueStart = &*tokenIter; |
| 742 | while (Definition::Type::kPunctuation != tokenIter->fType) { |
| 743 | std::advance(tokenIter, 1); |
| 744 | SkASSERT(child.fParent->fTokens.end() != tokenIter); |
| 745 | } |
| 746 | Definition* valueEnd = &*tokenIter; |
| 747 | if (valueStart != valueEnd) { |
| 748 | this->indentToColumn(fStructValueTab); |
| 749 | this->writeString("="); |
| 750 | this->writeSpace(); |
| 751 | this->writeBlock((int) (valueEnd->fStart - valueStart->fContentStart), |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 752 | valueStart->fContentStart); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 753 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 754 | this->writeString(";"); |
| 755 | if (isShort) { |
| 756 | this->indentToColumn(fStructCommentTab); |
| 757 | this->writeString("//!<"); |
| 758 | this->writeSpace(); |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 759 | this->rewriteBlock(commentLen, commentStart, Phrase::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 760 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 761 | this->lf(2); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 762 | return valueEnd; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 763 | } |
| 764 | |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 765 | void IncludeWriter::structSizeMembers(const Definition& child) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 766 | int longestType = 0; |
| 767 | Definition* typeStart = nullptr; |
| 768 | int longestName = 0; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 769 | int longestValue = 0; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 770 | SkASSERT(child.fChildren.size() == 1 || child.fChildren.size() == 2); |
| 771 | bool inEnum = false; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 772 | bool inMethod = false; |
| 773 | bool inMember = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 774 | auto brace = child.fChildren[0]; |
| 775 | SkASSERT(Bracket::kBrace == brace->fBracket); |
| 776 | for (auto& token : brace->fTokens) { |
| 777 | if (Definition::Type::kBracket == token.fType) { |
| 778 | if (Bracket::kSlashSlash == token.fBracket) { |
| 779 | continue; // ignore old inline comments |
| 780 | } |
| 781 | if (Bracket::kSlashStar == token.fBracket) { |
| 782 | continue; // ignore old inline comments |
| 783 | } |
| 784 | if (Bracket::kParen == token.fBracket) { |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 785 | if (inMethod) { |
| 786 | continue; |
| 787 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 788 | break; |
| 789 | } |
| 790 | SkASSERT(0); // incomplete |
| 791 | } |
| 792 | if (Definition::Type::kKeyWord == token.fType) { |
| 793 | switch (token.fKeyWord) { |
| 794 | case KeyWord::kEnum: |
| 795 | inEnum = true; |
| 796 | break; |
| 797 | case KeyWord::kConst: |
| 798 | case KeyWord::kConstExpr: |
| 799 | case KeyWord::kStatic: |
| 800 | case KeyWord::kInt: |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 801 | case KeyWord::kUint8_t: |
| 802 | case KeyWord::kUint16_t: |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 803 | case KeyWord::kUint32_t: |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 804 | case KeyWord::kUint64_t: |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 805 | case KeyWord::kSize_t: |
| 806 | case KeyWord::kFloat: |
| 807 | case KeyWord::kBool: |
| 808 | case KeyWord::kVoid: |
| 809 | if (!typeStart) { |
| 810 | typeStart = &token; |
| 811 | } |
| 812 | break; |
| 813 | default: |
| 814 | break; |
| 815 | } |
| 816 | continue; |
| 817 | } |
| 818 | if (Definition::Type::kPunctuation == token.fType) { |
| 819 | if (inEnum) { |
| 820 | SkASSERT(Punctuation::kSemicolon == token.fPunctuation); |
| 821 | inEnum = false; |
| 822 | } |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 823 | if (inMethod) { |
| 824 | if (Punctuation::kColon == token.fPunctuation) { |
| 825 | inMethod = false; |
| 826 | } else if (Punctuation::kLeftBrace == token.fPunctuation) { |
| 827 | inMethod = false; |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 828 | } else if (Punctuation::kSemicolon == token.fPunctuation) { |
| 829 | inMethod = false; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 830 | } else { |
| 831 | SkASSERT(0); // incomplete |
| 832 | } |
| 833 | } |
| 834 | if (inMember) { |
| 835 | SkASSERT(Punctuation::kSemicolon == token.fPunctuation); |
| 836 | typeStart = nullptr; |
| 837 | inMember = false; |
| 838 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 839 | continue; |
| 840 | } |
| 841 | if (Definition::Type::kWord != token.fType) { |
| 842 | SkASSERT(0); // incomplete |
| 843 | } |
| 844 | if (MarkType::kMember == token.fMarkType) { |
| 845 | TextParser typeStr(token.fFileName, typeStart->fContentStart, token.fContentStart, |
| 846 | token.fLineCount); |
| 847 | typeStr.trimEnd(); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 848 | longestType = SkTMax(longestType, (int) (typeStr.fEnd - typeStr.fStart)); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 849 | longestName = SkTMax(longestName, (int) (token.fContentEnd - token.fContentStart)); |
| 850 | typeStart->fMemberStart = true; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 851 | inMember = true; |
| 852 | continue; |
| 853 | } |
| 854 | if (MarkType::kMethod == token.fMarkType) { |
| 855 | inMethod = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 856 | continue; |
| 857 | } |
| 858 | SkASSERT(MarkType::kNone == token.fMarkType); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 859 | if (typeStart) { |
| 860 | if (inMember) { |
| 861 | longestValue = |
| 862 | SkTMax(longestValue, (int) (token.fContentEnd - token.fContentStart)); |
| 863 | } |
| 864 | } else { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 865 | typeStart = &token; |
| 866 | } |
| 867 | } |
| 868 | fStructMemberTab = longestType + fIndent + 1 /* space before name */ ; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 869 | fStructValueTab = fStructMemberTab + longestName + 2 /* space ; */ ; |
| 870 | fStructCommentTab = fStructValueTab; |
| 871 | if (longestValue) { |
| 872 | fStructCommentTab += longestValue + 3 /* space = space */ ; |
| 873 | fStructValueTab -= 1 /* ; */ ; |
| 874 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 875 | // iterate through bmh children and see which comments fit on include lines |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 876 | for (auto& member : fBmhStructDef->fChildren) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 877 | if (MarkType::kMember != member->fMarkType) { |
| 878 | continue; |
| 879 | } |
| 880 | TextParser memberLine(member); |
| 881 | memberLine.trimEnd(); |
| 882 | const char* commentStart = memberLine.fChar; |
| 883 | memberLine.skipLine(); |
| 884 | ptrdiff_t lineLen = memberLine.fChar - commentStart + 5 /* //!< space */ ; |
| 885 | if (!memberLine.eof()) { |
| 886 | memberLine.skipWhiteSpace(); |
| 887 | } |
| 888 | member->fShort = memberLine.eof() && fStructCommentTab + lineLen < 100; |
| 889 | } |
| 890 | } |
| 891 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 892 | static bool find_start(const Definition* startDef, const char* start) { |
| 893 | for (const auto& child : startDef->fTokens) { |
| 894 | if (child.fContentStart == start) { |
| 895 | return MarkType::kMethod == child.fMarkType; |
| 896 | } |
| 897 | if (child.fContentStart >= start) { |
| 898 | break; |
| 899 | } |
| 900 | if (find_start(&child, start)) { |
| 901 | return true; |
| 902 | } |
| 903 | } |
| 904 | return false; |
| 905 | } |
| 906 | |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 907 | bool IncludeWriter::populate(Definition* def, ParentPair* prevPair, RootDefinition* root) { |
| 908 | ParentPair pair = { def, prevPair }; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 909 | // write bulk of original include up to class, method, enum, etc., excepting preceding comment |
| 910 | // find associated bmh object |
| 911 | // write any associated comments in Doxygen form |
| 912 | // skip include comment |
| 913 | // if there is a series of same named methods, write one set of comments, then write all methods |
| 914 | string methodName; |
Kevin Lubick | 4284613 | 2018-01-05 10:11:11 -0500 | [diff] [blame] | 915 | const Definition* method = nullptr; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 916 | const Definition* clonedMethod = nullptr; |
| 917 | const Definition* memberStart = nullptr; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 918 | const Definition* memberEnd = nullptr; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 919 | fContinuation = nullptr; |
| 920 | bool inStruct = false; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 921 | bool inConstructor = false; |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 922 | bool inInline = false; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 923 | bool eatOperator = false; |
| 924 | const Definition* requireDense = nullptr; |
| 925 | const Definition* startDef = nullptr; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 926 | for (auto& child : def->fTokens) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 927 | if (KeyWord::kOperator == child.fKeyWord && method && |
| 928 | Definition::MethodType::kOperator == method->fMethodType) { |
| 929 | eatOperator = true; |
| 930 | continue; |
| 931 | } |
| 932 | if (eatOperator) { |
| 933 | if (Bracket::kSquare == child.fBracket || Bracket::kParen == child.fBracket) { |
| 934 | continue; |
| 935 | } |
| 936 | eatOperator = false; |
| 937 | fContinuation = nullptr; |
| 938 | if (KeyWord::kConst == child.fKeyWord) { |
| 939 | continue; |
| 940 | } |
| 941 | } |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 942 | if (memberEnd) { |
| 943 | if (memberEnd != &child) { |
| 944 | continue; |
| 945 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 946 | startDef = &child; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 947 | fStart = child.fContentStart + 1; |
| 948 | memberEnd = nullptr; |
| 949 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 950 | if (child.fPrivate) { |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 951 | if (MarkType::kMethod == child.fMarkType) { |
| 952 | inInline = true; |
| 953 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 954 | continue; |
| 955 | } |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 956 | if (inInline) { |
| 957 | if (Definition::Type::kKeyWord == child.fType) { |
| 958 | SkASSERT(MarkType::kMethod != child.fMarkType); |
| 959 | continue; |
| 960 | } |
| 961 | if (Definition::Type::kPunctuation == child.fType) { |
| 962 | if (Punctuation::kLeftBrace == child.fPunctuation) { |
| 963 | inInline = false; |
| 964 | } else { |
| 965 | SkASSERT(Punctuation::kAsterisk == child.fPunctuation); |
| 966 | } |
| 967 | continue; |
| 968 | } |
| 969 | if (Definition::Type::kWord == child.fType) { |
| 970 | string name(child.fContentStart, child.fContentEnd - child.fContentStart); |
| 971 | SkASSERT(string::npos != name.find("::")); |
| 972 | continue; |
| 973 | } |
| 974 | if (Definition::Type::kBracket == child.fType) { |
| 975 | SkASSERT(Bracket::kParen == child.fBracket); |
| 976 | continue; |
| 977 | } |
| 978 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 979 | if (fContinuation) { |
| 980 | if (Definition::Type::kKeyWord == child.fType) { |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 981 | if (KeyWord::kFriend == child.fKeyWord || |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 982 | KeyWord::kSK_API == child.fKeyWord) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 983 | continue; |
| 984 | } |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 985 | const IncludeKey& includeKey = kKeyWords[(int) child.fKeyWord]; |
| 986 | if (KeyProperty::kNumber == includeKey.fProperty) { |
| 987 | continue; |
| 988 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 989 | } |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 990 | if (Definition::Type::kBracket == child.fType) { |
| 991 | if (Bracket::kAngle == child.fBracket) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 992 | continue; |
| 993 | } |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 994 | if (Bracket::kParen == child.fBracket) { |
| 995 | if (!clonedMethod) { |
| 996 | if (inConstructor) { |
| 997 | fContinuation = child.fContentStart; |
| 998 | } |
| 999 | continue; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1000 | } |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 1001 | int alternate = 1; |
| 1002 | ptrdiff_t childLen = child.fContentEnd - child.fContentStart; |
| 1003 | SkASSERT(')' == child.fContentStart[childLen]); |
| 1004 | ++childLen; |
| 1005 | do { |
| 1006 | TextParser params(clonedMethod->fFileName, clonedMethod->fStart, |
| 1007 | clonedMethod->fContentStart, clonedMethod->fLineCount); |
| 1008 | params.skipToEndBracket('('); |
| 1009 | if (params.startsWith(child.fContentStart, childLen)) { |
| 1010 | this->methodOut(clonedMethod, child); |
| 1011 | break; |
| 1012 | } |
| 1013 | ++alternate; |
| 1014 | string alternateMethod = methodName + '_' + to_string(alternate); |
| 1015 | clonedMethod = root->find(alternateMethod, |
| 1016 | RootDefinition::AllowParens::kNo); |
| 1017 | } while (clonedMethod); |
| 1018 | if (!clonedMethod) { |
| 1019 | return this->reportError<bool>("cloned method not found"); |
| 1020 | } |
| 1021 | clonedMethod = nullptr; |
| 1022 | continue; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1023 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1024 | } |
| 1025 | if (Definition::Type::kWord == child.fType) { |
| 1026 | if (clonedMethod) { |
| 1027 | continue; |
| 1028 | } |
| 1029 | size_t len = (size_t) (child.fContentEnd - child.fContentStart); |
| 1030 | const char operatorStr[] = "operator"; |
| 1031 | size_t operatorLen = sizeof(operatorStr) - 1; |
| 1032 | if (len >= operatorLen && !strncmp(child.fContentStart, operatorStr, operatorLen)) { |
| 1033 | fContinuation = child.fContentEnd; |
| 1034 | continue; |
| 1035 | } |
| 1036 | } |
| 1037 | if (Definition::Type::kPunctuation == child.fType && |
| 1038 | (Punctuation::kSemicolon == child.fPunctuation || |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1039 | Punctuation::kLeftBrace == child.fPunctuation || |
| 1040 | (Punctuation::kColon == child.fPunctuation && inConstructor))) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1041 | SkASSERT(fContinuation[0] == '('); |
| 1042 | const char* continueEnd = child.fContentStart; |
| 1043 | while (continueEnd > fContinuation && isspace(continueEnd[-1])) { |
| 1044 | --continueEnd; |
| 1045 | } |
| 1046 | methodName += string(fContinuation, continueEnd - fContinuation); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1047 | method = root->find(methodName, RootDefinition::AllowParens::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1048 | if (!method) { |
| 1049 | fLineCount = child.fLineCount; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1050 | return this->reportError<bool>("method not found"); |
| 1051 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1052 | this->methodOut(method, child); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1053 | continue; |
| 1054 | } |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1055 | if (Definition::Type::kPunctuation == child.fType && |
| 1056 | Punctuation::kAsterisk == child.fPunctuation && |
| 1057 | clonedMethod) { |
| 1058 | continue; |
| 1059 | } |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1060 | if (inConstructor) { |
| 1061 | continue; |
| 1062 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1063 | method = root->find(methodName + "()", RootDefinition::AllowParens::kNo); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1064 | if (method && MarkType::kDefinedBy == method->fMarkType) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1065 | method = method->fParent; |
| 1066 | } |
| 1067 | if (method) { |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1068 | if (method->fCloned) { |
| 1069 | clonedMethod = method; |
| 1070 | continue; |
| 1071 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1072 | this->methodOut(method, child); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1073 | continue; |
| 1074 | } |
| 1075 | fLineCount = child.fLineCount; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1076 | return this->reportError<bool>("method not found"); |
| 1077 | } |
| 1078 | if (Bracket::kSlashSlash == child.fBracket || Bracket::kSlashStar == child.fBracket) { |
| 1079 | if (!fDeferComment) { |
| 1080 | fDeferComment = &child; |
| 1081 | } |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1082 | fLastComment = &child; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1083 | continue; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1084 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1085 | if (MarkType::kMethod == child.fMarkType) { |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1086 | if (this->internalName(child)) { |
| 1087 | continue; |
| 1088 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1089 | const char* bodyEnd = fDeferComment ? fDeferComment->fContentStart - 1 : |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1090 | fAttrDeprecated ? fAttrDeprecated->fContentStart - 1 : |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1091 | child.fContentStart; |
Cary Clark | 7cfcbca | 2018-01-04 16:11:51 -0500 | [diff] [blame] | 1092 | if (Definition::Type::kBracket == def->fType && Bracket::kDebugCode == def->fBracket) { |
| 1093 | auto tokenIter = def->fParent->fTokens.begin(); |
| 1094 | std::advance(tokenIter, def->fParentIndex - 1); |
| 1095 | Definition* prior = &*tokenIter; |
| 1096 | if (Definition::Type::kBracket == def->fType && |
| 1097 | Bracket::kSlashStar == prior->fBracket) { |
| 1098 | bodyEnd = prior->fContentStart - 1; |
| 1099 | } |
| 1100 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1101 | // FIXME: roll end-trimming into writeBlockTrim call |
| 1102 | while (fStart < bodyEnd && ' ' >= bodyEnd[-1]) { |
| 1103 | --bodyEnd; |
| 1104 | } |
| 1105 | int blockSize = (int) (bodyEnd - fStart); |
| 1106 | if (blockSize) { |
Cary Clark | 7cfcbca | 2018-01-04 16:11:51 -0500 | [diff] [blame] | 1107 | string debugstr(fStart, blockSize); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1108 | this->writeBlock(blockSize, fStart); |
| 1109 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1110 | startDef = &child; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1111 | fStart = child.fContentStart; |
| 1112 | methodName = root->fName + "::" + child.fName; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1113 | inConstructor = root->fName == child.fName; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1114 | fContinuation = child.fContentEnd; |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1115 | method = root->find(methodName, RootDefinition::AllowParens::kNo); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1116 | // if (!method) { |
| 1117 | // method = root->find(methodName + "()", RootDefinition::AllowParens::kNo); |
| 1118 | // } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1119 | if (!method) { |
| 1120 | continue; |
| 1121 | } |
| 1122 | if (method->fCloned) { |
| 1123 | clonedMethod = method; |
| 1124 | continue; |
| 1125 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1126 | this->methodOut(method, child); |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1127 | if (fAttrDeprecated) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1128 | startDef = fAttrDeprecated; |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1129 | fStart = fAttrDeprecated->fContentStart; |
| 1130 | fAttrDeprecated = nullptr; |
| 1131 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1132 | continue; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1133 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1134 | if (Definition::Type::kKeyWord == child.fType) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1135 | if (fIndentNext) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1136 | // too soon |
| 1137 | #if 0 // makes struct Lattice indent when it oughtn't |
| 1138 | if (KeyWord::kEnum == child.fKeyWord) { |
| 1139 | fIndent += 4; |
| 1140 | } |
| 1141 | if (KeyWord::kPublic != child.fKeyWord) { |
| 1142 | fIndentNext = false; |
| 1143 | } |
| 1144 | #endif |
| 1145 | } |
| 1146 | const Definition* cIncludeStructDef = nullptr; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1147 | switch (child.fKeyWord) { |
| 1148 | case KeyWord::kStruct: |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1149 | case KeyWord::kClass: |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1150 | // if struct contains members, compute their name and comment tabs |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1151 | if (child.fChildren.size() > 0) { |
| 1152 | const ParentPair* testPair = &pair; |
| 1153 | while ((testPair = testPair->fPrev)) { |
| 1154 | if (KeyWord::kClass == testPair->fParent->fKeyWord) { |
| 1155 | inStruct = fInStruct = true; |
| 1156 | break; |
| 1157 | } |
| 1158 | } |
| 1159 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1160 | if (fInStruct) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1161 | // try child; root+child; root->parent+child; etc. |
| 1162 | int trial = 0; |
| 1163 | const RootDefinition* search = root; |
| 1164 | const Definition* parent = search->fParent; |
| 1165 | do { |
| 1166 | string name; |
| 1167 | if (0 == trial) { |
| 1168 | name = child.fName; |
| 1169 | } else if (1 == trial) { |
| 1170 | name = root->fName + "::" + child.fName; |
| 1171 | } else { |
| 1172 | SkASSERT(parent); |
| 1173 | name = parent->fName + "::" + child.fName; |
| 1174 | search = parent->asRoot(); |
| 1175 | parent = search->fParent; |
| 1176 | } |
| 1177 | fBmhStructDef = search->find(name, RootDefinition::AllowParens::kNo); |
| 1178 | } while (!fBmhStructDef && ++trial); |
| 1179 | root = const_cast<RootDefinition*>(fBmhStructDef->asRoot()); |
| 1180 | SkASSERT(root); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1181 | fIndent += 4; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1182 | this->structSizeMembers(child); |
| 1183 | fIndent -= 4; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1184 | SkASSERT(!fIndentNext); |
| 1185 | fIndentNext = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1186 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1187 | if (child.fChildren.size() > 0) { |
| 1188 | const char* bodyEnd = fDeferComment ? fDeferComment->fContentStart - 1 : |
| 1189 | child.fContentStart; |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1190 | this->writeBlockTrim((int) (bodyEnd - fStart), fStart); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1191 | if (fPendingMethod) { |
| 1192 | fIndent -= 4; |
| 1193 | fPendingMethod = false; |
| 1194 | } |
| 1195 | startDef = requireDense ? requireDense : &child; |
| 1196 | fStart = requireDense ? requireDense->fContentStart : child.fContentStart; |
| 1197 | requireDense = nullptr; |
| 1198 | if (!fInStruct && child.fName != root->fName) { |
| 1199 | root = &fBmhParser->fClassMap[child.fName]; |
| 1200 | fRootTopic = root->fParent; |
| 1201 | SkASSERT(!root->fVisited); |
| 1202 | root->clearVisited(); |
| 1203 | fIndent = 0; |
| 1204 | fBmhStructDef = root; |
| 1205 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1206 | if (child.fName == root->fName) { |
| 1207 | if (Definition* parent = root->fParent) { |
| 1208 | if (MarkType::kTopic == parent->fMarkType || |
| 1209 | MarkType::kSubtopic == parent->fMarkType) { |
Cary Clark | e4aa371 | 2017-09-15 02:56:12 -0400 | [diff] [blame] | 1210 | const char* commentStart = root->fContentStart; |
| 1211 | const char* commentEnd = root->fChildren[0]->fStart; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1212 | this->structOut(root, *root, commentStart, commentEnd); |
| 1213 | } else { |
| 1214 | SkASSERT(0); // incomplete |
| 1215 | } |
| 1216 | } else { |
| 1217 | SkASSERT(0); // incomplete |
| 1218 | } |
| 1219 | } else { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1220 | SkASSERT(fInStruct); |
| 1221 | #if 0 |
| 1222 | fBmhStructDef = root->find(child.fName, RootDefinition::AllowParens::kNo); |
| 1223 | if (nullptr == fBmhStructDef) { |
| 1224 | fBmhStructDef = root->find(root->fName + "::" + child.fName, |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1225 | RootDefinition::AllowParens::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1226 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1227 | if (!fBmhStructDef) { |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1228 | this->lf(2); |
| 1229 | fIndent = 0; |
| 1230 | this->writeBlock((int) (fStart - bodyEnd), bodyEnd); |
| 1231 | this->lfcr(); |
| 1232 | continue; |
| 1233 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1234 | #endif |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1235 | Definition* codeBlock = nullptr; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1236 | Definition* nextBlock = nullptr; |
| 1237 | for (auto test : fBmhStructDef->fChildren) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1238 | if (MarkType::kCode == test->fMarkType) { |
| 1239 | SkASSERT(!codeBlock); // FIXME: check enum for correct order earlier |
| 1240 | codeBlock = test; |
| 1241 | continue; |
| 1242 | } |
| 1243 | if (codeBlock) { |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1244 | nextBlock = test; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1245 | break; |
| 1246 | } |
| 1247 | } |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1248 | // FIXME: trigger error earlier if inner #Struct or #Class is missing #Code |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1249 | SkASSERT(nextBlock); // FIXME: check enum for correct order earlier |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1250 | const char* commentStart = codeBlock->fTerminator; |
| 1251 | const char* commentEnd = nextBlock->fStart; |
| 1252 | if (fIndentNext) { |
| 1253 | // fIndent += 4; |
| 1254 | } |
| 1255 | fIndentNext = true; |
| 1256 | this->structOut(root, *fBmhStructDef, commentStart, commentEnd); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1257 | } |
| 1258 | fDeferComment = nullptr; |
| 1259 | } else { |
| 1260 | ; // empty forward reference, nothing to do here |
| 1261 | } |
| 1262 | break; |
| 1263 | case KeyWord::kEnum: { |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1264 | fInEnum = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1265 | this->enumHeaderOut(root, child); |
| 1266 | this->enumSizeItems(child); |
| 1267 | } break; |
| 1268 | case KeyWord::kConst: |
| 1269 | case KeyWord::kConstExpr: |
| 1270 | case KeyWord::kStatic: |
| 1271 | case KeyWord::kInt: |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1272 | case KeyWord::kUint8_t: |
| 1273 | case KeyWord::kUint16_t: |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1274 | case KeyWord::kUint32_t: |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1275 | case KeyWord::kUint64_t: |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1276 | case KeyWord::kUnsigned: |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1277 | case KeyWord::kSize_t: |
| 1278 | case KeyWord::kFloat: |
| 1279 | case KeyWord::kBool: |
| 1280 | case KeyWord::kVoid: |
| 1281 | if (!memberStart) { |
| 1282 | memberStart = &child; |
| 1283 | } |
| 1284 | break; |
| 1285 | case KeyWord::kPublic: |
| 1286 | case KeyWord::kPrivate: |
| 1287 | case KeyWord::kProtected: |
| 1288 | case KeyWord::kFriend: |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1289 | case KeyWord::kInline: |
| 1290 | case KeyWord::kSK_API: |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1291 | case KeyWord::kTypedef: |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1292 | break; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1293 | case KeyWord::kSK_BEGIN_REQUIRE_DENSE: |
| 1294 | requireDense = &child; |
| 1295 | break; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1296 | default: |
| 1297 | SkASSERT(0); |
| 1298 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1299 | if (cIncludeStructDef) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1300 | TextParser structName(&child); |
| 1301 | SkAssertResult(structName.skipToEndBracket('{')); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1302 | startDef = &child; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1303 | fStart = structName.fChar + 1; |
| 1304 | this->writeBlock((int) (fStart - child.fStart), child.fStart); |
| 1305 | this->lf(2); |
| 1306 | fIndent += 4; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1307 | if (!this->populate(&child, &pair, const_cast<Definition*>(cIncludeStructDef)->asRoot())) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1308 | return false; |
| 1309 | } |
| 1310 | // output any remaining definitions at current indent level |
| 1311 | const char* structEnd = child.fContentEnd; |
| 1312 | SkAssertResult('}' == structEnd[-1]); |
| 1313 | --structEnd; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1314 | this->writeBlockTrim((int) (structEnd - fStart), fStart); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1315 | this->lf(2); |
| 1316 | fStart = structEnd; |
| 1317 | fIndent -= 4; |
| 1318 | fContinuation = nullptr; |
| 1319 | fDeferComment = nullptr; |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1320 | } else if (KeyWord::kUint8_t == child.fKeyWord) { |
| 1321 | continue; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1322 | } else { |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1323 | if (fInEnum && KeyWord::kClass == child.fChildren[0]->fKeyWord) { |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1324 | if (!this->populate(child.fChildren[0], &pair, root)) { |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1325 | return false; |
| 1326 | } |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1327 | } else { |
| 1328 | if (!this->populate(&child, &pair, root)) { |
| 1329 | return false; |
| 1330 | } |
| 1331 | if (KeyWord::kClass == child.fKeyWord || KeyWord::kStruct == child.fKeyWord) { |
| 1332 | fStructMemberTab = 0; |
| 1333 | if (fInStruct) { |
| 1334 | fInStruct = false; |
| 1335 | do { |
| 1336 | SkASSERT(root); |
| 1337 | root = const_cast<RootDefinition*>(root->fParent->asRoot()); |
| 1338 | } while (MarkType::kTopic == root->fMarkType || |
| 1339 | MarkType::kSubtopic == root->fMarkType); |
| 1340 | SkASSERT(MarkType::kStruct == root->fMarkType || |
| 1341 | MarkType::kClass == root->fMarkType); |
| 1342 | fPendingMethod = false; |
| 1343 | if (startDef) { |
| 1344 | fPendingMethod = find_start(startDef, fStart); |
| 1345 | } |
| 1346 | fOutdentNext = !fPendingMethod; |
| 1347 | } |
| 1348 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | continue; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1352 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1353 | if (Definition::Type::kBracket == child.fType) { |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1354 | if (KeyWord::kEnum == child.fParent->fKeyWord || |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1355 | (KeyWord::kClass == child.fParent->fKeyWord && child.fParent->fParent && |
| 1356 | KeyWord::kEnum == child.fParent->fParent->fKeyWord)) { |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1357 | SkASSERT(Bracket::kBrace == child.fBracket); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1358 | this->enumMembersOut(root, child); |
| 1359 | this->writeString("};"); |
| 1360 | this->lf(2); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1361 | startDef = child.fParent; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1362 | fStart = child.fParent->fContentEnd; |
| 1363 | SkASSERT(';' == fStart[0]); |
| 1364 | ++fStart; |
| 1365 | fDeferComment = nullptr; |
| 1366 | fInEnum = false; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1367 | if (fIndentNext) { |
| 1368 | // fIndent -= 4; |
| 1369 | fIndentNext = false; |
| 1370 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1371 | continue; |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1372 | } |
| 1373 | if (fAttrDeprecated) { |
| 1374 | continue; |
| 1375 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1376 | fDeferComment = nullptr; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1377 | if (KeyWord::kClass == def->fKeyWord || KeyWord::kStruct == def->fKeyWord) { |
| 1378 | fIndentNext = true; |
| 1379 | } |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1380 | if (!this->populate(&child, &pair, root)) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1381 | return false; |
| 1382 | } |
| 1383 | continue; |
| 1384 | } |
| 1385 | if (Definition::Type::kWord == child.fType) { |
| 1386 | if (MarkType::kMember == child.fMarkType) { |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 1387 | if (!memberStart) { |
| 1388 | auto iter = def->fTokens.begin(); |
| 1389 | std::advance(iter, child.fParentIndex - 1); |
| 1390 | memberStart = &*iter; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1391 | if (!fStructMemberTab) { |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 1392 | SkASSERT(KeyWord::kStruct == def->fParent->fKeyWord); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1393 | fIndent += 4; |
| 1394 | this->structSizeMembers(*def->fParent); |
| 1395 | fIndent -= 4; |
| 1396 | // SkASSERT(!fIndentNext); |
| 1397 | fIndentNext = true; |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 1398 | } |
| 1399 | } |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1400 | memberEnd = this->structMemberOut(memberStart, child); |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1401 | startDef = &child; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1402 | fStart = child.fContentEnd + 1; |
| 1403 | fDeferComment = nullptr; |
| 1404 | } |
| 1405 | if (child.fMemberStart) { |
| 1406 | memberStart = &child; |
| 1407 | } |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1408 | const char attrDeprecated[] = "SK_ATTR_DEPRECATED"; |
| 1409 | const size_t attrDeprecatedLen = sizeof(attrDeprecated) - 1; |
| 1410 | if (attrDeprecatedLen == child.fContentEnd - child.fContentStart && |
| 1411 | !strncmp(attrDeprecated, child.fStart, attrDeprecatedLen)) { |
| 1412 | fAttrDeprecated = &child; |
| 1413 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1414 | continue; |
| 1415 | } |
| 1416 | if (Definition::Type::kPunctuation == child.fType) { |
| 1417 | if (Punctuation::kSemicolon == child.fPunctuation) { |
| 1418 | memberStart = nullptr; |
| 1419 | if (inStruct) { |
| 1420 | fInStruct = false; |
| 1421 | } |
| 1422 | continue; |
| 1423 | } |
| 1424 | if (Punctuation::kLeftBrace == child.fPunctuation || |
| 1425 | Punctuation::kColon == child.fPunctuation || |
| 1426 | Punctuation::kAsterisk == child.fPunctuation |
| 1427 | ) { |
| 1428 | continue; |
| 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | return true; |
| 1433 | } |
| 1434 | |
| 1435 | bool IncludeWriter::populate(BmhParser& bmhParser) { |
| 1436 | bool allPassed = true; |
| 1437 | for (auto& includeMapper : fIncludeMap) { |
| 1438 | size_t lastSlash = includeMapper.first.rfind('/'); |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1439 | if (string::npos == lastSlash) { |
| 1440 | lastSlash = includeMapper.first.rfind('\\'); |
| 1441 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1442 | if (string::npos == lastSlash || lastSlash >= includeMapper.first.length() - 1) { |
| 1443 | return this->reportError<bool>("malformed include name"); |
| 1444 | } |
| 1445 | string fileName = includeMapper.first.substr(lastSlash + 1); |
| 1446 | if (".h" != fileName.substr(fileName.length() - 2)) { |
| 1447 | return this->reportError<bool>("expected fileName.h"); |
| 1448 | } |
| 1449 | string skClassName = fileName.substr(0, fileName.length() - 2); |
| 1450 | fOut = fopen(fileName.c_str(), "wb"); |
| 1451 | if (!fOut) { |
| 1452 | SkDebugf("could not open output file %s\n", fileName.c_str()); |
| 1453 | return false; |
| 1454 | } |
| 1455 | if (bmhParser.fClassMap.end() == bmhParser.fClassMap.find(skClassName)) { |
| 1456 | return this->reportError<bool>("could not find bmh class"); |
| 1457 | } |
| 1458 | fBmhParser = &bmhParser; |
| 1459 | RootDefinition* root = &bmhParser.fClassMap[skClassName]; |
| 1460 | fRootTopic = root->fParent; |
| 1461 | root->clearVisited(); |
| 1462 | fStart = includeMapper.second.fContentStart; |
| 1463 | fEnd = includeMapper.second.fContentEnd; |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 1464 | fAnonymousEnumCount = 1; |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1465 | allPassed &= this->populate(&includeMapper.second, nullptr, root); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1466 | this->writeBlock((int) (fEnd - fStart), fStart); |
| 1467 | fIndent = 0; |
| 1468 | this->lfcr(); |
| 1469 | this->writePending(); |
| 1470 | fclose(fOut); |
Cary Clark | 7cfcbca | 2018-01-04 16:11:51 -0500 | [diff] [blame] | 1471 | fflush(fOut); |
| 1472 | size_t slash = fFileName.find_last_of('/'); |
| 1473 | if (string::npos == slash) { |
| 1474 | slash = 0; |
| 1475 | } |
| 1476 | size_t back = fFileName.find_last_of('\\'); |
| 1477 | if (string::npos == back) { |
| 1478 | back = 0; |
| 1479 | } |
| 1480 | string dir = fFileName.substr(0, SkTMax(slash, back) + 1); |
| 1481 | string readname = dir + fileName; |
| 1482 | if (this->writtenFileDiffers(fileName, readname)) { |
| 1483 | SkDebugf("wrote updated %s\n", fileName.c_str()); |
| 1484 | } else { |
| 1485 | remove(fileName.c_str()); |
| 1486 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1487 | } |
| 1488 | return allPassed; |
| 1489 | } |
| 1490 | |
| 1491 | // change Xxx_Xxx to xxx xxx |
| 1492 | static string ConvertRef(const string str, bool first) { |
| 1493 | string substitute; |
| 1494 | for (char c : str) { |
| 1495 | if ('_' == c) { |
| 1496 | c = ' '; // change Xxx_Xxx to xxx xxx |
| 1497 | } else if (isupper(c) && !first) { |
| 1498 | c = tolower(c); |
| 1499 | } |
| 1500 | substitute += c; |
| 1501 | first = false; |
| 1502 | } |
| 1503 | return substitute; |
| 1504 | } |
| 1505 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1506 | string IncludeWriter::resolveMethod(const char* start, const char* end, bool first) { |
| 1507 | string methodname(start, end - start); |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1508 | if (string::npos != methodname.find("()")) { |
| 1509 | return ""; |
| 1510 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1511 | string substitute; |
| 1512 | auto rootDefIter = fBmhParser->fMethodMap.find(methodname); |
| 1513 | if (fBmhParser->fMethodMap.end() != rootDefIter) { |
| 1514 | substitute = methodname + "()"; |
| 1515 | } else { |
Cary Clark | 1eace2d | 2017-07-31 07:52:43 -0400 | [diff] [blame] | 1516 | RootDefinition* parent = nullptr; |
| 1517 | for (auto candidate : fRootTopic->fChildren) { |
| 1518 | if (MarkType::kClass == candidate->fMarkType |
| 1519 | || MarkType::kStruct == candidate->fMarkType) { |
| 1520 | parent = candidate->asRoot(); |
| 1521 | break; |
| 1522 | } |
| 1523 | } |
| 1524 | SkASSERT(parent); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1525 | auto defRef = parent->find(parent->fName + "::" + methodname, |
| 1526 | RootDefinition::AllowParens::kNo); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1527 | if (defRef && MarkType::kMethod == defRef->fMarkType) { |
| 1528 | substitute = methodname + "()"; |
| 1529 | } |
| 1530 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1531 | if (fMethodDef && methodname == fMethodDef->fName) { |
| 1532 | TextParser report(fBmhMethod); |
| 1533 | report.reportError("method should not include references to itself"); |
| 1534 | return ""; |
| 1535 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1536 | if (fBmhMethod) { |
| 1537 | for (auto child : fBmhMethod->fChildren) { |
| 1538 | if (MarkType::kParam != child->fMarkType) { |
| 1539 | continue; |
| 1540 | } |
| 1541 | if (methodname == child->fName) { |
| 1542 | return ""; |
| 1543 | } |
| 1544 | } |
| 1545 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1546 | return substitute; |
| 1547 | } |
| 1548 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1549 | string IncludeWriter::resolveRef(const char* start, const char* end, bool first, |
| 1550 | RefType* refType) { |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1551 | // look up Xxx_Xxx |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1552 | string undername(start, end - start); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1553 | for (const auto& external : fBmhParser->fExternals) { |
| 1554 | if (external.fName == undername) { |
| 1555 | *refType = RefType::kExternal; |
| 1556 | return external.fName; |
| 1557 | } |
| 1558 | } |
| 1559 | *refType = RefType::kNormal; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1560 | SkASSERT(string::npos == undername.find(' ')); |
| 1561 | const Definition* rootDef = nullptr; |
| 1562 | { |
| 1563 | auto rootDefIter = fBmhParser->fTopicMap.find(undername); |
| 1564 | if (fBmhParser->fTopicMap.end() != rootDefIter) { |
| 1565 | rootDef = rootDefIter->second; |
| 1566 | } else { |
| 1567 | string prefixedName = fRootTopic->fName + '_' + undername; |
| 1568 | rootDefIter = fBmhParser->fTopicMap.find(prefixedName); |
| 1569 | if (fBmhParser->fTopicMap.end() != rootDefIter) { |
| 1570 | rootDef = rootDefIter->second; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1571 | } else if (fBmhStructDef) { |
| 1572 | string localPrefix = fBmhStructDef->fFiddle + '_' + undername; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1573 | rootDefIter = fBmhParser->fTopicMap.find(localPrefix); |
| 1574 | if (fBmhParser->fTopicMap.end() != rootDefIter) { |
| 1575 | rootDef = rootDefIter->second; |
| 1576 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1577 | } else { |
| 1578 | auto aliasIter = fBmhParser->fAliasMap.find(undername); |
| 1579 | if (fBmhParser->fAliasMap.end() != aliasIter) { |
| 1580 | rootDef = aliasIter->second->fParent; |
| 1581 | } else if (!first) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1582 | SkDebugf("unfound: %s\n", undername.c_str()); |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1583 | this->reportError("reference unfound"); |
| 1584 | return ""; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1585 | } |
| 1586 | } |
| 1587 | } |
| 1588 | } |
| 1589 | string substitute; |
| 1590 | if (rootDef) { |
| 1591 | for (auto child : rootDef->fChildren) { |
| 1592 | if (MarkType::kSubstitute == child->fMarkType) { |
| 1593 | substitute = string(child->fContentStart, |
| 1594 | (int) (child->fContentEnd - child->fContentStart)); |
| 1595 | break; |
| 1596 | } |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1597 | } |
| 1598 | if (!substitute.length()) { |
| 1599 | for (auto child : rootDef->fChildren) { |
| 1600 | if (MarkType::kClass == child->fMarkType || |
| 1601 | MarkType::kStruct == child->fMarkType || |
| 1602 | (MarkType::kEnum == child->fMarkType && !child->fAnonymous) || |
| 1603 | MarkType::kEnumClass == child->fMarkType) { |
| 1604 | substitute = child->fName; |
| 1605 | if (MarkType::kEnum == child->fMarkType && fInEnum) { |
| 1606 | size_t parentClassEnd = substitute.find("::"); |
| 1607 | SkASSERT(string::npos != parentClassEnd); |
| 1608 | substitute = substitute.substr(parentClassEnd + 2); |
| 1609 | } |
| 1610 | break; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1611 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1612 | } |
| 1613 | } |
| 1614 | if (!substitute.length()) { |
| 1615 | auto parent = rootDef->fParent; |
| 1616 | if (parent) { |
| 1617 | if (MarkType::kClass == parent->fMarkType || |
| 1618 | MarkType::kStruct == parent->fMarkType || |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1619 | (MarkType::kEnum == parent->fMarkType && !parent->fAnonymous) || |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1620 | MarkType::kEnumClass == parent->fMarkType) { |
| 1621 | if (parent->fParent != fRootTopic) { |
| 1622 | substitute = parent->fName; |
| 1623 | size_t under = undername.find('_'); |
| 1624 | SkASSERT(string::npos != under); |
| 1625 | string secondHalf(&undername[under], (size_t) (undername.length() - under)); |
| 1626 | substitute += ConvertRef(secondHalf, false); |
| 1627 | } else { |
| 1628 | substitute += ConvertRef(undername, first); |
| 1629 | } |
| 1630 | } |
| 1631 | } |
| 1632 | } |
| 1633 | } |
Cary Clark | 1eace2d | 2017-07-31 07:52:43 -0400 | [diff] [blame] | 1634 | // Ensure first word after period is capitalized if substitute is lower cased. |
| 1635 | if (first && isupper(start[0]) && substitute.length() > 0 && islower(substitute[0])) { |
| 1636 | substitute[0] = start[0]; |
| 1637 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1638 | return substitute; |
| 1639 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1640 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1641 | int IncludeWriter::lookupMethod(const PunctuationState punctuation, const Word word, |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1642 | const int lastSpace, const int run, int lastWrite, const char* data, |
| 1643 | bool hasIndirection) { |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1644 | int wordStart = lastSpace; |
| 1645 | while (' ' >= data[wordStart]) { |
| 1646 | ++wordStart; |
| 1647 | } |
| 1648 | const int wordEnd = PunctuationState::kDelimiter == punctuation || |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1649 | PunctuationState::kPeriod == punctuation ? run - 1 : run; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1650 | string temp; |
| 1651 | if (hasIndirection && '(' != data[wordEnd - 1] && ')' != data[wordEnd - 1]) { |
| 1652 | // FIXME: hard-coded to assume a.b or a->b is a.b() or a->b(). |
| 1653 | // need to check class a for member b to see if this is so |
| 1654 | TextParser parser(fFileName, &data[wordStart], &data[wordEnd], fLineCount); |
| 1655 | const char* indirection = parser.anyOf(".>"); |
| 1656 | if (&data[wordEnd] <= &indirection[2] || 'f' != indirection[1] || |
| 1657 | !isupper(indirection[2])) { |
| 1658 | temp = string(&data[wordStart], wordEnd - wordStart) + "()"; |
| 1659 | } |
| 1660 | } else { |
| 1661 | temp = this->resolveMethod(&data[wordStart], &data[wordEnd], Word::kFirst == word); |
| 1662 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1663 | if (temp.length()) { |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1664 | if (wordStart > lastWrite) { |
| 1665 | SkASSERT(data[wordStart - 1] >= ' '); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1666 | if (' ' == data[lastWrite]) { |
| 1667 | this->writeSpace(); |
| 1668 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1669 | this->writeBlockTrim(wordStart - lastWrite, &data[lastWrite]); |
| 1670 | if (' ' == data[wordStart - 1]) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1671 | this->writeSpace(); |
| 1672 | } |
| 1673 | } |
| 1674 | SkASSERT(temp[temp.length() - 1] > ' '); |
| 1675 | this->writeString(temp.c_str()); |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1676 | lastWrite = wordEnd; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1677 | } |
| 1678 | return lastWrite; |
| 1679 | } |
| 1680 | |
| 1681 | int IncludeWriter::lookupReference(const PunctuationState punctuation, const Word word, |
| 1682 | const int start, const int run, int lastWrite, const char last, const char* data) { |
| 1683 | const int end = PunctuationState::kDelimiter == punctuation || |
| 1684 | PunctuationState::kPeriod == punctuation ? run - 1 : run; |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1685 | RefType refType = RefType::kUndefined; |
| 1686 | string resolved = string(&data[start], (size_t) (end - start)); |
| 1687 | string temp = this->resolveRef(&data[start], &data[end], Word::kFirst == word, &refType); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1688 | if (!temp.length()) { |
| 1689 | if (Word::kFirst != word && '_' != last) { |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1690 | temp = ConvertRef(resolved, false); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1691 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1692 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1693 | if (temp.length()) { |
| 1694 | if (start > lastWrite) { |
| 1695 | SkASSERT(data[start - 1] >= ' '); |
| 1696 | if (' ' == data[lastWrite]) { |
| 1697 | this->writeSpace(); |
| 1698 | } |
| 1699 | this->writeBlockTrim(start - lastWrite, &data[lastWrite]); |
| 1700 | if (' ' == data[start - 1]) { |
| 1701 | this->writeSpace(); |
| 1702 | } |
| 1703 | } |
| 1704 | SkASSERT(temp[temp.length() - 1] > ' '); |
| 1705 | this->writeString(temp.c_str()); |
| 1706 | lastWrite = end; |
| 1707 | } |
| 1708 | return lastWrite; |
| 1709 | } |
| 1710 | |
| 1711 | /* returns true if rewriteBlock wrote linefeeds */ |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1712 | IncludeWriter::Wrote IncludeWriter::rewriteBlock(int size, const char* data, Phrase phrase) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1713 | bool wroteLineFeeds = false; |
| 1714 | while (size > 0 && data[0] <= ' ') { |
| 1715 | --size; |
| 1716 | ++data; |
| 1717 | } |
| 1718 | while (size > 0 && data[size - 1] <= ' ') { |
| 1719 | --size; |
| 1720 | } |
| 1721 | if (0 == size) { |
| 1722 | return Wrote::kNone; |
| 1723 | } |
| 1724 | int run = 0; |
| 1725 | Word word = Word::kStart; |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1726 | PunctuationState punctuation = Phrase::kNo == phrase ? |
| 1727 | PunctuationState::kStart : PunctuationState::kSpace; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1728 | int start = 0; |
| 1729 | int lastWrite = 0; |
| 1730 | int lineFeeds = 0; |
| 1731 | int lastPrintable = 0; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1732 | int lastSpace = -1; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1733 | char c = 0; |
Kevin Lubick | 4284613 | 2018-01-05 10:11:11 -0500 | [diff] [blame] | 1734 | char last = 0; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1735 | bool embeddedIndirection = false; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1736 | bool embeddedSymbol = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1737 | bool hasLower = false; |
| 1738 | bool hasUpper = false; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1739 | bool hasIndirection = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1740 | bool hasSymbol = false; |
| 1741 | while (run < size) { |
| 1742 | last = c; |
| 1743 | c = data[run]; |
| 1744 | SkASSERT(' ' <= c || '\n' == c); |
| 1745 | if (lineFeeds && ' ' < c) { |
| 1746 | if (lastPrintable >= lastWrite) { |
| 1747 | if (' ' == data[lastWrite]) { |
| 1748 | this->writeSpace(); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1749 | lastWrite++; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1750 | } |
| 1751 | this->writeBlock(lastPrintable - lastWrite + 1, &data[lastWrite]); |
| 1752 | } |
| 1753 | if (lineFeeds > 1) { |
| 1754 | this->lf(2); |
| 1755 | } |
| 1756 | this->lfcr(); // defer the indent until non-whitespace is seen |
| 1757 | lastWrite = run; |
| 1758 | lineFeeds = 0; |
| 1759 | } |
| 1760 | if (' ' < c) { |
| 1761 | lastPrintable = run; |
| 1762 | } |
| 1763 | switch (c) { |
| 1764 | case '\n': |
| 1765 | ++lineFeeds; |
| 1766 | wroteLineFeeds = true; |
| 1767 | case ' ': |
| 1768 | switch (word) { |
| 1769 | case Word::kStart: |
| 1770 | break; |
| 1771 | case Word::kUnderline: |
| 1772 | case Word::kCap: |
| 1773 | case Word::kFirst: |
| 1774 | if (!hasLower) { |
| 1775 | break; |
| 1776 | } |
| 1777 | lastWrite = this->lookupReference(punctuation, word, start, run, |
| 1778 | lastWrite, last, data); |
| 1779 | break; |
| 1780 | case Word::kMixed: |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1781 | if (hasUpper && hasLower && !hasSymbol && lastSpace > 0) { |
| 1782 | lastWrite = this->lookupMethod(punctuation, word, lastSpace, run, |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1783 | lastWrite, data, hasIndirection && !hasSymbol); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1784 | } |
| 1785 | break; |
| 1786 | default: |
| 1787 | SkASSERT(0); |
| 1788 | } |
Cary Clark | 1eace2d | 2017-07-31 07:52:43 -0400 | [diff] [blame] | 1789 | punctuation = PunctuationState::kPeriod == punctuation || |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1790 | (PunctuationState::kStart == punctuation && ' ' >= last) ? |
Cary Clark | 1eace2d | 2017-07-31 07:52:43 -0400 | [diff] [blame] | 1791 | PunctuationState::kStart : PunctuationState::kSpace; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1792 | word = Word::kStart; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1793 | embeddedIndirection = false; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1794 | embeddedSymbol = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1795 | hasLower = false; |
| 1796 | hasUpper = false; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1797 | hasIndirection = false; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1798 | hasSymbol = false; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1799 | lastSpace = run; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1800 | break; |
| 1801 | case '.': |
| 1802 | switch (word) { |
| 1803 | case Word::kStart: |
| 1804 | punctuation = PunctuationState::kDelimiter; |
| 1805 | case Word::kCap: |
| 1806 | case Word::kFirst: |
| 1807 | case Word::kUnderline: |
| 1808 | case Word::kMixed: |
| 1809 | if (PunctuationState::kDelimiter == punctuation || |
| 1810 | PunctuationState::kPeriod == punctuation) { |
| 1811 | word = Word::kMixed; |
| 1812 | } |
| 1813 | punctuation = PunctuationState::kPeriod; |
| 1814 | break; |
| 1815 | default: |
| 1816 | SkASSERT(0); |
| 1817 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1818 | embeddedIndirection = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1819 | break; |
| 1820 | case ',': case ';': case ':': |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1821 | switch (word) { |
| 1822 | case Word::kStart: |
| 1823 | punctuation = PunctuationState::kDelimiter; |
| 1824 | case Word::kCap: |
| 1825 | case Word::kFirst: |
| 1826 | case Word::kUnderline: |
| 1827 | case Word::kMixed: |
| 1828 | if (PunctuationState::kDelimiter == punctuation || |
| 1829 | PunctuationState::kPeriod == punctuation) { |
| 1830 | word = Word::kMixed; |
| 1831 | } |
| 1832 | punctuation = PunctuationState::kDelimiter; |
| 1833 | break; |
| 1834 | default: |
| 1835 | SkASSERT(0); |
| 1836 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1837 | embeddedSymbol = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1838 | break; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1839 | case '>': |
| 1840 | if ('-' == last) { |
| 1841 | embeddedIndirection = true; |
| 1842 | break; |
| 1843 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1844 | case '\'': // possessive apostrophe isn't treated as delimiting punctation |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1845 | case '\"': // quote is passed straight through |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1846 | case '=': |
| 1847 | case '!': // assumed not to be punctuation, but a programming symbol |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1848 | case '&': case '<': case '{': case '}': case '/': case '*': case '[': case ']': |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1849 | word = Word::kMixed; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1850 | embeddedSymbol = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1851 | break; |
| 1852 | case '(': |
| 1853 | if (' ' == last) { |
| 1854 | punctuation = PunctuationState::kDelimiter; |
| 1855 | } else { |
| 1856 | word = Word::kMixed; |
| 1857 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1858 | embeddedSymbol = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1859 | break; |
| 1860 | case ')': // assume word type has already been set |
| 1861 | punctuation = PunctuationState::kDelimiter; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1862 | embeddedSymbol = true; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1863 | break; |
| 1864 | case '_': |
| 1865 | switch (word) { |
| 1866 | case Word::kStart: |
| 1867 | word = Word::kMixed; |
| 1868 | break; |
| 1869 | case Word::kCap: |
| 1870 | case Word::kFirst: |
| 1871 | case Word::kUnderline: |
| 1872 | word = Word::kUnderline; |
| 1873 | break; |
| 1874 | case Word::kMixed: |
| 1875 | break; |
| 1876 | default: |
| 1877 | SkASSERT(0); |
| 1878 | } |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1879 | hasSymbol |= embeddedSymbol; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1880 | break; |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 1881 | case '+': |
| 1882 | // hackery to allow C++ |
| 1883 | SkASSERT('C' == last || '+' == last); // FIXME: don't allow + outside of #Formula |
| 1884 | break; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1885 | case 'A': case 'B': case 'C': case 'D': case 'E': |
| 1886 | case 'F': case 'G': case 'H': case 'I': case 'J': |
| 1887 | case 'K': case 'L': case 'M': case 'N': case 'O': |
| 1888 | case 'P': case 'Q': case 'R': case 'S': case 'T': |
| 1889 | case 'U': case 'V': case 'W': case 'X': case 'Y': |
| 1890 | case 'Z': |
| 1891 | switch (word) { |
| 1892 | case Word::kStart: |
| 1893 | word = PunctuationState::kStart == punctuation ? Word::kFirst : Word::kCap; |
| 1894 | start = run; |
| 1895 | break; |
| 1896 | case Word::kCap: |
| 1897 | case Word::kFirst: |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1898 | if (!isupper(last) && '~' != last) { |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1899 | word = Word::kMixed; |
| 1900 | } |
| 1901 | break; |
| 1902 | case Word::kUnderline: |
| 1903 | // some word in Xxx_XXX_Xxx can be all upper, but all can't: XXX_XXX |
| 1904 | if ('_' != last && !isupper(last)) { |
| 1905 | word = Word::kMixed; |
| 1906 | } |
| 1907 | break; |
| 1908 | case Word::kMixed: |
| 1909 | break; |
| 1910 | default: |
| 1911 | SkASSERT(0); |
| 1912 | } |
| 1913 | hasUpper = true; |
| 1914 | if (PunctuationState::kPeriod == punctuation || |
| 1915 | PunctuationState::kDelimiter == punctuation) { |
| 1916 | word = Word::kMixed; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1917 | } |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1918 | hasIndirection |= embeddedIndirection; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1919 | hasSymbol |= embeddedSymbol; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1920 | break; |
| 1921 | case 'a': case 'b': case 'c': case 'd': case 'e': |
| 1922 | case 'f': case 'g': case 'h': case 'i': case 'j': |
| 1923 | case 'k': case 'l': case 'm': case 'n': case 'o': |
| 1924 | case 'p': case 'q': case 'r': case 's': case 't': |
| 1925 | case 'u': case 'v': case 'w': case 'x': case 'y': |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1926 | case 'z': |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1927 | case '0': case '1': case '2': case '3': case '4': |
| 1928 | case '5': case '6': case '7': case '8': case '9': |
| 1929 | case '-': |
| 1930 | switch (word) { |
| 1931 | case Word::kStart: |
| 1932 | word = Word::kMixed; |
| 1933 | break; |
| 1934 | case Word::kMixed: |
| 1935 | case Word::kCap: |
| 1936 | case Word::kFirst: |
| 1937 | case Word::kUnderline: |
| 1938 | break; |
| 1939 | default: |
| 1940 | SkASSERT(0); |
| 1941 | } |
| 1942 | hasLower = true; |
| 1943 | punctuation = PunctuationState::kStart; |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1944 | hasIndirection |= embeddedIndirection; |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1945 | hasSymbol |= embeddedSymbol; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1946 | break; |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1947 | case '~': |
| 1948 | SkASSERT(Word::kStart == word); |
| 1949 | word = PunctuationState::kStart == punctuation ? Word::kFirst : Word::kCap; |
| 1950 | start = run; |
| 1951 | hasUpper = true; |
| 1952 | hasIndirection |= embeddedIndirection; |
| 1953 | hasSymbol |= embeddedSymbol; |
| 1954 | break; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1955 | default: |
| 1956 | SkASSERT(0); |
| 1957 | } |
| 1958 | ++run; |
| 1959 | } |
| 1960 | if ((word == Word::kCap || word == Word::kFirst || word == Word::kUnderline) && hasLower) { |
| 1961 | lastWrite = this->lookupReference(punctuation, word, start, run, lastWrite, last, data); |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 1962 | } else if (word == Word::kMixed && hasUpper && hasLower && !hasSymbol && lastSpace > 0) { |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1963 | lastWrite = this->lookupMethod(punctuation, word, lastSpace, run, lastWrite, data, |
| 1964 | hasIndirection && !hasSymbol); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1965 | } |
| 1966 | if (run > lastWrite) { |
| 1967 | if (' ' == data[lastWrite]) { |
| 1968 | this->writeSpace(); |
| 1969 | } |
| 1970 | this->writeBlock(run - lastWrite, &data[lastWrite]); |
| 1971 | } |
| 1972 | return wroteLineFeeds ? Wrote::kLF : Wrote::kChars; |
| 1973 | } |