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