blob: f2afe6d7c322645df4602639dbcd3be937b6042c [file] [log] [blame]
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +00001//===-- 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.
12//
13//===---------------------------------------------------------------------===//
14
15#ifndef LLVM_TOOLS_LLVMRC_RESOURCESCRIPTPARSER_H
16#define LLVM_TOOLS_LLVMRC_RESOURCESCRIPTPARSER_H
17
18#include "ResourceScriptStmt.h"
19#include "ResourceScriptToken.h"
20
21#include "llvm/Support/Compiler.h"
22#include "llvm/Support/raw_ostream.h"
23
24#include <system_error>
25#include <vector>
26
27namespace llvm {
28namespace rc {
29
30class RCParser {
31public:
32 using LocIter = std::vector<RCToken>::iterator;
33 using ParseType = Expected<std::unique_ptr<RCResource>>;
34 using ParseOptionType = Expected<std::unique_ptr<OptionalStmt>>;
35
36 // Class describing a single failure of parser.
37 class ParserError : public ErrorInfo<ParserError> {
38 public:
39 ParserError(Twine Expected, const LocIter CurLoc, const LocIter End);
40
41 void log(raw_ostream &OS) const override { OS << CurMessage; }
42 std::error_code convertToErrorCode() const override {
43 return std::make_error_code(std::errc::invalid_argument);
44 }
45 const std::string &getMessage() const { return CurMessage; }
46
47 static char ID; // Keep llvm::Error happy.
48
49 private:
50 std::string CurMessage;
51 LocIter ErrorLoc, FileEnd;
52 };
53
54 RCParser(const std::vector<RCToken> &TokenList);
55 RCParser(std::vector<RCToken> &&TokenList);
56
57 // Reads and returns a single resource definition, or error message if any
58 // occurred.
59 ParseType parseSingleResource();
60
61 bool isEof() const;
62
63private:
64 using Kind = RCToken::Kind;
65
66 // Checks if the current parser state points to the token of type TokenKind.
67 bool isNextTokenKind(Kind TokenKind) const;
68
69 // These methods assume that the parser is not in EOF state.
70
71 // Take a look at the current token. Do not fetch it.
72 const RCToken &look() const;
73 // Read the current token and advance the state by one token.
74 const RCToken &read();
75 // Advance the state by one token, discarding the current token.
76 void consume();
77
78 // The following methods try to read a single token, check if it has the
79 // correct type and then parse it.
Marek Sokolowski7e89ee72017-09-28 23:53:25 +000080 // Each integer can be written as an arithmetic expression producing an
81 // unsigned 32-bit integer.
Zachary Turner07bc04f2017-10-06 21:26:06 +000082 Expected<RCInt> readInt(); // Parse an integer.
Marek Sokolowski7f110522017-08-28 22:58:31 +000083 Expected<StringRef> readString(); // Parse a string.
84 Expected<StringRef> readIdentifier(); // Parse an identifier.
85 Expected<IntOrString> readIntOrString(); // Parse an integer or a string.
86 Expected<IntOrString> readTypeOrName(); // Parse an integer or an identifier.
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +000087
Marek Sokolowski7e89ee72017-09-28 23:53:25 +000088 // Helper integer expression parsing methods.
Zachary Turner07bc04f2017-10-06 21:26:06 +000089 Expected<RCInt> parseIntExpr1();
90 Expected<RCInt> parseIntExpr2();
Marek Sokolowski7e89ee72017-09-28 23:53:25 +000091
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +000092 // Advance the state by one, discarding the current token.
93 // If the discarded token had an incorrect type, fail.
94 Error consumeType(Kind TokenKind);
95
96 // Check the current token type. If it's TokenKind, discard it.
97 // Return true if the parser consumed this token successfully.
98 bool consumeOptionalType(Kind TokenKind);
99
100 // Read at least MinCount, and at most MaxCount integers separated by
101 // commas. The parser stops reading after fetching MaxCount integers
102 // or after an error occurs. Whenever the parser reads a comma, it
103 // expects an integer to follow.
Zachary Turner07bc04f2017-10-06 21:26:06 +0000104 Expected<SmallVector<RCInt, 8>> readIntsWithCommas(size_t MinCount,
105 size_t MaxCount);
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +0000106
Marek Sokolowski7f110522017-08-28 22:58:31 +0000107 // Read an unknown number of flags preceded by commas. Each correct flag
108 // has an entry in FlagDesc array of length NumFlags. In case i-th
Marek Sokolowskic75a0872017-09-29 17:46:32 +0000109 // flag (0-based) has been read, the result is OR-ed with FlagValues[i].
Marek Sokolowski7f110522017-08-28 22:58:31 +0000110 // As long as parser has a comma to read, it expects to be fed with
111 // a correct flag afterwards.
Marek Sokolowskic75a0872017-09-29 17:46:32 +0000112 Expected<uint32_t> parseFlags(ArrayRef<StringRef> FlagDesc,
113 ArrayRef<uint32_t> FlagValues);
Marek Sokolowski7f110522017-08-28 22:58:31 +0000114
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +0000115 // Reads a set of optional statements. These can change the behavior of
116 // a number of resource types (e.g. STRINGTABLE, MENU or DIALOG) if provided
117 // before the main block with the contents of the resource.
118 // Usually, resources use a basic set of optional statements:
119 // CHARACTERISTICS, LANGUAGE, VERSION
120 // However, DIALOG and DIALOGEX extend this list by the following items:
121 // CAPTION, CLASS, EXSTYLE, FONT, MENU, STYLE
122 // UseExtendedStatements flag (off by default) allows the parser to read
123 // the additional types of statements.
124 //
125 // Ref (to the list of all optional statements):
126 // msdn.microsoft.com/en-us/library/windows/desktop/aa381002(v=vs.85).aspx
Zachary Turner420090a2017-10-06 20:51:20 +0000127 enum class OptStmtType { BasicStmt, DialogStmt, DialogExStmt };
128
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +0000129 Expected<OptionalStmtList>
Zachary Turner420090a2017-10-06 20:51:20 +0000130 parseOptionalStatements(OptStmtType StmtsType = OptStmtType::BasicStmt);
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +0000131
132 // Read a single optional statement.
133 Expected<std::unique_ptr<OptionalStmt>>
Zachary Turner420090a2017-10-06 20:51:20 +0000134 parseSingleOptionalStatement(OptStmtType StmtsType = OptStmtType::BasicStmt);
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +0000135
136 // Top-level resource parsers.
137 ParseType parseLanguageResource();
Marek Sokolowski7f110522017-08-28 22:58:31 +0000138 ParseType parseAcceleratorsResource();
Marek Sokolowski72aa9372017-08-28 21:59:54 +0000139 ParseType parseCursorResource();
Marek Sokolowski4ac54d92017-08-29 16:49:59 +0000140 ParseType parseDialogResource(bool IsExtended);
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +0000141 ParseType parseIconResource();
Marek Sokolowski72aa9372017-08-28 21:59:54 +0000142 ParseType parseHTMLResource();
Marek Sokolowski99ecb0e2017-08-28 23:46:30 +0000143 ParseType parseMenuResource();
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +0000144 ParseType parseStringTableResource();
Marek Sokolowskib5f39a02017-09-29 00:14:18 +0000145 ParseType parseUserDefinedResource(IntOrString Type);
Marek Sokolowskifb74cb12017-09-28 22:41:38 +0000146 ParseType parseVersionInfoResource();
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +0000147
Marek Sokolowski4ac54d92017-08-29 16:49:59 +0000148 // Helper DIALOG parser - a single control.
149 Expected<Control> parseControl();
150
Marek Sokolowski99ecb0e2017-08-28 23:46:30 +0000151 // Helper MENU parser.
152 Expected<MenuDefinitionList> parseMenuItemsList();
153
Marek Sokolowskifb74cb12017-09-28 22:41:38 +0000154 // Helper VERSIONINFO parser - read the contents of a single BLOCK statement,
155 // from BEGIN to END.
156 Expected<std::unique_ptr<VersionInfoBlock>>
157 parseVersionInfoBlockContents(StringRef BlockName);
158 // Helper VERSIONINFO parser - read either VALUE or BLOCK statement.
159 Expected<std::unique_ptr<VersionInfoStmt>> parseVersionInfoStmt();
160 // Helper VERSIONINFO parser - read fixed VERSIONINFO statements.
161 Expected<VersionInfoResource::VersionInfoFixed> parseVersionInfoFixed();
162
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +0000163 // Optional statement parsers.
164 ParseOptionType parseLanguageStmt();
165 ParseOptionType parseCharacteristicsStmt();
166 ParseOptionType parseVersionStmt();
Marek Sokolowski4ac54d92017-08-29 16:49:59 +0000167 ParseOptionType parseCaptionStmt();
Zachary Turner420090a2017-10-06 20:51:20 +0000168 ParseOptionType parseFontStmt(OptStmtType DialogType);
Marek Sokolowski4ac54d92017-08-29 16:49:59 +0000169 ParseOptionType parseStyleStmt();
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +0000170
171 // Raises an error. If IsAlreadyRead = false (default), this complains about
172 // the token that couldn't be parsed. If the flag is on, this complains about
173 // the correctly read token that makes no sense (that is, the current parser
174 // state is beyond the erroneous token.)
175 Error getExpectedError(const Twine Message, bool IsAlreadyRead = false);
176
177 std::vector<RCToken> Tokens;
178 LocIter CurLoc;
179 const LocIter End;
180};
181
182} // namespace rc
183} // namespace llvm
184
185#endif