Marek Sokolowski | 719e22d | 2017-08-10 16:21:44 +0000 | [diff] [blame] | 1 | //===-- ResourceScriptToken.h -----------------------------------*- C++-*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Marek Sokolowski | 719e22d | 2017-08-10 16:21:44 +0000 | [diff] [blame] | 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 |
David Blaikie | b961d29 | 2017-11-21 00:23:19 +0000 | [diff] [blame] | 11 | // ResourceScriptTokenList.def. |
Marek Sokolowski | 719e22d | 2017-08-10 16:21:44 +0000 | [diff] [blame] | 12 | // |
Martin Storsjo | 7562e34 | 2018-05-08 12:33:54 +0000 | [diff] [blame] | 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. |
Marek Sokolowski | 719e22d | 2017-08-10 16:21:44 +0000 | [diff] [blame] | 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 <string> |
| 33 | #include <vector> |
| 34 | |
| 35 | namespace llvm { |
| 36 | |
| 37 | // A definition of a single resource script token. Each token has its kind |
| 38 | // (declared in ResourceScriptTokenList) and holds a value - a reference |
| 39 | // representation of the token. |
| 40 | // RCToken does not claim ownership on its value. A memory buffer containing |
| 41 | // the token value should be stored in a safe place and cannot be freed |
| 42 | // nor reallocated. |
| 43 | class RCToken { |
| 44 | public: |
| 45 | enum class Kind { |
| 46 | #define TOKEN(Name) Name, |
| 47 | #define SHORT_TOKEN(Name, Ch) Name, |
David Blaikie | b961d29 | 2017-11-21 00:23:19 +0000 | [diff] [blame] | 48 | #include "ResourceScriptTokenList.def" |
Marek Sokolowski | 719e22d | 2017-08-10 16:21:44 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | RCToken(RCToken::Kind RCTokenKind, StringRef Value); |
| 52 | |
| 53 | // Get an integer value of the integer token. |
| 54 | uint32_t intValue() const; |
Zachary Turner | 07bc04f | 2017-10-06 21:26:06 +0000 | [diff] [blame] | 55 | bool isLongInt() const; |
Marek Sokolowski | 719e22d | 2017-08-10 16:21:44 +0000 | [diff] [blame] | 56 | |
| 57 | StringRef value() const; |
| 58 | Kind kind() const; |
| 59 | |
Marek Sokolowski | 7e89ee7 | 2017-09-28 23:53:25 +0000 | [diff] [blame] | 60 | // Check if a token describes a binary operator. |
| 61 | bool isBinaryOp() const; |
| 62 | |
Marek Sokolowski | 719e22d | 2017-08-10 16:21:44 +0000 | [diff] [blame] | 63 | private: |
| 64 | Kind TokenKind; |
| 65 | StringRef TokenValue; |
| 66 | }; |
| 67 | |
| 68 | // Tokenize Input. |
Malcolm Parsons | 21e545d | 2018-01-24 10:33:39 +0000 | [diff] [blame] | 69 | // In case no error occurred, the return value contains |
Marek Sokolowski | 719e22d | 2017-08-10 16:21:44 +0000 | [diff] [blame] | 70 | // tokens in order they were in the input file. |
| 71 | // In case of any error, the return value contains |
| 72 | // a textual representation of error. |
| 73 | // |
| 74 | // Tokens returned by this function hold only references to the parts |
| 75 | // of the Input. Memory buffer containing Input cannot be freed, |
| 76 | // modified or reallocated. |
| 77 | Expected<std::vector<RCToken>> tokenizeRC(StringRef Input); |
| 78 | |
| 79 | } // namespace llvm |
| 80 | |
| 81 | #endif |