libUTL++
RDparser.h
1 #pragma once
2 
4 
5 #include <libutl/Graph.h>
6 #include <libutl/Queue.h>
7 #include <libutl/Regex.h>
8 #include <libutl/Token.h>
9 
11 
12 UTL_NS_BEGIN;
13 
15 
16 class GrammarNode;
17 class Production;
18 class ParseNode;
19 class RDparserState;
20 class Terminal;
21 
23 // RDparser ////////////////////////////////////////////////////////////////////////////////////////
25 
101 
103 class RDparser : public Object, protected FlagsMI
104 {
106 
107 public:
108  void clear();
109 
110  virtual void copy(const Object& rhs);
111 
118  void addProduction(uint_t id, const String& name, const String& rhs);
119 
126  void addTerminal(uint_t id, const String& name, const String& regex);
127 
132  bool compile();
133 
138  bool
139  ok() const
140  {
141  return getFlag(flg_ok);
142  }
143 
150  Graph* parse(Stream& stream, const String* prod = nullptr);
151 
158  Graph* parse(const String& str, const String* prod = nullptr);
159 
160 private:
161  enum flg_t
162  {
163  flg_ok
164  };
165 
166 private:
167  void init();
168  void
169  deInit()
170  {
171  }
172  static String getToken(const TDequeIt<String>& it);
173  static bool isSymbol(char c);
174  static bool isSymbol(const TDequeIt<String>& it);
175  ParseNode*
176  parse(RDparserState& state, TDequeIt<Token>& it, Graph* parseTree, const GrammarNode* gn);
177  GrammarNode* parseExpressionI(TDequeIt<String>& it, GrammarNode* expr = nullptr);
178  GrammarNode* parseBranchI(TDequeIt<String>& it);
179  GrammarNode* parseAtomI(TDequeIt<String>& it);
180  void scan(RDparserState& state, Stream& stream);
181  void scanI(Queue<String>& queue, const String& str);
182  void
183  setOK(bool _ok)
184  {
185  setFlag(flg_ok, _ok);
186  }
187  ParseNode*
188  tryParse(RDparserState& state, TDequeIt<Token>& it, Graph* parseTree, const GrammarNode* gn);
189 
190 private:
191  TRBtree<Production> _productions;
192  TRBtree<Terminal> _terminals;
193  TArray<Terminal> _terminalsArray;
194  String _rootProd;
195  Graph _gramTree;
196 };
197 
199 // ParseNode ///////////////////////////////////////////////////////////////////////////////////////
201 
209 
211 class ParseNode : public Vertex
212 {
214 
215 public:
216  virtual void copy(const Object& rhs);
217 
219  bool
220  isProduction() const
221  {
222  return (_prodId != uint_t_max);
223  }
224 
226  uint_t
227  productionId() const
228  {
229  return _prodId;
230  }
231 
233  bool
234  isToken() const
235  {
236  return (_token != nullptr);
237  }
238 
240  const Token&
241  token() const
242  {
243  ASSERTD(_token != nullptr);
244  return *_token;
245  }
246 
248  void
250  {
251  _prodId = prodId;
252  }
253 
255  void
256  setToken(const Token* token)
257  {
258  delete _token;
259  _token = token;
260  if (_token != nullptr) _token = _token->clone();
261  }
262 
263 private:
264  void
265  init()
266  {
267  _prodId = uint_t_max;
268  _token = nullptr;
269  }
270  void
271  deInit()
272  {
273  delete _token;
274  }
275 
276 private:
277  uint_t _prodId;
278  const Token* _token;
279 };
280 
282 
288 
290 // ParseEx /////////////////////////////////////////////////////////////////////////////////////////
292 
300 
302 class ParseEx : public RDparserEx
303 {
305 
306 public:
307  ParseEx(const Token* token);
308  virtual void copy(const Object& rhs);
309  virtual void dump(Stream& os) const;
310 
311 private:
312  void
313  init()
314  {
315  _token = nullptr;
316  }
317  void deInit();
318 
319 private:
320  const Token* _token;
321 };
322 
324 // ScanEx //////////////////////////////////////////////////////////////////////////////////////////
326 
334 
336 class ScanEx : public RDparserEx
337 {
340 
341 public:
342  ScanEx(String* line, size_t lineNo, size_t colNo);
343  virtual void copy(const Object& rhs);
344  virtual void dump(Stream& os) const;
345 
346 private:
347  size_t _lineNo, _colNo;
348 };
349 
351 
352 UTL_NS_END;
#define UTL_CLASS_DEFID
Default init() and deInit() (which are merely place-holders).
Definition: macros.h:532
void setToken(const Token *token)
Set the token.
Definition: RDparser.h:256
Parse-tree node.
Definition: RDparser.h:211
void deInit()
De-initialize UTL++.
void setProductionId(uint_t prodId)
Set the production id.
Definition: RDparser.h:249
FIFO (first-in, first-out) data structure.
Definition: Queue.h:24
Template version of DequeIt.
Definition: TDequeIt.h:23
#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
Graph vertex.
Definition: Vertex.h:32
Mix-in to provide 64-bits for space-efficient storage of up to 64 boolean flags.
Definition: FlagsMI.h:25
#define UTL_EXCEPTION_DECL(exName, baseExName)
Declare a simple exception type.
Definition: Exception.h:41
bool isProduction() const
Determine whether self is a production node.
Definition: RDparser.h:220
Root of UTL++ exception class hierarchy.
Definition: Exception.h:109
Graph (directed or undirected).
Definition: Graph.h:36
Base class for exceptions thrown by RDparser.
Definition: RDparser.h:287
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
Recursive-descent parser.
Definition: RDparser.h:103
uint_t productionId() const
Get the production id.
Definition: RDparser.h:227
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
Scanned token.
Definition: Token.h:22
const uint_t uint_t_max
Maximum uint_t value.
Stream I/O abstraction.
Definition: Stream.h:68
bool ok() const
Determine whether a properly defined grammar has been compiled.
Definition: RDparser.h:139
Scan exception.
Definition: RDparser.h:336
Parse exception.
Definition: RDparser.h:302
bool isToken() const
Determine whether self is a terminal node.
Definition: RDparser.h:234
Root of UTL++ class hierarchy.
Definition: Object.h:52
void dump(const FwdIt &begin, const FwdIt &end, Stream &os, uint_t level=uint_t_max, bool key=false, bool printClassName=false, uint_t indent=0, const char *separator=nullptr)
Dump objects to the given stream (with Object::dump()).
void init()
Initialize UTL++.
#define ASSERTD
Do an assertion in DEBUG mode only.
const Token & token() const
Get the token.
Definition: RDparser.h:241