libUTL++
Tokenizer.h
1 #pragma once
2 
4 
5 #include <libutl/Array.h>
6 #include <libutl/List.h>
7 #include <libutl/Token.h>
8 
10 
11 UTL_NS_BEGIN;
12 
14 
15 class Stream;
16 class TokenizerTokens;
17 
19 // Tokenizer ///////////////////////////////////////////////////////////////////////////////////////
21 
33 
35 class Tokenizer : public Object
36 {
39 
40 public:
41  virtual void copy(const Object& rhs);
42 
44  void clear();
45 
51  void addTerminal(uint_t id, const String& regex);
52 
59  void scan(TokenizerTokens& tokens, Stream* is, bool owner = true) const;
60 
61 protected:
62  Array _terminals;
63  bool _reverse;
64 
65 private:
66  void
67  init()
68  {
69  _reverse = false;
70  }
71  void
72  deInit()
73  {
74  }
75 };
76 
78 // StringTokenizer /////////////////////////////////////////////////////////////////////////////////
80 
91 
93 class StringTokenizer : public Tokenizer
94 {
97 
98 public:
104  void scan(TokenizerTokens& tokens, const String* s) const;
105 };
106 
108 // TokenizerTokens /////////////////////////////////////////////////////////////////////////////////
110 
118 
120 class TokenizerTokens : public Object, protected FlagsMI
121 {
124 
125 public:
126  void
127  clear()
128  {
129  _tokens.clear();
130  }
131 
132  void
133  add(String* tk)
134  {
135  _tokens.pushBack(tk);
136  }
137 
142  bool
143  empty() const
144  {
145  return _tokens.empty();
146  }
147 
152  Token peek() const;
153 
158  Token next();
159 
161  void skip(size_t numTokens);
162 
164  bool
165  isReverse() const
166  {
167  return getFlag(flg_reverse);
168  }
169 
171  void
173  {
174  setFlag(flg_reverse, reverse);
175  }
176 
177 private:
178  enum flg_t
179  {
180  flg_reverse
181  };
182 
183 private:
184  List _tokens;
185 };
186 
188 
189 UTL_NS_END;
Stream tokenizer.
Definition: Tokenizer.h:35
#define UTL_CLASS_DEFID
Default init() and deInit() (which are merely place-holders).
Definition: macros.h:532
Doubly-linked list.
Definition: List.h:80
void setReverse(bool reverse)
Set reverse flag.
Definition: Tokenizer.h:172
String tokenizer.
Definition: Tokenizer.h:93
void deInit()
De-initialize UTL++.
bool empty() const
No more tokens?
Definition: Tokenizer.h:143
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
Character string.
Definition: String.h:31
void reverse(const BidIt &begin, const BidIt &end)
Reverse a sequence.
Mix-in to provide 64-bits for space-efficient storage of up to 64 boolean flags.
Definition: FlagsMI.h:25
bool isReverse() const
Get reverse flag.
Definition: Tokenizer.h:165
SortedCollection that stores objects in an array.
Definition: Array.h:116
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
Store tokens collected by Tokenizer.
Definition: Tokenizer.h:120
Scanned token.
Definition: Token.h:22
Stream I/O abstraction.
Definition: Stream.h:68
#define UTL_CLASS_NO_COMPARE
Declare that a class cannot do compare().
Definition: macros.h:344
Root of UTL++ class hierarchy.
Definition: Object.h:52
void init()
Initialize UTL++.