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