| 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 | #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) |
| 25 | #pragma GCC system_header |
| 26 | #endif |
| 27 | |
| 28 | #include "../common.h" |
| 29 | #include <kj/string.h> |
| 30 | #include <kj/exception.h> |
| 31 | #include <kj/vector.h> |
| 32 | #include <kj/filesystem.h> |
| 33 | |
| 34 | namespace capnp { |
| 35 | namespace compiler { |
| 36 | |
| 37 | class ErrorReporter { |
| 38 | // Callback for reporting errors within a particular file. |
| 39 | |
| 40 | public: |
| 41 | virtual void addError(uint32_t startByte, uint32_t endByte, kj::StringPtr message) = 0; |
| 42 | // Report an error at the given location in the input text. `startByte` and `endByte` indicate |
| 43 | // the span of text that is erroneous. They may be equal, in which case the parser was only |
| 44 | // able to identify where the error begins, not where it ends. |
| 45 | |
| 46 | template <typename T> |
| 47 | inline void addErrorOn(T&& decl, kj::StringPtr message) { |
| 48 | // Works for any `T` that defines `getStartByte()` and `getEndByte()` methods, which many |
| 49 | // of the Cap'n Proto types defined in `grammar.capnp` do. |
| 50 | |
| 51 | addError(decl.getStartByte(), decl.getEndByte(), message); |
| 52 | } |
| 53 | |
| 54 | virtual bool hadErrors() = 0; |
| 55 | // Return true if any errors have been reported, globally. The main use case for this callback |
| 56 | // is to inhibit the reporting of errors which may have been caused by previous errors, or to |
| 57 | // allow the compiler to bail out entirely if it gets confused and thinks this could be because |
| 58 | // of previous errors. |
| 59 | }; |
| 60 | |
| 61 | class GlobalErrorReporter { |
| 62 | // Callback for reporting errors in any file. |
| 63 | |
| 64 | public: |
| 65 | struct SourcePos { |
| 66 | uint byte; |
| 67 | uint line; |
| 68 | uint column; |
| 69 | }; |
| 70 | |
| 71 | virtual void addError(const kj::ReadableDirectory& directory, kj::PathPtr path, |
| 72 | SourcePos start, SourcePos end, |
| 73 | kj::StringPtr message) = 0; |
| 74 | // Report an error at the given location in the given file. |
| 75 | |
| 76 | virtual bool hadErrors() = 0; |
| 77 | // Return true if any errors have been reported, globally. The main use case for this callback |
| 78 | // is to inhibit the reporting of errors which may have been caused by previous errors, or to |
| 79 | // allow the compiler to bail out entirely if it gets confused and thinks this could be because |
| 80 | // of previous errors. |
| 81 | }; |
| 82 | |
| 83 | class LineBreakTable { |
| 84 | public: |
| 85 | LineBreakTable(kj::ArrayPtr<const char> content); |
| 86 | |
| 87 | GlobalErrorReporter::SourcePos toSourcePos(uint32_t byteOffset) const; |
| 88 | |
| 89 | private: |
| 90 | kj::Vector<uint> lineBreaks; |
| 91 | // Byte offsets of the first byte in each source line. The first element is always zero. |
| 92 | // Initialized the first time the module is loaded. |
| 93 | }; |
| 94 | |
| 95 | } // namespace compiler |
| 96 | } // namespace capnp |
| 97 | |