eRPC Generator (erpcgen)  Rev. 1.9.0
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 
16 #include <map>
17 #include <stack>
18 #include <string>
19 #include <typeinfo>
20 #include <vector>
21 
23 // Classes
25 
26 namespace erpcgen {
27 
36 class AstNode
37 {
38 public:
39  typedef std::map<std::string, Value *> attribute_map_t;
41  typedef std::vector<AstNode *> child_list_t;
42  typedef child_list_t::iterator iterator;
43  typedef child_list_t::const_iterator const_iterator;
56  AstNode(const Token &token)
57  : m_token(token)
58  , m_parent(nullptr)
59  {
60  }
61 
74  AstNode(Token &token, AstNode *parent)
75  : m_token(token)
76  , m_parent(parent)
77  {
78  }
79 
90  AstNode(const AstNode &other);
91 
103  AstNode &operator=(const AstNode &other);
104 
110  virtual ~AstNode();
111 
117  virtual AstNode *clone() const { return new AstNode(*this); }
118 
124  virtual std::string nodeName() const { return typeid(*this).name(); }
125 
127 
128 
135  Token &getToken() { return m_token; }
136 
144  const Token &getToken() const { return m_token; }
145 
151  void setToken(Token &token) { m_token = token; }
152 
159 
165  std::string getTokenString() { return m_token.getValue() ? m_token.getValue()->toString() : ""; }
167 
169 
170 
175  AstNode *getParent() const { return m_parent; }
176 
182  void setParent(AstNode *newParent) { m_parent = newParent; }
184 
186 
187 
192  size_t attributeCount() const { return m_attributes.size(); }
193 
207  bool hasAttribute(const std::string &name) const;
208 
222  Value *getAttribute(const std::string &name);
223 
236  void setAttribute(const std::string &name, Value *node);
237 
249  void removeAttribute(const std::string &name);
250 
261  Value *&operator[](const std::string &name) { return m_attributes[name]; }
263 
265 
266 
275  void appendChild(AstNode *node);
276 
282  size_t childCount() const { return m_children.size(); }
283 
294  AstNode *getChild(int index) const { return m_children[index]; }
295 
307  size_t getIndexOfChild(AstNode *child);
308 
317  size_t getIndex();
318 
329  void replaceChild(AstNode *original, AstNode *replacement);
330 
338  void reverseExpr();
339 
352  AstNode *&operator[](int index) { return m_children[index]; }
353 
366  const AstNode *operator[](int index) const { return m_children[index]; }
368 
370 
371  inline iterator begin() { return m_children.begin(); }
372  inline iterator end() { return m_children.end(); }
373  inline const_iterator begin() const { return m_children.begin(); }
374  inline const_iterator end() const { return m_children.end(); }
376 
382  std::string getDescription() const;
383 
384 protected:
387  attribute_map_t m_attributes;
388  child_list_t m_children;
389 };
390 
395 {
396 public:
405  : m_root(root)
406  , m_depth(0)
407  {
408  }
409 
417  void dispatch() { dispatch(m_root, 0); }
418 
428  void dispatch(AstNode *node, int childIndex = 0);
429 
436  void print(AstNode *node, int childIndex = 0);
437 
438 protected:
440  int m_depth;
441  std::stack<int> m_depthStack;
448  void printIndent(int indent) const;
449 };
450 
451 } // namespace erpcgen
452 
453 #endif // _EMBEDDED_RPC__ASTNODE_H_
AstNode * getChild(int index) const
This function return child node from node children.
Definition: AstNode.h:294
AstNode(const Token &token)
This function is constructor of AstNode class.
Definition: AstNode.h:56
Token & getToken()
This function returns token of this node.
Definition: AstNode.h:135
std::string getTokenString()
This function returns string representation of this node token value.
Definition: AstNode.h:165
void setParent(AstNode *newParent)
This function set parent for this node.
Definition: AstNode.h:182
Value * getAttribute(const std::string &name)
This function return attribute from AstNode attributes.
Definition: AstNode.cpp:109
Value *& operator[](const std::string &name)
Square brackets.
Definition: AstNode.h:261
void removeAttribute(const std::string &name)
This function remove attribute from AstNode attributes.
Definition: AstNode.cpp:127
size_t getIndexOfChild(AstNode *child)
This function return index of searched child node.
Definition: AstNode.cpp:144
void setToken(Token &token)
This function set token to this node.
Definition: AstNode.h:151
Token m_token
Definition: AstNode.h:385
child_list_t::const_iterator const_iterator
Definition: AstNode.h:43
AstNode * getParent() const
This function returns pointer to parent node.
Definition: AstNode.h:175
const Token & getToken() const
This function returns constant token of this node.
Definition: AstNode.h:144
attribute_map_t m_attributes
Definition: AstNode.h:387
virtual AstNode * clone() const
This function returns an exact duplicate of this object.
Definition: AstNode.h:117
child_list_t m_children
Definition: AstNode.h:388
virtual std::string nodeName() const
This function returns name of this node.
Definition: AstNode.h:124
const AstNode * operator[](int index) const
Square brackets.
Definition: AstNode.h:366
Homogeneous AST node class.
Definition: AstNode.h:36
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:42
std::string getDescription() const
This function returns string description of the node.
Definition: AstNode.cpp:188
size_t attributeCount() const
This function returns count of node attributes.
Definition: AstNode.h:192
void replaceChild(AstNode *original, AstNode *replacement)
This function replace child in AstNode tree.
Definition: AstNode.cpp:180
Abstract base class for values of arbitrary types.
Definition: Value.h:27
void setAttribute(const std::string &name, Value *node)
This function set attribute in AstNode attributes.
Definition: AstNode.cpp:122
size_t childCount() const
This function return count of children for current node.
Definition: AstNode.h:282
void reverseExpr()
This function replace children location in AstNode tree.
Definition: AstNode.cpp:163
AstNode *& operator[](int index)
Square brackets.
Definition: AstNode.h:352
AstNode & operator=(const AstNode &other)
Assignment operator.
Definition: AstNode.cpp:52
void appendChild(AstNode *node)
This function add given node to the end of children list for current AstNode.
Definition: AstNode.cpp:135
bool hasAttribute(const std::string &name) const
This function find attribute in AstNode attributes.
Definition: AstNode.cpp:103
Encapsulates all information about a token.
Definition: Token.h:60
virtual ~AstNode()
This function is destructor of AstNode class.
Definition: AstNode.cpp:82
AstNode * m_root
Definition: AstNode.h:439
void dispatch()
This function call for dispatch function.
Definition: AstNode.h:417
size_t getIndex()
This function return index of current node in parent&#39;s list of children.
Definition: AstNode.cpp:158
std::vector< AstNode * > child_list_t
Definition: AstNode.h:41
AstNode * m_parent
Definition: AstNode.h:386
Value * getTokenValue()
This function returns value of token of this node.
Definition: AstNode.h:158
Dumps an AST tree.
Definition: AstNode.h:394
std::map< std::string, Value * > attribute_map_t
Definition: AstNode.h:39
AstPrinter(AstNode *root)
This function is constructor of AstPrinter class.
Definition: AstNode.h:404
int m_depth
Definition: AstNode.h:440
Definition: AstNode.h:26
AstNode(Token &token, AstNode *parent)
This function is constructor of AstNode class.
Definition: AstNode.h:74
std::stack< int > m_depthStack
Definition: AstNode.h:441