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 15 #ifndef SRC_READER_READER_H_ 16 #define SRC_READER_READER_H_ 17 18 #include <string> 19 20 #include "src/program.h" 21 22 namespace tint { 23 namespace reader { 24 25 /// Base class for input readers 26 class Reader { 27 public: 28 virtual ~Reader(); 29 30 /// Parses the input data 31 /// @returns true if the parse was successful 32 virtual bool Parse() = 0; 33 34 /// @returns true if an error was encountered. has_error()35 bool has_error() const { return diags_.contains_errors(); } 36 37 /// @returns the parser error string error()38 std::string error() const { 39 diag::Formatter formatter{{false, false, false, false}}; 40 return formatter.format(diags_); 41 } 42 43 /// @returns the full list of diagnostic messages. diagnostics()44 const diag::List& diagnostics() const { return diags_; } 45 46 /// @returns the program. The program builder in the parser will be reset 47 /// after this. 48 virtual Program program() = 0; 49 50 protected: 51 /// Constructor 52 Reader(); 53 54 /// Sets the diagnostic messages 55 /// @param diags the list of diagnostic messages set_diagnostics(const diag::List & diags)56 void set_diagnostics(const diag::List& diags) { diags_ = diags; } 57 58 /// All diagnostic messages from the reader. 59 diag::List diags_; 60 }; 61 62 } // namespace reader 63 } // namespace tint 64 65 #endif // SRC_READER_READER_H_ 66