annotate tools/llvm-rc/ResourceScriptParser.h @ 122:36195a0db682

merging ( incomplete )
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 17 Nov 2017 20:32:31 +0900
parents 803732b1fca8
children c2174574ed3a
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 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
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 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 // This defines the RC scripts parser. It takes a sequence of RC tokens
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 // and then provides the method to parse the resources one by one.
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
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15 #ifndef LLVM_TOOLS_LLVMRC_RESOURCESCRIPTPARSER_H
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 #define LLVM_TOOLS_LLVMRC_RESOURCESCRIPTPARSER_H
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 #include "ResourceScriptStmt.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 #include "ResourceScriptToken.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 #include "llvm/Support/Compiler.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22 #include "llvm/Support/raw_ostream.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 #include <system_error>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 #include <vector>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27 namespace llvm {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 namespace opt {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 class InputArgList;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 namespace rc {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 class RCParser {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 using LocIter = std::vector<RCToken>::iterator;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 using ParseType = Expected<std::unique_ptr<RCResource>>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 using ParseOptionType = Expected<std::unique_ptr<OptionalStmt>>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 // Class describing a single failure of parser.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 class ParserError : public ErrorInfo<ParserError> {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 ParserError(const Twine &Expected, const LocIter CurLoc, const LocIter End);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 void log(raw_ostream &OS) const override { OS << CurMessage; }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 std::error_code convertToErrorCode() const override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 return std::make_error_code(std::errc::invalid_argument);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 const std::string &getMessage() const { return CurMessage; }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 static char ID; // Keep llvm::Error happy.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 std::string CurMessage;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 LocIter ErrorLoc, FileEnd;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 explicit RCParser(std::vector<RCToken> TokenList);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 // Reads and returns a single resource definition, or error message if any
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 // occurred.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 ParseType parseSingleResource();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 bool isEof() const;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 using Kind = RCToken::Kind;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 // Checks if the current parser state points to the token of type TokenKind.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 bool isNextTokenKind(Kind TokenKind) const;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 // These methods assume that the parser is not in EOF state.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73 // Take a look at the current token. Do not fetch it.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 const RCToken &look() const;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 // Read the current token and advance the state by one token.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76 const RCToken &read();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 // Advance the state by one token, discarding the current token.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78 void consume();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80 // The following methods try to read a single token, check if it has the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81 // correct type and then parse it.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82 // Each integer can be written as an arithmetic expression producing an
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83 // unsigned 32-bit integer.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84 Expected<RCInt> readInt(); // Parse an integer.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85 Expected<StringRef> readString(); // Parse a string.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86 Expected<StringRef> readIdentifier(); // Parse an identifier.
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.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91 Expected<RCInt> parseIntExpr1();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 Expected<RCInt> parseIntExpr2();
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
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
131 Expected<OptionalStmtList>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
132 parseOptionalStatements(OptStmtType StmtsType = OptStmtType::BasicStmt);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
133
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
134 // Read a single optional statement.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
135 Expected<std::unique_ptr<OptionalStmt>>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
136 parseSingleOptionalStatement(OptStmtType StmtsType = OptStmtType::BasicStmt);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
137
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
138 // Top-level resource parsers.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
139 ParseType parseLanguageResource();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
140 ParseType parseAcceleratorsResource();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
141 ParseType parseCursorResource();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
142 ParseType parseDialogResource(bool IsExtended);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
143 ParseType parseIconResource();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
144 ParseType parseHTMLResource();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
145 ParseType parseMenuResource();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
146 ParseType parseStringTableResource();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
147 ParseType parseUserDefinedResource(IntOrString Type);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
148 ParseType parseVersionInfoResource();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
149
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
150 // Helper DIALOG parser - a single control.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
151 Expected<Control> parseControl();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
152
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
153 // Helper MENU parser.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
154 Expected<MenuDefinitionList> parseMenuItemsList();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
155
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
156 // Helper VERSIONINFO parser - read the contents of a single BLOCK statement,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
157 // from BEGIN to END.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
158 Expected<std::unique_ptr<VersionInfoBlock>>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
159 parseVersionInfoBlockContents(StringRef BlockName);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
160 // Helper VERSIONINFO parser - read either VALUE or BLOCK statement.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
161 Expected<std::unique_ptr<VersionInfoStmt>> parseVersionInfoStmt();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
162 // Helper VERSIONINFO parser - read fixed VERSIONINFO statements.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
163 Expected<VersionInfoResource::VersionInfoFixed> parseVersionInfoFixed();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
164
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
165 // Optional statement parsers.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
166 ParseOptionType parseLanguageStmt();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
167 ParseOptionType parseCharacteristicsStmt();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
168 ParseOptionType parseVersionStmt();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
169 ParseOptionType parseCaptionStmt();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
170 ParseOptionType parseFontStmt(OptStmtType DialogType);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
171 ParseOptionType parseStyleStmt();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
172
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
173 // Raises an error. If IsAlreadyRead = false (default), this complains about
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
174 // the token that couldn't be parsed. If the flag is on, this complains about
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
175 // the correctly read token that makes no sense (that is, the current parser
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
176 // state is beyond the erroneous token.)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
177 Error getExpectedError(const Twine &Message, bool IsAlreadyRead = false);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
178
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
179 std::vector<RCToken> Tokens;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
180 LocIter CurLoc;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
181 const LocIter End;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
182 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
183
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
184 } // namespace rc
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
185 } // namespace llvm
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
186
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
187 #endif