Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 1 | //===-- ResourceScriptParser.h ----------------------------------*- C++-*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===---------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This defines the RC scripts parser. It takes a sequence of RC tokens |
| 11 | // and then provides the method to parse the resources one by one. |
| 12 | // |
| 13 | //===---------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_TOOLS_LLVMRC_RESOURCESCRIPTPARSER_H |
| 16 | #define LLVM_TOOLS_LLVMRC_RESOURCESCRIPTPARSER_H |
| 17 | |
| 18 | #include "ResourceScriptStmt.h" |
| 19 | #include "ResourceScriptToken.h" |
| 20 | |
| 21 | #include "llvm/Support/Compiler.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | |
| 24 | #include <system_error> |
| 25 | #include <vector> |
| 26 | |
| 27 | namespace llvm { |
| 28 | namespace rc { |
| 29 | |
| 30 | class RCParser { |
| 31 | public: |
| 32 | using LocIter = std::vector<RCToken>::iterator; |
| 33 | using ParseType = Expected<std::unique_ptr<RCResource>>; |
| 34 | using ParseOptionType = Expected<std::unique_ptr<OptionalStmt>>; |
| 35 | |
| 36 | // Class describing a single failure of parser. |
| 37 | class ParserError : public ErrorInfo<ParserError> { |
| 38 | public: |
Zachary Turner | 514b710 | 2017-10-09 18:50:29 +0000 | [diff] [blame^] | 39 | ParserError(const Twine &Expected, const LocIter CurLoc, const LocIter End); |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 40 | |
| 41 | void log(raw_ostream &OS) const override { OS << CurMessage; } |
| 42 | std::error_code convertToErrorCode() const override { |
| 43 | return std::make_error_code(std::errc::invalid_argument); |
| 44 | } |
| 45 | const std::string &getMessage() const { return CurMessage; } |
| 46 | |
| 47 | static char ID; // Keep llvm::Error happy. |
| 48 | |
| 49 | private: |
| 50 | std::string CurMessage; |
| 51 | LocIter ErrorLoc, FileEnd; |
| 52 | }; |
| 53 | |
Zachary Turner | 514b710 | 2017-10-09 18:50:29 +0000 | [diff] [blame^] | 54 | RCParser(std::vector<RCToken> TokenList); |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 55 | |
| 56 | // Reads and returns a single resource definition, or error message if any |
| 57 | // occurred. |
| 58 | ParseType parseSingleResource(); |
| 59 | |
| 60 | bool isEof() const; |
| 61 | |
| 62 | private: |
| 63 | using Kind = RCToken::Kind; |
| 64 | |
| 65 | // Checks if the current parser state points to the token of type TokenKind. |
| 66 | bool isNextTokenKind(Kind TokenKind) const; |
| 67 | |
| 68 | // These methods assume that the parser is not in EOF state. |
| 69 | |
| 70 | // Take a look at the current token. Do not fetch it. |
| 71 | const RCToken &look() const; |
| 72 | // Read the current token and advance the state by one token. |
| 73 | const RCToken &read(); |
| 74 | // Advance the state by one token, discarding the current token. |
| 75 | void consume(); |
| 76 | |
| 77 | // The following methods try to read a single token, check if it has the |
| 78 | // correct type and then parse it. |
Marek Sokolowski | 7e89ee7 | 2017-09-28 23:53:25 +0000 | [diff] [blame] | 79 | // Each integer can be written as an arithmetic expression producing an |
| 80 | // unsigned 32-bit integer. |
Zachary Turner | 07bc04f | 2017-10-06 21:26:06 +0000 | [diff] [blame] | 81 | Expected<RCInt> readInt(); // Parse an integer. |
Marek Sokolowski | 7f11052 | 2017-08-28 22:58:31 +0000 | [diff] [blame] | 82 | Expected<StringRef> readString(); // Parse a string. |
| 83 | Expected<StringRef> readIdentifier(); // Parse an identifier. |
| 84 | Expected<IntOrString> readIntOrString(); // Parse an integer or a string. |
| 85 | Expected<IntOrString> readTypeOrName(); // Parse an integer or an identifier. |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 86 | |
Marek Sokolowski | 7e89ee7 | 2017-09-28 23:53:25 +0000 | [diff] [blame] | 87 | // Helper integer expression parsing methods. |
Zachary Turner | 07bc04f | 2017-10-06 21:26:06 +0000 | [diff] [blame] | 88 | Expected<RCInt> parseIntExpr1(); |
| 89 | Expected<RCInt> parseIntExpr2(); |
Marek Sokolowski | 7e89ee7 | 2017-09-28 23:53:25 +0000 | [diff] [blame] | 90 | |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 91 | // Advance the state by one, discarding the current token. |
| 92 | // If the discarded token had an incorrect type, fail. |
| 93 | Error consumeType(Kind TokenKind); |
| 94 | |
| 95 | // Check the current token type. If it's TokenKind, discard it. |
| 96 | // Return true if the parser consumed this token successfully. |
| 97 | bool consumeOptionalType(Kind TokenKind); |
| 98 | |
| 99 | // Read at least MinCount, and at most MaxCount integers separated by |
| 100 | // commas. The parser stops reading after fetching MaxCount integers |
| 101 | // or after an error occurs. Whenever the parser reads a comma, it |
| 102 | // expects an integer to follow. |
Zachary Turner | 07bc04f | 2017-10-06 21:26:06 +0000 | [diff] [blame] | 103 | Expected<SmallVector<RCInt, 8>> readIntsWithCommas(size_t MinCount, |
| 104 | size_t MaxCount); |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 105 | |
Marek Sokolowski | 7f11052 | 2017-08-28 22:58:31 +0000 | [diff] [blame] | 106 | // Read an unknown number of flags preceded by commas. Each correct flag |
| 107 | // has an entry in FlagDesc array of length NumFlags. In case i-th |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame] | 108 | // flag (0-based) has been read, the result is OR-ed with FlagValues[i]. |
Marek Sokolowski | 7f11052 | 2017-08-28 22:58:31 +0000 | [diff] [blame] | 109 | // As long as parser has a comma to read, it expects to be fed with |
| 110 | // a correct flag afterwards. |
Marek Sokolowski | c75a087 | 2017-09-29 17:46:32 +0000 | [diff] [blame] | 111 | Expected<uint32_t> parseFlags(ArrayRef<StringRef> FlagDesc, |
| 112 | ArrayRef<uint32_t> FlagValues); |
Marek Sokolowski | 7f11052 | 2017-08-28 22:58:31 +0000 | [diff] [blame] | 113 | |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 114 | // Reads a set of optional statements. These can change the behavior of |
| 115 | // a number of resource types (e.g. STRINGTABLE, MENU or DIALOG) if provided |
| 116 | // before the main block with the contents of the resource. |
| 117 | // Usually, resources use a basic set of optional statements: |
| 118 | // CHARACTERISTICS, LANGUAGE, VERSION |
| 119 | // However, DIALOG and DIALOGEX extend this list by the following items: |
| 120 | // CAPTION, CLASS, EXSTYLE, FONT, MENU, STYLE |
| 121 | // UseExtendedStatements flag (off by default) allows the parser to read |
| 122 | // the additional types of statements. |
| 123 | // |
| 124 | // Ref (to the list of all optional statements): |
| 125 | // msdn.microsoft.com/en-us/library/windows/desktop/aa381002(v=vs.85).aspx |
Zachary Turner | 420090a | 2017-10-06 20:51:20 +0000 | [diff] [blame] | 126 | enum class OptStmtType { BasicStmt, DialogStmt, DialogExStmt }; |
| 127 | |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 128 | Expected<OptionalStmtList> |
Zachary Turner | 420090a | 2017-10-06 20:51:20 +0000 | [diff] [blame] | 129 | parseOptionalStatements(OptStmtType StmtsType = OptStmtType::BasicStmt); |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 130 | |
| 131 | // Read a single optional statement. |
| 132 | Expected<std::unique_ptr<OptionalStmt>> |
Zachary Turner | 420090a | 2017-10-06 20:51:20 +0000 | [diff] [blame] | 133 | parseSingleOptionalStatement(OptStmtType StmtsType = OptStmtType::BasicStmt); |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 134 | |
| 135 | // Top-level resource parsers. |
| 136 | ParseType parseLanguageResource(); |
Marek Sokolowski | 7f11052 | 2017-08-28 22:58:31 +0000 | [diff] [blame] | 137 | ParseType parseAcceleratorsResource(); |
Marek Sokolowski | 72aa937 | 2017-08-28 21:59:54 +0000 | [diff] [blame] | 138 | ParseType parseCursorResource(); |
Marek Sokolowski | 4ac54d9 | 2017-08-29 16:49:59 +0000 | [diff] [blame] | 139 | ParseType parseDialogResource(bool IsExtended); |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 140 | ParseType parseIconResource(); |
Marek Sokolowski | 72aa937 | 2017-08-28 21:59:54 +0000 | [diff] [blame] | 141 | ParseType parseHTMLResource(); |
Marek Sokolowski | 99ecb0e | 2017-08-28 23:46:30 +0000 | [diff] [blame] | 142 | ParseType parseMenuResource(); |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 143 | ParseType parseStringTableResource(); |
Marek Sokolowski | b5f39a0 | 2017-09-29 00:14:18 +0000 | [diff] [blame] | 144 | ParseType parseUserDefinedResource(IntOrString Type); |
Marek Sokolowski | fb74cb1 | 2017-09-28 22:41:38 +0000 | [diff] [blame] | 145 | ParseType parseVersionInfoResource(); |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 146 | |
Marek Sokolowski | 4ac54d9 | 2017-08-29 16:49:59 +0000 | [diff] [blame] | 147 | // Helper DIALOG parser - a single control. |
| 148 | Expected<Control> parseControl(); |
| 149 | |
Marek Sokolowski | 99ecb0e | 2017-08-28 23:46:30 +0000 | [diff] [blame] | 150 | // Helper MENU parser. |
| 151 | Expected<MenuDefinitionList> parseMenuItemsList(); |
| 152 | |
Marek Sokolowski | fb74cb1 | 2017-09-28 22:41:38 +0000 | [diff] [blame] | 153 | // Helper VERSIONINFO parser - read the contents of a single BLOCK statement, |
| 154 | // from BEGIN to END. |
| 155 | Expected<std::unique_ptr<VersionInfoBlock>> |
| 156 | parseVersionInfoBlockContents(StringRef BlockName); |
| 157 | // Helper VERSIONINFO parser - read either VALUE or BLOCK statement. |
| 158 | Expected<std::unique_ptr<VersionInfoStmt>> parseVersionInfoStmt(); |
| 159 | // Helper VERSIONINFO parser - read fixed VERSIONINFO statements. |
| 160 | Expected<VersionInfoResource::VersionInfoFixed> parseVersionInfoFixed(); |
| 161 | |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 162 | // Optional statement parsers. |
| 163 | ParseOptionType parseLanguageStmt(); |
| 164 | ParseOptionType parseCharacteristicsStmt(); |
| 165 | ParseOptionType parseVersionStmt(); |
Marek Sokolowski | 4ac54d9 | 2017-08-29 16:49:59 +0000 | [diff] [blame] | 166 | ParseOptionType parseCaptionStmt(); |
Zachary Turner | 420090a | 2017-10-06 20:51:20 +0000 | [diff] [blame] | 167 | ParseOptionType parseFontStmt(OptStmtType DialogType); |
Marek Sokolowski | 4ac54d9 | 2017-08-29 16:49:59 +0000 | [diff] [blame] | 168 | ParseOptionType parseStyleStmt(); |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 169 | |
| 170 | // Raises an error. If IsAlreadyRead = false (default), this complains about |
| 171 | // the token that couldn't be parsed. If the flag is on, this complains about |
| 172 | // the correctly read token that makes no sense (that is, the current parser |
| 173 | // state is beyond the erroneous token.) |
Zachary Turner | 514b710 | 2017-10-09 18:50:29 +0000 | [diff] [blame^] | 174 | Error getExpectedError(const Twine &Message, bool IsAlreadyRead = false); |
Marek Sokolowski | 5cd3d5c | 2017-08-18 18:24:17 +0000 | [diff] [blame] | 175 | |
| 176 | std::vector<RCToken> Tokens; |
| 177 | LocIter CurLoc; |
| 178 | const LocIter End; |
| 179 | }; |
| 180 | |
| 181 | } // namespace rc |
| 182 | } // namespace llvm |
| 183 | |
| 184 | #endif |