eRPC Generator (erpcgen)  Rev. 1.7.2
NXP Semiconductors
AstNode.h
1 /*
2  * Copyright (c) 2014, Freescale Semiconductor, Inc.
3  * Copyright 2016 NXP
4  * All rights reserved.
5  *
6  *
7  * SPDX-License-Identifier: BSD-3-Clause
8  */
9 
10 #ifndef _EMBEDDED_RPC__ASTNODE_H_
11 #define _EMBEDDED_RPC__ASTNODE_H_
12 
13 #include "Token.h"
14 #include "smart_ptr.h"
15 #include <map>
16 #include <stack>
17 #include <string>
18 #include <typeinfo>
19 #include <vector>
20 
22 // Classes
24 
25 namespace erpcgen {
26 
35 class AstNode
36 {
37 public:
38  typedef std::map<std::string, Value *> attribute_map_t;
40  typedef std::vector<AstNode *> child_list_t;
41  typedef child_list_t::iterator iterator;
42  typedef child_list_t::const_iterator const_iterator;
55  AstNode(const Token &token)
56  : m_token(token)
57  , m_parent(nullptr)
58  {
59  }
60 
73  AstNode(Token &token, AstNode *parent)
74  : m_token(token)
75  , m_parent(parent)
76  {
77  }
78 
89  AstNode(const AstNode &other);
90 
102  AstNode &operator=(const AstNode &other);
103 
109  virtual ~AstNode();
110 
116  virtual AstNode *clone() const { return new AstNode(*this); }
117 
123  virtual std::string nodeName() const { return typeid(*this).name(); }
124 
126 
127 
134  Token &getToken() { return m_token; }
135 
143  const Token &getToken() const { return m_token; }
144 
150  void setToken(Token &token) { m_token = token; }
151 
158 
164  std::string getTokenString() { return m_token.getValue() ? m_token.getValue()->toString() : ""; }
166 
168 
169 
174  AstNode *getParent() const { return m_parent; }
175 
181  void setParent(AstNode *newParent) { m_parent = newParent; }
183 
185 
186 
191  size_t attributeCount() const { return m_attributes.size(); }
192 
206  bool hasAttribute(const std::string &name) const;
207 
221  Value *getAttribute(const std::string &name);
222 
235  void setAttribute(const std::string &name, Value *node);
236 
248  void removeAttribute(const std::string &name);
249 
260  Value *&operator[](const std::string &name) { return m_attributes[name]; }
262 
264 
265 
274  void appendChild(AstNode *node);
275 
281  size_t childCount() const { return m_children.size(); }
282 
293  AstNode *getChild(int index) const { return m_children[index]; }
294 
306  size_t getIndexOfChild(AstNode *child);
307 
316  size_t getIndex();
317 
328  void replaceChild(AstNode *original, AstNode *replacement);
329 
337  void reverseExpr();
338 
351  AstNode *&operator[](int index) { return m_children[index]; }
352 
365  const AstNode *operator[](int index) const { return m_children[index]; }
367 
369 
370  inline iterator begin() { return m_children.begin(); }
371  inline iterator end() { return m_children.end(); }
372  inline const_iterator begin() const { return m_children.begin(); }
373  inline const_iterator end() const { return m_children.end(); }
375 
381  std::string getDescription() const;
382 
383 protected:
386  attribute_map_t m_attributes;
387  child_list_t m_children;
388 };
389 
394 {
395 public:
404  : m_root(root)
405  , m_depth(0)
406  {
407  }
408 
416  void dispatch() { dispatch(m_root, 0); }
417 
427  void dispatch(AstNode *node, int childIndex = 0);
428 
435  void print(AstNode *node, int childIndex = 0);
436 
437 protected:
439  int m_depth;
440  std::stack<int> m_depthStack;
447  void printIndent(int indent) const;
448 };
449 
450 } // namespace erpcgen
451 
452 #endif // _EMBEDDED_RPC__ASTNODE_H_
AstNode * getChild(int index) const
This function return child node from node children.
Definition: AstNode.h:293
AstNode(const Token &token)
This function is constructor of AstNode class.
Definition: AstNode.h:55
Token & getToken()
This function returns token of this node.
Definition: AstNode.h:134
std::string getTokenString()
This function returns string representation of this node token value.
Definition: AstNode.h:164
void setParent(AstNode *newParent)
This function set parent for this node.
Definition: AstNode.h:181
Value * getAttribute(const std::string &name)
This function return attribute from AstNode attributes.
Definition: AstNode.cpp:107
Value *& operator[](const std::string &name)
Square brackets.
Definition: AstNode.h:260
void removeAttribute(const std::string &name)
This function remove attribute from AstNode attributes.
Definition: AstNode.cpp:125
size_t getIndexOfChild(AstNode *child)
This function return index of searched child node.
Definition: AstNode.cpp:142
void setToken(Token &token)
This function set token to this node.
Definition: AstNode.h:150
Token m_token
Definition: AstNode.h:384
child_list_t::const_iterator const_iterator
Definition: AstNode.h:42
AstNode * getParent() const
This function returns pointer to parent node.
Definition: AstNode.h:174
const Token & getToken() const
This function returns constant token of this node.
Definition: AstNode.h:143
attribute_map_t m_attributes
Definition: AstNode.h:386
virtual AstNode * clone() const
This function returns an exact duplicate of this object.
Definition: AstNode.h:116
child_list_t m_children
Definition: AstNode.h:387
virtual std::string nodeName() const
This function returns name of this node.
Definition: AstNode.h:123
const AstNode * operator[](int index) const
Square brackets.
Definition: AstNode.h:365
Homogeneous AST node class.
Definition: AstNode.h:35
Value * getValue()
This function returns token value.
Definition: Token.h:221
virtual std::string toString() const =0
Get Value type string representation.
child_list_t::iterator iterator
Definition: AstNode.h:41
std::string getDescription() const
This function returns string description of the node.
Definition: AstNode.cpp:186
size_t attributeCount() const
This function returns count of node attributes.
Definition: AstNode.h:191
void replaceChild(AstNode *original, AstNode *replacement)
This function replace child in AstNode tree.
Definition: AstNode.cpp:178
Abstract base class for values of arbitrary types.
Definition: Value.h:21
void setAttribute(const std::string &name, Value *node)
This function set attribute in AstNode attributes.
Definition: AstNode.cpp:120
size_t childCount() const
This function return count of children for current node.
Definition: AstNode.h:281
void reverseExpr()
This function replace children location in AstNode tree.
Definition: AstNode.cpp:161
AstNode *& operator[](int index)
Square brackets.
Definition: AstNode.h:351
AstNode & operator=(const AstNode &other)
Assignment operator.
Definition: AstNode.cpp:50
void appendChild(AstNode *node)
This function add given node to the end of children list for current AstNode.
Definition: AstNode.cpp:133
bool hasAttribute(const std::string &name) const
This function find attribute in AstNode attributes.
Definition: AstNode.cpp:101
Encapsulates all information about a token.
Definition: Token.h:60
virtual ~AstNode()
This function is destructor of AstNode class.
Definition: AstNode.cpp:80
AstNode * m_root
Definition: AstNode.h:438
void dispatch()
This function call for dispatch function.
Definition: AstNode.h:416
size_t getIndex()
This function return index of current node in parent&#39;s list of children.
Definition: AstNode.cpp:156
std::vector< AstNode * > child_list_t
Definition: AstNode.h:40
AstNode * m_parent
Definition: AstNode.h:385
Value * getTokenValue()
This function returns value of token of this node.
Definition: AstNode.h:157
Dumps an AST tree.
Definition: AstNode.h:393
std::map< std::string, Value * > attribute_map_t
Definition: AstNode.h:38
AstPrinter(AstNode *root)
This function is constructor of AstPrinter class.
Definition: AstNode.h:403
int m_depth
Definition: AstNode.h:439
Definition: AstNode.h:25
AstNode(Token &token, AstNode *parent)
This function is constructor of AstNode class.
Definition: AstNode.h:73
std::stack< int > m_depthStack
Definition: AstNode.h:440