annotate tools/llvm-rc/ResourceScriptParser.h @ 148:63bd29f05246

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