Jpp 19.3.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
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
18namespace JLANG {}
19namespace JPP { using namespace JLANG; }
20
21namespace 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 override
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 override
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
123 istreambuf_iterator i = __begin;
124
125 if (i == __end || (i = do_ignore(i, __end)) == __end) {
126
127 result |= ios_base::failbit;
128 result |= ios_base::eofbit;
129
130 } else {
131
132 char c = *i;
133
134 for (++i; i != __end; c = *i, ++i) {
135
136 // skip comment line(s)
137
139 i = do_ignore_single_line_comment(i, __end);
140 else if (is_begin_multi_line_comment (c,*i))
141 i = do_ignore_multi_line_comment (i, __end);
142 else if (c == '_' || isalpha(c)) // first character must be letter or underscore
143 break;
144
145 i = do_ignore(i, __end);
146 }
147
148 if (i == __end) {
149
150 result |= ios_base::failbit;
151 result |= ios_base::eofbit;
152
153 } else {
154
155 buffer.clear();
156 buffer.push_back(c);
157
158 for (--n; i != __end && n != 0 && (*i == '_' ||
159 isalnum(*i)); // following characters may also be digits
160 ++i, --n) {
161 buffer.push_back(*i);
162 }
163
164 if (i == __end) {
165 result |= ios_base::eofbit;
166 }
167 }
168 }
169
170 return i;
171 }
172
173
174 /**
175 * Put string.
176 *
177 * \param out begin position of output stream
178 * \param format format
179 * \param c fill character
180 * \param buffer input string
181 * \return current position of output stream
182 */
184 const std::ios_base& format,
185 const char c,
186 const std::string& buffer) const override
187 {
188 using namespace std;
189
190 if (format.flags() & ios_base::right) {
191 for (streamsize i = buffer.size(); i < format.width(); ++i, ++out) {
192 *out = c;
193 }
194 }
195
196 for (string::const_iterator i = buffer.begin(); i != buffer.end(); ++i, ++out) {
197 *out = *i;
198 }
199
200 if (format.flags() & ios_base::left) {
201 for (streamsize i = buffer.size(); i < format.width(); ++i, ++out) {
202 *out = c;
203 }
204 }
205
206 return out;
207 }
208
209
210 /**
211 * Ignore white space and end of line characters.
212 *
213 * \param __begin begin position of input stream
214 * \param __end end position of input stream
215 * \return position of input stream
216 */
218 const istreambuf_iterator __end) const override
219 {
220 istreambuf_iterator i = __begin;
221
222 while (i != __end && (isspace(*i) || is_eol(*i))) {
223 ++i;
224 }
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
248 return i;
249 }
250
251
252 /**
253 * Ignore characters until next end of comment.
254 *
255 * \param __begin begin position of input stream
256 * \param __end end position of input stream
257 * \return position of input stream
258 */
260 const istreambuf_iterator __end) const
261 {
262 istreambuf_iterator i = __begin;
263
264 if (i != __end) {
265
266 char c = *i;
267
268 for (++i; i != __end && !is_end_multi_line_comment(c,*i); c = *i, ++i) {}
269
270 if (i != __end) {
271 ++i; // skip end of line
272 }
273 }
274
275 return i;
276 }
277
278 private:
279
280 JCppFacet(const JCppFacet&); // not defined
281 void operator=(const JCppFacet&); // not defined
282 };
283}
284
285#endif
TCanvas * c1
Global variables to handle mouse events.
Facet class to specify parsing of a C[++] variable name.
Definition JCppFacet.hh:29
virtual istreambuf_iterator do_ignore(const istreambuf_iterator __begin, const istreambuf_iterator __end) const override
Ignore white space and end of line characters.
Definition JCppFacet.hh:217
static bool is_begin_multi_line_comment(const char c1, const char c2)
Definition JCppFacet.hh:52
istreambuf_iterator 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:68
static bool is_end_multi_line_comment(const char c1, const char c2)
Definition JCppFacet.hh:55
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 ostreambuf_iterator do_put(ostreambuf_iterator out, const std::ios_base &format, const char c, const std::string &buffer) const override
Put string.
Definition JCppFacet.hh:183
void operator=(const JCppFacet &)
static bool is_begin_single_line_comment(const char c1, const char c2)
Definition JCppFacet.hh:51
JCppFacet()
Constructor.
Definition JCppFacet.hh:34
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_eol(const char c)
Definition JCppFacet.hh:49
JCppFacet(const JCppFacet &)
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:259
static bool is_end_single_line_comment(const char c1)
Definition JCppFacet.hh:54
virtual JCppFacet * clone() const override
Clone this facet.
Definition JCppFacet.hh:43
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 override
Get string.
Definition JCppFacet.hh:107
Facet class to specify parsing of a JLANG::JString object.
std::istreambuf_iterator< char, std::char_traits< char > > istreambuf_iterator
std::ostreambuf_iterator< char, std::char_traits< char > > ostreambuf_iterator
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).