1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 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 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_GL_COMPILER_PREPROCESSOR_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_GL_COMPILER_PREPROCESSOR_H_ 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 23 #include "absl/strings/string_view.h" 24 #include "tensorflow/lite/delegates/gpu/common/status.h" 25 26 namespace tflite { 27 namespace gpu { 28 namespace gl { 29 30 enum class RewriteStatus { 31 SUCCESS = 0, 32 NOT_RECOGNIZED = 1, 33 ERROR = 2, 34 }; 35 36 // Inline rewrite matches a string and rewrites it. 37 class InlineRewrite { 38 public: 39 virtual ~InlineRewrite() = default; 40 41 virtual RewriteStatus Rewrite(absl::string_view input, 42 std::string* output) = 0; 43 }; 44 45 // Text preprocessor runs a collection of registered rewrites. 46 // It uses a single character prefix as inline delimiter that needs to quote 47 // text to be rewritten. 48 class TextPreprocessor { 49 public: 50 // @param keep_unknown_rewrites if true, will keep unhandled rewrites as is 51 // instead of reporting an error. TextPreprocessor(char inline_delimiter,bool keep_unknown_rewrites)52 TextPreprocessor(char inline_delimiter, bool keep_unknown_rewrites) 53 : inline_delimiter_(inline_delimiter), 54 keep_unknown_rewrites_(keep_unknown_rewrites) {} 55 AddRewrite(InlineRewrite * rewrite)56 void AddRewrite(InlineRewrite* rewrite) { 57 inline_rewrites_.push_back(rewrite); 58 } 59 60 // input and output may point to the same object. 61 absl::Status Rewrite(const std::string& input, std::string* output); 62 63 private: 64 const char inline_delimiter_; 65 const bool keep_unknown_rewrites_; 66 67 std::vector<InlineRewrite*> inline_rewrites_; 68 }; 69 70 } // namespace gl 71 } // namespace gpu 72 } // namespace tflite 73 74 #endif // TENSORFLOW_LITE_DELEGATES_GPU_GL_COMPILER_PREPROCESSOR_H_ 75