ResourceScriptParser.h 7.2 KB

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