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