1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 #ifndef ZORBA_XQUERY_EXCEPTION_H
18 #define ZORBA_XQUERY_EXCEPTION_H
19 
20 #include <zorba/xquery_exception.h>
21 #include "compiler/parser/query_loc.h"
22 #include "diagnostic.h"
23 
24 namespace zorba {
25 
26 ////////// XQueryException creation ///////////////////////////////////////////
27 
28 /**
29  * Makes an XQueryException.
30  *
31  * @param raise_file The C++ source-code file name whence the exception was
32  * raised.
33  * @param raise_line The C++ source-code line number whence the exception was
34  * raised.
35  * @param diagnostic The diagnostic.
36  * @param params The diagnostic parameters.
37  * @param loc The XQuery source-code location.
38  * @return Returns a new XQueryException.
39  */
40 XQueryException
41 make_xquery_exception( char const *raise_file,
42                        ZorbaException::line_type raise_line,
43                        Diagnostic const &diagnostic,
44                        internal::diagnostic::parameters const &params,
45                        internal::diagnostic::location const &loc =
46                         internal::diagnostic::location::empty );
47 
48 /**
49  * Makes an XQueryException.
50  *
51  * @param raise_file The C++ source-code file name whence the exception was
52  * raised.
53  * @param raise_line The C++ source-code line number whence the exception was
54  * raised.
55  * @param diagnostic The diagnostic.
56  * @param loc The diagnostic XQuery source-code location.
57  * @return Returns a new XQueryException.
58  */
59 inline XQueryException
60 make_xquery_exception( char const *raise_file,
61                        ZorbaException::line_type raise_line,
62                        Diagnostic const &diagnostic,
63                        internal::diagnostic::location const &loc =
64                         internal::diagnostic::location::empty ) {
65   return make_xquery_exception(
66     raise_file, raise_line, diagnostic,
67     internal::diagnostic::parameters::empty, loc
68   );
69 }
70 
71 /**
72  * Dynamically allocates an XQueryException.
73  *
74  * @param raise_file The C++ source-code file name whence the exception was
75  * raised.
76  * @param raise_line The C++ source-code line number whence the exception was
77  * raised.
78  * @param diagnostic The diagnostic.
79  * @param params The diagnostic parameters.
80  * @param loc The diagnostic XQuery source-code location.
81  * @return Returns a new XQueryException.
82  */
83 XQueryException*
84 new_xquery_exception( char const *raise_file,
85                       ZorbaException::line_type raise_line,
86                       Diagnostic const &diagnostic,
87                       internal::diagnostic::parameters const &params,
88                       internal::diagnostic::location const &loc =
89                         internal::diagnostic::location::empty );
90 
91 /**
92  * Dynamically allocates an XQueryException.
93  *
94  * @param raise_file The C++ source-code file name whence the exception was
95  * raised.
96  * @param raise_line The C++ source-code line number whence the exception was
97  * raised.
98  * @param diagnostic The diagnostic.
99  * @param loc The diagnostic XQuery source-code location.
100  * @return Returns a new XQueryException.
101  */
102 inline XQueryException*
103 new_xquery_exception( char const *raise_file,
104                       ZorbaException::line_type raise_line,
105                       Diagnostic const &diagnostic,
106                       internal::diagnostic::location const &loc =
107                         internal::diagnostic::location::empty ) {
108   return new_xquery_exception(
109     raise_file, raise_line, diagnostic,
110     internal::diagnostic::parameters::empty, loc
111   );
112 }
113 
114 /**
115  * Creates an XQueryException using the given Diagnostic variable.
116  * \hideinitializer
117  */
118 #define XQUERY_EXCEPTION_VAR(...) \
119   ::zorba::make_xquery_exception( __FILE__, __LINE__, __VA_ARGS__ )
120 
121 /**
122  * Creates an XQueryException.
123  * \hideinitializer
124  */
125 #define XQUERY_EXCEPTION(...) \
126   XQUERY_EXCEPTION_VAR( ::zorba:: __VA_ARGS__ )
127 
128 /**
129  * Creates a dynamically allocated XQueryException.
130  * \hideinitializer
131  */
132 #define NEW_XQUERY_EXCEPTION(...) \
133   ::zorba::new_xquery_exception( __FILE__, __LINE__, ::zorba:: __VA_ARGS__ )
134 
135 ////////// XQuery diagnostic source location //////////////////////////////////
136 
137 /**
138  * Sets the XQuery source location of the given ZorbaException but only if it's
139  * actually an XQueryException.  If it's actually a ZorbaException, constructs
140  * a new XQueryException (copying the information from the ZorbaException) and
141  * throws it.
142  *
143  * @param ze The ZorbaException to set the location of.
144  * @param file The XQuery file name.
145  * @param line The line number.
146  * @param col The column number.
147  * @param line_end The end line number.
148  * @param col_end The end column number.
149  * @param overwrite If \c false, sets the location only if the exception
150  * doesn't already have one; if \c true, always sets the location even if the
151  * exception already has one.
152  */
153 void set_source( ZorbaException &ze, char const *file,
154                  XQueryException::line_type line,
155                  XQueryException::column_type col,
156                  XQueryException::line_type line_end,
157                  XQueryException::column_type col_end,
158                  bool overwrite = true );
159 
160 /**
161  * Sets the XQuery source location of the given ZorbaException but only if it's
162  * actually an XQueryException.
163  *
164  * @tparam StringType The \a file string type.
165  * @param ze The ZorbaException to set the location of.
166  * @param file The XQuery file name.
167  * @param line The line number.
168  * @param col The column number.
169  * @param line_end The end line number.
170  * @param col_end The end column number.
171  * @param overwrite If \c false, sets the location only if the exception
172  * doesn't already have one; if \c true, always sets the location even if the
173  * exception already has one.
174  */
175 template<class StringType> inline
176 void set_source( ZorbaException &ze, StringType const &file,
177                  XQueryException::line_type line,
178                  XQueryException::column_type col,
179                  XQueryException::line_type line_end,
180                  XQueryException::column_type col_end,
181                  bool overwrite = true ) {
182   set_source( ze, file.c_str(), line, col, line_end, col_end, overwrite );
183 }
184 
185 /**
186  * Sets the XQuery source location of the given ZorbaException but only if it's
187  * actually an XQueryException.
188  *
189  * @param ze The ZorbaException to set the location of.
190  * @param loc The query location.
191  * @param overwrite If \c false, sets the location only if the exception
192  * doesn't already have one; if \c true, always sets the location even if the
193  * exception already has one.
194  */
195 inline void set_source( ZorbaException &ze, QueryLoc const &loc,
196                         bool overwrite = true ) {
197   set_source(
198     ze,
199     loc.getFilename(),
200     loc.getLineBegin(),
201     loc.getColumnBegin(),
202     loc.getLineEnd(),
203     loc.getColumnEnd(),
204     overwrite
205   );
206 }
207 
208 /**
209  * Sets the XQuery source location of the given ZorbaException but only if it's
210  * actually an XQueryException.
211  *
212  * @param to The ZorbaException to set the location of.
213  * @param from The ZorbaException to get the location from but only if it's
214  * actually an XQueryException.
215  * @param overwrite If \c false, sets the location only if the exception
216  * doesn't already have one; if \c true, always sets the location even if the
217  * exception already has one.
218  */
219 inline void set_source( ZorbaException &to, ZorbaException const &from,
220                         bool overwrite = true ) {
221   if ( XQueryException const *const xe =
222         dynamic_cast<XQueryException const*>( &from ) ) {
223     set_source(
224       to,
225       xe->source_uri(),
226       xe->source_line(),
227       xe->source_column(),
228       xe->source_line_end(),
229       xe->source_column_end(),
230       overwrite
231     );
232   }
233 }
234 
235 ///////////////////////////////////////////////////////////////////////////////
236 
237 } // namespace zorba
238 #endif /* ZORBA_XQUERY_EXCEPTION_H */
239 /* vim:set et sw=2 ts=2: */
240