Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JCppFacet.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JCPPFACET__
2 #define __JLANG__JCPPFACET__
3 
4 #include <istream>
5 #include <ostream>
6 #include <locale>
7 #include <string>
8 #include <iterator>
9 #include <limits>
10 
11 
12 #include "JLang/JStringFacet.hh"
13 
14 /**
15  * \author mdejong
16  */
17 
18 namespace JLANG {}
19 namespace JPP { using namespace JLANG; }
20 
21 namespace JLANG {
22 
23 
24  /**
25  * Facet class to specify parsing of a C[++] variable name.
26  */
27  class JCppFacet:
28  public JStringFacet
29  {
30  public:
31  /**
32  * Constructor.
33  */
35  {}
36 
37 
38  /**
39  * Clone this facet.
40  *
41  * \return pointer to newly created facet
42  */
43  virtual JCppFacet* clone() const
44  {
45  return new JCppFacet();
46  }
47 
48 
49  static inline bool is_eol (const char c) { return c == ';'; }
50 
51  static inline bool is_begin_single_line_comment(const char c1, const char c2) { return c1 == '/' && c2 == '/'; }
52  static inline bool is_begin_multi_line_comment (const char c1, const char c2) { return c1 == '/' && c2 == '*'; }
53 
54  static inline bool is_end_single_line_comment (const char c1) { return c1 == '\n'; }
55  static inline bool is_end_multi_line_comment (const char c1, const char c2) { return c1 == '*' && c2 == '/'; }
56 
57 
58  /**
59  * Get string.
60  *
61  * \param __begin begin position of input stream
62  * \param __end end position of input stream
63  * \param format format
64  * \param result status after input operation
65  * \param buffer output string
66  * \return position of input stream
67  */
69  const istreambuf_iterator __end,
70  const std::ios_base& format,
71  std::ios_base::iostate& result,
72  std::string& buffer) const
73  {
74  return do_get(__begin, __end, format, result, buffer);
75  }
76 
77 
78  /**
79  * Put string.
80  *
81  * \param out begin position of output stream
82  * \param format format
83  * \param c fill character
84  * \param buffer input string
85  * \return position of output stream buffer
86  */
88  const std::ios_base& format,
89  const char c,
90  const std::string& buffer) const
91  {
92  return do_put(out, format, c, buffer);
93  }
94 
95 
96  protected:
97  /**
98  * Get string.
99  *
100  * \param __begin begin position of input stream
101  * \param __end end position of input stream
102  * \param format format
103  * \param result status after input operation
104  * \param buffer output string
105  * \return position of input stream
106  */
108  const istreambuf_iterator __end,
109  const std::ios_base& format,
110  std::ios_base::iostate& result,
111  std::string& buffer) const
112  {
113  using namespace std;
114 
115  result = (ios_base::iostate) 0; // reset I/O status
116 
117  streamsize n = format.width(); // number of characters to read
118 
119  if (n == 0)
120  n = numeric_limits<streamsize>::max();
121 
122  istreambuf_iterator i = __begin;
123 
124  if (i == __end || (i = do_ignore(i, __end)) == __end) {
125 
126  result |= ios_base::failbit;
127  result |= ios_base::eofbit;
128 
129  } else {
130 
131  // skip comment line(s)
132 
133  char c = *i;
134 
135  for (++i; i != __end; c = *i, ++i) {
136 
138  i = do_ignore_single_line_comment(i, __end);
139  else if (is_begin_multi_line_comment (c,*i))
140  i = do_ignore_multi_line_comment (i, __end);
141  else
142  break;
143 
144  i = do_ignore(i, __end);
145  }
146 
147  if (i == __end) {
148 
149  result |= ios_base::failbit;
150  result |= ios_base::eofbit;
151 
152  } else {
153 
154  // first character must be a letter or '_'
155 
156  if (c == '_' || isalpha(c)) {
157 
158  buffer.clear();
159  buffer.push_back(c);
160 
161  // following characters may also be digits
162 
163  for (--n; i != __end && n != 0 && (*i == '_' ||
164  isalpha(*i) ||
165  isdigit(*i)); ++i, --n)
166  buffer.push_back(*i);
167  }
168 
169  if (i == __end)
170  result |= ios_base::eofbit;
171  }
172  }
173 
174  return i;
175  }
176 
177 
178  /**
179  * Put string.
180  *
181  * \param out begin position of output stream
182  * \param format format
183  * \param c fill character
184  * \param buffer input string
185  * \return current position of output stream
186  */
188  const std::ios_base& format,
189  const char c,
190  const std::string& buffer) const
191  {
192  using namespace std;
193 
194  if (format.flags() & ios_base::right) {
195  for (streamsize i = buffer.size(); i < format.width(); ++i, ++out)
196  *out = c;
197  }
198 
199  for (string::const_iterator i = buffer.begin(); i != buffer.end(); ++i, ++out)
200  *out = *i;
201 
202  if (format.flags() & ios_base::left) {
203  for (streamsize i = buffer.size(); i < format.width(); ++i, ++out)
204  *out = c;
205  }
206 
207  return out;
208  }
209 
210 
211  /**
212  * Ignore white space and end of line characters.
213  *
214  * \param __begin begin position of input stream
215  * \param __end end position of input stream
216  * \return position of input stream
217  */
219  const istreambuf_iterator __end) const
220  {
221  istreambuf_iterator i = __begin;
222 
223  while (i != __end && (isspace(*i) || is_eol(*i)))
224  ++i;
225 
226  return i;
227  }
228 
229 
230  /**
231  * Ignore characters until end of line.
232  *
233  * \param __begin begin position of input stream
234  * \param __end end position of input stream
235  * \return position of input stream
236  */
238  const istreambuf_iterator __end) const
239  {
240  istreambuf_iterator i = __begin;
241 
242  for ( ; i != __end && !is_end_single_line_comment(*i); ++i) {}
243 
244  if (i != __end)
245  ++i; // skip end of line
246 
247  return i;
248  }
249 
250 
251  /**
252  * Ignore characters until next end of comment.
253  *
254  * \param __begin begin position of input stream
255  * \param __end end position of input stream
256  * \return position of input stream
257  */
259  const istreambuf_iterator __end) const
260  {
261  istreambuf_iterator i = __begin;
262 
263  if (i != __end) {
264 
265  char c = *i;
266 
267  for (++i; i != __end && !is_end_multi_line_comment(c,*i); c = *i, ++i) {}
268 
269  if (i != __end)
270  ++i; // skip end of line
271  }
272 
273  return i;
274  }
275 
276  private:
277 
278  JCppFacet(const JCppFacet&); // not defined
279  void operator=(const JCppFacet&); // not defined
280  };
281 }
282 
283 #endif
ostreambuf_iterator put(ostreambuf_iterator out, const std::ios_base &format, const char c, const std::string &buffer) const
Put string.
Definition: JCppFacet.hh:87
static bool is_begin_single_line_comment(const char c1, const char c2)
Definition: JCppFacet.hh:51
virtual istreambuf_iterator do_ignore(const istreambuf_iterator __begin, const istreambuf_iterator __end) const
Ignore white space and end of line characters.
Definition: JCppFacet.hh:218
void operator=(const JCppFacet &)
std::istreambuf_iterator< char, std::char_traits< char > > istreambuf_iterator
Definition: JStringFacet.hh:34
virtual ostreambuf_iterator do_put(ostreambuf_iterator out, const std::ios_base &format, const char c, const std::string &buffer) const
Put string.
Definition: JCppFacet.hh:187
std::ostreambuf_iterator< char, std::char_traits< char > > ostreambuf_iterator
Definition: JStringFacet.hh:35
virtual istreambuf_iterator do_get(const istreambuf_iterator __begin, const istreambuf_iterator __end, const std::ios_base &format, std::ios_base::iostate &result, std::string &buffer) const
Get string.
Definition: JCppFacet.hh:107
static bool is_end_single_line_comment(const char c1)
Definition: JCppFacet.hh:54
JCppFacet()
Constructor.
Definition: JCppFacet.hh:34
virtual istreambuf_iterator do_ignore_single_line_comment(const istreambuf_iterator __begin, const istreambuf_iterator __end) const
Ignore characters until end of line.
Definition: JCppFacet.hh:237
virtual JCppFacet * clone() const
Clone this facet.
Definition: JCppFacet.hh:43
virtual istreambuf_iterator do_ignore_multi_line_comment(const istreambuf_iterator __begin, const istreambuf_iterator __end) const
Ignore characters until next end of comment.
Definition: JCppFacet.hh:258
TCanvas * c1
Global variables to handle mouse events.
Facet class to specify parsing of a C[++] variable name.
Definition: JCppFacet.hh:27
static bool is_eol(const char c)
Definition: JCppFacet.hh:49
Facet class to specify parsing of a JLANG::JString object.
Definition: JStringFacet.hh:26
static bool is_begin_multi_line_comment(const char c1, const char c2)
Definition: JCppFacet.hh:52
static bool is_end_multi_line_comment(const char c1, const char c2)
Definition: JCppFacet.hh:55