eRPC Generator (erpcgen)  Rev. 1.7.0
NXP Semiconductors
AstNode.h
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2014, Freescale Semiconductor, Inc.
4  * Copyright 2016 NXP
5  * All rights reserved.
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted (subject to the limitations in the disclaimer below) provided
10  * that the following conditions are met:
11  *
12  * o Redistributions of source code must retain the above copyright notice, this list
13  * of conditions and the following disclaimer.
14  *
15  * o Redistributions in binary form must reproduce the above copyright notice, this
16  * list of conditions and the following disclaimer in the documentation and/or
17  * other materials provided with the distribution.
18  *
19  * o Neither the name of the copyright holder nor the names of its
20  * contributors may be used to endorse or promote products derived from this
21  * software without specific prior written permission.
22  *
23  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef _EMBEDDED_RPC__ASTNODE_H_
37 #define _EMBEDDED_RPC__ASTNODE_H_
38 
39 #include "Token.h"
40 #include "smart_ptr.h"
41 #include <map>
42 #include <stack>
43 #include <string>
44 #include <typeinfo>
45 #include <vector>
46 
48 // Classes
50 
51 namespace erpcgen {
52 
61 class AstNode
62 {
63 public:
64  typedef std::map<std::string, Value *> attribute_map_t;
66  typedef std::vector<AstNode *> child_list_t;
67  typedef child_list_t::iterator iterator;
68  typedef child_list_t::const_iterator const_iterator;
81  AstNode(const Token &token)
82  : m_token(token)
83  , m_parent(nullptr)
84  {
85  }
86 
99  AstNode(Token &token, AstNode *parent)
100  : m_token(token)
101  , m_parent(parent)
102  {
103  }
104 
115  AstNode(const AstNode &other);
116 
128  AstNode &operator=(const AstNode &other);
129 
135  virtual ~AstNode();
136 
142  virtual AstNode *clone() const { return new AstNode(*this); }
143 
149  virtual std::string nodeName() const { return typeid(*this).name(); }
150 
152 
153 
160  Token &getToken() { return m_token; }
161 
169  const Token &getToken() const { return m_token; }
170 
176  void setToken(Token &token) { m_token = token; }
177 
184 
190  std::string getTokenString() { return m_token.getValue() ? m_token.getValue()->toString() : ""; }
192 
194 
195 
200  AstNode *getParent() const { return m_parent; }
201 
207  void setParent(AstNode *newParent) { m_parent = newParent; }
209 
211 
212 
217  size_t attributeCount() const { return m_attributes.size(); }
218 
232  bool hasAttribute(const std::string &name) const;
233 
247  Value *getAttribute(const std::string &name);
248 
261  void setAttribute(const std::string &name, Value *node);
262 
274  void removeAttribute(const std::string &name);
275 
286  Value *&operator[](const std::string &name) { return m_attributes[name]; }
288 
290 
291 
300  void appendChild(AstNode *node);
301 
307  size_t childCount() const { return m_children.size(); }
308 
319  AstNode *getChild(int index) const { return m_children[index]; }
320 
332  size_t getIndexOfChild(AstNode *child);
333 
342  size_t getIndex();
343 
354  void replaceChild(AstNode *original, AstNode *replacement);
355 
363  void reverseExpr();
364 
377  AstNode *&operator[](int index) { return m_children[index]; }
378 
391  const AstNode *operator[](int index) const { return m_children[index]; }
393 
395 
396  inline iterator begin() { return m_children.begin(); }
397  inline iterator end() { return m_children.end(); }
398  inline const_iterator begin() const { return m_children.begin(); }
399  inline const_iterator end() const { return m_children.end(); }
401 
407  std::string getDescription() const;
408 
409 protected:
412  attribute_map_t m_attributes;
413  child_list_t m_children;
414 };
415 
420 {
421 public:
430  : m_root(root)
431  , m_depth(0)
432  {
433  }
434 
442  void dispatch() { dispatch(m_root, 0); }
443 
453  void dispatch(AstNode *node, int childIndex = 0);
454 
461  void print(AstNode *node, int childIndex = 0);
462 
463 protected:
465  int m_depth;
466  std::stack<int> m_depthStack;
473  void printIndent(int indent) const;
474 };
475 
476 } // namespace erpcgen
477 
478 #endif // _EMBEDDED_RPC__ASTNODE_H_
AstNode * getChild(int index) const
This function return child node from node children.
Definition: AstNode.h:319
AstNode(const Token &token)
This function is constructor of AstNode class.
Definition: AstNode.h:81
Token & getToken()
This function returns token of this node.
Definition: AstNode.h:160
std::string getTokenString()
This function returns string representation of this node token value.
Definition: AstNode.h:190
void setParent(AstNode *newParent)
This function set parent for this node.
Definition: AstNode.h:207
Value * getAttribute(const std::string &name)
This function return attribute from AstNode attributes.
Definition: AstNode.cpp:133
Value *& operator[](const std::string &name)
Square brackets.
Definition: AstNode.h:286
void removeAttribute(const std::string &name)
This function remove attribute from AstNode attributes.
Definition: AstNode.cpp:151
size_t getIndexOfChild(AstNode *child)
This function return index of searched child node.
Definition: AstNode.cpp:168
void setToken(Token &token)
This function set token to this node.
Definition: AstNode.h:176
Token m_token
Definition: AstNode.h:410
child_list_t::const_iterator const_iterator
Definition: AstNode.h:68
AstNode * getParent() const
This function returns pointer to parent node.
Definition: AstNode.h:200
const Token & getToken() const
This function returns constant token of this node.
Definition: AstNode.h:169
attribute_map_t m_attributes
Definition: AstNode.h:412
virtual AstNode * clone() const
This function returns an exact duplicate of this object.
Definition: AstNode.h:142
child_list_t m_children
Definition: AstNode.h:413
virtual std::string nodeName() const
This function returns name of this node.
Definition: AstNode.h:149
const AstNode * operator[](int index) const
Square brackets.
Definition: AstNode.h:391
Homogeneous AST node class.
Definition: AstNode.h:61
Value * getValue()
This function returns token value.
Definition: Token.h:247
virtual std::string toString() const =0
Get Value type string representation.
child_list_t::iterator iterator
Definition: AstNode.h:67
std::string getDescription() const
This function returns string description of the node.
Definition: AstNode.cpp:212
size_t attributeCount() const
This function returns count of node attributes.
Definition: AstNode.h:217
void replaceChild(AstNode *original, AstNode *replacement)
This function replace child in AstNode tree.
Definition: AstNode.cpp:204
Abstract base class for values of arbitrary types.
Definition: Value.h:47
void setAttribute(const std::string &name, Value *node)
This function set attribute in AstNode attributes.
Definition: AstNode.cpp:146
size_t childCount() const
This function return count of children for current node.
Definition: AstNode.h:307
void reverseExpr()
This function replace children location in AstNode tree.
Definition: AstNode.cpp:187
AstNode *& operator[](int index)
Square brackets.
Definition: AstNode.h:377
AstNode & operator=(const AstNode &other)
Assignment operator.
Definition: AstNode.cpp:76
void appendChild(AstNode *node)
This function add given node to the end of children list for current AstNode.
Definition: AstNode.cpp:159
bool hasAttribute(const std::string &name) const
This function find attribute in AstNode attributes.
Definition: AstNode.cpp:127
Encapsulates all information about a token.
Definition: Token.h:86
virtual ~AstNode()
This function is destructor of AstNode class.
Definition: AstNode.cpp:106
AstNode * m_root
Definition: AstNode.h:464
void dispatch()
This function call for dispatch function.
Definition: AstNode.h:442
size_t getIndex()
This function return index of current node in parent&#39;s list of children.
Definition: AstNode.cpp:182
std::vector< AstNode * > child_list_t
Definition: AstNode.h:66
AstNode * m_parent
Definition: AstNode.h:411
Value * getTokenValue()
This function returns value of token of this node.
Definition: AstNode.h:183
Dumps an AST tree.
Definition: AstNode.h:419
std::map< std::string, Value * > attribute_map_t
Definition: AstNode.h:64
AstPrinter(AstNode *root)
This function is constructor of AstPrinter class.
Definition: AstNode.h:429
int m_depth
Definition: AstNode.h:465
Definition: AstNode.h:51
AstNode(Token &token, AstNode *parent)
This function is constructor of AstNode class.
Definition: AstNode.h:99
std::stack< int > m_depthStack
Definition: AstNode.h:466