blob: 339018b1e616fe0d22c925c9260a0e1afbe0c6d2 [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
David Blaikieb961d292017-11-21 00:23:19 +000012// ResourceScriptTokenList.def.
Marek Sokolowski719e22d2017-08-10 16:21:44 +000013//
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,
David Blaikieb961d292017-11-21 00:23:19 +000050#include "ResourceScriptTokenList.def"
Marek Sokolowski719e22d2017-08-10 16:21:44 +000051 };
52
53 RCToken(RCToken::Kind RCTokenKind, StringRef Value);
54
55 // Get an integer value of the integer token.
56 uint32_t intValue() const;
Zachary Turner07bc04f2017-10-06 21:26:06 +000057 bool isLongInt() const;
Marek Sokolowski719e22d2017-08-10 16:21:44 +000058
59 StringRef value() const;
60 Kind kind() const;
61
Marek Sokolowski7e89ee72017-09-28 23:53:25 +000062 // Check if a token describes a binary operator.
63 bool isBinaryOp() const;
64
Marek Sokolowski719e22d2017-08-10 16:21:44 +000065private:
66 Kind TokenKind;
67 StringRef TokenValue;
68};
69
70// Tokenize Input.
Malcolm Parsons21e545d2018-01-24 10:33:39 +000071// In case no error occurred, the return value contains
Marek Sokolowski719e22d2017-08-10 16:21:44 +000072// tokens in order they were in the input file.
73// In case of any error, the return value contains
74// a textual representation of error.
75//
76// Tokens returned by this function hold only references to the parts
77// of the Input. Memory buffer containing Input cannot be freed,
78// modified or reallocated.
79Expected<std::vector<RCToken>> tokenizeRC(StringRef Input);
80
81} // namespace llvm
82
83#endif