Nearly CC
An educational compiler skeleton
exceptions.h
Go to the documentation of this file.
1 // Copyright (c) 2021-2023, David H. Hovemeyer <david.hovemeyer@gmail.com>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
17 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19 // OTHER DEALINGS IN THE SOFTWARE.
20 
21 #ifndef EXCEPTIONS_H
22 #define EXCEPTIONS_H
23 
24 #include <stdexcept>
25 #include "location.h"
26 
32 
34 // a semantic error, or a general runtime error.
35 class BaseException : public std::runtime_error {
36 private:
37  Location m_loc;
38 
39 public:
43  BaseException(const Location &loc, const std::string &desc);
44 
47  BaseException(const BaseException &other);
48 
49  virtual ~BaseException();
50 
54  bool has_location() const { return m_loc.is_valid(); }
55 
58  const Location &get_loc() const;
59 };
60 
61 #ifdef __GNUC__
62 # define RT_PRINTF_FORMAT __attribute__ ((format (printf, 1, 2), noreturn))
63 # define EX_PRINTF_FORMAT __attribute__ ((format (printf, 2, 3), noreturn))
64 #else
65 # define RT_PRINTF_FORMAT
66 # define EX_PRINTF_FORMAT
67 #endif
68 
71 class RuntimeError : public BaseException {
72 public:
75  RuntimeError(const std::string &desc);
76 
79  RuntimeError(const RuntimeError &other);
80 
81  virtual ~RuntimeError();
82 
87  static void raise(const char *fmt, ...) RT_PRINTF_FORMAT;
88 };
89 
91 class SyntaxError : public BaseException {
92 public:
96  SyntaxError(const Location &loc, const std::string &desc);
97 
100  SyntaxError(const SyntaxError &other);
101 
102  virtual ~SyntaxError();
103 
108  static void raise(const Location &loc, const char *fmt, ...) EX_PRINTF_FORMAT;
109 };
110 
112 class SemanticError : public BaseException {
113 public:
117  SemanticError(const Location &loc, const std::string &desc);
118 
121  SemanticError(const SemanticError &other);
122 
123  virtual ~SemanticError();
124 
129  static void raise(const Location &loc, const char *fmt, ...) EX_PRINTF_FORMAT;
130 };
131 
132 #endif // EXCEPTIONS_H
Base type for exceptions indicating a syntax error,.
Definition: exceptions.h:35
BaseException(const Location &loc, const std::string &desc)
Constructor.
Definition: exceptions.cpp:31
bool has_location() const
Check whether this exception has a valud source Location.
Definition: exceptions.h:54
const Location & get_loc() const
Get the source Location.
Definition: exceptions.cpp:44
Definition: location.h:26
Exception type for general runtime errors that aren't associated with source code.
Definition: exceptions.h:71
RuntimeError(const std::string &desc)
Constuctor.
Definition: exceptions.cpp:52
Exception type for semantic errors.
Definition: exceptions.h:112
SemanticError(const Location &loc, const std::string &desc)
Constructor.
Definition: exceptions.cpp:100
Exception type for lexical or syntax errors.
Definition: exceptions.h:91
SyntaxError(const Location &loc, const std::string &desc)
Constructor.
Definition: exceptions.cpp:76