| 1 | //===-- ResourceScriptToken.h -----------------------------------*- C++-*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===---------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This declares the .rc script tokens and defines an interface for tokenizing |
| 10 | // the input data. The list of available tokens is located at |
| 11 | // ResourceScriptTokenList.def. |
| 12 | // |
| 13 | // Note that the tokenizer does not support preprocessor directives. The |
| 14 | // preprocessor should do its work on the .rc file before running llvm-rc. |
| 15 | // |
| 16 | // As for now, it is possible to parse ASCII files only (the behavior on |
| 17 | // UTF files might be undefined). However, it already consumes UTF-8 BOM, if |
| 18 | // there is any. Thus, ASCII-compatible UTF-8 files are tokenized correctly. |
| 19 | // |
| 20 | // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380599(v=vs.85).aspx |
| 21 | // |
| 22 | //===---------------------------------------------------------------------===// |
| 23 | |
| 24 | #ifndef LLVM_TOOLS_LLVMRC_RESOURCESCRIPTTOKEN_H |
| 25 | #define LLVM_TOOLS_LLVMRC_RESOURCESCRIPTTOKEN_H |
| 26 | |
| 27 | #include "llvm/ADT/StringRef.h" |
| 28 | #include "llvm/Support/Error.h" |
| 29 | |
| 30 | #include <cstdint> |
| 31 | #include <map> |
| 32 | #include <vector> |
| 33 | |
| 34 | namespace llvm { |
| 35 | |
| 36 | // A definition of a single resource script token. Each token has its kind |
| 37 | // (declared in ResourceScriptTokenList) and holds a value - a reference |
| 38 | // representation of the token. |
| 39 | // RCToken does not claim ownership on its value. A memory buffer containing |
| 40 | // the token value should be stored in a safe place and cannot be freed |
| 41 | // nor reallocated. |
| 42 | class RCToken { |
| 43 | public: |
| 44 | enum class Kind { |
| 45 | #define TOKEN(Name) Name, |
| 46 | #define SHORT_TOKEN(Name, Ch) Name, |
| 47 | #include "ResourceScriptTokenList.def" |
| 48 | }; |
| 49 | |
| 50 | RCToken(RCToken::Kind RCTokenKind, StringRef Value); |
| 51 | |
| 52 | // Get an integer value of the integer token. |
| 53 | uint32_t intValue() const; |
| 54 | bool isLongInt() const; |
| 55 | |
| 56 | StringRef value() const; |
| 57 | Kind kind() const; |
| 58 | |
| 59 | // Check if a token describes a low precedence binary operator. |
| 60 | bool isLowPrecedenceBinaryOp() const; |
| 61 | |
| 62 | // Check if a token describes a high precedence binary operator. |
| 63 | bool isHighPrecedenceBinaryOp() const; |
| 64 | |
| 65 | private: |
| 66 | Kind TokenKind; |
| 67 | StringRef TokenValue; |
| 68 | }; |
| 69 | |
| 70 | // Tokenize Input. |
| 71 | // In case no error occurred, the return value contains |
| 72 | // 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. |
| 79 | Expected<std::vector<RCToken>> tokenizeRC(StringRef Input); |
| 80 | |
| 81 | } // namespace llvm |
| 82 | |
| 83 | #endif |
| 84 | |