blob: d1a2ae7a41bbfeb83600d119d9b840922cea974a [file] [log] [blame]
Marek Sokolowski6c9cbed2017-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 Blaikiebb5acf92017-11-21 00:23:19 +000012// ResourceScriptTokenList.def.
Marek Sokolowski6c9cbed2017-08-10 16:21:44 +000013//
Martin Storsjo9affe202018-05-08 12:33:54 +000014// Note that the tokenizer does not support preprocessor directives. The
15// preprocessor should do its work on the .rc file before running llvm-rc.
Marek Sokolowski6c9cbed2017-08-10 16:21:44 +000016//
17// As for now, it is possible to parse ASCII files only (the behavior on
18// UTF files might be undefined). However, it already consumes UTF-8 BOM, if
19// there is any. Thus, ASCII-compatible UTF-8 files are tokenized correctly.
20//
21// Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380599(v=vs.85).aspx
22//
23//===---------------------------------------------------------------------===//
24
25#ifndef LLVM_TOOLS_LLVMRC_RESOURCESCRIPTTOKEN_H
26#define LLVM_TOOLS_LLVMRC_RESOURCESCRIPTTOKEN_H
27
28#include "llvm/ADT/StringRef.h"
29#include "llvm/Support/Error.h"
30
31#include <cstdint>
32#include <map>
33#include <string>
34#include <vector>
35
36namespace llvm {
37
38// A definition of a single resource script token. Each token has its kind
39// (declared in ResourceScriptTokenList) and holds a value - a reference
40// representation of the token.
41// RCToken does not claim ownership on its value. A memory buffer containing
42// the token value should be stored in a safe place and cannot be freed
43// nor reallocated.
44class RCToken {
45public:
46 enum class Kind {
47#define TOKEN(Name) Name,
48#define SHORT_TOKEN(Name, Ch) Name,
David Blaikiebb5acf92017-11-21 00:23:19 +000049#include "ResourceScriptTokenList.def"
Marek Sokolowski6c9cbed2017-08-10 16:21:44 +000050 };
51
52 RCToken(RCToken::Kind RCTokenKind, StringRef Value);
53
54 // Get an integer value of the integer token.
55 uint32_t intValue() const;
Zachary Turner93bb30d2017-10-06 21:26:06 +000056 bool isLongInt() const;
Marek Sokolowski6c9cbed2017-08-10 16:21:44 +000057
58 StringRef value() const;
59 Kind kind() const;
60
Marek Sokolowski59066482017-09-28 23:53:25 +000061 // Check if a token describes a binary operator.
62 bool isBinaryOp() const;
63
Marek Sokolowski6c9cbed2017-08-10 16:21:44 +000064private:
65 Kind TokenKind;
66 StringRef TokenValue;
67};
68
69// Tokenize Input.
Malcolm Parsons5fc96282018-01-24 10:33:39 +000070// In case no error occurred, the return value contains
Marek Sokolowski6c9cbed2017-08-10 16:21:44 +000071// tokens in order they were in the input file.
72// In case of any error, the return value contains
73// a textual representation of error.
74//
75// Tokens returned by this function hold only references to the parts
76// of the Input. Memory buffer containing Input cannot be freed,
77// modified or reallocated.
78Expected<std::vector<RCToken>> tokenizeRC(StringRef Input);
79
80} // namespace llvm
81
82#endif