blob: b217a73ae765f2b258aa74d974c2da02145e5ad7 [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.
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> readTypeOrName(); // Parse an integer or an identifier.
84
85 // Advance the state by one, discarding the current token.
86 // If the discarded token had an incorrect type, fail.
87 Error consumeType(Kind TokenKind);
88
89 // Check the current token type. If it's TokenKind, discard it.
90 // Return true if the parser consumed this token successfully.
91 bool consumeOptionalType(Kind TokenKind);
92
93 // Read at least MinCount, and at most MaxCount integers separated by
94 // commas. The parser stops reading after fetching MaxCount integers
95 // or after an error occurs. Whenever the parser reads a comma, it
96 // expects an integer to follow.
97 Expected<SmallVector<uint32_t, 8>> readIntsWithCommas(size_t MinCount,
98 size_t MaxCount);
99
100 // Reads a set of optional statements. These can change the behavior of
101 // a number of resource types (e.g. STRINGTABLE, MENU or DIALOG) if provided
102 // before the main block with the contents of the resource.
103 // Usually, resources use a basic set of optional statements:
104 // CHARACTERISTICS, LANGUAGE, VERSION
105 // However, DIALOG and DIALOGEX extend this list by the following items:
106 // CAPTION, CLASS, EXSTYLE, FONT, MENU, STYLE
107 // UseExtendedStatements flag (off by default) allows the parser to read
108 // the additional types of statements.
109 //
110 // Ref (to the list of all optional statements):
111 // msdn.microsoft.com/en-us/library/windows/desktop/aa381002(v=vs.85).aspx
112 Expected<OptionalStmtList>
113 parseOptionalStatements(bool UseExtendedStatements = false);
114
115 // Read a single optional statement.
116 Expected<std::unique_ptr<OptionalStmt>>
117 parseSingleOptionalStatement(bool UseExtendedStatements = false);
118
119 // Top-level resource parsers.
120 ParseType parseLanguageResource();
Marek Sokolowski72aa9372017-08-28 21:59:54 +0000121 ParseType parseCursorResource();
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +0000122 ParseType parseIconResource();
Marek Sokolowski72aa9372017-08-28 21:59:54 +0000123 ParseType parseHTMLResource();
Marek Sokolowski5cd3d5c2017-08-18 18:24:17 +0000124 ParseType parseStringTableResource();
125
126 // Optional statement parsers.
127 ParseOptionType parseLanguageStmt();
128 ParseOptionType parseCharacteristicsStmt();
129 ParseOptionType parseVersionStmt();
130
131 // Raises an error. If IsAlreadyRead = false (default), this complains about
132 // the token that couldn't be parsed. If the flag is on, this complains about
133 // the correctly read token that makes no sense (that is, the current parser
134 // state is beyond the erroneous token.)
135 Error getExpectedError(const Twine Message, bool IsAlreadyRead = false);
136
137 std::vector<RCToken> Tokens;
138 LocIter CurLoc;
139 const LocIter End;
140};
141
142} // namespace rc
143} // namespace llvm
144
145#endif