1 /* 2 * Copyright 2018 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 #ifndef bmhParser_DEFINED 9 #define bmhParser_DEFINED 10 11 #include "SkCommandLineFlags.h" 12 13 #include "definition.h" 14 #include "parserCommon.h" 15 16 class BmhParser : public ParserCommon { 17 public: 18 enum class MarkLookup { 19 kRequire, 20 kAllowUnknown, 21 }; 22 23 enum class ExampleOptions { 24 kText, 25 kPng, 26 kAll 27 }; 28 29 enum class Exemplary { 30 kNo, 31 kYes, 32 kOptional, 33 }; 34 35 enum class TableState { 36 kNone, 37 kColumnStart, 38 kColumnEnd, 39 }; 40 41 enum class HasTag { 42 kNo, 43 kYes, 44 }; 45 46 enum class TrimExtract { 47 kNo, 48 kYes, 49 }; 50 BmhParser(bool skip)51 BmhParser(bool skip) : ParserCommon() 52 , fMaps { 53 { &fClassMap, MarkType::kClass } 54 , { &fConstMap, MarkType::kConst } 55 , { &fDefineMap, MarkType::kDefine } 56 , { &fEnumMap, MarkType::kEnum } 57 , { &fClassMap, MarkType::kEnumClass } 58 , { &fMethodMap, MarkType::kMethod } 59 , { &fClassMap, MarkType::kStruct } 60 , { &fClassMap, MarkType::kTemplate } 61 , { &fTypedefMap, MarkType::kTypedef } 62 } 63 , fSkip(skip) { 64 this->reset(); 65 } 66 ~BmhParser()67 ~BmhParser() override {} 68 69 bool addDefinition(const char* defStart, bool hasEnd, MarkType markType, 70 const vector<string>& typeNameBuilder, HasTag hasTag); 71 bool checkEndMarker(MarkType markType, string name) const; 72 bool checkExamples() const; 73 const char* checkForFullTerminal(const char* end, const Definition* ) const; 74 bool checkParamReturn(const Definition* definition) const; 75 bool dumpExamples(FILE* fiddleOut, Definition& def, bool* continuation) const; 76 bool dumpExamples(const char* fiddleJsonFileName) const; 77 bool checkExampleHashes() const; 78 bool childOf(MarkType markType) const; 79 string className(MarkType markType); 80 bool collectExternals(); 81 int endHashCount() const; 82 bool endTableColumn(const char* end, const char* terminator); 83 bool exampleToScript(Definition*, ExampleOptions, string* result ) const; 84 string extractText(const Definition* , TrimExtract ) const; 85 RootDefinition* findBmhObject(MarkType markType, string typeName); 86 bool findDefinitions(); 87 Definition* findExample(string name) const; 88 MarkType getMarkType(MarkLookup lookup) const; 89 bool hasEndToken() const; 90 static bool IsExemplary(const Definition* ); 91 string loweredTopic(string name, Definition* def); 92 string memberName(); 93 string methodName(); 94 const Definition* parentSpace() const; 95 parseFromFile(const char * path)96 bool parseFromFile(const char* path) override { 97 if (!INHERITED::parseSetup(path)) { 98 return false; 99 } 100 fCheckMethods = !strstr(path, "undocumented.bmh"); 101 return findDefinitions(); 102 } 103 104 void parseHashAnchor(Definition* ); 105 void parseHashFormula(Definition* ); 106 void parseHashLine(Definition* ); 107 bool popParentStack(Definition* ); 108 void reportDuplicates(const Definition& , string dup) const; 109 void resetExampleHashes(); 110 reset()111 void reset() override { 112 INHERITED::resetCommon(); 113 fRoot = nullptr; 114 fWorkingColumn = nullptr; 115 fRow = nullptr; 116 fTableState = TableState::kNone; 117 fMC = '#'; 118 fInChar = false; 119 fInCharCommentString = false; 120 fInComment = false; 121 fInEnum = false; 122 fInString = false; 123 fCheckMethods = false; 124 } 125 126 void setUpGlobalSubstitutes(); 127 void setUpPartialSubstitute(string name); 128 void setUpSubstitute(string name, Definition* def); 129 void setUpSubstitutes(const Definition* parent, NameMap* ); 130 void setWrapper(Definition* def) const; 131 bool skipNoName(); 132 bool skipToDefinitionEnd(MarkType markType); 133 bool skipToString(); 134 void spellCheck(const char* match, SkCommandLineFlags::StringArray report) const; 135 void spellStatus(const char* match, SkCommandLineFlags::StringArray report) const; 136 vector<string> topicName(); 137 vector<string> typeName(MarkType markType, bool* expectEnd); 138 string typedefName() override; 139 string uniqueName(string base, MarkType markType); 140 string uniqueRootName(string base, MarkType markType); 141 void validate() const; 142 string word(string prefix, string delimiter); 143 144 public: 145 struct MarkProps { 146 const char* fName; 147 MarkType fMarkType; 148 Resolvable fResolve; 149 Exemplary fExemplary; // worthy of an example 150 uint64_t fParentMask; 151 }; 152 153 struct DefinitionMap { 154 unordered_map<string, RootDefinition>* fMap; 155 MarkType fMarkType; 156 }; 157 158 vector<DefinitionMap> fMaps; 159 160 static MarkProps kMarkProps[Last_MarkType + 1]; 161 forward_list<RootDefinition> fTopics; 162 forward_list<Definition> fMarkup; 163 forward_list<RootDefinition> fExternals; 164 vector<string> fInputFiles; 165 unordered_map<string, RootDefinition> fClassMap; 166 unordered_map<string, RootDefinition> fConstMap; 167 unordered_map<string, RootDefinition> fDefineMap; 168 unordered_map<string, RootDefinition> fEnumMap; 169 unordered_map<string, RootDefinition> fMethodMap; 170 unordered_map<string, RootDefinition> fTypedefMap; 171 unordered_map<string, Definition*> fTopicMap; 172 unordered_map<string, Definition*> fAliasMap; 173 unordered_map<string, Definition*> fPhraseMap; 174 NameMap fGlobalNames; 175 RootDefinition* fRoot; 176 Definition* fWorkingColumn; 177 Definition* fRow; 178 const char* fColStart; 179 TableState fTableState; 180 mutable char fMC; // markup character 181 bool fAnonymous; 182 bool fCloned; 183 bool fInChar; 184 bool fInCharCommentString; 185 bool fInEnum; 186 bool fInComment; 187 bool fInString; 188 bool fCheckMethods; 189 bool fSkip = false; 190 bool fWroteOut = false; 191 private: 192 typedef ParserCommon INHERITED; 193 }; 194 195 #endif 196