ResourceScriptParser.h 7.2 KB

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