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 | |
| 10 | enum class KeyProperty { |
| 11 | kNone, |
| 12 | kClassSection, |
| 13 | kFunction, |
| 14 | kModifier, |
| 15 | kNumber, |
| 16 | kObject, |
| 17 | kPreprocessor, |
| 18 | }; |
| 19 | |
| 20 | struct IncludeKey { |
| 21 | const char* fName; |
| 22 | KeyWord fKeyWord; |
| 23 | KeyProperty fProperty; |
| 24 | }; |
| 25 | |
| 26 | const IncludeKey kKeyWords[] = { |
| 27 | { "", KeyWord::kNone, KeyProperty::kNone }, |
| 28 | { "bool", KeyWord::kBool, KeyProperty::kNumber }, |
| 29 | { "char", KeyWord::kChar, KeyProperty::kNumber }, |
| 30 | { "class", KeyWord::kClass, KeyProperty::kObject }, |
| 31 | { "const", KeyWord::kConst, KeyProperty::kModifier }, |
| 32 | { "constexpr", KeyWord::kConstExpr, KeyProperty::kModifier }, |
| 33 | { "define", KeyWord::kDefine, KeyProperty::kPreprocessor }, |
| 34 | { "double", KeyWord::kDouble, KeyProperty::kNumber }, |
| 35 | { "elif", KeyWord::kElif, KeyProperty::kPreprocessor }, |
| 36 | { "else", KeyWord::kElse, KeyProperty::kPreprocessor }, |
| 37 | { "endif", KeyWord::kEndif, KeyProperty::kPreprocessor }, |
| 38 | { "enum", KeyWord::kEnum, KeyProperty::kObject }, |
| 39 | { "float", KeyWord::kFloat, KeyProperty::kNumber }, |
| 40 | { "friend", KeyWord::kFriend, KeyProperty::kModifier }, |
| 41 | { "if", KeyWord::kIf, KeyProperty::kPreprocessor }, |
| 42 | { "ifdef", KeyWord::kIfdef, KeyProperty::kPreprocessor }, |
| 43 | { "ifndef", KeyWord::kIfndef, KeyProperty::kPreprocessor }, |
| 44 | { "include", KeyWord::kInclude, KeyProperty::kPreprocessor }, |
| 45 | { "inline", KeyWord::kInline, KeyProperty::kModifier }, |
| 46 | { "int", KeyWord::kInt, KeyProperty::kNumber }, |
| 47 | { "operator", KeyWord::kOperator, KeyProperty::kFunction }, |
| 48 | { "private", KeyWord::kPrivate, KeyProperty::kClassSection }, |
| 49 | { "protected", KeyWord::kProtected, KeyProperty::kClassSection }, |
| 50 | { "public", KeyWord::kPublic, KeyProperty::kClassSection }, |
| 51 | { "signed", KeyWord::kSigned, KeyProperty::kNumber }, |
| 52 | { "size_t", KeyWord::kSize_t, KeyProperty::kNumber }, |
| 53 | { "static", KeyWord::kStatic, KeyProperty::kModifier }, |
| 54 | { "struct", KeyWord::kStruct, KeyProperty::kObject }, |
| 55 | { "template", KeyWord::kTemplate, KeyProperty::kObject }, |
| 56 | { "typedef", KeyWord::kTypedef, KeyProperty::kObject }, |
| 57 | { "uint32_t", KeyWord::kUint32_t, KeyProperty::kNumber }, |
| 58 | { "union", KeyWord::kUnion, KeyProperty::kObject }, |
| 59 | { "unsigned", KeyWord::kUnsigned, KeyProperty::kNumber }, |
| 60 | { "void", KeyWord::kVoid, KeyProperty::kNumber }, |
| 61 | }; |
| 62 | |
| 63 | const size_t kKeyWordCount = SK_ARRAY_COUNT(kKeyWords); |
| 64 | |
| 65 | KeyWord IncludeParser::FindKey(const char* start, const char* end) { |
| 66 | int ch = 0; |
| 67 | for (size_t index = 0; index < kKeyWordCount; ) { |
| 68 | if (start[ch] > kKeyWords[index].fName[ch]) { |
| 69 | ++index; |
| 70 | if (ch > 0 && kKeyWords[index - 1].fName[ch - 1] < kKeyWords[index].fName[ch - 1]) { |
| 71 | return KeyWord::kNone; |
| 72 | } |
| 73 | continue; |
| 74 | } |
| 75 | if (start[ch] < kKeyWords[index].fName[ch]) { |
| 76 | return KeyWord::kNone; |
| 77 | } |
| 78 | ++ch; |
| 79 | if (start + ch >= end) { |
| 80 | return kKeyWords[index].fKeyWord; |
| 81 | } |
| 82 | } |
| 83 | return KeyWord::kNone; |
| 84 | } |
| 85 | |
| 86 | void IncludeParser::ValidateKeyWords() { |
| 87 | for (size_t index = 1; index < kKeyWordCount; ++index) { |
| 88 | SkASSERT((int) kKeyWords[index - 1].fKeyWord + 1 |
| 89 | == (int) kKeyWords[index].fKeyWord); |
| 90 | SkASSERT(0 > strcmp(kKeyWords[index - 1].fName, kKeyWords[index].fName)); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void IncludeParser::addKeyword(KeyWord keyWord) { |
| 95 | fParent->fTokens.emplace_back(keyWord, fIncludeWord, fChar, fLineCount, fParent); |
| 96 | fIncludeWord = nullptr; |
| 97 | if (KeyProperty::kObject == kKeyWords[(int) keyWord].fProperty) { |
| 98 | Definition* def = &fParent->fTokens.back(); |
| 99 | this->addDefinition(def); |
| 100 | if (KeyWord::kEnum == fParent->fKeyWord) { |
| 101 | fInEnum = true; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void IncludeParser::checkForMissingParams(const vector<string>& methodParams, |
| 107 | const vector<string>& foundParams) { |
| 108 | for (auto& methodParam : methodParams) { |
| 109 | bool found = false; |
| 110 | for (auto& foundParam : foundParams) { |
| 111 | if (methodParam == foundParam) { |
| 112 | found = true; |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | if (!found) { |
| 117 | this->keywordStart("Param"); |
| 118 | fprintf(fOut, "%s ", methodParam.c_str()); |
| 119 | this->keywordEnd(); |
| 120 | } |
| 121 | } |
| 122 | for (auto& foundParam : foundParams) { |
| 123 | bool found = false; |
| 124 | for (auto& methodParam : methodParams) { |
| 125 | if (methodParam == foundParam) { |
| 126 | found = true; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | if (!found) { |
| 131 | this->reportError("doxygen param does not match method declaration"); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | bool IncludeParser::checkForWord() { |
| 137 | if (!fIncludeWord) { |
| 138 | return true; |
| 139 | } |
| 140 | KeyWord keyWord = FindKey(fIncludeWord, fChar); |
| 141 | if (KeyWord::kNone != keyWord) { |
| 142 | if (KeyProperty::kPreprocessor != kKeyWords[(int) keyWord].fProperty) { |
| 143 | this->addKeyword(keyWord); |
| 144 | return true; |
| 145 | } |
| 146 | } else { |
| 147 | this->addWord(); |
| 148 | return true; |
| 149 | } |
| 150 | Definition* poundDef = fParent; |
| 151 | if (!fParent) { |
| 152 | return reportError<bool>("expected parent"); |
| 153 | } |
| 154 | if (Definition::Type::kBracket != poundDef->fType) { |
| 155 | return reportError<bool>("expected bracket"); |
| 156 | } |
| 157 | if (Bracket::kPound != poundDef->fBracket) { |
| 158 | return reportError<bool>("expected preprocessor"); |
| 159 | } |
| 160 | if (KeyWord::kNone != poundDef->fKeyWord) { |
| 161 | return reportError<bool>("already found keyword"); |
| 162 | } |
| 163 | poundDef->fKeyWord = keyWord; |
| 164 | fIncludeWord = nullptr; |
| 165 | switch (keyWord) { |
| 166 | // these do not link to other # directives |
| 167 | case KeyWord::kDefine: |
| 168 | case KeyWord::kInclude: |
| 169 | break; |
| 170 | // these start a # directive link |
| 171 | case KeyWord::kIf: |
| 172 | case KeyWord::kIfdef: |
| 173 | case KeyWord::kIfndef: |
| 174 | break; |
| 175 | // these continue a # directive link |
| 176 | case KeyWord::kElif: |
| 177 | case KeyWord::kElse: { |
| 178 | this->popObject(); // pop elif |
| 179 | if (Bracket::kPound != fParent->fBracket) { |
| 180 | return this->reportError<bool>("expected preprocessor directive"); |
| 181 | } |
| 182 | this->popBracket(); // pop if |
| 183 | poundDef->fParent = fParent; |
| 184 | this->addDefinition(poundDef); // push elif back |
| 185 | } break; |
| 186 | // this ends a # directive link |
| 187 | case KeyWord::kEndif: |
| 188 | // FIXME : should this be calling popBracket() instead? |
| 189 | this->popObject(); // pop endif |
| 190 | if (Bracket::kPound != fParent->fBracket) { |
| 191 | return this->reportError<bool>("expected preprocessor directive"); |
| 192 | } |
| 193 | this->popBracket(); // pop if/else |
| 194 | break; |
| 195 | default: |
| 196 | SkASSERT(0); |
| 197 | } |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | string IncludeParser::className() const { |
| 202 | string name(fParent->fName); |
| 203 | size_t slash = name.find_last_of("/"); |
| 204 | if (string::npos == slash) { |
| 205 | slash = name.find_last_of("\\"); |
| 206 | } |
| 207 | SkASSERT(string::npos != slash); |
| 208 | string result = name.substr(slash); |
| 209 | result = result.substr(1, result.size() - 3); |
| 210 | return result; |
| 211 | } |
| 212 | |
| 213 | bool IncludeParser::crossCheck(BmhParser& bmhParser) { |
| 214 | string className = this->className(); |
| 215 | string classPrefix = className + "::"; |
| 216 | RootDefinition* root = &bmhParser.fClassMap[className]; |
| 217 | root->clearVisited(); |
| 218 | for (auto& classMapper : fIClassMap) { |
| 219 | if (className != classMapper.first |
| 220 | && classPrefix != classMapper.first.substr(0, classPrefix.length())) { |
| 221 | continue; |
| 222 | } |
| 223 | auto& classMap = classMapper.second; |
| 224 | auto& tokens = classMap.fTokens; |
| 225 | for (const auto& token : tokens) { |
| 226 | if (token.fPrivate) { |
| 227 | continue; |
| 228 | } |
| 229 | string fullName = classMapper.first + "::" + token.fName; |
| 230 | const Definition* def = root->find(fullName); |
| 231 | switch (token.fMarkType) { |
| 232 | case MarkType::kMethod: { |
| 233 | if (0 == token.fName.find("internal_") |
| 234 | || 0 == token.fName.find("Internal_") |
| 235 | || 0 == token.fName.find("legacy_") |
| 236 | || 0 == token.fName.find("temporary_")) { |
| 237 | continue; |
| 238 | } |
| 239 | const char* methodID = bmhParser.fMaps[(int) token.fMarkType].fName; |
| 240 | if (!def) { |
| 241 | string paramName = className + "::"; |
| 242 | paramName += string(token.fContentStart, |
| 243 | token.fContentEnd - token.fContentStart); |
| 244 | def = root->find(paramName); |
| 245 | if (!def && 0 == token.fName.find("operator")) { |
| 246 | string operatorName = className + "::"; |
| 247 | TextParser oper("", token.fStart, token.fContentEnd, 0); |
| 248 | const char* start = oper.strnstr("operator", token.fContentEnd); |
| 249 | SkASSERT(start); |
| 250 | oper.skipTo(start); |
| 251 | oper.skipToEndBracket('('); |
| 252 | int parens = 0; |
| 253 | do { |
| 254 | if ('(' == oper.peek()) { |
| 255 | ++parens; |
| 256 | } else if (')' == oper.peek()) { |
| 257 | --parens; |
| 258 | } |
| 259 | } while (!oper.eof() && oper.next() && parens > 0); |
| 260 | operatorName += string(start, oper.fChar - start); |
| 261 | def = root->find(operatorName); |
| 262 | } |
| 263 | } |
| 264 | if (!def) { |
| 265 | int skip = !strncmp(token.fContentStart, "explicit ", 9) ? 9 : 0; |
| 266 | skip = !strncmp(token.fContentStart, "virtual ", 8) ? 8 : skip; |
| 267 | string constructorName = className + "::"; |
| 268 | constructorName += string(token.fContentStart + skip, |
| 269 | token.fContentEnd - token.fContentStart - skip); |
| 270 | def = root->find(constructorName); |
| 271 | } |
| 272 | if (!def && 0 == token.fName.find("SK_")) { |
| 273 | string incName = token.fName + "()"; |
| 274 | string macroName = className + "::" + incName; |
| 275 | def = root->find(macroName); |
| 276 | if (def) { |
| 277 | if (def->fName == incName) { |
| 278 | def->fVisited = true; |
| 279 | if ("SK_TO_STRING_NONVIRT" == token.fName) { |
| 280 | def = root->find(className + "::toString"); |
| 281 | if (def) { |
| 282 | def->fVisited = true; |
| 283 | } else { |
| 284 | SkDebugf("missing toString bmh: %s\n", fullName.c_str()); |
| 285 | } |
| 286 | } |
| 287 | break; |
| 288 | } else { |
| 289 | SkDebugf("method macro differs from bmh: %s\n", fullName.c_str()); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | if (!def) { |
| 294 | bool allLower = true; |
| 295 | for (size_t index = 0; index < token.fName.length(); ++index) { |
| 296 | if (!islower(token.fName[index])) { |
| 297 | allLower = false; |
| 298 | break; |
| 299 | } |
| 300 | } |
| 301 | if (allLower) { |
| 302 | string lowerName = className + "::" + token.fName + "()"; |
| 303 | def = root->find(lowerName); |
| 304 | } |
| 305 | } |
| 306 | if (!def) { |
| 307 | SkDebugf("method missing from bmh: %s\n", fullName.c_str()); |
| 308 | break; |
| 309 | } |
| 310 | if (def->crossCheck(methodID, token)) { |
| 311 | def->fVisited = true; |
| 312 | } else { |
| 313 | SkDebugf("method differs from bmh: %s\n", fullName.c_str()); |
| 314 | } |
| 315 | } break; |
| 316 | case MarkType::kComment: |
| 317 | break; |
| 318 | case MarkType::kEnum: { |
| 319 | if (!def) { |
| 320 | // work backwards from first word to deduce #Enum name |
| 321 | TextParser firstMember("", token.fStart, token.fContentEnd, 0); |
| 322 | SkAssertResult(firstMember.skipName("enum")); |
| 323 | SkAssertResult(firstMember.skipToEndBracket('{')); |
| 324 | firstMember.next(); |
| 325 | firstMember.skipWhiteSpace(); |
| 326 | SkASSERT('k' == firstMember.peek()); |
| 327 | const char* savePos = firstMember.fChar; |
| 328 | firstMember.skipToNonAlphaNum(); |
| 329 | const char* wordEnd = firstMember.fChar; |
| 330 | firstMember.fChar = savePos; |
| 331 | const char* lastUnderscore = nullptr; |
| 332 | do { |
| 333 | if (!firstMember.skipToEndBracket('_')) { |
| 334 | break; |
| 335 | } |
| 336 | if (firstMember.fChar > wordEnd) { |
| 337 | break; |
| 338 | } |
| 339 | lastUnderscore = firstMember.fChar; |
| 340 | } while (firstMember.next()); |
| 341 | if (lastUnderscore) { |
| 342 | ++lastUnderscore; |
| 343 | string anonName = className + "::" + string(lastUnderscore, |
| 344 | wordEnd - lastUnderscore) + 's'; |
| 345 | def = root->find(anonName); |
| 346 | } |
| 347 | if (!def) { |
| 348 | SkDebugf("enum missing from bmh: %s\n", fullName.c_str()); |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | def->fVisited = true; |
| 353 | for (auto& child : def->fChildren) { |
| 354 | if (MarkType::kCode == child->fMarkType) { |
| 355 | def = child; |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | if (MarkType::kCode != def->fMarkType) { |
| 360 | SkDebugf("enum code missing from bmh: %s\n", fullName.c_str()); |
| 361 | break; |
| 362 | } |
| 363 | if (def->crossCheck(token)) { |
| 364 | def->fVisited = true; |
| 365 | } else { |
| 366 | SkDebugf("enum differs from bmh: %s\n", def->fName.c_str()); |
| 367 | } |
| 368 | for (auto& child : token.fChildren) { |
| 369 | string constName = className + "::" + child->fName; |
| 370 | def = root->find(constName); |
| 371 | if (!def) { |
| 372 | string innerName = classMapper.first + "::" + child->fName; |
| 373 | def = root->find(innerName); |
| 374 | } |
| 375 | if (!def) { |
| 376 | if (string::npos == child->fName.find("Legacy_")) { |
| 377 | SkDebugf("const missing from bmh: %s\n", constName.c_str()); |
| 378 | } |
| 379 | } else { |
| 380 | def->fVisited = true; |
| 381 | } |
| 382 | } |
| 383 | } break; |
| 384 | case MarkType::kMember: |
| 385 | if (def) { |
| 386 | def->fVisited = true; |
| 387 | } else { |
| 388 | SkDebugf("member missing from bmh: %s\n", fullName.c_str()); |
| 389 | } |
| 390 | break; |
| 391 | default: |
| 392 | SkASSERT(0); // unhandled |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | if (!root->dumpUnVisited()) { |
| 398 | SkDebugf("some struct elements not found; struct finding in includeParser is missing\n"); |
| 399 | } |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | IClassDefinition* IncludeParser::defineClass(const Definition& includeDef, |
| 404 | const string& name) { |
| 405 | string className; |
| 406 | const Definition* test = fParent; |
| 407 | while (Definition::Type::kFileType != test->fType) { |
| 408 | if (Definition::Type::kMark == test->fType && KeyWord::kClass == test->fKeyWord) { |
| 409 | className = test->fName + "::"; |
| 410 | break; |
| 411 | } |
| 412 | test = test->fParent; |
| 413 | } |
| 414 | className += name; |
| 415 | unordered_map<string, IClassDefinition>& map = fIClassMap; |
| 416 | IClassDefinition& markupDef = map[className]; |
| 417 | if (markupDef.fStart) { |
| 418 | typedef IClassDefinition* IClassDefPtr; |
| 419 | return INHERITED::reportError<IClassDefPtr>("class already defined"); |
| 420 | } |
| 421 | markupDef.fFileName = fFileName; |
| 422 | markupDef.fStart = includeDef.fStart; |
| 423 | markupDef.fContentStart = includeDef.fStart; |
| 424 | markupDef.fName = className; |
| 425 | markupDef.fContentEnd = includeDef.fContentEnd; |
| 426 | markupDef.fTerminator = includeDef.fTerminator; |
| 427 | markupDef.fParent = fParent; |
| 428 | markupDef.fLineCount = fLineCount; |
| 429 | markupDef.fMarkType = KeyWord::kStruct == includeDef.fKeyWord ? |
| 430 | MarkType::kStruct : MarkType::kClass; |
| 431 | markupDef.fKeyWord = includeDef.fKeyWord; |
| 432 | markupDef.fType = Definition::Type::kMark; |
| 433 | fParent = &markupDef; |
| 434 | return &markupDef; |
| 435 | } |
| 436 | |
| 437 | void IncludeParser::dumpClassTokens(IClassDefinition& classDef) { |
| 438 | auto& tokens = classDef.fTokens; |
| 439 | for (auto& token : tokens) { |
| 440 | if (Definition::Type::kMark == token.fType && MarkType::kComment == token.fMarkType) { |
| 441 | continue; |
| 442 | } |
| 443 | if (MarkType::kMember != token.fMarkType) { |
| 444 | fprintf(fOut, "%s", |
| 445 | "# ------------------------------------------------------------------------------\n"); |
| 446 | fprintf(fOut, "" "\n"); |
| 447 | } |
| 448 | switch (token.fMarkType) { |
| 449 | case MarkType::kEnum: |
| 450 | fprintf(fOut, "#Enum %s" "\n", |
| 451 | token.fName.c_str()); |
| 452 | fprintf(fOut, "" "\n"); |
| 453 | fprintf(fOut, "#Code" "\n"); |
| 454 | fprintf(fOut, " enum %s {" "\n", |
| 455 | token.fName.c_str()); |
| 456 | for (auto& child : token.fChildren) { |
| 457 | fprintf(fOut, " %s %.*s" "\n", |
| 458 | child->fName.c_str(), child->length(), child->fContentStart); |
| 459 | } |
| 460 | fprintf(fOut, " };" "\n"); |
| 461 | fprintf(fOut, "##" "\n"); |
| 462 | fprintf(fOut, "" "\n"); |
| 463 | this->dumpComment(&token); |
| 464 | for (auto& child : token.fChildren) { |
| 465 | fprintf(fOut, "#Const %s", child->fName.c_str()); |
| 466 | TextParser val(child); |
| 467 | if (!val.eof()) { |
| 468 | if ('=' == val.fStart[0] || ',' == val.fStart[0]) { |
| 469 | val.next(); |
| 470 | val.skipSpace(); |
| 471 | const char* valEnd = val.anyOf(",\n"); |
| 472 | if (!valEnd) { |
| 473 | valEnd = val.fEnd; |
| 474 | } |
| 475 | fprintf(fOut, " %.*s", (int) (valEnd - val.fStart), val.fStart); |
| 476 | } else { |
| 477 | fprintf(fOut, " %.*s", |
| 478 | (int) (child->fContentEnd - child->fContentStart), |
| 479 | child->fContentStart); |
| 480 | } |
| 481 | } |
| 482 | fprintf(fOut, "" "\n"); |
| 483 | for (auto& token : child->fTokens) { |
| 484 | if (MarkType::kComment == token.fMarkType) { |
| 485 | this->dumpComment(&token); |
| 486 | } |
| 487 | } |
| 488 | fprintf(fOut, "##" "\n"); |
| 489 | } |
| 490 | fprintf(fOut, "" "\n"); |
| 491 | break; |
| 492 | case MarkType::kMethod: |
| 493 | fprintf(fOut, "#Method %.*s" "\n", |
| 494 | token.length(), token.fStart); |
| 495 | lfAlways(1); |
| 496 | this->dumpComment(&token); |
| 497 | break; |
| 498 | case MarkType::kMember: |
| 499 | this->keywordStart("Member"); |
| 500 | fprintf(fOut, "%.*s %s ", (int) (token.fContentEnd - token.fContentStart), |
| 501 | token.fContentStart, token.fName.c_str()); |
| 502 | lfAlways(1); |
| 503 | for (auto child : token.fChildren) { |
| 504 | fprintf(fOut, "%.*s", (int) (child->fContentEnd - child->fContentStart), |
| 505 | child->fContentStart); |
| 506 | lfAlways(1); |
| 507 | } |
| 508 | this->keywordEnd(); |
| 509 | continue; |
| 510 | break; |
| 511 | default: |
| 512 | SkASSERT(0); |
| 513 | } |
| 514 | this->lf(2); |
| 515 | fprintf(fOut, "#Example" "\n"); |
| 516 | fprintf(fOut, "##" "\n"); |
| 517 | fprintf(fOut, "" "\n"); |
| 518 | fprintf(fOut, "#ToDo incomplete ##" "\n"); |
| 519 | fprintf(fOut, "" "\n"); |
| 520 | fprintf(fOut, "##" "\n"); |
| 521 | fprintf(fOut, "" "\n"); |
| 522 | } |
| 523 | } |
| 524 | void IncludeParser::dumpComment(Definition* token) { |
| 525 | fLineCount = token->fLineCount; |
| 526 | fChar = fLine = token->fContentStart; |
| 527 | fEnd = token->fContentEnd; |
| 528 | bool sawParam = false; |
| 529 | bool multiline = false; |
| 530 | bool sawReturn = false; |
| 531 | bool sawComment = false; |
| 532 | bool methodHasReturn = false; |
| 533 | vector<string> methodParams; |
| 534 | vector<string> foundParams; |
| 535 | Definition methodName; |
| 536 | TextParser methodParser(token->fFileName, token->fContentStart, token->fContentEnd, |
| 537 | token->fLineCount); |
| 538 | if (MarkType::kMethod == token->fMarkType) { |
| 539 | methodName.fName = string(token->fContentStart, |
| 540 | (int) (token->fContentEnd - token->fContentStart)); |
| 541 | methodHasReturn = !methodParser.startsWith("void ") |
| 542 | && !methodParser.strnchr('~', methodParser.fEnd); |
| 543 | const char* paren = methodParser.strnchr('(', methodParser.fEnd); |
| 544 | const char* nextEnd = paren; |
| 545 | do { |
| 546 | string paramName; |
| 547 | methodParser.fChar = nextEnd + 1; |
| 548 | methodParser.skipSpace(); |
| 549 | if (!methodName.nextMethodParam(&methodParser, &nextEnd, ¶mName)) { |
| 550 | continue; |
| 551 | } |
| 552 | methodParams.push_back(paramName); |
| 553 | } while (')' != nextEnd[0]); |
| 554 | } |
| 555 | for (const auto& child : token->fTokens) { |
| 556 | if (Definition::Type::kMark == child.fType && MarkType::kComment == child.fMarkType) { |
| 557 | if ('@' == child.fContentStart[0]) { |
| 558 | TextParser parser(&child); |
| 559 | do { |
| 560 | parser.next(); |
| 561 | if (parser.startsWith("param ")) { |
| 562 | parser.skipWord("param"); |
| 563 | const char* parmStart = parser.fChar; |
| 564 | parser.skipToSpace(); |
| 565 | string parmName = string(parmStart, (int) (parser.fChar - parmStart)); |
| 566 | parser.skipWhiteSpace(); |
| 567 | do { |
| 568 | size_t nextComma = parmName.find(','); |
| 569 | string piece; |
| 570 | if (string::npos == nextComma) { |
| 571 | piece = parmName; |
| 572 | parmName = ""; |
| 573 | } else { |
| 574 | piece = parmName.substr(0, nextComma); |
| 575 | parmName = parmName.substr(nextComma + 1); |
| 576 | } |
| 577 | if (sawParam) { |
| 578 | if (multiline) { |
| 579 | this->lfAlways(1); |
| 580 | } |
| 581 | this->keywordEnd(); |
| 582 | } else { |
| 583 | if (sawComment) { |
| 584 | this->nl(); |
| 585 | } |
| 586 | this->lf(2); |
| 587 | } |
| 588 | foundParams.emplace_back(piece); |
| 589 | this->keywordStart("Param"); |
| 590 | fprintf(fOut, "%s ", piece.c_str()); |
| 591 | fprintf(fOut, "%.*s", (int) (parser.fEnd - parser.fChar), parser.fChar); |
| 592 | this->lfAlways(1); |
| 593 | sawParam = true; |
| 594 | sawComment = false; |
| 595 | } while (parmName.length()); |
| 596 | parser.skipTo(parser.fEnd); |
| 597 | } else if (parser.startsWith("return ") || parser.startsWith("returns ")) { |
| 598 | parser.skipWord("return"); |
| 599 | if ('s' == parser.peek()) { |
| 600 | parser.next(); |
| 601 | } |
| 602 | if (sawParam) { |
| 603 | if (multiline) { |
| 604 | this->lfAlways(1); |
| 605 | } |
| 606 | this->keywordEnd(); |
| 607 | } |
| 608 | this->checkForMissingParams(methodParams, foundParams); |
| 609 | sawParam = false; |
| 610 | sawComment = false; |
| 611 | multiline = false; |
| 612 | this->lf(2); |
| 613 | this->keywordStart("Return"); |
| 614 | fprintf(fOut, "%.*s ", (int) (parser.fEnd - parser.fChar), |
| 615 | parser.fChar); |
| 616 | this->lfAlways(1); |
| 617 | sawReturn = true; |
| 618 | parser.skipTo(parser.fEnd); |
| 619 | } else { |
| 620 | this->reportError("unexpected doxygen directive"); |
| 621 | } |
| 622 | } while (!parser.eof()); |
| 623 | } else { |
| 624 | if (sawComment) { |
| 625 | this->nl(); |
| 626 | } |
| 627 | this->lf(1); |
| 628 | fprintf(fOut, "%.*s ", child.length(), child.fContentStart); |
| 629 | sawComment = true; |
| 630 | if (sawParam || sawReturn) { |
| 631 | multiline = true; |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | if (sawParam || sawReturn) { |
| 637 | if (multiline) { |
| 638 | this->lfAlways(1); |
| 639 | } |
| 640 | this->keywordEnd(); |
| 641 | } |
| 642 | if (!sawReturn) { |
| 643 | if (!sawParam) { |
| 644 | if (sawComment) { |
| 645 | this->nl(); |
| 646 | } |
| 647 | this->lf(2); |
| 648 | } |
| 649 | this->checkForMissingParams(methodParams, foundParams); |
| 650 | } |
| 651 | if (methodHasReturn != sawReturn) { |
| 652 | if (!methodHasReturn) { |
| 653 | this->reportError("unexpected doxygen return"); |
| 654 | } else { |
| 655 | if (sawComment) { |
| 656 | this->nl(); |
| 657 | } |
| 658 | this->lf(2); |
| 659 | this->keywordStart("Return"); |
| 660 | this->keywordEnd(); |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | // dump equivalent markup |
| 666 | void IncludeParser::dumpTokens() { |
| 667 | string skClassName = this->className(); |
| 668 | string fileName = skClassName + ".bmh"; |
| 669 | fOut = fopen(fileName.c_str(), "wb"); |
| 670 | if (!fOut) { |
| 671 | SkDebugf("could not open output file %s\n", fileName.c_str()); |
| 672 | return; |
| 673 | } |
| 674 | string prefixName = skClassName.substr(0, 2); |
| 675 | string topicName = skClassName.length() > 2 && isupper(skClassName[2]) && |
| 676 | ("Sk" == prefixName || "Gr" == prefixName) ? skClassName.substr(2) : skClassName; |
| 677 | fprintf(fOut, "#Topic %s", topicName.c_str()); |
| 678 | this->lfAlways(2); |
| 679 | fprintf(fOut, "#Class %s", skClassName.c_str()); |
| 680 | this->lfAlways(2); |
| 681 | auto& classMap = fIClassMap[skClassName]; |
| 682 | auto& tokens = classMap.fTokens; |
| 683 | for (auto& token : tokens) { |
| 684 | if (Definition::Type::kMark != token.fType || MarkType::kComment != token.fMarkType) { |
| 685 | continue; |
| 686 | } |
| 687 | fprintf(fOut, "%.*s", (int) (token.fContentEnd - token.fContentStart), |
| 688 | token.fContentStart); |
| 689 | this->lfAlways(1); |
| 690 | } |
| 691 | this->lf(2); |
| 692 | string className(skClassName.substr(2)); |
| 693 | vector<string> sortedClasses; |
| 694 | size_t maxLen = 0; |
| 695 | for (const auto& oneClass : fIClassMap) { |
| 696 | if (skClassName + "::" != oneClass.first.substr(0, skClassName.length() + 2)) { |
| 697 | continue; |
| 698 | } |
| 699 | string structName = oneClass.first.substr(skClassName.length() + 2); |
| 700 | maxLen = SkTMax(maxLen, structName.length()); |
| 701 | sortedClasses.emplace_back(structName); |
| 702 | } |
| 703 | fprintf(fOut, "#Topic Overview"); |
| 704 | this->lfAlways(2); |
| 705 | fprintf(fOut, "#Subtopic %s_Structs", className.c_str()); |
| 706 | this->lfAlways(1); |
| 707 | fprintf(fOut, "#Table"); |
| 708 | this->lfAlways(1); |
| 709 | fprintf(fOut, "#Legend"); |
| 710 | this->lfAlways(1); |
| 711 | fprintf(fOut, "# %-*s # description ##", (int) maxLen, "struct"); |
| 712 | this->lfAlways(1); |
| 713 | fprintf(fOut, "#Legend ##"); |
| 714 | this->lfAlways(1); |
| 715 | fprintf(fOut, "#Table ##"); |
| 716 | this->lfAlways(1); |
| 717 | for (auto& name : sortedClasses) { |
| 718 | fprintf(fOut, "# %-*s # ##", (int) maxLen, name.c_str()); |
| 719 | this->lfAlways(1); |
| 720 | } |
| 721 | fprintf(fOut, "#Subtopic ##"); |
| 722 | this->lfAlways(2); |
| 723 | fprintf(fOut, "#Subtopic %s_Member_Functions", className.c_str()); |
| 724 | this->lfAlways(1); |
| 725 | fprintf(fOut, "#Table"); |
| 726 | this->lfAlways(1); |
| 727 | fprintf(fOut, "#Legend"); |
| 728 | this->lfAlways(1); |
| 729 | maxLen = 0; |
| 730 | vector<string> sortedNames; |
| 731 | for (const auto& token : classMap.fTokens) { |
| 732 | if (Definition::Type::kMark != token.fType || MarkType::kMethod != token.fMarkType) { |
| 733 | continue; |
| 734 | } |
| 735 | const string& name = token.fName; |
| 736 | if (name.substr(0, 7) == "android" || string::npos != name.find("nternal_")) { |
| 737 | continue; |
| 738 | } |
| 739 | if (name[name.length() - 2] == '_' && isdigit(name[name.length() - 1])) { |
| 740 | continue; |
| 741 | } |
| 742 | size_t paren = name.find('('); |
| 743 | size_t funcLen = string::npos == paren ? name.length() : paren; |
| 744 | maxLen = SkTMax(maxLen, funcLen); |
| 745 | sortedNames.emplace_back(name); |
| 746 | } |
| 747 | std::sort(sortedNames.begin(), sortedNames.end()); |
| 748 | fprintf(fOut, "# %-*s # description ##" "\n", |
| 749 | (int) maxLen, "function"); |
| 750 | fprintf(fOut, "#Legend ##" "\n"); |
| 751 | for (auto& name : sortedNames) { |
| 752 | size_t paren = name.find('('); |
| 753 | size_t funcLen = string::npos == paren ? name.length() : paren; |
| 754 | fprintf(fOut, "# %-*s # ##" "\n", |
| 755 | (int) maxLen, name.substr(0, funcLen).c_str()); |
| 756 | } |
| 757 | fprintf(fOut, "#Table ##" "\n"); |
| 758 | fprintf(fOut, "#Subtopic ##" "\n"); |
| 759 | fprintf(fOut, "" "\n"); |
| 760 | fprintf(fOut, "#Topic ##" "\n"); |
| 761 | fprintf(fOut, "" "\n"); |
| 762 | |
| 763 | for (auto& oneClass : fIClassMap) { |
| 764 | if (skClassName + "::" != oneClass.first.substr(0, skClassName.length() + 2)) { |
| 765 | continue; |
| 766 | } |
| 767 | string innerName = oneClass.first.substr(skClassName.length() + 2); |
| 768 | fprintf(fOut, "%s", |
| 769 | "# ------------------------------------------------------------------------------"); |
| 770 | this->lfAlways(2); |
| 771 | fprintf(fOut, "#Struct %s", innerName.c_str()); |
| 772 | this->lfAlways(2); |
| 773 | for (auto& token : oneClass.second.fTokens) { |
| 774 | if (Definition::Type::kMark != token.fType || MarkType::kComment != token.fMarkType) { |
| 775 | continue; |
| 776 | } |
| 777 | fprintf(fOut, "%.*s", (int) (token.fContentEnd - token.fContentStart), |
| 778 | token.fContentStart); |
| 779 | this->lfAlways(1); |
| 780 | } |
| 781 | this->lf(2); |
| 782 | this->dumpClassTokens(oneClass.second); |
| 783 | this->lf(2); |
| 784 | fprintf(fOut, "#Struct %s ##", innerName.c_str()); |
| 785 | this->lfAlways(2); |
| 786 | } |
| 787 | this->dumpClassTokens(classMap); |
| 788 | fprintf(fOut, "#Class %s ##" "\n", |
| 789 | skClassName.c_str()); |
| 790 | fprintf(fOut, "" "\n"); |
| 791 | fprintf(fOut, "#Topic %s ##" "\n", |
| 792 | topicName.c_str()); |
| 793 | fclose(fOut); |
| 794 | } |
| 795 | |
| 796 | bool IncludeParser::findComments(const Definition& includeDef, Definition* markupDef) { |
| 797 | // add comment preceding class, if any |
| 798 | const Definition* parent = includeDef.fParent; |
| 799 | int index = includeDef.fParentIndex; |
| 800 | auto wordIter = parent->fTokens.begin(); |
| 801 | std::advance(wordIter, index); |
| 802 | SkASSERT(&*wordIter == &includeDef); |
| 803 | while (parent->fTokens.begin() != wordIter) { |
| 804 | auto testIter = std::prev(wordIter); |
| 805 | if (Definition::Type::kWord != testIter->fType |
| 806 | && Definition::Type::kKeyWord != testIter->fType |
| 807 | && (Definition::Type::kBracket != testIter->fType |
| 808 | || Bracket::kAngle != testIter->fBracket) |
| 809 | && (Definition::Type::kPunctuation != testIter->fType |
| 810 | || Punctuation::kAsterisk != testIter->fPunctuation)) { |
| 811 | break; |
| 812 | } |
| 813 | wordIter = testIter; |
| 814 | } |
| 815 | auto commentIter = wordIter; |
| 816 | while (parent->fTokens.begin() != commentIter) { |
| 817 | auto testIter = std::prev(commentIter); |
| 818 | bool isComment = Definition::Type::kBracket == testIter->fType |
| 819 | && (Bracket::kSlashSlash == testIter->fBracket |
| 820 | || Bracket::kSlashStar == testIter->fBracket); |
| 821 | if (!isComment) { |
| 822 | break; |
| 823 | } |
| 824 | commentIter = testIter; |
| 825 | } |
| 826 | while (commentIter != wordIter) { |
| 827 | if (!this->parseComment(commentIter->fFileName, commentIter->fContentStart, |
| 828 | commentIter->fContentEnd, commentIter->fLineCount, markupDef)) { |
| 829 | return false; |
| 830 | } |
| 831 | commentIter = std::next(commentIter); |
| 832 | } |
| 833 | return true; |
| 834 | } |
| 835 | |
| 836 | // caller calls reportError, so just return false here |
| 837 | bool IncludeParser::parseClass(Definition* includeDef, IsStruct isStruct) { |
| 838 | SkASSERT(includeDef->fTokens.size() > 0); |
| 839 | if (includeDef->fTokens.size() == 1) { |
| 840 | return true; // forward declaration only |
| 841 | } |
| 842 | // parse class header |
| 843 | auto iter = includeDef->fTokens.begin(); |
| 844 | if (!strncmp(iter->fStart, "SK_API", iter->fContentEnd - iter->fStart)) { |
| 845 | // todo : documentation is ignoring this for now |
| 846 | iter = std::next(iter); |
| 847 | } |
| 848 | string nameStr(iter->fStart, iter->fContentEnd - iter->fStart); |
| 849 | includeDef->fName = nameStr; |
| 850 | do { |
| 851 | if (iter == includeDef->fTokens.end()) { |
| 852 | return false; |
| 853 | } |
| 854 | if ('{' == iter->fStart[0] && Definition::Type::kPunctuation == iter->fType) { |
| 855 | break; |
| 856 | } |
| 857 | } while (static_cast<void>(iter = std::next(iter)), true); |
| 858 | if (Punctuation::kLeftBrace != iter->fPunctuation) { |
| 859 | return false; |
| 860 | } |
| 861 | IClassDefinition* markupDef = this->defineClass(*includeDef, nameStr); |
| 862 | if (!markupDef) { |
| 863 | return false; |
| 864 | } |
| 865 | markupDef->fStart = iter->fStart; |
| 866 | if (!this->findComments(*includeDef, markupDef)) { |
| 867 | return false; |
| 868 | } |
| 869 | // if (1 != includeDef->fChildren.size()) { |
| 870 | // return false; // fix me: SkCanvasClipVisitor isn't correctly parsed |
| 871 | // } |
| 872 | includeDef = includeDef->fChildren.front(); |
| 873 | iter = includeDef->fTokens.begin(); |
| 874 | // skip until public |
| 875 | int publicIndex = 0; |
| 876 | if (IsStruct::kNo == isStruct) { |
| 877 | const char* publicName = kKeyWords[(int) KeyWord::kPublic].fName; |
| 878 | size_t publicLen = strlen(publicName); |
| 879 | while (iter != includeDef->fTokens.end() |
| 880 | && (publicLen != (size_t) (iter->fContentEnd - iter->fStart) |
| 881 | || strncmp(iter->fStart, publicName, publicLen))) { |
| 882 | iter = std::next(iter); |
| 883 | ++publicIndex; |
| 884 | } |
| 885 | } |
| 886 | auto childIter = includeDef->fChildren.begin(); |
| 887 | while (childIter != includeDef->fChildren.end() && (*childIter)->fParentIndex < publicIndex) { |
| 888 | (*childIter)->fPrivate = true; |
| 889 | childIter = std::next(childIter); |
| 890 | } |
| 891 | int lastPublic = publicIndex; |
| 892 | const char* protectedName = kKeyWords[(int) KeyWord::kProtected].fName; |
| 893 | size_t protectedLen = strlen(protectedName); |
| 894 | const char* privateName = kKeyWords[(int) KeyWord::kPrivate].fName; |
| 895 | size_t privateLen = strlen(privateName); |
| 896 | while (iter != includeDef->fTokens.end() |
| 897 | && (protectedLen != (size_t) (iter->fContentEnd - iter->fStart) |
| 898 | || strncmp(iter->fStart, protectedName, protectedLen)) |
| 899 | && (privateLen != (size_t) (iter->fContentEnd - iter->fStart) |
| 900 | || strncmp(iter->fStart, privateName, privateLen))) { |
| 901 | iter = std::next(iter); |
| 902 | ++lastPublic; |
| 903 | } |
| 904 | while (childIter != includeDef->fChildren.end() && (*childIter)->fParentIndex < lastPublic) { |
| 905 | Definition* child = *childIter; |
| 906 | if (!this->parseObject(child, markupDef)) { |
| 907 | return false; |
| 908 | } |
| 909 | childIter = std::next(childIter); |
| 910 | } |
| 911 | while (childIter != includeDef->fChildren.end()) { |
| 912 | (*childIter)->fPrivate = true; |
| 913 | childIter = std::next(childIter); |
| 914 | } |
| 915 | SkASSERT(fParent->fParent); |
| 916 | fParent = fParent->fParent; |
| 917 | return true; |
| 918 | } |
| 919 | |
| 920 | bool IncludeParser::parseComment(const string& filename, const char* start, const char* end, |
| 921 | int lineCount, Definition* markupDef) { |
| 922 | TextParser parser(filename, start, end, lineCount); |
| 923 | // parse doxygen if present |
| 924 | if (parser.startsWith("**")) { |
| 925 | parser.next(); |
| 926 | parser.next(); |
| 927 | parser.skipWhiteSpace(); |
| 928 | if ('\\' == parser.peek()) { |
| 929 | parser.next(); |
| 930 | if (!parser.skipWord(kKeyWords[(int) markupDef->fKeyWord].fName)) { |
| 931 | return reportError<bool>("missing object type"); |
| 932 | } |
| 933 | if (!parser.skipWord(markupDef->fName.c_str())) { |
| 934 | return reportError<bool>("missing object name"); |
| 935 | } |
| 936 | |
| 937 | } |
| 938 | } |
| 939 | // remove leading '*' if present |
| 940 | Definition* parent = markupDef->fTokens.size() ? &markupDef->fTokens.back() : markupDef; |
| 941 | while (!parser.eof() && parser.skipWhiteSpace()) { |
| 942 | while ('*' == parser.peek()) { |
| 943 | parser.next(); |
| 944 | if (parser.eof()) { |
| 945 | break; |
| 946 | } |
| 947 | parser.skipWhiteSpace(); |
| 948 | } |
| 949 | if (parser.eof()) { |
| 950 | break; |
| 951 | } |
| 952 | const char* lineEnd = parser.trimmedLineEnd(); |
| 953 | markupDef->fTokens.emplace_back(MarkType::kComment, parser.fChar, lineEnd, |
| 954 | parser.fLineCount, parent); |
| 955 | parser.skipToEndBracket('\n'); |
| 956 | } |
| 957 | return true; |
| 958 | } |
| 959 | |
| 960 | bool IncludeParser::parseDefine() { |
| 961 | |
| 962 | return true; |
| 963 | } |
| 964 | |
| 965 | bool IncludeParser::parseEnum(Definition* child, Definition* markupDef) { |
| 966 | string nameStr; |
| 967 | if (child->fTokens.size() > 0) { |
| 968 | auto token = child->fTokens.begin(); |
| 969 | if (Definition::Type::kKeyWord == token->fType && KeyWord::kClass == token->fKeyWord) { |
| 970 | token = token->fTokens.begin(); |
| 971 | } |
| 972 | if (Definition::Type::kWord == token->fType) { |
| 973 | nameStr += string(token->fStart, token->fContentEnd - token->fStart); |
| 974 | } |
| 975 | } |
| 976 | markupDef->fTokens.emplace_back(MarkType::kEnum, child->fContentStart, child->fContentEnd, |
| 977 | child->fLineCount, markupDef); |
| 978 | Definition* markupChild = &markupDef->fTokens.back(); |
| 979 | SkASSERT(KeyWord::kNone == markupChild->fKeyWord); |
| 980 | markupChild->fKeyWord = KeyWord::kEnum; |
| 981 | TextParser enumName(child); |
| 982 | enumName.skipExact("enum "); |
| 983 | const char* nameStart = enumName.fChar; |
| 984 | enumName.skipToSpace(); |
| 985 | markupChild->fName = markupDef->fName + "::" + |
| 986 | string(nameStart, (size_t) (enumName.fChar - nameStart)); |
| 987 | if (!this->findComments(*child, markupChild)) { |
| 988 | return false; |
| 989 | } |
| 990 | TextParser parser(child); |
| 991 | parser.skipToEndBracket('{'); |
| 992 | const char* dataEnd; |
| 993 | do { |
| 994 | parser.next(); |
| 995 | parser.skipWhiteSpace(); |
| 996 | if ('}' == parser.peek()) { |
| 997 | break; |
| 998 | } |
| 999 | Definition* comment = nullptr; |
| 1000 | // note that comment, if any, can be before or after (on the same line, though) as member |
| 1001 | if ('#' == parser.peek()) { |
| 1002 | // fixme: handle preprecessor, but just skip it for now |
| 1003 | parser.skipToLineStart(); |
| 1004 | } |
| 1005 | while (parser.startsWith("/*") || parser.startsWith("//")) { |
| 1006 | parser.next(); |
| 1007 | const char* start = parser.fChar; |
| 1008 | const char* end; |
| 1009 | if ('*' == parser.peek()) { |
| 1010 | end = parser.strnstr("*/", parser.fEnd); |
| 1011 | parser.fChar = end; |
| 1012 | parser.next(); |
| 1013 | parser.next(); |
| 1014 | } else { |
| 1015 | end = parser.trimmedLineEnd(); |
| 1016 | parser.skipToLineStart(); |
| 1017 | } |
| 1018 | markupChild->fTokens.emplace_back(MarkType::kComment, start, end, parser.fLineCount, |
| 1019 | markupChild); |
| 1020 | comment = &markupChild->fTokens.back(); |
| 1021 | comment->fTerminator = end; |
| 1022 | if (!this->parseComment(parser.fFileName, start, end, parser.fLineCount, comment)) { |
| 1023 | return false; |
| 1024 | } |
| 1025 | parser.skipWhiteSpace(); |
| 1026 | } |
| 1027 | parser.skipWhiteSpace(); |
| 1028 | const char* memberStart = parser.fChar; |
| 1029 | if ('}' == memberStart[0]) { |
| 1030 | break; |
| 1031 | } |
| 1032 | parser.skipToNonAlphaNum(); |
| 1033 | string memberName(memberStart, parser.fChar); |
| 1034 | parser.skipWhiteSpace(); |
| 1035 | const char* dataStart = parser.fChar; |
| 1036 | SkASSERT('=' == dataStart[0] || ',' == dataStart[0] || '}' == dataStart[0] |
| 1037 | || '/' == dataStart[0]); |
| 1038 | dataEnd = parser.anyOf(",}"); |
| 1039 | markupChild->fTokens.emplace_back(MarkType::kMember, dataStart, dataEnd, parser.fLineCount, |
| 1040 | markupChild); |
| 1041 | Definition* member = &markupChild->fTokens.back(); |
| 1042 | member->fName = memberName; |
| 1043 | if (comment) { |
| 1044 | member->fChildren.push_back(comment); |
| 1045 | } |
| 1046 | markupChild->fChildren.push_back(member); |
| 1047 | parser.skipToEndBracket(dataEnd[0]); |
| 1048 | } while (',' == dataEnd[0]); |
| 1049 | for (size_t index = 1; index < child->fChildren.size(); ++index) { |
| 1050 | const Definition* follower = child->fChildren[index]; |
| 1051 | if (Definition::Type::kKeyWord == follower->fType) { |
| 1052 | markupChild->fTokens.emplace_back(MarkType::kMember, follower->fContentStart, |
| 1053 | follower->fContentEnd, follower->fLineCount, markupChild); |
| 1054 | Definition* member = &markupChild->fTokens.back(); |
| 1055 | member->fName = follower->fName; |
| 1056 | markupChild->fChildren.push_back(member); |
| 1057 | } |
| 1058 | } |
| 1059 | IClassDefinition& classDef = fIClassMap[markupDef->fName]; |
| 1060 | SkASSERT(classDef.fStart); |
| 1061 | string uniqueName = this->uniqueName(classDef.fEnums, nameStr); |
| 1062 | markupChild->fName = uniqueName; |
| 1063 | classDef.fEnums[uniqueName] = markupChild; |
| 1064 | return true; |
| 1065 | } |
| 1066 | |
| 1067 | bool IncludeParser::parseInclude(const string& name) { |
| 1068 | fParent = &fIncludeMap[name]; |
| 1069 | fParent->fName = name; |
| 1070 | fParent->fFileName = fFileName; |
| 1071 | fParent->fType = Definition::Type::kFileType; |
| 1072 | fParent->fContentStart = fChar; |
| 1073 | fParent->fContentEnd = fEnd; |
| 1074 | // parse include file into tree |
| 1075 | while (fChar < fEnd) { |
| 1076 | if (!this->parseChar()) { |
| 1077 | return false; |
| 1078 | } |
| 1079 | } |
| 1080 | // parse tree and add named objects to maps |
| 1081 | fParent = &fIncludeMap[name]; |
| 1082 | if (!this->parseObjects(fParent, nullptr)) { |
| 1083 | return false; |
| 1084 | } |
| 1085 | return true; |
| 1086 | } |
| 1087 | |
| 1088 | bool IncludeParser::parseMember(Definition* child, Definition* markupDef) { |
| 1089 | const char* typeStart = child->fChildren[0]->fContentStart; |
| 1090 | markupDef->fTokens.emplace_back(MarkType::kMember, typeStart, child->fContentStart, |
| 1091 | child->fLineCount, markupDef); |
| 1092 | Definition* markupChild = &markupDef->fTokens.back(); |
| 1093 | TextParser nameParser(child); |
| 1094 | nameParser.skipToNonAlphaNum(); |
| 1095 | string nameStr = string(child->fContentStart, nameParser.fChar - child->fContentStart); |
| 1096 | IClassDefinition& classDef = fIClassMap[markupDef->fName]; |
| 1097 | string uniqueName = this->uniqueName(classDef.fMethods, nameStr); |
| 1098 | markupChild->fName = uniqueName; |
| 1099 | classDef.fMembers[uniqueName] = markupChild; |
| 1100 | if (child->fParentIndex >= 2) { |
| 1101 | auto comment = child->fParent->fTokens.begin(); |
| 1102 | std::advance(comment, child->fParentIndex - 2); |
| 1103 | if (Definition::Type::kBracket == comment->fType |
| 1104 | && (Bracket::kSlashStar == comment->fBracket |
| 1105 | || Bracket::kSlashSlash == comment->fBracket)) { |
| 1106 | TextParser parser(&*comment); |
| 1107 | do { |
| 1108 | parser.skipToAlpha(); |
| 1109 | if (parser.eof()) { |
| 1110 | break; |
| 1111 | } |
| 1112 | const char* start = parser.fChar; |
| 1113 | const char* end = parser.trimmedBracketEnd('\n', OneLine::kYes); |
| 1114 | if (Bracket::kSlashStar == comment->fBracket) { |
| 1115 | const char* commentEnd = parser.strnstr("*/", end); |
| 1116 | if (commentEnd) { |
| 1117 | end = commentEnd; |
| 1118 | } |
| 1119 | } |
| 1120 | markupDef->fTokens.emplace_back(MarkType::kComment, start, end, child->fLineCount, |
| 1121 | markupDef); |
| 1122 | Definition* commentChild = &markupDef->fTokens.back(); |
| 1123 | markupChild->fChildren.emplace_back(commentChild); |
| 1124 | parser.skipTo(end); |
| 1125 | } while (!parser.eof()); |
| 1126 | } |
| 1127 | } |
| 1128 | return true; |
| 1129 | } |
| 1130 | |
| 1131 | bool IncludeParser::parseMethod(Definition* child, Definition* markupDef) { |
| 1132 | auto tokenIter = child->fParent->fTokens.begin(); |
| 1133 | std::advance(tokenIter, child->fParentIndex); |
| 1134 | tokenIter = std::prev(tokenIter); |
| 1135 | string nameStr(tokenIter->fStart, tokenIter->fContentEnd - tokenIter->fStart); |
| 1136 | while (tokenIter != child->fParent->fTokens.begin()) { |
| 1137 | auto testIter = std::prev(tokenIter); |
| 1138 | switch (testIter->fType) { |
| 1139 | case Definition::Type::kWord: |
| 1140 | goto keepGoing; |
| 1141 | case Definition::Type::kKeyWord: { |
| 1142 | KeyProperty keyProperty = kKeyWords[(int) testIter->fKeyWord].fProperty; |
| 1143 | if (KeyProperty::kNumber == keyProperty || KeyProperty::kModifier == keyProperty) { |
| 1144 | goto keepGoing; |
| 1145 | } |
| 1146 | } break; |
| 1147 | case Definition::Type::kBracket: |
| 1148 | if (Bracket::kAngle == testIter->fBracket) { |
| 1149 | goto keepGoing; |
| 1150 | } |
| 1151 | break; |
| 1152 | case Definition::Type::kPunctuation: |
| 1153 | if (Punctuation::kSemicolon == testIter->fPunctuation |
| 1154 | || Punctuation::kLeftBrace == testIter->fPunctuation |
| 1155 | || Punctuation::kColon == testIter->fPunctuation) { |
| 1156 | break; |
| 1157 | } |
| 1158 | keepGoing: |
| 1159 | tokenIter = testIter; |
| 1160 | continue; |
| 1161 | default: |
| 1162 | break; |
| 1163 | } |
| 1164 | break; |
| 1165 | } |
| 1166 | tokenIter->fName = nameStr; |
| 1167 | tokenIter->fMarkType = MarkType::kMethod; |
| 1168 | auto testIter = child->fParent->fTokens.begin(); |
| 1169 | SkASSERT(child->fParentIndex > 0); |
| 1170 | std::advance(testIter, child->fParentIndex - 1); |
| 1171 | const char* start = tokenIter->fContentStart; |
| 1172 | const char* end = tokenIter->fContentEnd; |
| 1173 | const char kDebugCodeStr[] = "SkDEBUGCODE"; |
| 1174 | const size_t kDebugCodeLen = sizeof(kDebugCodeStr) - 1; |
| 1175 | if (end - start == kDebugCodeLen && !strncmp(start, kDebugCodeStr, kDebugCodeLen)) { |
| 1176 | std::advance(testIter, 1); |
| 1177 | start = testIter->fContentStart + 1; |
| 1178 | end = testIter->fContentEnd - 1; |
| 1179 | } else { |
| 1180 | end = testIter->fContentEnd; |
| 1181 | while (testIter != child->fParent->fTokens.end()) { |
| 1182 | testIter = std::next(testIter); |
| 1183 | switch (testIter->fType) { |
| 1184 | case Definition::Type::kPunctuation: |
| 1185 | SkASSERT(Punctuation::kSemicolon == testIter->fPunctuation |
| 1186 | || Punctuation::kLeftBrace == testIter->fPunctuation |
| 1187 | || Punctuation::kColon == testIter->fPunctuation); |
| 1188 | end = testIter->fStart; |
| 1189 | break; |
| 1190 | case Definition::Type::kKeyWord: { |
| 1191 | KeyProperty keyProperty = kKeyWords[(int) testIter->fKeyWord].fProperty; |
| 1192 | if (KeyProperty::kNumber == keyProperty || KeyProperty::kModifier == keyProperty) { |
| 1193 | continue; |
| 1194 | } |
| 1195 | } break; |
| 1196 | default: |
| 1197 | continue; |
| 1198 | } |
| 1199 | break; |
| 1200 | } |
| 1201 | } |
| 1202 | markupDef->fTokens.emplace_back(MarkType::kMethod, start, end, tokenIter->fLineCount, |
| 1203 | markupDef); |
| 1204 | Definition* markupChild = &markupDef->fTokens.back(); |
| 1205 | // do find instead -- I wonder if there is a way to prevent this in c++ |
| 1206 | IClassDefinition& classDef = fIClassMap[markupDef->fName]; |
| 1207 | SkASSERT(classDef.fStart); |
| 1208 | string uniqueName = this->uniqueName(classDef.fMethods, nameStr); |
| 1209 | markupChild->fName = uniqueName; |
| 1210 | if (!this->findComments(*child, markupChild)) { |
| 1211 | return false; |
| 1212 | } |
| 1213 | classDef.fMethods[uniqueName] = markupChild; |
| 1214 | return true; |
| 1215 | } |
| 1216 | |
| 1217 | void IncludeParser::keywordEnd() { |
| 1218 | fprintf(fOut, "##"); |
| 1219 | this->lfAlways(1); |
| 1220 | } |
| 1221 | |
| 1222 | void IncludeParser::keywordStart(const char* keyword) { |
| 1223 | this->lf(1); |
| 1224 | fprintf(fOut, "#%s ", keyword); |
| 1225 | } |
| 1226 | |
| 1227 | bool IncludeParser::parseObjects(Definition* parent, Definition* markupDef) { |
| 1228 | for (auto& child : parent->fChildren) { |
| 1229 | if (!this->parseObject(child, markupDef)) { |
| 1230 | return false; |
| 1231 | } |
| 1232 | } |
| 1233 | return true; |
| 1234 | } |
| 1235 | |
| 1236 | bool IncludeParser::parseObject(Definition* child, Definition* markupDef) { |
| 1237 | // set up for error reporting |
| 1238 | fLine = fChar = child->fStart; |
| 1239 | fEnd = child->fContentEnd; |
| 1240 | // todo: put original line number in child as well |
| 1241 | switch (child->fType) { |
| 1242 | case Definition::Type::kKeyWord: |
| 1243 | switch (child->fKeyWord) { |
| 1244 | case KeyWord::kClass: |
| 1245 | if (!this->parseClass(child, IsStruct::kNo)) { |
| 1246 | return this->reportError<bool>("failed to parse class"); |
| 1247 | } |
| 1248 | break; |
| 1249 | case KeyWord::kEnum: |
| 1250 | if (!this->parseEnum(child, markupDef)) { |
| 1251 | return this->reportError<bool>("failed to parse enum"); |
| 1252 | } |
| 1253 | break; |
| 1254 | case KeyWord::kStruct: |
| 1255 | if (!this->parseClass(child, IsStruct::kYes)) { |
| 1256 | return this->reportError<bool>("failed to parse struct"); |
| 1257 | } |
| 1258 | break; |
| 1259 | case KeyWord::kTemplate: |
| 1260 | if (!this->parseTemplate()) { |
| 1261 | return this->reportError<bool>("failed to parse template"); |
| 1262 | } |
| 1263 | break; |
| 1264 | case KeyWord::kTypedef: |
| 1265 | if (!this->parseTypedef()) { |
| 1266 | return this->reportError<bool>("failed to parse typedef"); |
| 1267 | } |
| 1268 | break; |
| 1269 | case KeyWord::kUnion: |
| 1270 | if (!this->parseUnion()) { |
| 1271 | return this->reportError<bool>("failed to parse union"); |
| 1272 | } |
| 1273 | break; |
| 1274 | default: |
| 1275 | return this->reportError<bool>("unhandled keyword"); |
| 1276 | } |
| 1277 | break; |
| 1278 | case Definition::Type::kBracket: |
| 1279 | switch (child->fBracket) { |
| 1280 | case Bracket::kParen: |
| 1281 | if (!this->parseMethod(child, markupDef)) { |
| 1282 | return this->reportError<bool>("failed to parse method"); |
| 1283 | } |
| 1284 | break; |
| 1285 | case Bracket::kSlashSlash: |
| 1286 | case Bracket::kSlashStar: |
| 1287 | // comments are picked up by parsing objects first |
| 1288 | break; |
| 1289 | case Bracket::kPound: |
| 1290 | // special-case the #xxx xxx_DEFINED entries |
| 1291 | switch (child->fKeyWord) { |
| 1292 | case KeyWord::kIfndef: |
| 1293 | case KeyWord::kIfdef: |
| 1294 | if (child->boilerplateIfDef(fParent)) { |
| 1295 | if (!this->parseObjects(child, markupDef)) { |
| 1296 | return false; |
| 1297 | } |
| 1298 | break; |
| 1299 | } |
| 1300 | goto preproError; |
| 1301 | case KeyWord::kDefine: |
| 1302 | if (child->boilerplateDef(fParent)) { |
| 1303 | break; |
| 1304 | } |
| 1305 | goto preproError; |
| 1306 | case KeyWord::kEndif: |
| 1307 | if (child->boilerplateEndIf()) { |
| 1308 | break; |
| 1309 | } |
| 1310 | case KeyWord::kInclude: |
| 1311 | // ignored for now |
| 1312 | break; |
| 1313 | case KeyWord::kElse: |
| 1314 | case KeyWord::kElif: |
| 1315 | // todo: handle these |
| 1316 | break; |
| 1317 | default: |
| 1318 | preproError: |
| 1319 | return this->reportError<bool>("unhandled preprocessor"); |
| 1320 | } |
| 1321 | break; |
| 1322 | case Bracket::kAngle: |
| 1323 | // pick up templated function pieces when method is found |
| 1324 | break; |
| 1325 | default: |
| 1326 | return this->reportError<bool>("unhandled bracket"); |
| 1327 | } |
| 1328 | break; |
| 1329 | case Definition::Type::kWord: |
| 1330 | if (MarkType::kMember != child->fMarkType) { |
| 1331 | return this->reportError<bool>("unhandled word type"); |
| 1332 | } |
| 1333 | if (!this->parseMember(child, markupDef)) { |
| 1334 | return this->reportError<bool>("unparsable member"); |
| 1335 | } |
| 1336 | break; |
| 1337 | default: |
| 1338 | return this->reportError<bool>("unhandled type"); |
| 1339 | break; |
| 1340 | } |
| 1341 | return true; |
| 1342 | } |
| 1343 | |
| 1344 | bool IncludeParser::parseTemplate() { |
| 1345 | |
| 1346 | return true; |
| 1347 | } |
| 1348 | |
| 1349 | bool IncludeParser::parseTypedef() { |
| 1350 | |
| 1351 | return true; |
| 1352 | } |
| 1353 | |
| 1354 | bool IncludeParser::parseUnion() { |
| 1355 | |
| 1356 | return true; |
| 1357 | } |
| 1358 | |
| 1359 | bool IncludeParser::parseChar() { |
| 1360 | char test = *fChar; |
| 1361 | if ('\\' == fPrev) { |
| 1362 | if ('\n' == test) { |
| 1363 | ++fLineCount; |
| 1364 | fLine = fChar + 1; |
| 1365 | } |
| 1366 | goto done; |
| 1367 | } |
| 1368 | switch (test) { |
| 1369 | case '\n': |
| 1370 | ++fLineCount; |
| 1371 | fLine = fChar + 1; |
| 1372 | if (fInChar) { |
| 1373 | return reportError<bool>("malformed char"); |
| 1374 | } |
| 1375 | if (fInString) { |
| 1376 | return reportError<bool>("malformed string"); |
| 1377 | } |
| 1378 | if (!this->checkForWord()) { |
| 1379 | return false; |
| 1380 | } |
| 1381 | if (Bracket::kPound == this->topBracket()) { |
| 1382 | KeyWord keyWord = fParent->fKeyWord; |
| 1383 | if (KeyWord::kNone == keyWord) { |
| 1384 | return this->reportError<bool>("unhandled preprocessor directive"); |
| 1385 | } |
| 1386 | if (KeyWord::kInclude == keyWord || KeyWord::kDefine == keyWord) { |
| 1387 | this->popBracket(); |
| 1388 | } |
| 1389 | } else if (Bracket::kSlashSlash == this->topBracket()) { |
| 1390 | this->popBracket(); |
| 1391 | } |
| 1392 | break; |
| 1393 | case '*': |
| 1394 | if (!fInCharCommentString && '/' == fPrev) { |
| 1395 | this->pushBracket(Bracket::kSlashStar); |
| 1396 | } |
| 1397 | if (!this->checkForWord()) { |
| 1398 | return false; |
| 1399 | } |
| 1400 | if (!fInCharCommentString) { |
| 1401 | this->addPunctuation(Punctuation::kAsterisk); |
| 1402 | } |
| 1403 | break; |
| 1404 | case '/': |
| 1405 | if ('*' == fPrev) { |
| 1406 | if (!fInCharCommentString) { |
| 1407 | return reportError<bool>("malformed closing comment"); |
| 1408 | } |
| 1409 | if (Bracket::kSlashStar == this->topBracket()) { |
| 1410 | this->popBracket(); |
| 1411 | } |
| 1412 | break; |
| 1413 | } |
| 1414 | if (!fInCharCommentString && '/' == fPrev) { |
| 1415 | this->pushBracket(Bracket::kSlashSlash); |
| 1416 | break; |
| 1417 | } |
| 1418 | if (!this->checkForWord()) { |
| 1419 | return false; |
| 1420 | } |
| 1421 | break; |
| 1422 | case '\'': |
| 1423 | if (Bracket::kChar == this->topBracket()) { |
| 1424 | this->popBracket(); |
| 1425 | } else if (!fInComment && !fInString) { |
| 1426 | if (fIncludeWord) { |
| 1427 | return this->reportError<bool>("word then single-quote"); |
| 1428 | } |
| 1429 | this->pushBracket(Bracket::kChar); |
| 1430 | } |
| 1431 | break; |
| 1432 | case '\"': |
| 1433 | if (Bracket::kString == this->topBracket()) { |
| 1434 | this->popBracket(); |
| 1435 | } else if (!fInComment && !fInChar) { |
| 1436 | if (fIncludeWord) { |
| 1437 | return this->reportError<bool>("word then double-quote"); |
| 1438 | } |
| 1439 | this->pushBracket(Bracket::kString); |
| 1440 | } |
| 1441 | break; |
| 1442 | case ':': |
| 1443 | case '(': |
| 1444 | case '[': |
| 1445 | case '{': { |
| 1446 | if (fInCharCommentString) { |
| 1447 | break; |
| 1448 | } |
| 1449 | if (':' == test && (fInBrace || ':' == fChar[-1] || ':' == fChar[1])) { |
| 1450 | break; |
| 1451 | } |
| 1452 | if (!fInBrace) { |
| 1453 | if (!this->checkForWord()) { |
| 1454 | return false; |
| 1455 | } |
| 1456 | if (':' == test && !fInFunction) { |
| 1457 | break; |
| 1458 | } |
| 1459 | if ('{' == test) { |
| 1460 | this->addPunctuation(Punctuation::kLeftBrace); |
| 1461 | } else if (':' == test) { |
| 1462 | this->addPunctuation(Punctuation::kColon); |
| 1463 | } |
| 1464 | } |
| 1465 | if (fInBrace && '{' == test && Definition::Type::kBracket == fInBrace->fType |
| 1466 | && Bracket::kColon == fInBrace->fBracket) { |
| 1467 | Definition* braceParent = fParent->fParent; |
| 1468 | braceParent->fChildren.pop_back(); |
| 1469 | braceParent->fTokens.pop_back(); |
| 1470 | fParent = braceParent; |
| 1471 | fInBrace = nullptr; |
| 1472 | } |
| 1473 | this->pushBracket( |
| 1474 | '(' == test ? Bracket::kParen : |
| 1475 | '[' == test ? Bracket::kSquare : |
| 1476 | '{' == test ? Bracket::kBrace : |
| 1477 | Bracket::kColon); |
| 1478 | if (!fInBrace |
| 1479 | && ('{' == test || (':' == test && ' ' >= fChar[1])) |
| 1480 | && fInFunction) { |
| 1481 | fInBrace = fParent; |
| 1482 | } |
| 1483 | } break; |
| 1484 | case '<': |
| 1485 | if (fInCharCommentString || fInBrace) { |
| 1486 | break; |
| 1487 | } |
| 1488 | if (!this->checkForWord()) { |
| 1489 | return false; |
| 1490 | } |
| 1491 | if (fInEnum) { |
| 1492 | break; |
| 1493 | } |
| 1494 | this->pushBracket(Bracket::kAngle); |
| 1495 | break; |
| 1496 | case ')': |
| 1497 | case ']': |
| 1498 | case '}': { |
| 1499 | if (fInCharCommentString) { |
| 1500 | break; |
| 1501 | } |
| 1502 | if (!fInBrace) { |
| 1503 | if (!this->checkForWord()) { |
| 1504 | return false; |
| 1505 | } |
| 1506 | } |
| 1507 | bool popBraceParent = fInBrace == fParent; |
| 1508 | if ((')' == test ? Bracket::kParen : |
| 1509 | ']' == test ? Bracket::kSquare : Bracket::kBrace) == this->topBracket()) { |
| 1510 | this->popBracket(); |
| 1511 | if (!fInFunction) { |
| 1512 | bool deprecatedMacro = false; |
| 1513 | if (')' == test) { |
| 1514 | auto iter = fParent->fTokens.end(); |
| 1515 | bool lookForWord = false; |
| 1516 | while (fParent->fTokens.begin() != iter) { |
| 1517 | --iter; |
| 1518 | if (lookForWord) { |
| 1519 | if (Definition::Type::kWord != iter->fType) { |
| 1520 | break; |
| 1521 | } |
| 1522 | string word(iter->fContentStart, iter->length()); |
| 1523 | if ("SK_ATTR_EXTERNALLY_DEPRECATED" == word) { |
| 1524 | deprecatedMacro = true; |
| 1525 | // remove macro paren (would confuse method parsing later) |
| 1526 | fParent->fTokens.pop_back(); |
| 1527 | fParent->fChildren.pop_back(); |
| 1528 | } |
| 1529 | break; |
| 1530 | } |
| 1531 | if (Definition::Type::kBracket != iter->fType) { |
| 1532 | break; |
| 1533 | } |
| 1534 | if (Bracket::kParen != iter->fBracket) { |
| 1535 | break; |
| 1536 | } |
| 1537 | lookForWord = true; |
| 1538 | } |
| 1539 | } |
| 1540 | fInFunction = ')' == test && !deprecatedMacro; |
| 1541 | } else { |
| 1542 | fInFunction = '}' != test; |
| 1543 | } |
| 1544 | } else { |
| 1545 | return reportError<bool>("malformed close bracket"); |
| 1546 | } |
| 1547 | if (popBraceParent) { |
| 1548 | Definition* braceParent = fInBrace->fParent; |
| 1549 | braceParent->fChildren.pop_back(); |
| 1550 | braceParent->fTokens.pop_back(); |
| 1551 | fInBrace = nullptr; |
| 1552 | } |
| 1553 | } break; |
| 1554 | case '>': |
| 1555 | if (fInCharCommentString || fInBrace) { |
| 1556 | break; |
| 1557 | } |
| 1558 | if (!this->checkForWord()) { |
| 1559 | return false; |
| 1560 | } |
| 1561 | if (fInEnum) { |
| 1562 | break; |
| 1563 | } |
| 1564 | if (Bracket::kAngle == this->topBracket()) { |
| 1565 | this->popBracket(); |
| 1566 | } else { |
| 1567 | return reportError<bool>("malformed close angle bracket"); |
| 1568 | } |
| 1569 | break; |
| 1570 | case '#': { |
| 1571 | if (fInCharCommentString || fInBrace) { |
| 1572 | break; |
| 1573 | } |
| 1574 | SkASSERT(!fIncludeWord); // don't expect this, curious if it is triggered |
| 1575 | this->pushBracket(Bracket::kPound); |
| 1576 | break; |
| 1577 | } |
| 1578 | case '&': |
| 1579 | case ',': |
| 1580 | case ' ': |
| 1581 | if (fInCharCommentString || fInBrace) { |
| 1582 | break; |
| 1583 | } |
| 1584 | if (!this->checkForWord()) { |
| 1585 | return false; |
| 1586 | } |
| 1587 | break; |
| 1588 | case ';': |
| 1589 | if (fInCharCommentString || fInBrace) { |
| 1590 | break; |
| 1591 | } |
| 1592 | if (!this->checkForWord()) { |
| 1593 | return false; |
| 1594 | } |
| 1595 | if (Definition::Type::kKeyWord == fParent->fType |
| 1596 | && KeyProperty::kObject == (kKeyWords[(int) fParent->fKeyWord].fProperty)) { |
| 1597 | if (KeyWord::kEnum == fParent->fKeyWord) { |
| 1598 | fInEnum = false; |
| 1599 | } |
| 1600 | this->popObject(); |
| 1601 | } else if (Definition::Type::kBracket == fParent->fType |
| 1602 | && fParent->fParent && Definition::Type::kKeyWord == fParent->fParent->fType |
| 1603 | && KeyWord::kStruct == fParent->fParent->fKeyWord) { |
| 1604 | list<Definition>::iterator baseIter = fParent->fTokens.end(); |
| 1605 | list<Definition>::iterator namedIter = fParent->fTokens.end(); |
| 1606 | for (auto tokenIter = fParent->fTokens.end(); |
| 1607 | fParent->fTokens.begin() != tokenIter--; ) { |
| 1608 | if (tokenIter->fLineCount == fLineCount) { |
| 1609 | if ('f' == tokenIter->fStart[0] && isupper(tokenIter->fStart[1])) { |
| 1610 | if (namedIter != fParent->fTokens.end()) { |
| 1611 | return reportError<bool>("found two named member tokens"); |
| 1612 | } |
| 1613 | namedIter = tokenIter; |
| 1614 | } |
| 1615 | baseIter = tokenIter; |
| 1616 | } else { |
| 1617 | break; |
| 1618 | } |
| 1619 | } |
| 1620 | // FIXME: if a member definition spans multiple lines, this won't work |
| 1621 | if (namedIter != fParent->fTokens.end()) { |
| 1622 | if (baseIter == namedIter) { |
| 1623 | return this->reportError<bool>("expected type before named token"); |
| 1624 | } |
| 1625 | Definition* member = &*namedIter; |
| 1626 | member->fMarkType = MarkType::kMember; |
| 1627 | fParent->fChildren.push_back(member); |
| 1628 | for (auto nameType = baseIter; nameType != namedIter; ++nameType) { |
| 1629 | member->fChildren.push_back(&*nameType); |
| 1630 | } |
| 1631 | |
| 1632 | } |
| 1633 | } else if (fParent->fChildren.size() > 0) { |
| 1634 | auto lastIter = fParent->fChildren.end(); |
| 1635 | Definition* priorEnum; |
| 1636 | while (fParent->fChildren.begin() != lastIter) { |
| 1637 | std::advance(lastIter, -1); |
| 1638 | priorEnum = *lastIter; |
| 1639 | if (Definition::Type::kBracket != priorEnum->fType || |
| 1640 | (Bracket::kSlashSlash != priorEnum->fBracket |
| 1641 | && Bracket::kSlashStar != priorEnum->fBracket)) { |
| 1642 | break; |
| 1643 | } |
| 1644 | } |
| 1645 | if (Definition::Type::kKeyWord == priorEnum->fType |
| 1646 | && KeyWord::kEnum == priorEnum->fKeyWord) { |
| 1647 | auto tokenWalker = fParent->fTokens.begin(); |
| 1648 | std::advance(tokenWalker, priorEnum->fParentIndex); |
| 1649 | SkASSERT(KeyWord::kEnum == tokenWalker->fKeyWord); |
| 1650 | while (tokenWalker != fParent->fTokens.end()) { |
| 1651 | std::advance(tokenWalker, 1); |
| 1652 | if (Punctuation::kSemicolon == tokenWalker->fPunctuation) { |
| 1653 | break; |
| 1654 | } |
| 1655 | } |
| 1656 | while (tokenWalker != fParent->fTokens.end()) { |
| 1657 | std::advance(tokenWalker, 1); |
| 1658 | const Definition* test = &*tokenWalker; |
| 1659 | if (Definition::Type::kBracket != test->fType || |
| 1660 | (Bracket::kSlashSlash != test->fBracket |
| 1661 | && Bracket::kSlashStar != test->fBracket)) { |
| 1662 | break; |
| 1663 | } |
| 1664 | } |
| 1665 | Definition* start = &*tokenWalker; |
| 1666 | bool foundExpected = true; |
| 1667 | for (KeyWord expected : {KeyWord::kStatic, KeyWord::kConstExpr, KeyWord::kInt}){ |
| 1668 | const Definition* test = &*tokenWalker; |
| 1669 | if (expected != test->fKeyWord) { |
| 1670 | foundExpected = false; |
| 1671 | break; |
| 1672 | } |
| 1673 | if (tokenWalker == fParent->fTokens.end()) { |
| 1674 | break; |
| 1675 | } |
| 1676 | std::advance(tokenWalker, 1); |
| 1677 | } |
| 1678 | if (foundExpected && tokenWalker != fParent->fTokens.end()) { |
| 1679 | const char* nameStart = tokenWalker->fStart; |
| 1680 | std::advance(tokenWalker, 1); |
| 1681 | if (tokenWalker != fParent->fTokens.end()) { |
| 1682 | TextParser tp(fFileName, nameStart, tokenWalker->fStart, fLineCount); |
| 1683 | tp.skipToNonAlphaNum(); |
| 1684 | start->fName = string(nameStart, tp.fChar - nameStart); |
| 1685 | start->fContentEnd = fChar; |
| 1686 | priorEnum->fChildren.emplace_back(start); |
| 1687 | } |
| 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | this->addPunctuation(Punctuation::kSemicolon); |
| 1692 | fInFunction = false; |
| 1693 | break; |
| 1694 | case '~': |
| 1695 | if (fInEnum) { |
| 1696 | break; |
| 1697 | } |
| 1698 | case '0': case '1': case '2': case '3': case '4': |
| 1699 | case '5': case '6': case '7': case '8': case '9': |
| 1700 | // TODO: don't want to parse numbers, but do need to track for enum defs |
| 1701 | // break; |
| 1702 | case 'A': case 'B': case 'C': case 'D': case 'E': |
| 1703 | case 'F': case 'G': case 'H': case 'I': case 'J': |
| 1704 | case 'K': case 'L': case 'M': case 'N': case 'O': |
| 1705 | case 'P': case 'Q': case 'R': case 'S': case 'T': |
| 1706 | case 'U': case 'V': case 'W': case 'X': case 'Y': |
| 1707 | case 'Z': case '_': |
| 1708 | case 'a': case 'b': case 'c': case 'd': case 'e': |
| 1709 | case 'f': case 'g': case 'h': case 'i': case 'j': |
| 1710 | case 'k': case 'l': case 'm': case 'n': case 'o': |
| 1711 | case 'p': case 'q': case 'r': case 's': case 't': |
| 1712 | case 'u': case 'v': case 'w': case 'x': case 'y': |
| 1713 | case 'z': |
| 1714 | if (fInCharCommentString || fInBrace) { |
| 1715 | break; |
| 1716 | } |
| 1717 | if (!fIncludeWord) { |
| 1718 | fIncludeWord = fChar; |
| 1719 | } |
| 1720 | break; |
| 1721 | } |
| 1722 | done: |
| 1723 | fPrev = test; |
| 1724 | ++fChar; |
| 1725 | return true; |
| 1726 | } |
| 1727 | |
| 1728 | void IncludeParser::validate() const { |
| 1729 | for (int index = 0; index <= (int) Last_MarkType; ++index) { |
| 1730 | SkASSERT(fMaps[index].fMarkType == (MarkType) index); |
| 1731 | } |
| 1732 | IncludeParser::ValidateKeyWords(); |
| 1733 | } |