Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Ben Clayton | 7fd1c2a | 2023-08-01 00:37:35 +0000 | [diff] [blame] | 15 | #ifndef SRC_TINT_UTILS_GENERATOR_TEXT_GENERATOR_H_ |
| 16 | #define SRC_TINT_UTILS_GENERATOR_TEXT_GENERATOR_H_ |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 17 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <unordered_map> |
| 20 | #include <utility> |
| 21 | #include <vector> |
| 22 | |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 23 | #include "src/tint/utils/diagnostic/diagnostic.h" |
| 24 | #include "src/tint/utils/text/string_stream.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 25 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 26 | namespace tint { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 27 | |
| 28 | /// Helper methods for generators which are creating text output |
| 29 | class TextGenerator { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 30 | public: |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 31 | /// LineInfo holds a single line of text |
| 32 | struct LineInfo { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 33 | /// The indentation of the line in blankspace |
| 34 | uint32_t indent = 0; |
| 35 | /// The content of the line, without a trailing newline character |
| 36 | std::string content; |
| 37 | }; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 38 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 39 | /// TextBuffer holds a list of lines of text. |
| 40 | struct TextBuffer { |
| 41 | // Constructor |
| 42 | TextBuffer(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 43 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 44 | // Destructor |
| 45 | ~TextBuffer(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 46 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 47 | /// IncrementIndent increases the indentation of lines that will be written |
| 48 | /// to the TextBuffer |
| 49 | void IncrementIndent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 50 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 51 | /// DecrementIndent decreases the indentation of lines that will be written |
| 52 | /// to the TextBuffer |
| 53 | void DecrementIndent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 54 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 55 | /// Appends the line to the end of the TextBuffer |
| 56 | /// @param line the line to append to the TextBuffer |
| 57 | void Append(const std::string& line); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 58 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 59 | /// Inserts the line to the TextBuffer before the line with index `before` |
| 60 | /// @param line the line to append to the TextBuffer |
| 61 | /// @param before the zero-based index of the line to insert the text before |
| 62 | /// @param indent the indentation to apply to the inserted lines |
| 63 | void Insert(const std::string& line, size_t before, uint32_t indent); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 64 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 65 | /// Appends the lines of `tb` to the end of this TextBuffer |
| 66 | /// @param tb the TextBuffer to append to the end of this TextBuffer |
| 67 | void Append(const TextBuffer& tb); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 68 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 69 | /// Inserts the lines of `tb` to the TextBuffer before the line with index |
| 70 | /// `before` |
| 71 | /// @param tb the TextBuffer to insert into this TextBuffer |
| 72 | /// @param before the zero-based index of the line to insert the text before |
| 73 | /// @param indent the indentation to apply to the inserted lines |
| 74 | void Insert(const TextBuffer& tb, size_t before, uint32_t indent); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 75 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 76 | /// @returns the buffer's content as a single string |
| 77 | /// @param indent additional indentation to apply to each line |
| 78 | std::string String(uint32_t indent = 0) const; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 79 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 80 | /// The current indentation of the TextBuffer. Lines appended to the |
| 81 | /// TextBuffer will use this indentation. |
| 82 | uint32_t current_indent = 0; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 83 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 84 | /// The lines |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 85 | std::vector<LineInfo> lines; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 86 | }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 87 | /// LineWriter is a helper that acts as a string buffer, who's content is |
| 88 | /// emitted to the TextBuffer as a single line on destruction. |
| 89 | struct LineWriter { |
| 90 | public: |
| 91 | /// Constructor |
| 92 | /// @param buffer the TextBuffer that the LineWriter will append its |
| 93 | /// content to on destruction, at the end of the buffer. |
| 94 | explicit LineWriter(TextBuffer* buffer); |
| 95 | |
| 96 | /// Move constructor |
| 97 | /// @param rhs the LineWriter to move |
| 98 | LineWriter(LineWriter&& rhs); |
| 99 | /// Destructor |
| 100 | ~LineWriter(); |
| 101 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 102 | /// @returns the StringStream |
| 103 | operator StringStream&() { return os; } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 104 | |
| 105 | /// @param rhs the value to write to the line |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 106 | /// @returns the StringStream so calls can be chained |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 107 | template <typename T> |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 108 | StringStream& operator<<(T&& rhs) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 109 | return os << std::forward<T>(rhs); |
| 110 | } |
| 111 | |
| 112 | private: |
| 113 | LineWriter(const LineWriter&) = delete; |
| 114 | LineWriter& operator=(const LineWriter&) = delete; |
| 115 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 116 | StringStream os; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 117 | TextBuffer* buffer; |
| 118 | }; |
| 119 | |
dan sinclair | 9d8229b | 2023-06-26 16:35:07 +0000 | [diff] [blame] | 120 | /// Increment the emitter indent level |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 121 | void IncrementIndent() { current_buffer_->IncrementIndent(); } |
dan sinclair | 9d8229b | 2023-06-26 16:35:07 +0000 | [diff] [blame] | 122 | /// Decrement the emitter indent level |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 123 | void DecrementIndent() { current_buffer_->DecrementIndent(); } |
dan sinclair | 9d8229b | 2023-06-26 16:35:07 +0000 | [diff] [blame] | 124 | |
| 125 | /// @returns a new LineWriter, used for buffering and writing a line to |
| 126 | /// the end of #current_buffer_. |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 127 | LineWriter Line() { return LineWriter(current_buffer_); } |
| 128 | /// @param buffer the TextBuffer to write the line to |
| 129 | /// @returns a new LineWriter, used for buffering and writing a line to |
| 130 | /// the end of `buffer`. |
| 131 | static LineWriter Line(TextBuffer* buffer) { return LineWriter(buffer); } |
dan sinclair | 9d8229b | 2023-06-26 16:35:07 +0000 | [diff] [blame] | 132 | |
| 133 | /// @returns the result data |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 134 | virtual std::string Result() const { return main_buffer_.String(); } |
dan sinclair | 9d8229b | 2023-06-26 16:35:07 +0000 | [diff] [blame] | 135 | |
| 136 | /// @returns the list of diagnostics raised by the generator. |
| 137 | const diag::List& Diagnostics() const { return diagnostics_; } |
| 138 | |
| 139 | protected: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 140 | /// Helper for writing a '(' on construction and a ')' destruction. |
| 141 | struct ScopedParen { |
| 142 | /// Constructor |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 143 | /// @param stream the StringStream that will be written to |
| 144 | explicit ScopedParen(StringStream& stream); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 145 | /// Destructor |
| 146 | ~ScopedParen(); |
| 147 | |
| 148 | private: |
| 149 | ScopedParen(ScopedParen&& rhs) = delete; |
| 150 | ScopedParen(const ScopedParen&) = delete; |
| 151 | ScopedParen& operator=(const ScopedParen&) = delete; |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 152 | StringStream& s; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | /// Helper for incrementing indentation on construction and decrementing |
| 156 | /// indentation on destruction. |
| 157 | struct ScopedIndent { |
| 158 | /// Constructor |
| 159 | /// @param buffer the TextBuffer that the ScopedIndent will indent |
| 160 | explicit ScopedIndent(TextBuffer* buffer); |
| 161 | /// Constructor |
| 162 | /// @param generator ScopedIndent will indent the generator's |
| 163 | /// `current_buffer_` |
| 164 | explicit ScopedIndent(TextGenerator* generator); |
| 165 | /// Destructor |
| 166 | ~ScopedIndent(); |
| 167 | |
| 168 | private: |
| 169 | ScopedIndent(ScopedIndent&& rhs) = delete; |
| 170 | ScopedIndent(const ScopedIndent&) = delete; |
| 171 | ScopedIndent& operator=(const ScopedIndent&) = delete; |
| 172 | TextBuffer* buffer_; |
| 173 | }; |
| 174 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 175 | /// Constructor |
| 176 | TextGenerator(); |
| 177 | virtual ~TextGenerator(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 178 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 179 | /// Diagnostics generated by the generator |
| 180 | diag::List diagnostics_; |
| 181 | /// The buffer the TextGenerator is currently appending lines to |
| 182 | TextBuffer* current_buffer_ = &main_buffer_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 183 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 184 | /// The primary text buffer that the generator will emit |
| 185 | TextBuffer main_buffer_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 188 | } // namespace tint |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 189 | |
Ben Clayton | 7fd1c2a | 2023-08-01 00:37:35 +0000 | [diff] [blame] | 190 | #endif // SRC_TINT_UTILS_GENERATOR_TEXT_GENERATOR_H_ |