blob: 6846e096848ca4b92a97fa31b564a34135f26249 [file] [log] [blame]
Marek Sokolowski719e22d2017-08-10 16:21:44 +00001//===-- ResourceScriptToken.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 declares the .rc script tokens and defines an interface for tokenizing
11// the input data. The list of available tokens is located at
12// ResourceScriptTokenList.h.
13//
14// Note that the tokenizer does not support comments or preprocessor
15// directives. The preprocessor should do its work on the .rc file before
16// running llvm-rc.
17//
18// As for now, it is possible to parse ASCII files only (the behavior on
19// UTF files might be undefined). However, it already consumes UTF-8 BOM, if
20// there is any. Thus, ASCII-compatible UTF-8 files are tokenized correctly.
21//
22// Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380599(v=vs.85).aspx
23//
24//===---------------------------------------------------------------------===//
25
26#ifndef LLVM_TOOLS_LLVMRC_RESOURCESCRIPTTOKEN_H
27#define LLVM_TOOLS_LLVMRC_RESOURCESCRIPTTOKEN_H
28
29#include "llvm/ADT/StringRef.h"
30#include "llvm/Support/Error.h"
31
32#include <cstdint>
33#include <map>
34#include <string>
35#include <vector>
36
37namespace llvm {
38
39// A definition of a single resource script token. Each token has its kind
40// (declared in ResourceScriptTokenList) and holds a value - a reference
41// representation of the token.
42// RCToken does not claim ownership on its value. A memory buffer containing
43// the token value should be stored in a safe place and cannot be freed
44// nor reallocated.
45class RCToken {
46public:
47 enum class Kind {
48#define TOKEN(Name) Name,
49#define SHORT_TOKEN(Name, Ch) Name,
50#include "ResourceScriptTokenList.h"
51#undef TOKEN
52#undef SHORT_TOKEN
53 };
54
55 RCToken(RCToken::Kind RCTokenKind, StringRef Value);
56
57 // Get an integer value of the integer token.
58 uint32_t intValue() const;
59
60 StringRef value() const;
61 Kind kind() const;
62
Marek Sokolowski7e89ee72017-09-28 23:53:25 +000063 // Check if a token describes a binary operator.
64 bool isBinaryOp() const;
65
Marek Sokolowski719e22d2017-08-10 16:21:44 +000066private:
67 Kind TokenKind;
68 StringRef TokenValue;
69};
70
71// Tokenize Input.
72// In case no error occured, the return value contains
73// tokens in order they were in the input file.
74// In case of any error, the return value contains
75// a textual representation of error.
76//
77// Tokens returned by this function hold only references to the parts
78// of the Input. Memory buffer containing Input cannot be freed,
79// modified or reallocated.
80Expected<std::vector<RCToken>> tokenizeRC(StringRef Input);
81
82} // namespace llvm
83
84#endif