aboutsummaryrefslogtreecommitdiff
blob: 9eb65c0aa0052d04e3228c57273d6a7cfe99d81e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
   Please use git log for copyright holder and year information

   This file is part of libbash.

   libbash is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 2 of the License, or
   (at your option) any later version.

   libbash is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with libbash.  If not, see <http://www.gnu.org/licenses/>.
*/
///
/// \file builtin_exceptions.h
/// \brief implementations for builtin exceptions
///

#ifndef LIBBASH_BUILTINS_BUILTIN_EXCEPTIONS_H_
#define LIBBASH_BUILTINS_BUILTIN_EXCEPTIONS_H_

#include <stdexcept>

#include "exceptions.h"

///
/// \class return_exception
/// \brief thrown when executing the return builtin
///
class return_exception: public std::runtime_error
{
public:
  explicit return_exception():
    runtime_error("return exception"){}
};

///
/// \class loop_control_exception
/// \brief base class used by break and continue
///
class loop_control_exception
{
  int count;

  virtual void rethrow() = 0;

protected:
  virtual ~loop_control_exception() {}

public:
  /// \brief the construtor
  /// \param c the nested block counter
  explicit loop_control_exception(int c): count(c) {}

  /// \brief rethrow the exception if the counter is greater than 1
  void rethrow_unless_correct_frame()
  {
    if(count != 1)
    {
      --count;
      rethrow();
    }
  }
};

///
/// \class continue_exception
/// \brief thrown when executing the continue builtin
///
class continue_exception: public loop_control_exception
{
protected:
  /// \brief rethrow the exception itself
  virtual void rethrow()
  {
    throw *this;
  }

public:
  /// \brief the constructor of the exception
  /// \param c the nested block counter
  explicit continue_exception(int c): loop_control_exception(c) {
    if(c < 1)
      throw libbash::illegal_argument_exception("continue: argument should be greater than or equal to 1");
  }
};

///
/// \class break_exception
/// \brief thrown when executing the break builtin
///
class break_exception: public loop_control_exception
{
protected:
  /// \brief rethrow the exception itself
  virtual void rethrow()
  {
    throw *this;
  }

public:
  /// \brief the constructor of the exception
  /// \param c the nested block counter
  explicit break_exception(int c): loop_control_exception(c) {
    if(c < 1)
      throw libbash::illegal_argument_exception("break: argument should be greater than or equal to 1");
  }
};

class suppress_output: public std::exception
{
};
#endif