2017-08-19 02:24:17 +08:00
|
|
|
//===-- ResourceScriptParser.h ----------------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This defines the RC scripts parser. It takes a sequence of RC tokens
|
|
|
|
// and then provides the method to parse the resources one by one.
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_TOOLS_LLVMRC_RESOURCESCRIPTPARSER_H
|
|
|
|
#define LLVM_TOOLS_LLVMRC_RESOURCESCRIPTPARSER_H
|
|
|
|
|
|
|
|
#include "ResourceScriptStmt.h"
|
|
|
|
#include "ResourceScriptToken.h"
|
|
|
|
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
#include <system_error>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
2017-10-12 04:12:09 +08:00
|
|
|
namespace opt {
|
|
|
|
class InputArgList;
|
|
|
|
}
|
2017-08-19 02:24:17 +08:00
|
|
|
namespace rc {
|
|
|
|
|
|
|
|
class RCParser {
|
|
|
|
public:
|
|
|
|
using LocIter = std::vector<RCToken>::iterator;
|
|
|
|
using ParseType = Expected<std::unique_ptr<RCResource>>;
|
|
|
|
using ParseOptionType = Expected<std::unique_ptr<OptionalStmt>>;
|
|
|
|
|
|
|
|
// Class describing a single failure of parser.
|
|
|
|
class ParserError : public ErrorInfo<ParserError> {
|
|
|
|
public:
|
2017-10-10 02:50:29 +08:00
|
|
|
ParserError(const Twine &Expected, const LocIter CurLoc, const LocIter End);
|
2017-08-19 02:24:17 +08:00
|
|
|
|
|
|
|
void log(raw_ostream &OS) const override { OS << CurMessage; }
|
|
|
|
std::error_code convertToErrorCode() const override {
|
|
|
|
return std::make_error_code(std::errc::invalid_argument);
|
|
|
|
}
|
|
|
|
const std::string &getMessage() const { return CurMessage; }
|
|
|
|
|
|
|
|
static char ID; // Keep llvm::Error happy.
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string CurMessage;
|
|
|
|
LocIter ErrorLoc, FileEnd;
|
|
|
|
};
|
|
|
|
|
2017-10-12 04:12:09 +08:00
|
|
|
explicit RCParser(std::vector<RCToken> TokenList);
|
2017-08-19 02:24:17 +08:00
|
|
|
|
|
|
|
// Reads and returns a single resource definition, or error message if any
|
|
|
|
// occurred.
|
|
|
|
ParseType parseSingleResource();
|
|
|
|
|
|
|
|
bool isEof() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
using Kind = RCToken::Kind;
|
|
|
|
|
|
|
|
// Checks if the current parser state points to the token of type TokenKind.
|
|
|
|
bool isNextTokenKind(Kind TokenKind) const;
|
|
|
|
|
|
|
|
// These methods assume that the parser is not in EOF state.
|
|
|
|
|
|
|
|
// Take a look at the current token. Do not fetch it.
|
|
|
|
const RCToken &look() const;
|
|
|
|
// Read the current token and advance the state by one token.
|
|
|
|
const RCToken &read();
|
|
|
|
// Advance the state by one token, discarding the current token.
|
|
|
|
void consume();
|
|
|
|
|
|
|
|
// The following methods try to read a single token, check if it has the
|
|
|
|
// correct type and then parse it.
|
2017-09-29 07:53:25 +08:00
|
|
|
// Each integer can be written as an arithmetic expression producing an
|
|
|
|
// unsigned 32-bit integer.
|
2017-10-07 05:26:06 +08:00
|
|
|
Expected<RCInt> readInt(); // Parse an integer.
|
2017-08-29 06:58:31 +08:00
|
|
|
Expected<StringRef> readString(); // Parse a string.
|
|
|
|
Expected<StringRef> readIdentifier(); // Parse an identifier.
|
2018-05-08 16:47:37 +08:00
|
|
|
Expected<StringRef> readFilename(); // Parse a filename.
|
2017-08-29 06:58:31 +08:00
|
|
|
Expected<IntOrString> readIntOrString(); // Parse an integer or a string.
|
|
|
|
Expected<IntOrString> readTypeOrName(); // Parse an integer or an identifier.
|
2017-08-19 02:24:17 +08:00
|
|
|
|
2017-09-29 07:53:25 +08:00
|
|
|
// Helper integer expression parsing methods.
|
2017-10-07 05:26:06 +08:00
|
|
|
Expected<RCInt> parseIntExpr1();
|
|
|
|
Expected<RCInt> parseIntExpr2();
|
2017-09-29 07:53:25 +08:00
|
|
|
|
2017-08-19 02:24:17 +08:00
|
|
|
// Advance the state by one, discarding the current token.
|
|
|
|
// If the discarded token had an incorrect type, fail.
|
|
|
|
Error consumeType(Kind TokenKind);
|
|
|
|
|
|
|
|
// Check the current token type. If it's TokenKind, discard it.
|
|
|
|
// Return true if the parser consumed this token successfully.
|
|
|
|
bool consumeOptionalType(Kind TokenKind);
|
|
|
|
|
|
|
|
// Read at least MinCount, and at most MaxCount integers separated by
|
|
|
|
// commas. The parser stops reading after fetching MaxCount integers
|
|
|
|
// or after an error occurs. Whenever the parser reads a comma, it
|
|
|
|
// expects an integer to follow.
|
2017-10-07 05:26:06 +08:00
|
|
|
Expected<SmallVector<RCInt, 8>> readIntsWithCommas(size_t MinCount,
|
|
|
|
size_t MaxCount);
|
2017-08-19 02:24:17 +08:00
|
|
|
|
2017-08-29 06:58:31 +08:00
|
|
|
// Read an unknown number of flags preceded by commas. Each correct flag
|
|
|
|
// has an entry in FlagDesc array of length NumFlags. In case i-th
|
2017-09-30 01:46:32 +08:00
|
|
|
// flag (0-based) has been read, the result is OR-ed with FlagValues[i].
|
2017-08-29 06:58:31 +08:00
|
|
|
// As long as parser has a comma to read, it expects to be fed with
|
|
|
|
// a correct flag afterwards.
|
2017-09-30 01:46:32 +08:00
|
|
|
Expected<uint32_t> parseFlags(ArrayRef<StringRef> FlagDesc,
|
|
|
|
ArrayRef<uint32_t> FlagValues);
|
2017-08-29 06:58:31 +08:00
|
|
|
|
2017-08-19 02:24:17 +08:00
|
|
|
// Reads a set of optional statements. These can change the behavior of
|
|
|
|
// a number of resource types (e.g. STRINGTABLE, MENU or DIALOG) if provided
|
|
|
|
// before the main block with the contents of the resource.
|
|
|
|
// Usually, resources use a basic set of optional statements:
|
|
|
|
// CHARACTERISTICS, LANGUAGE, VERSION
|
|
|
|
// However, DIALOG and DIALOGEX extend this list by the following items:
|
|
|
|
// CAPTION, CLASS, EXSTYLE, FONT, MENU, STYLE
|
|
|
|
// UseExtendedStatements flag (off by default) allows the parser to read
|
|
|
|
// the additional types of statements.
|
|
|
|
//
|
|
|
|
// Ref (to the list of all optional statements):
|
|
|
|
// msdn.microsoft.com/en-us/library/windows/desktop/aa381002(v=vs.85).aspx
|
2017-10-07 04:51:20 +08:00
|
|
|
enum class OptStmtType { BasicStmt, DialogStmt, DialogExStmt };
|
|
|
|
|
2018-05-15 14:35:29 +08:00
|
|
|
uint16_t parseMemoryFlags(uint16_t DefaultFlags);
|
|
|
|
|
2017-08-19 02:24:17 +08:00
|
|
|
Expected<OptionalStmtList>
|
2017-10-07 04:51:20 +08:00
|
|
|
parseOptionalStatements(OptStmtType StmtsType = OptStmtType::BasicStmt);
|
2017-08-19 02:24:17 +08:00
|
|
|
|
|
|
|
// Read a single optional statement.
|
|
|
|
Expected<std::unique_ptr<OptionalStmt>>
|
2017-10-07 04:51:20 +08:00
|
|
|
parseSingleOptionalStatement(OptStmtType StmtsType = OptStmtType::BasicStmt);
|
2017-08-19 02:24:17 +08:00
|
|
|
|
|
|
|
// Top-level resource parsers.
|
|
|
|
ParseType parseLanguageResource();
|
2017-08-29 06:58:31 +08:00
|
|
|
ParseType parseAcceleratorsResource();
|
2018-05-08 04:27:37 +08:00
|
|
|
ParseType parseBitmapResource();
|
2017-08-29 05:59:54 +08:00
|
|
|
ParseType parseCursorResource();
|
2017-08-30 00:49:59 +08:00
|
|
|
ParseType parseDialogResource(bool IsExtended);
|
2017-08-19 02:24:17 +08:00
|
|
|
ParseType parseIconResource();
|
2017-08-29 05:59:54 +08:00
|
|
|
ParseType parseHTMLResource();
|
2017-08-29 07:46:30 +08:00
|
|
|
ParseType parseMenuResource();
|
2017-08-19 02:24:17 +08:00
|
|
|
ParseType parseStringTableResource();
|
2017-09-29 08:14:18 +08:00
|
|
|
ParseType parseUserDefinedResource(IntOrString Type);
|
2017-09-29 06:41:38 +08:00
|
|
|
ParseType parseVersionInfoResource();
|
2017-08-19 02:24:17 +08:00
|
|
|
|
2017-08-30 00:49:59 +08:00
|
|
|
// Helper DIALOG parser - a single control.
|
|
|
|
Expected<Control> parseControl();
|
|
|
|
|
2017-08-29 07:46:30 +08:00
|
|
|
// Helper MENU parser.
|
|
|
|
Expected<MenuDefinitionList> parseMenuItemsList();
|
|
|
|
|
2017-09-29 06:41:38 +08:00
|
|
|
// Helper VERSIONINFO parser - read the contents of a single BLOCK statement,
|
|
|
|
// from BEGIN to END.
|
|
|
|
Expected<std::unique_ptr<VersionInfoBlock>>
|
|
|
|
parseVersionInfoBlockContents(StringRef BlockName);
|
|
|
|
// Helper VERSIONINFO parser - read either VALUE or BLOCK statement.
|
|
|
|
Expected<std::unique_ptr<VersionInfoStmt>> parseVersionInfoStmt();
|
|
|
|
// Helper VERSIONINFO parser - read fixed VERSIONINFO statements.
|
|
|
|
Expected<VersionInfoResource::VersionInfoFixed> parseVersionInfoFixed();
|
|
|
|
|
2017-08-19 02:24:17 +08:00
|
|
|
// Optional statement parsers.
|
|
|
|
ParseOptionType parseLanguageStmt();
|
|
|
|
ParseOptionType parseCharacteristicsStmt();
|
|
|
|
ParseOptionType parseVersionStmt();
|
2017-08-30 00:49:59 +08:00
|
|
|
ParseOptionType parseCaptionStmt();
|
2018-05-16 03:21:28 +08:00
|
|
|
ParseOptionType parseClassStmt();
|
2017-10-07 04:51:20 +08:00
|
|
|
ParseOptionType parseFontStmt(OptStmtType DialogType);
|
2017-08-30 00:49:59 +08:00
|
|
|
ParseOptionType parseStyleStmt();
|
2017-08-19 02:24:17 +08:00
|
|
|
|
|
|
|
// Raises an error. If IsAlreadyRead = false (default), this complains about
|
|
|
|
// the token that couldn't be parsed. If the flag is on, this complains about
|
|
|
|
// the correctly read token that makes no sense (that is, the current parser
|
|
|
|
// state is beyond the erroneous token.)
|
2017-10-10 02:50:29 +08:00
|
|
|
Error getExpectedError(const Twine &Message, bool IsAlreadyRead = false);
|
2017-08-19 02:24:17 +08:00
|
|
|
|
|
|
|
std::vector<RCToken> Tokens;
|
|
|
|
LocIter CurLoc;
|
|
|
|
const LocIter End;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rc
|
|
|
|
} // namespace llvm
|
|
|
|
|
|
|
|
#endif
|