Fabrique

A build language for complex systems

 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
exceptions.cc
Go to the documentation of this file.
1 
2 /*
3  * Copyright (c) 2013 Jonathan Anderson
4  * All rights reserved.
5  *
6  * This software was developed by SRI International and the University of
7  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8  * ("CTSRD"), as part of the DARPA CRASH research programme.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "Support/Bytestream.h"
33 #include "Support/ErrorReport.h"
34 #include "Support/exceptions.h"
35 
36 using namespace fabrique;
37 using std::string;
38 
39 
40 AssertionFailure::AssertionFailure(const string& condition, const string& msg)
41  : condition_(condition),
42  message_(msg.empty() ? ("Assertion failed: " + condition) : msg)
43 {
44 }
45 
46 AssertionFailure::AssertionFailure(const AssertionFailure& orig)
47  : condition_(orig.condition_), message_(orig.message_)
48 {
49 }
50 
51 AssertionFailure::~AssertionFailure() {}
52 
53 const char* AssertionFailure::what() const noexcept
54 {
55  return message_.c_str();
56 }
57 
58 
59 OSError::OSError(const string& message, const string& description)
60  : message_(message), description_(description),
61  completeMessage_(message + ": " + description)
62 {
63 }
64 
65 OSError::OSError(const OSError& orig)
66  : message_(orig.message_), description_(orig.description_),
67  completeMessage_(orig.completeMessage_)
68 {
69 }
70 
71 OSError::~OSError() {}
72 
73 void OSError::PrettyPrint(Bytestream& out, size_t /*indent*/) const
74 {
75  out
76  << Bytestream::Error << "OS error"
77  << Bytestream::Reset << ": " << message_ << ": "
78  << Bytestream::ErrorMessage << description_
79  << Bytestream::Reset
80  ;
81 }
82 
83 
84 UserError::UserError(const string& message)
85  : message_(message)
86 {
87 }
88 
89 UserError::UserError(const UserError& orig)
90  : message_(orig.message_)
91 {
92 }
93 
94 UserError::~UserError() {}
95 
96 void UserError::PrettyPrint(Bytestream& out, size_t /*indent*/) const
97 {
98  out << Bytestream::ErrorMessage << message_ << Bytestream::Reset;
99 }
100 
101 
102 SourceCodeException::SourceCodeException(const string& m, const SourceRange& l)
103  : HasSource(l), err_(ErrorReport::Create(m, l))
104 {
105 }
106 
107 SourceCodeException::SourceCodeException(const SourceCodeException& orig)
108  : HasSource(orig.source()), err_(orig.err_)
109 {
110 }
111 
112 SourceCodeException::~SourceCodeException()
113 {
114 }
115 
116 const string& SourceCodeException::message() const
117 {
118  return err_->getMessage();
119 }
120 
121 const char* SourceCodeException::what() const noexcept
122 {
123  return message().c_str();
124 }
125 
126 void SourceCodeException::PrettyPrint(Bytestream& out, size_t indent) const
127 {
128  err_->PrettyPrint(out, indent);
129 }
130 
131 
132 SyntaxError::SyntaxError(const string& message, const SourceRange& loc)
133  : SourceCodeException(message, loc)
134 {
135 }
136 
137 SyntaxError::SyntaxError(const SyntaxError& orig)
138  : SourceCodeException(orig.message(), orig.source())
139 {
140 }
141 
142 SyntaxError::~SyntaxError() {}
143 
144 SemanticException::SemanticException(const string& m, const SourceRange& loc)
145  : SourceCodeException(m, loc)
146 {
147 }
148 
149 SemanticException::~SemanticException() {}
virtual void PrettyPrint(Bytestream &, size_t indent=0) const
Print a human-readable representation to an output stream and return that output stream.
Definition: exceptions.cc:73
Base class for exceptions related to invalid source code.
Definition: exceptions.h:111
An error in user input.
Definition: exceptions.h:90
Some code may choose to throw this exception rather than assert() out.
Definition: exceptions.h:47
An error that has an OS-specific description.
Definition: exceptions.h:68
A mixin type for something that has a location in source code.
Declaration of basic Fabrique exceptions.
virtual void PrettyPrint(Bytestream &, size_t indent=0) const override
Print a human-readable representation to an output stream and return that output stream.
Definition: exceptions.cc:126
A non-exceptional representation of a problem in source code.
Definition: ErrorReport.h:45
virtual void PrettyPrint(Bytestream &, size_t indent=0) const override
Print a human-readable representation to an output stream and return that output stream.
Definition: exceptions.cc:96
A syntactic error is present in the Fabrique description.
Definition: exceptions.h:132
A ostream-like class that may support formatting.
Definition: Bytestream.h:43
A range of characters in source code.
Declaration of fabrique::ErrorReport.
Declaration of fabrique::Bytestream.