error-reporter.h 3.61 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
1 2
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
3
//
Kenton Varda's avatar
Kenton Varda committed
4 5 6 7 8 9
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
10
//
Kenton Varda's avatar
Kenton Varda committed
11 12
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
13
//
Kenton Varda's avatar
Kenton Varda committed
14 15 16 17 18 19 20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
21 22 23 24

#ifndef ERROR_REPORTER_H_
#define ERROR_REPORTER_H_

25
#if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
26 27 28
#pragma GCC system_header
#endif

29 30
#include "../common.h"
#include <kj/string.h>
31
#include <kj/exception.h>
32
#include <kj/vector.h>
33 34 35 36 37

namespace capnp {
namespace compiler {

class ErrorReporter {
38 39
  // Callback for reporting errors within a particular file.

40
public:
41
  virtual void addError(uint32_t startByte, uint32_t endByte, kj::StringPtr message) = 0;
42 43 44
  // Report an error at the given location in the input text.  `startByte` and `endByte` indicate
  // the span of text that is erroneous.  They may be equal, in which case the parser was only
  // able to identify where the error begins, not where it ends.
Kenton Varda's avatar
Kenton Varda committed
45 46

  template <typename T>
47
  inline void addErrorOn(T&& decl, kj::StringPtr message) {
Kenton Varda's avatar
Kenton Varda committed
48 49 50 51 52
    // Works for any `T` that defines `getStartByte()` and `getEndByte()` methods, which many
    // of the Cap'n Proto types defined in `grammar.capnp` do.

    addError(decl.getStartByte(), decl.getEndByte(), message);
  }
53

54
  virtual bool hadErrors() = 0;
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  // Return true if any errors have been reported, globally.  The main use case for this callback
  // is to inhibit the reporting of errors which may have been caused by previous errors, or to
  // allow the compiler to bail out entirely if it gets confused and thinks this could be because
  // of previous errors.
};

class GlobalErrorReporter {
  // Callback for reporting errors in any file.

public:
  struct SourcePos {
    uint byte;
    uint line;
    uint column;
  };

  virtual void addError(kj::StringPtr file, SourcePos start, SourcePos end,
72
                        kj::StringPtr message) = 0;
73 74
  // Report an error at the given location in the given file.

75
  virtual bool hadErrors() = 0;
76 77 78 79
  // Return true if any errors have been reported, globally.  The main use case for this callback
  // is to inhibit the reporting of errors which may have been caused by previous errors, or to
  // allow the compiler to bail out entirely if it gets confused and thinks this could be because
  // of previous errors.
80 81
};

82 83 84 85 86 87 88 89 90 91 92 93
class LineBreakTable {
public:
  LineBreakTable(kj::ArrayPtr<const char> content);

  GlobalErrorReporter::SourcePos toSourcePos(uint32_t byteOffset) const;

private:
  kj::Vector<uint> lineBreaks;
  // Byte offsets of the first byte in each source line.  The first element is always zero.
  // Initialized the first time the module is loaded.
};

94 95 96 97
}  // namespace compiler
}  // namespace capnp

#endif  // ERROR_REPORTER_H_