error-reporter.h (3668B)
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors 2 // Licensed under the MIT License: 3 // 4 // Permission is hereby granted, free of charge, to any person obtaining a copy 5 // of this software and associated documentation files (the "Software"), to deal 6 // in the Software without restriction, including without limitation the rights 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 // copies of the Software, and to permit persons to whom the Software is 9 // furnished to do so, subject to the following conditions: 10 // 11 // The above copyright notice and this permission notice shall be included in 12 // all copies or substantial portions of the Software. 13 // 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 // THE SOFTWARE. 21 22 #pragma once 23 24 #include "../common.h" 25 #include <kj/string.h> 26 #include <kj/exception.h> 27 #include <kj/vector.h> 28 #include <kj/filesystem.h> 29 30 CAPNP_BEGIN_HEADER 31 32 namespace capnp { 33 namespace compiler { 34 35 class ErrorReporter { 36 // Callback for reporting errors within a particular file. 37 38 public: 39 virtual void addError(uint32_t startByte, uint32_t endByte, kj::StringPtr message) = 0; 40 // Report an error at the given location in the input text. `startByte` and `endByte` indicate 41 // the span of text that is erroneous. They may be equal, in which case the parser was only 42 // able to identify where the error begins, not where it ends. 43 44 template <typename T> 45 inline void addErrorOn(T&& decl, kj::StringPtr message) { 46 // Works for any `T` that defines `getStartByte()` and `getEndByte()` methods, which many 47 // of the Cap'n Proto types defined in `grammar.capnp` do. 48 49 addError(decl.getStartByte(), decl.getEndByte(), message); 50 } 51 52 virtual bool hadErrors() = 0; 53 // Return true if any errors have been reported, globally. The main use case for this callback 54 // is to inhibit the reporting of errors which may have been caused by previous errors, or to 55 // allow the compiler to bail out entirely if it gets confused and thinks this could be because 56 // of previous errors. 57 }; 58 59 class GlobalErrorReporter { 60 // Callback for reporting errors in any file. 61 62 public: 63 struct SourcePos { 64 uint byte; 65 uint line; 66 uint column; 67 }; 68 69 virtual void addError(const kj::ReadableDirectory& directory, kj::PathPtr path, 70 SourcePos start, SourcePos end, 71 kj::StringPtr message) = 0; 72 // Report an error at the given location in the given file. 73 74 virtual bool hadErrors() = 0; 75 // Return true if any errors have been reported, globally. The main use case for this callback 76 // is to inhibit the reporting of errors which may have been caused by previous errors, or to 77 // allow the compiler to bail out entirely if it gets confused and thinks this could be because 78 // of previous errors. 79 }; 80 81 class LineBreakTable { 82 public: 83 LineBreakTable(kj::ArrayPtr<const char> content); 84 85 GlobalErrorReporter::SourcePos toSourcePos(uint32_t byteOffset) const; 86 87 private: 88 kj::Vector<uint> lineBreaks; 89 // Byte offsets of the first byte in each source line. The first element is always zero. 90 // Initialized the first time the module is loaded. 91 }; 92 93 } // namespace compiler 94 } // namespace capnp 95 96 CAPNP_END_HEADER