commore  1.0.6-SNAPSHOT
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Parser.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2006-2007 Raphael David / CANTOR
3 //
4 
5 #ifndef CMR_DUMMY_PARSER_INCLUDED
6 #define CMR_DUMMY_PARSER_INCLUDED
7 
8 #include "commore/Commore.h"
9 #include "commore/Tuple.h"
10 
11 namespace commore
12 {
13  class Path;
14  //
15  // Parser dictionary
16  // Resolve unknow type
17  //
19  {
20  public:
21  ParserDoctype(const AString& name);
22  virtual ~ParserDoctype();
23 
24  public:
25  //
26  // Check a markup meaning
27  // return true if it has a special meaning
28  // markup_name - the input markup name
29  // type - return the attribute type
30  // is_tuple - if true means that markup is
31  // not an attribute but directly a tuple (type must be T_TUPLE or T_LIST_TUPLE)
32  // att_name - is the attribut name
33  //
34  virtual bool get_att_type(const AString& markup_name, Types& type, bool& is_tuple, AString& attName) = 0;
35  static ParserDoctype* get_parser_doc_type(const AChar* name);
36 
37  private:
38  AString name_;
39  ParserDoctype* next_;
40 
41  };
42  //
43  // Dummy text parser
44  // for parse C style lexical syntax
45  //
46  class CMREXD CmrParser : public Tuple
47  {
48  public:
49  class Reader;
50  enum WORD_TYPE
51  {
57  NUMBER
58  };
59 
60  public:
61  //
62  // Initialize on a stream source
63  // and read first token
64  //
65  CmrParser(IBStream& isource);
66  CmrParser(const Path& file);
67  CmrParser(const AString& s);
68  ~CmrParser();
69 
70  public:
71  //
72  // Check if sources is valid
73  //
74  bool is_open();
75 
76 
77  //
78  // Return the source file name
79  //
80  const AString& get_source_name();
81 
82  //
83  // Read next token
84  //
85  WORD_TYPE read_next();
86 
87  //
88  // Get text content of current token
89  //
91  {
92  return word_;
93  }
94  //
95  // Get type of current token
96  //
98  {
99  return type_;
100  }
101 
102  //
103  // Check end of source
104  //
105  bool is_eof()
106  {
107  return type_ == EMPTY;
108  }
109 
110  //
111  // Check oprator
112  // _read : if true read next token
113  //
114  bool is_oper()
115  {
116  return type_ == OPER;
117  }
118  bool is_oper(const char* s);
119  bool is_oper_read(const char* s);
120 
121  //
122  // Check ident
123  //
124  bool is_ident()
125  {
126  return type_ == IDENT;
127  }
128  bool is_ident(const char* s);
129  bool is_identu(const char* s);
130  bool is_ident_read(const char* s);
131  bool is_identu_read(const char* s);
132  bool is_ident_read(AString& s);
133 
134  //
135  // Check string
136  //
137  bool is_string()
138  {
139  return type_ == STRING;
140  }
142  {
143  bool r = type_ == STRING;
144  if (r)
145  {
146  s = word_;
147  read_next();
148  }
149  return r;
150  }
151 
152  //
153  // Check qstring
154  //
155  bool is_qstring()
156  {
157  return type_ == QSTRING;
158  }
160  {
161  bool r = type_ == QSTRING;
162  if (r)
163  {
164  s = word_;
165  read_next();
166  }
167  return r;
168  }
169 
170  //
171  // Check number
172  //
173  bool is_number();
174  bool is_number(int& v);
175  bool is_number_read(int& v);
176  bool is_number_read(AString& v);
177  int get_number();
178  //
179  // Conytrol how are parsed string
180  // Default is C like, aternate is VB like
181  // (how is escaped the " char)
182  //
183  bool get_string_mode() const
184  {
185  return string_mode_;
186  }
187  void set_string_mode(bool basic = true)
188  {
189  string_mode_ = basic;
190  }
191 
192  //
193  // Get comment and layout
194  // !(layout contains comment)
195  //
196  const StringBuffer& get_layout() const
197  {
198  return layout_;
199  }
200  const ListAString& get_comment() const
201  {
202  return comment_;
203  }
204  AString get_expected() const;
205  int get_line() const;
206 
207  private:
208  IBStream& isource();
209  int read_char_();
210 
211  private:
212  int line_;
213  AString word_;
214  StringBuffer layout_;
215  ListAString comment_;
216  WORD_TYPE type_;
217  int current_;
218  int next_;
219  Reader* reader_;
220  bool string_mode_;
221  ListAString expected_;
222  };
223 
224  //
225  // A trivial XML parser
226  // It reads XML stream one markup by one markup
227  // It is an XML iterator
228  //
230  {
231  public:
232  XmlParser(IBStream& i);
233  ~XmlParser();
234 
235  public:
236  //
237  // The markup attribute iterator
238  //
240  {
241  AttIterator() : name(""), value(""), next_(0) {}
242  const char* name;
243  const char* value;
244  void* next_;
245  bool next();
246  operator bool () const
247  {
248  return next_ != 0;
249  }
250  };
251  //
252  // Read next markup.
253  // - read stream util it get a markup start char '<'
254  // - skip comment and <! <? markup
255  //
256  // Read stream until end of markup.
257  // Read the text between the markup and next markup.
258  // Try to read next markup if it is the end
259  // markup of current markup. In this
260  // case the markup is tagged as begin_end markup and get_value
261  // return the text between the two markup.
262  //
263  int read_next();
264 
265  //
266  // Check end of stream or error reached
267  //
268  bool is_end_stream() const;
269  //
270  // Is current markup a begin markup
271  //
272  bool is_begin_markup() const;
273  //
274  // Is current markup a end markup
275  // tag : the markup name
276  //
277  bool is_end_markup() const;
278  bool is_end_markup(const char* tag) const;
279 
280  //
281  // Is current markup a begin and end markup
282  //
283  bool is_begin_end_markup() const;
284  bool is_markup(const char* tag) const;
285 
286  //
287  // Return name of current markup
288  //
289  const AString get_markup_name() const;
290  //
291  // Check if makup attribute is set
292  //
293  bool is_set(const AString& attName) const;
294 
295  //
296  // Get list of markup attribute
297  //
298  int get_set(ListAString& attList) const;
299 
300  //
301  // Get an iterator on makup attribute list
302  //
303  AttIterator get_att_list();
304 
305  //
306  // Get string value of markup
307  // Text between current makup and next markup
308  // It does not remove spaces characters.
309  //
310  AString get_value() const;
311  void get_value(StringBuffer& buf) const;
312  void get_value(AString& buf) const;
313 
314  //
315  // Get string value of a makup attribute of
316  // current markup.
317  //
318  AString get(const AString& attName) const;
319  bool get(const AString& attName, AString& value) const;
320 
321  //
322  // Debug purpose : dump content of current markup
323  //
324  AString to_string() const;
325 
326  //
327  // Get current doc type detected by parser
328  //
329  ParserDoctype* get_doc_type();
330  const AChar* dump();
331 
332  private:
333  //
334  // Not allowed operation
335  //
336  XmlParser(const XmlParser&);
337  XmlParser& operator = (const XmlParser&);
338 
339 
340  private:
341  class Impl;
342  Impl* impl_;
343  };
344 
345  //
346  // Parser for ini file
347  // Not yet used
348  //
350  {
351  public:
352  IniParser(const Path& iniFileName);
353  ~IniParser();
354 
355  public:
356  bool match(const AChar* section, const AChar* name) const;
357  bool match(const AChar* section, AString& name) const;
358  bool match(AString& section, AString& name) const;
359  bool match_index(const AChar* section, const AChar* name) const;
360  const AChar* get_index() const;
361  int get_index_num() const;
362  void get_value(AString& value) const;
363  int get_valueNum() const;
364  const AChar* get_value() const;
365  bool read_next();
366 
367  private:
368  class Impl;
369  Impl* impl_;
370 
371  };
372 };
373 
374 #endif
WORD_TYPE get_type()
Definition: Parser.h:97
Definition: Parser.cpp:19
Definition: Tuple.h:29
Definition: AString.h:39
Definition: Parser.h:229
bool is_qstring_read(AString &s)
Definition: Parser.h:159
Definition: Parser.h:349
Definition: Parser.cpp:1499
Definition: StringBuffer.h:21
Definition: Parser.h:239
WORD_TYPE
Definition: Parser.h:50
char AChar
Definition: Type.h:65
AString to_string(long number, int width=0, char pad= '0')
Definition: Time.cpp:1405
bool is_ident()
Definition: Parser.h:124
bool is_oper()
Definition: Parser.h:114
#define CMREXD
Definition: Compiler.h:22
AString & get_word()
Definition: Parser.h:90
Definition: Parser.h:46
AttIterator()
Definition: Parser.h:241
bool is_string()
Definition: Parser.h:137
const StringBuffer & get_layout() const
Definition: Parser.h:196
bool is_string_read(AString &s)
Definition: Parser.h:141
Definition: Path.h:19
Definition: Parser.h:56
Definition: Parser.h:54
Definition: Parser.h:53
void * next_
Definition: Parser.h:244
bool is_qstring()
Definition: Parser.h:155
Definition: Parser.h:52
const char * value
Definition: Parser.h:243
bool is_eof()
Definition: Parser.h:105
const ListAString & get_comment() const
Definition: Parser.h:200
const char * name
Definition: Parser.h:242
void set_string_mode(bool basic=true)
Definition: Parser.h:187
Definition: IOBStream.h:29
Definition: Parser.h:55
Definition: Parser.h:18
Types
Definition: Type.h:131
bool get_string_mode() const
Definition: Parser.h:183