internal/diagnostic.h
Go to the documentation of this file.
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 
17 #ifndef ZORBA_INTERNAL_DIAGNOSTIC_H
18 #define ZORBA_INTERNAL_DIAGNOSTIC_H
19 
20 #include <string>
21 #include <vector>
22 
23 #include <zorba/diagnostic.h>
24 
25 #include "ztd.h"
26 
27 namespace zorba {
28 
29 namespace internal {
30  namespace diagnostic {
31  class location;
32  }
33 }
34 namespace serialization {
35  class Archiver;
36  void operator&( serialization::Archiver&, internal::diagnostic::location& );
37 }
38 
39 namespace internal {
40 namespace diagnostic {
41 
42 ///////////////////////////////////////////////////////////////////////////////
43 
44 /**
45  * A %location hold the file location of an error.
46  */
47 class ZORBA_DLL_PUBLIC location {
48 public:
49  /**
50  * The line-number type.
51  */
52  typedef unsigned line_type;
53 
54  /**
55  * The column-number type.
56  */
57  typedef unsigned short column_type;
58 
59  /**
60  * A empty instance for convenience.
61  */
62  static location const empty;
63 
64  /**
65  * Constructs a default (empty) %location.
66  */
67  location() : line_( 0 ), column_( 0 ), line_end_( 0 ), column_end_( 0 ) {
68  }
69 
70  /**
71  * Constructs a %location.
72  *
73  * @param file The name of the file where the error occurred.
74  * @param line The line number of the file where the expression that
75  * raises the error begins.
76  * @param column The column number, if any, of the file where the expression
77  * that raises the error begins.
78  * @param line_end The end line number, if any, of the file where the expression
79  * causing the error ends.
80  * @param column_end The end column number, if any, of the file where
81  * the xpression causing the error ends.
82  */
84  char const *file,
85  line_type line,
86  column_type column = 0,
87  line_type line_end = 0,
88  column_type column_end = 0) :
89  file_( file ), line_( line ), column_( column ),
90  line_end_( line_end ), column_end_( column_end )
91  {
92  }
93 
94  /**
95  * Constructs a %location.
96  *
97  * @tparam StringType The string type for \a file.
98  * @param file The name of the file where the error occurred.
99  * @param line The line number of the file where the error occurred.
100  * @param column The column number, if any, of the file where the error occurred.
101  * @param line_end The end line number, if any, of the file where the expression
102  * causing the error ends.
103  * @param column_end The end column number, if any, of the file where
104  * the xpression causing the error ends.
105  */
106  template<class StringType>
108  StringType const &file,
109  line_type line,
110  column_type column = 0,
111  line_type line_end = 0,
112  column_type column_end = 0) :
113  file_( file.c_str() ), line_( line ), column_( column ),
114  line_end_( line_end ), column_end_( column_end )
115  {
116  }
117 
118  /**
119  * Gets the file name, if any.
120  *
121  * @return Returns the file name or the empty string if unset.
122  */
123  char const* file() const {
124  return file_.c_str();
125  }
126 
127  /**
128  * Gets the line number, if any.
129  *
130  * @return Returns the line number or 0 if unset.
131  */
132  line_type line() const {
133  return line_;
134  }
135 
136  /**
137  * Gets the column number, if any.
138  *
139  * @return Returns the column number or 0 if unset.
140  */
141  column_type column() const {
142  return column_;
143  }
144 
145  /**
146  * Gets the ending line number, if any.
147  *
148  * @return Returns the line number or 0 if unset.
149  */
150  line_type line_end() const {
151  return line_end_;
152  }
153 
154  /**
155  * Gets the ending column number, if any.
156  *
157  * @return Returns the column number or 0 if unset.
158  */
160  return column_end_;
161  }
162 
163  /**
164  * Conversion to \c bool for testing whether this %location has been set.
165  *
166  * @return Returns \c true only if this %location has been set.
167  */
168  operator bool() const {
169  return !!line_;
170  }
171 
172  /**
173  * Checks whether this %location has not been set.
174  *
175  * @return Returns \c true only if this %location has not been set.
176  */
177  bool operator!() const {
178  return !line_;
179  }
180 
181  /**
182  * Sets the %location information.
183  *
184  * @param file The name of the file where the error occurred.
185  * @param line The line number of the file where the error occurred.
186  * @param column The column number, if any, of the file where the error
187  * occurred.
188  * @param line_end The end line of the file where the error occured.
189  * @param column_end The column number, if any, where the error ends.
190  * occurred.
191  */
192  void set(
193  char const *file,
194  line_type line,
195  column_type column = 0,
196  line_type line_end = 0,
197  column_type column_end = 0) {
198  file_ = file;
199  line_ = line;
200  column_ = column;
201  line_end_ = line_end;
202  column_end_ = column_end;
203  }
204 
205 private:
206  std::string file_;
207  line_type line_;
208  column_type column_;
209  line_type line_end_;
210  column_type column_end_;
211 
212  // for plan serialization
213  friend void serialization::operator&( serialization::Archiver&, location& );
214 };
215 
216 ///////////////////////////////////////////////////////////////////////////////
217 
218 /**
219  * \internal
220  * A %parameters holds the parameters for an error message.
221  */
222 class ZORBA_DLL_PUBLIC parameters {
223  typedef std::vector<std::string> params_type;
224 public:
225  typedef params_type::value_type value_type;
226  typedef params_type::size_type size_type;
227 
228  /**
229  * A empty instance for convenience.
230  */
231  static parameters const empty;
232 
233  /**
234  * Constructs a %parameters object.
235  */
236  parameters();
237 
238  /**
239  * Adds the string representation of the given object as the next parameter.
240  *
241  * @tparam T The object type.
242  * @param t The object.
243  * @return Returns \c *this.
244  */
245  template<typename T>
246  parameters& operator,( T const &t ) {
247  params_.push_back( ztd::to_string( t ) );
248  return *this;
249  }
250 
251  /**
252  * Gets the i'th parameter value.
253  * Parameter numbers start at 1.
254  *
255  * @param i The parameter to get.
256  * @return Returns said parameter value.
257  */
258  value_type const& operator[]( size_type i ) const {
259  return params_[ i - 1 ];
260  }
261 
262  /**
263  * Substitutes substrings of the given string. There are two forms:
264  *
265  * - <code>$</code><em>i</em>
266  * - <code>${</code><em>chars i chars</em><code>}</code>
267  *
268  * where <em>i</em> is an integer in the range <code>[1,9]</code>
269  * and <em>chars</em> are any characters except <code>[1-9}]</code>.
270  *
271  * The second form elides the addition characacters if the value of the
272  * <em>ith</em> parameter is empty. For example, <code>${"1"}</code> will
273  * substitute the value of the 1st parameter quoted if non-empty; if empty,
274  * the entire substitution set of characters (everything from the
275  * <code>$</code> to the <code>}</code>) will be elided.
276  *
277  * @param s The string to perform the substitutions on.
278  */
279  void substitute( value_type *s ) const;
280 
281 private:
282  params_type params_;
283 
284  value_type lookup_param( size_type i ) const;
285 };
286 
287 ///////////////////////////////////////////////////////////////////////////////
288 
289 } // namespace diagnostic
290 } // namespace internal
291 } // namespace zorba
292 #endif /* ZORBA_INTERNAL_DIAGNOSTIC_H */
293 /* vim:set et sw=2 ts=2: */
blog comments powered by Disqus