source_annotation.hpp

1
2
3
4
5
6
7
8
9
#pragma once


#include <felspar/test/source.hpp>
#include <string>
#include <utility>


namespace felspar::exceptions {

Capture the source annotation for this exception

13
14
15
16
17
    template<typename SC>
    class source_annotation : public SC {
        felspar::source_location loc;

      public:

Return the throw site source code location information

19
20
21
        auto const &thrown_from() const { return loc; }

      protected:

Used by sub-classes that need to pass arguments to the actual exception constructor. Note that we need to pass in the source location first.

25
26
27
        template<typename... Args>
        source_annotation(source_location loc, Args... args)
        : SC{std::forward<Args>(args)...}, loc{std::move(loc)} {}

Used by sub-classes that take a user-define error message to include the source location information in the message

31
32
33
34
35
36
37
38
39
40
41
42
43
44
        static std::string
                annotate(std::string m, felspar::source_location loc) {
            return m + "\n" + loc.file_name() + ":" + std::to_string(loc.line())
                    + ":" + std::to_string(loc.column());
        }
        static std::string annotate(felspar::source_location loc) {
            return std::string{loc.file_name()} + ":"
                    + std::to_string(loc.line()) + ":"
                    + std::to_string(loc.column());
        }
    };


}