Remove the default clause from a fully-covering switch
[llvm-core.git] / tools / llvm-rc / ResourceScriptParser.h
blobbce2e0b544e2046d8fbf43ee25876cb35ff4d5bc
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 rc {
30 class RCParser {
31 public:
32 using LocIter = std::vector<RCToken>::iterator;
33 using ParseType = Expected<std::unique_ptr<RCResource>>;
34 using ParseOptionType = Expected<std::unique_ptr<OptionalStmt>>;
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);
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);
45 const std::string &getMessage() const { return CurMessage; }
47 static char ID; // Keep llvm::Error happy.
49 private:
50 std::string CurMessage;
51 LocIter ErrorLoc, FileEnd;
54 RCParser(const std::vector<RCToken> &TokenList);
55 RCParser(std::vector<RCToken> &&TokenList);
57 // Reads and returns a single resource definition, or error message if any
58 // occurred.
59 ParseType parseSingleResource();
61 bool isEof() const;
63 private:
64 using Kind = RCToken::Kind;
66 // Checks if the current parser state points to the token of type TokenKind.
67 bool isNextTokenKind(Kind TokenKind) const;
69 // These methods assume that the parser is not in EOF state.
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();
78 // The following methods try to read a single token, check if it has the
79 // correct type and then parse it.
80 Expected<uint32_t> readInt(); // Parse an integer.
81 Expected<StringRef> readString(); // Parse a string.
82 Expected<StringRef> readIdentifier(); // Parse an identifier.
83 Expected<IntOrString> readIntOrString(); // Parse an integer or a string.
84 Expected<IntOrString> readTypeOrName(); // Parse an integer or an identifier.
86 // Advance the state by one, discarding the current token.
87 // If the discarded token had an incorrect type, fail.
88 Error consumeType(Kind TokenKind);
90 // Check the current token type. If it's TokenKind, discard it.
91 // Return true if the parser consumed this token successfully.
92 bool consumeOptionalType(Kind TokenKind);
94 // Read at least MinCount, and at most MaxCount integers separated by
95 // commas. The parser stops reading after fetching MaxCount integers
96 // or after an error occurs. Whenever the parser reads a comma, it
97 // expects an integer to follow.
98 Expected<SmallVector<uint32_t, 8>> readIntsWithCommas(size_t MinCount,
99 size_t MaxCount);
101 // Read an unknown number of flags preceded by commas. Each correct flag
102 // has an entry in FlagDesc array of length NumFlags. In case i-th
103 // flag (0-based) has been read, the i-th bit of the result is set.
104 // As long as parser has a comma to read, it expects to be fed with
105 // a correct flag afterwards.
106 Expected<uint32_t> parseFlags(ArrayRef<StringRef> FlagDesc);
108 // Reads a set of optional statements. These can change the behavior of
109 // a number of resource types (e.g. STRINGTABLE, MENU or DIALOG) if provided
110 // before the main block with the contents of the resource.
111 // Usually, resources use a basic set of optional statements:
112 // CHARACTERISTICS, LANGUAGE, VERSION
113 // However, DIALOG and DIALOGEX extend this list by the following items:
114 // CAPTION, CLASS, EXSTYLE, FONT, MENU, STYLE
115 // UseExtendedStatements flag (off by default) allows the parser to read
116 // the additional types of statements.
118 // Ref (to the list of all optional statements):
119 // msdn.microsoft.com/en-us/library/windows/desktop/aa381002(v=vs.85).aspx
120 Expected<OptionalStmtList>
121 parseOptionalStatements(bool UseExtendedStatements = false);
123 // Read a single optional statement.
124 Expected<std::unique_ptr<OptionalStmt>>
125 parseSingleOptionalStatement(bool UseExtendedStatements = false);
127 // Top-level resource parsers.
128 ParseType parseLanguageResource();
129 ParseType parseAcceleratorsResource();
130 ParseType parseCursorResource();
131 ParseType parseDialogResource(bool IsExtended);
132 ParseType parseIconResource();
133 ParseType parseHTMLResource();
134 ParseType parseMenuResource();
135 ParseType parseStringTableResource();
137 // Helper DIALOG parser - a single control.
138 Expected<Control> parseControl();
140 // Helper MENU parser.
141 Expected<MenuDefinitionList> parseMenuItemsList();
143 // Optional statement parsers.
144 ParseOptionType parseLanguageStmt();
145 ParseOptionType parseCharacteristicsStmt();
146 ParseOptionType parseVersionStmt();
147 ParseOptionType parseCaptionStmt();
148 ParseOptionType parseFontStmt();
149 ParseOptionType parseStyleStmt();
151 // Raises an error. If IsAlreadyRead = false (default), this complains about
152 // the token that couldn't be parsed. If the flag is on, this complains about
153 // the correctly read token that makes no sense (that is, the current parser
154 // state is beyond the erroneous token.)
155 Error getExpectedError(const Twine Message, bool IsAlreadyRead = false);
157 std::vector<RCToken> Tokens;
158 LocIter CurLoc;
159 const LocIter End;
162 } // namespace rc
163 } // namespace llvm
165 #endif