OMNeT++ NEDXML  6.0.3
astnode.h
Go to the documentation of this file.
1 //==========================================================================
2 // astnode.h -
3 //
4 // OMNeT++/OMNEST
5 // Discrete System Simulation in C++
6 //
7 // Contents:
8 // class ASTNode
9 //
10 //==========================================================================
11 
12 /*--------------------------------------------------------------*
13  Copyright (C) 2002-2017 Andras Varga
14  Copyright (C) 2006-2017 OpenSim Ltd.
15 
16  This file is distributed WITHOUT ANY WARRANTY. See the file
17  `license' for details on this and other legal matters.
18 *--------------------------------------------------------------*/
19 
20 #ifndef __OMNETPP_NEDXML_ASTNODE_H
21 #define __OMNETPP_NEDXML_ASTNODE_H
22 
23 #ifdef _MSC_VER
24 // disable 4996: VC8.0 warnings on Unix syscalls
25 #pragma warning(disable: 4996)
26 #endif
27 
28 #include <string>
29 #include "nedxmldefs.h"
30 #include "common/pooledstring.h"
31 
32 namespace omnetpp {
33 namespace nedxml {
34 
35 using omnetpp::common::opp_staticpooledstring;
36 
43 {
44  public:
46  UserData() {}
47 
49  virtual ~UserData() {}
50 };
51 
52 
60 {
61  int startLine = 0;
62  int startColumn = 0;
63  int endLine = 0;
64  int endColumn = 0;
65 };
66 
68 {
69  opp_staticpooledstring file;
70  int line = -1;
71 
72  FileLine() {}
73  FileLine(const char *file, int line=-1) : file(file), line(line) {}
74  bool empty() const {return file.empty();}
75  std::string str() const {return empty() ? "" : line == -1 ? file.str() : file.str() + ":" + std::to_string(line);}
76 };
77 
88 {
89  private:
90  long id;
91  FileLine srcLoc;
92  opp_staticpooledstring directory;
93  SourceRegion srcRegion;
94  ASTNode *parent = nullptr;
95  ASTNode *firstChild = nullptr;
96  ASTNode *lastChild = nullptr;
97  ASTNode *prevSibling = nullptr;
98  ASTNode *nextSibling = nullptr;
99  UserData *userData = nullptr;
100 
101  static long lastId;
102  static long numCreated;
103  static long numExisting;
104 
105  protected:
106  static bool stringToBool(const char *s);
107  static const char *boolToString(bool b);
108  static int stringToEnum(const char *s, const char *vals[], int nums[], int n);
109  static const char *enumToString(int b, const char *vals[], int nums[], int n);
110  static void validateEnum(int b, const char *vals[], int nums[], int n);
111 
112  public:
115 
119  ASTNode();
120 
124  ASTNode(ASTNode *parent);
125 
129  virtual ~ASTNode();
130 
135  virtual ASTNode *dup() const = 0;
136 
140  virtual ASTNode *dupTree() const;
142 
145 
150  virtual const char *getTagName() const = 0;
151 
156  virtual int getTagCode() const = 0;
157 
161  virtual long getId() const;
162 
166  virtual void setId(long id);
167 
171  virtual FileLine getSourceLocation() const;
172 
176  virtual void setSourceLocation(FileLine loc);
177 
181  void setSourceLocation(const char *fileName, int lineNumber) {setSourceLocation(FileLine(fileName,lineNumber));}
182 
186  virtual const char *getSourceFileName() const;
187 
191  virtual int getSourceLineNumber() const;
192 
196  virtual const char *getSourceFileDirectory() const;
197 
202  virtual const SourceRegion& getSourceRegion() const;
203 
208  virtual void setSourceRegion(const SourceRegion& region);
210 
213 
220  virtual void applyDefaults();
221 
226  virtual int getNumAttributes() const = 0;
227 
235  virtual const char *getAttributeName(int k) const = 0;
236 
241  virtual int lookupAttribute(const char *attr) const;
242 
251  virtual const char *getAttribute(int k) const = 0;
252 
259  virtual const char *getAttribute(const char *attr) const;
260 
269  virtual void setAttribute(int k, const char *value) = 0;
270 
277  virtual void setAttribute(const char *attr, const char *value);
278 
287  virtual const char *getAttributeDefault(int k) const = 0;
288 
295  virtual const char *getAttributeDefault(const char *attr) const;
297 
300 
304  virtual ASTNode *getParent() const;
305 
310  virtual ASTNode *getFirstChild() const;
311 
316  virtual ASTNode *getLastChild() const;
317 
333  virtual ASTNode *getNextSibling() const;
334 
339  virtual ASTNode *getPrevSibling() const;
340 
346  virtual void appendChild(ASTNode *node);
347 
356  virtual void insertChildBefore(ASTNode *where, ASTNode *newnode);
357 
363  virtual ASTNode *removeChild(ASTNode *node);
364 
369  virtual ASTNode *getFirstChildWithTag(int tagcode) const;
370 
385  virtual ASTNode *getNextSiblingWithTag(int tagcode) const;
386 
391  virtual ASTNode *getPreviousSiblingWithTag(int tagcode) const;
392 
396  virtual int getNumChildren() const;
397 
401  virtual int getNumChildrenWithTag(int tagcode) const;
403 
411  ASTNode *getFirstChildWithAttribute(int tagcode, const char *attr, const char *attrvalue=nullptr);
412 
417  ASTNode *getParentWithTag(int tagcode);
419 
422 
426  static long getNumCreated() {return numCreated;}
427 
432  static long getNumExisting() {return numExisting;}
434 
437 
441  virtual void setUserData(UserData *data);
442 
447  virtual UserData *getUserData() const;
449 };
450 
453 
460 {
461  public:
462  virtual ~ASTNodeFactory() {}
463 
467  virtual ASTNode *createElementWithTag(const char *tagName) = 0;
468 
472  virtual ASTNode *createElementWithTag(int tagCode) = 0;
473 };
474 
475 } // namespace nedxml
476 } // namespace omnetpp
477 
478 
479 #endif
480 
omnetpp::nedxml::FileLine
Definition: astnode.h:67
omnetpp::nedxml::createElementWithTag
ASTNode * createElementWithTag(ParseContext *np, ASTNodeFactory *factory, int tagcode, ASTNode *parent=nullptr)
omnetpp::nedxml::UserData
Subclass from this if you want to attach extra data to ASTNode objects.
Definition: astnode.h:42
omnetpp::nedxml::SourceRegion::endLine
int endLine
Definition: astnode.h:63
omnetpp::nedxml::SourceRegion
Stores a line:col..line:col region in a source file. Used for mapping ASTNodes back to the source cod...
Definition: astnode.h:59
omnetpp::nedxml::NedElement
ASTNode NedElement
Definition: astnode.h:451
omnetpp::nedxml::SourceRegion::endColumn
int endColumn
Definition: astnode.h:64
omnetpp::nedxml::UserData::UserData
UserData()
Definition: astnode.h:46
omnetpp::nedxml::SourceRegion::startLine
int startLine
Definition: astnode.h:61
NEDXML_API
#define NEDXML_API
Definition: nedxmldefs.h:31
omnetpp
Definition: astbuilder.h:25
omnetpp::nedxml::ASTNode::setSourceLocation
void setSourceLocation(const char *fileName, int lineNumber)
Definition: astnode.h:181
omnetpp::nedxml::FileLine::file
opp_staticpooledstring file
Definition: astnode.h:69
omnetpp::nedxml::ASTNodeFactory::~ASTNodeFactory
virtual ~ASTNodeFactory()
Definition: astnode.h:462
omnetpp::nedxml::ASTNode
Definition: astnode.h:87
omnetpp::nedxml::ASTNode::getNumExisting
static long getNumExisting()
Definition: astnode.h:432
omnetpp::nedxml::FileLine::FileLine
FileLine(const char *file, int line=-1)
Definition: astnode.h:73
omnetpp::nedxml::UserData::~UserData
virtual ~UserData()
Definition: astnode.h:49
omnetpp::nedxml::ASTNodeFactory
Base class for ASTNode factories.
Definition: astnode.h:459
omnetpp::nedxml::FileLine::FileLine
FileLine()
Definition: astnode.h:72
nedxmldefs.h
omnetpp::nedxml::FileLine::empty
bool empty() const
Definition: astnode.h:74
omnetpp::nedxml::SourceRegion::startColumn
int startColumn
Definition: astnode.h:62
omnetpp::nedxml::MsgElement
ASTNode MsgElement
Definition: astnode.h:452
omnetpp::nedxml::ASTNode::getNumCreated
static long getNumCreated()
Definition: astnode.h:426
omnetpp::nedxml::FileLine::str
std::string str() const
Definition: astnode.h:75