Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

DotParser.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Graph.h>
35 #include <ogdf/basic/HashArray.h>
38 
39 #include <iosfwd>
40 #include <set>
41 #include <string>
42 #include <vector>
43 
44 namespace ogdf {
45 class ClusterGraphAttributes;
46 class GraphAttributes;
47 
48 namespace dot {
49 
50 class Parser;
51 struct SubgraphData;
52 
54 
111 class Ast {
112 public:
113  struct AList;
114  struct AsgnStmt;
115  struct AttrList;
116  struct AttrStmt;
117  struct CompassPt;
118  struct EdgeLhs;
119  struct EdgeRhs;
120  struct EdgeStmt;
121  struct Graph;
122  struct NodeId;
123  struct NodeStmt;
124  struct Port;
125  struct Stmt;
126  struct StmtList;
127  struct Subgraph;
128 
129 private:
130  using Tokens = std::vector<Token>;
131  using Iterator = Tokens::const_iterator;
132 
135 
137 
138  Graph* parseGraph(Iterator current, Iterator& rest);
139  Subgraph* parseSubgraph(Iterator current, Iterator& rest);
140  NodeStmt* parseNodeStmt(Iterator current, Iterator& rest);
141  EdgeStmt* parseEdgeStmt(Iterator current, Iterator& rest);
142  AttrStmt* parseAttrStmt(Iterator current, Iterator& rest);
143  AsgnStmt* parseAsgnStmt(Iterator current, Iterator& rest);
144  EdgeRhs* parseEdgeRhs(Iterator current, Iterator& rest);
145  NodeId* parseNodeId(Iterator current, Iterator& rest);
146  Stmt* parseStmt(Iterator current, Iterator& rest);
147  StmtList* parseStmtList(Iterator current, Iterator& rest);
148  AttrList* parseAttrList(Iterator current, Iterator& rest);
149  AList* parseAList(Iterator current, Iterator& rest);
150  Port* parsePort(Iterator current, Iterator& rest);
151  CompassPt* parseCompassPt(Iterator current, Iterator& rest);
152 
153 public:
155 
158  explicit Ast(const Tokens& tokens);
159  ~Ast();
160 
162 
165  bool build();
166 
168  Graph* root() const;
169 
170  struct Graph {
171  const bool strict;
172  const bool directed;
173  std::string* id;
174 
176 
177  Graph(const bool& paramStrict, const bool& dir, std::string* idString,
178  StmtList* statementList);
179  ~Graph();
180 
181  bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
183  };
184 
185  struct StmtList {
188 
189  StmtList(Stmt* headSTMT, StmtList* tailStatementList);
190  ~StmtList();
191  };
192 
193  struct Stmt {
194  virtual ~Stmt() = 0;
195 
196  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
197  ClusterGraphAttributes* CA, const SubgraphData& data) = 0;
198  };
199 
200  struct NodeStmt : public Stmt {
203 
204  NodeStmt(NodeId* nodeID, AttrList* attrList);
205  ~NodeStmt();
206 
207  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
208  ClusterGraphAttributes* CA, const SubgraphData& data) override;
209  };
210 
211  struct EdgeStmt : public Stmt {
215 
216  EdgeStmt(EdgeLhs* edgeLHS, EdgeRhs* edgeRHS, AttrList* attrList);
217  ~EdgeStmt();
218 
219  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
220  ClusterGraphAttributes* CA, const SubgraphData& data) override;
221  };
222 
223  struct AsgnStmt : public Stmt {
224  const std::string lhs;
225  const std::string rhs;
226 
227  AsgnStmt(const std::string& lhsString, const std::string& rhsString);
228  ~AsgnStmt();
229 
230  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
231  ClusterGraphAttributes* CA, const SubgraphData& data) override;
232  };
233 
234  struct AttrStmt : public Stmt {
235  enum class Type { graph, edge, node };
236 
239 
240  AttrStmt(const Type& paramType, AttrList* attrList);
241  ~AttrStmt();
242 
243  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
244  ClusterGraphAttributes* CA, const SubgraphData& data) override;
245  };
246 
247  struct EdgeLhs {
248  virtual ~EdgeLhs() = 0;
249 
250  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
251  ClusterGraphAttributes* CA, const SubgraphData& data) = 0;
252  };
253 
254  struct Subgraph : public Stmt, EdgeLhs {
255  std::string* id;
257 
258  Subgraph(std::string* idString, StmtList* statementList);
259  ~Subgraph();
260 
261  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
262  ClusterGraphAttributes* CA, const SubgraphData& data) override;
263  };
264 
265  struct NodeId : public EdgeLhs {
266  const std::string id;
268 
269  NodeId(const std::string& idString, Port* paramPort);
270  ~NodeId();
271 
272  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
273  ClusterGraphAttributes* CA, const SubgraphData& data) override;
274  };
275 
276  struct CompassPt {
277  enum class Type { n, ne, e, se, s, sw, w, nw, c, wildcard };
279 
280  CompassPt(const Type& paramType);
281  ~CompassPt();
282  };
283 
284  struct Port {
285  std::string* id;
287 
288  Port(std::string* idString, CompassPt* compassPT);
289  ~Port();
290  };
291 
292  struct EdgeRhs {
295 
296  EdgeRhs(EdgeLhs* headEdgeLHS, EdgeRhs* tailEdgeRHS);
297  ~EdgeRhs();
298  };
299 
300  struct AttrList {
303 
304  AttrList(AList* headAList, AttrList* tailAttrList);
305  ~AttrList();
306  };
307 
308  struct AList {
311 
312  AList(AsgnStmt* headAsgnStmt, AList* tailAList);
313  ~AList();
314  };
315 };
316 
318 
325 class Parser {
326 private:
327  std::istream& m_in;
328 
329  // Maps node id to Graph node.
331 
333 
334 public:
336  explicit Parser(std::istream& in);
337 
338  bool read(Graph& G);
339  bool read(Graph& G, GraphAttributes& GA);
340  bool read(Graph& G, ClusterGraph& C);
341  bool read(Graph& G, ClusterGraph& C, ClusterGraphAttributes& CA);
342 
344 
356  const std::string& id);
357 };
358 
360 struct SubgraphData {
362  std::vector<Ast::AttrList*>& nodeDefaults;
363  std::vector<Ast::AttrList*>& edgeDefaults;
364  std::set<node>& nodes;
365 
367 
373  SubgraphData(cluster root, std::vector<Ast::AttrList*>& nodeDefaultsVector,
374  std::vector<Ast::AttrList*>& edgeDefaultsVector, std::set<node>& nodeSet);
375 
376 
378  SubgraphData withCluster(cluster newRootCluster) const;
379 
381  SubgraphData withDefaults(std::vector<Ast::AttrList*>& newNodeDefaults,
382  std::vector<Ast::AttrList*>& newEdgeDefaults) const;
384  SubgraphData withNodes(std::set<node>& newNodes) const;
385 };
386 
387 }
388 }
ogdf::dot::Ast::parsePort
Port * parsePort(Iterator current, Iterator &rest)
ogdf::dot::SubgraphData::SubgraphData
SubgraphData(cluster root, std::vector< Ast::AttrList * > &nodeDefaultsVector, std::vector< Ast::AttrList * > &edgeDefaultsVector, std::set< node > &nodeSet)
Initializes structure with given data.
ogdf::dot::Ast::AList::~AList
~AList()
HashArray.h
Declaration and implementation of HashArray class.
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::dot::SubgraphData::nodeDefaults
std::vector< Ast::AttrList * > & nodeDefaults
Definition: DotParser.h:362
ogdf::GraphAttributes
Stores additional attributes of a graph (like layout information).
Definition: GraphAttributes.h:72
ogdf::dot::Ast::EdgeStmt
Definition: DotParser.h:211
ogdf::dot::Ast::NodeId::~NodeId
~NodeId()
ogdf::dot::Ast::parseNodeId
NodeId * parseNodeId(Iterator current, Iterator &rest)
Graph.h
Includes declaration of graph class.
ogdf::dot::Ast::AsgnStmt::AsgnStmt
AsgnStmt(const std::string &lhsString, const std::string &rhsString)
ogdf::dot::Ast::CompassPt::Type::w
@ w
ogdf::dot::Ast::EdgeRhs::EdgeRhs
EdgeRhs(EdgeLhs *headEdgeLHS, EdgeRhs *tailEdgeRHS)
ogdf::dot::Parser::m_in
std::istream & m_in
Definition: DotParser.h:327
ogdf::dot::Ast::Graph::read
bool read(Parser &P, ogdf::Graph &G, GraphAttributes *GA, ClusterGraph *C, ClusterGraphAttributes *CA)
ogdf::dot::Ast::Graph::directed
const bool directed
Definition: DotParser.h:172
ogdf::dot::Ast::CompassPt::Type::n
@ n
ogdf::dot::Ast::StmtList
Definition: DotParser.h:185
ogdf::dot::Ast::Port::compassPt
CompassPt * compassPt
Definition: DotParser.h:286
ogdf::dot::Ast::AsgnStmt::rhs
const std::string rhs
Definition: DotParser.h:225
ogdf::dot::Ast::parseStmt
Stmt * parseStmt(Iterator current, Iterator &rest)
ogdf::dot::Ast::EdgeStmt::attrs
AttrList * attrs
Definition: DotParser.h:214
ogdf::dot::Ast::EdgeRhs::head
EdgeLhs * head
Definition: DotParser.h:293
ogdf::dot::Ast::EdgeStmt::read
virtual bool read(Parser &P, ogdf::Graph &G, GraphAttributes *GA, ClusterGraph *C, ClusterGraphAttributes *CA, const SubgraphData &data) override
ogdf::dot::Ast::parseAsgnStmt
AsgnStmt * parseAsgnStmt(Iterator current, Iterator &rest)
ogdf::dot::Parser::requestNode
node requestNode(Graph &G, GraphAttributes *GA, ClusterGraph *C, const SubgraphData &data, const std::string &id)
Perfoms a node query, returning node for given attribute.
ogdf::dot::Ast::Graph::strict
const bool strict
Definition: DotParser.h:171
ogdf::dot::Ast::parseAList
AList * parseAList(Iterator current, Iterator &rest)
ogdf::dot::Ast::CompassPt::Type::nw
@ nw
DotLexer.h
Declares stuff related to DOT format lexical analysis.
ogdf::dot::Ast::AList::AList
AList(AsgnStmt *headAsgnStmt, AList *tailAList)
ogdf::dot::Ast::Graph::id
std::string * id
Definition: DotParser.h:173
ogdf::dot::Ast::parseAttrStmt
AttrStmt * parseAttrStmt(Iterator current, Iterator &rest)
ogdf::dot::Ast::CompassPt::Type::se
@ se
ogdf::dot::Ast::AList::tail
AList * tail
Definition: DotParser.h:310
ogdf::dot::Ast::EdgeRhs::~EdgeRhs
~EdgeRhs()
ogdf::dot::SubgraphData::withNodes
SubgraphData withNodes(std::set< node > &newNodes) const
Returns almost the same structure, but with new node list.
ogdf::dot::Ast::NodeStmt::NodeStmt
NodeStmt(NodeId *nodeID, AttrList *attrList)
ogdf::dot::Parser::read
bool read(Graph &G)
ogdf::dot::Ast::EdgeStmt::EdgeStmt
EdgeStmt(EdgeLhs *edgeLHS, EdgeRhs *edgeRHS, AttrList *attrList)
ogdf::dot::Ast::Tokens
std::vector< Token > Tokens
Definition: DotParser.h:130
ogdf::dot::Ast::EdgeRhs::tail
EdgeRhs * tail
Definition: DotParser.h:294
ogdf::dot::Ast::AsgnStmt::read
virtual bool read(Parser &P, ogdf::Graph &G, GraphAttributes *GA, ClusterGraph *C, ClusterGraphAttributes *CA, const SubgraphData &data) override
ogdf::dot::Ast::AttrStmt::Type::graph
@ graph
ogdf::dot::Ast::EdgeStmt::rhs
EdgeRhs * rhs
Definition: DotParser.h:213
ogdf::dot::Ast::CompassPt
Definition: DotParser.h:276
ogdf::dot::Ast::Iterator
Tokens::const_iterator Iterator
Definition: DotParser.h:131
ogdf::dot::Ast::NodeStmt
Definition: DotParser.h:200
ogdf::dot::Ast::CompassPt::Type::s
@ s
ogdf::dot::Ast::EdgeLhs
Definition: DotParser.h:247
ogdf::dot::Ast::CompassPt::Type::wildcard
@ wildcard
ogdf::dot::Parser::Parser
Parser(std::istream &in)
Initializes parser class with given input (but does nothing to it).
ogdf::dot::Ast::CompassPt::CompassPt
CompassPt(const Type &paramType)
ogdf::dot::Ast::StmtList::head
Stmt * head
Definition: DotParser.h:186
ogdf::dot::Ast::EdgeLhs::read
virtual bool read(Parser &P, ogdf::Graph &G, GraphAttributes *GA, ClusterGraph *C, ClusterGraphAttributes *CA, const SubgraphData &data)=0
ogdf::dot::Ast::EdgeRhs
Definition: DotParser.h:292
ogdf::dot::Ast::CompassPt::Type::c
@ c
ogdf::dot::SubgraphData::withDefaults
SubgraphData withDefaults(std::vector< Ast::AttrList * > &newNodeDefaults, std::vector< Ast::AttrList * > &newEdgeDefaults) const
Returns almost the same structure, but with new defaults.
ogdf::dot::Ast::Stmt::~Stmt
virtual ~Stmt()=0
ogdf::dot::Ast::Stmt
Definition: DotParser.h:193
ogdf::dot::Parser::readGraph
bool readGraph(Graph &G, GraphAttributes *GA, ClusterGraph *C, ClusterGraphAttributes *CA)
ogdf::dot::SubgraphData
A helper structure containing information for recursive graph reading.
Definition: DotParser.h:360
ogdf::ClusterElement
Representation of clusters in a clustered graph.
Definition: ClusterGraph.h:62
ogdf::dot::Ast::NodeStmt::~NodeStmt
~NodeStmt()
ogdf::dot::Ast::Subgraph::id
std::string * id
Definition: DotParser.h:255
ogdf::dot::Ast::CompassPt::~CompassPt
~CompassPt()
ogdf::ClusterGraphAttributes
Stores additional attributes of a clustered graph (like layout information).
Definition: ClusterGraphAttributes.h:52
ogdf::dot::Ast::AttrList::~AttrList
~AttrList()
ogdf::dot::Ast::CompassPt::Type
Type
Definition: DotParser.h:277
ogdf::dot::Ast::parseSubgraph
Subgraph * parseSubgraph(Iterator current, Iterator &rest)
ogdf::dot::Ast::NodeId::port
Port * port
Definition: DotParser.h:267
ogdf::dot::Ast::Stmt::read
virtual bool read(Parser &P, ogdf::Graph &G, GraphAttributes *GA, ClusterGraph *C, ClusterGraphAttributes *CA, const SubgraphData &data)=0
ogdf::dot::Ast::Subgraph::statements
StmtList * statements
Definition: DotParser.h:256
ogdf::dot::Ast::AsgnStmt::~AsgnStmt
~AsgnStmt()
ogdf::dot::Ast::AttrStmt::read
virtual bool read(Parser &P, ogdf::Graph &G, GraphAttributes *GA, ClusterGraph *C, ClusterGraphAttributes *CA, const SubgraphData &data) override
ogdf::dot::SubgraphData::withCluster
SubgraphData withCluster(cluster newRootCluster) const
Returns almost the same structure, but with root cluster.
ogdf::dot::Ast::StmtList::~StmtList
~StmtList()
ogdf::dot::Ast::build
bool build()
Builds the DOT format AST.
ogdf::dot::Ast::parseNodeStmt
NodeStmt * parseNodeStmt(Iterator current, Iterator &rest)
ogdf::HashArray
Indexed arrays using hashing for element access.
Definition: HashArray.h:93
ogdf::dot::Ast::StmtList::StmtList
StmtList(Stmt *headSTMT, StmtList *tailStatementList)
ogdf::dot::Ast::AttrStmt::type
Type type
Definition: DotParser.h:237
ogdf::dot::Ast::NodeId
Definition: DotParser.h:265
ogdf::dot::Ast::AttrStmt::AttrStmt
AttrStmt(const Type &paramType, AttrList *attrList)
ogdf::dot::Ast::Port::Port
Port(std::string *idString, CompassPt *compassPT)
ogdf::dot::Ast::AttrStmt::attrs
AttrList * attrs
Definition: DotParser.h:238
ogdf::dot::Ast::m_graph
Graph * m_graph
Definition: DotParser.h:136
ogdf::dot::Ast::Graph
Definition: DotParser.h:170
ogdf::dot::Ast::Port::~Port
~Port()
ogdf::dot::Ast::AsgnStmt
Definition: DotParser.h:223
ogdf::dot::Ast::AsgnStmt::lhs
const std::string lhs
Definition: DotParser.h:224
ogdf::dot::Ast::Ast
Ast(const Tokens &tokens)
Initializes AST building but does not trigger the process itself.
ogdf::dot::Ast::AttrStmt::Type
Type
Definition: DotParser.h:235
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
ogdf::dot::Ast::CompassPt::Type::ne
@ ne
ogdf::dot::Ast::CompassPt::Type::e
@ e
ogdf::dot::Ast::AttrStmt
Definition: DotParser.h:234
ogdf::dot::Ast::Subgraph::Subgraph
Subgraph(std::string *idString, StmtList *statementList)
ogdf::dot::Ast::parseAttrList
AttrList * parseAttrList(Iterator current, Iterator &rest)
ogdf::dot::Ast::AttrList::tail
AttrList * tail
Definition: DotParser.h:302
ogdf::dot::Ast::Port::id
std::string * id
Definition: DotParser.h:285
ogdf::dot::Ast::NodeId::id
const std::string id
Definition: DotParser.h:266
ogdf::dot::Parser
DOT format parser class.
Definition: DotParser.h:325
ogdf::dot::Ast::StmtList::tail
StmtList * tail
Definition: DotParser.h:187
ogdf::dot::Ast::AList::head
AsgnStmt * head
Definition: DotParser.h:309
ogdf::dot::Parser::m_nodeId
HashArray< std::string, node > m_nodeId
Definition: DotParser.h:330
ogdf::dot::Ast::parseGraph
Graph * parseGraph(Iterator current, Iterator &rest)
ogdf::dot::SubgraphData::nodes
std::set< node > & nodes
Definition: DotParser.h:364
ogdf::dot::Ast::AttrStmt::~AttrStmt
~AttrStmt()
ogdf::dot::SubgraphData::rootCluster
cluster rootCluster
Definition: DotParser.h:361
ogdf::dot::Ast::Graph::Graph
Graph(const bool &paramStrict, const bool &dir, std::string *idString, StmtList *statementList)
ogdf::dot::Ast::Subgraph::~Subgraph
~Subgraph()
ogdf::dot::Ast::root
Graph * root() const
Returns the root of the AST (nullptr if none).
ogdf::dot::Ast::Graph::statements
StmtList * statements
Definition: DotParser.h:175
ClusterGraph.h
Derived class of GraphObserver providing additional functionality to handle clustered graphs.
ogdf::dot::Ast::EdgeStmt::lhs
EdgeLhs * lhs
Definition: DotParser.h:212
ogdf::dot::Ast::NodeStmt::nodeId
NodeId * nodeId
Definition: DotParser.h:201
ogdf::dot::Ast::CompassPt::type
Type type
Definition: DotParser.h:278
ogdf::dot::Ast::AttrList
Definition: DotParser.h:300
ogdf::dot::Ast::Port
Definition: DotParser.h:284
ogdf::dot::SubgraphData::edgeDefaults
std::vector< Ast::AttrList * > & edgeDefaults
Definition: DotParser.h:363
ogdf::dot::Ast::Subgraph
Definition: DotParser.h:254
ogdf::dot::Ast::m_tokens
const Tokens m_tokens
Definition: DotParser.h:133
ogdf::dot::Ast
DOT format abstract syntax tree class, based on official documentation.
Definition: DotParser.h:111
ogdf::dot::Ast::NodeId::NodeId
NodeId(const std::string &idString, Port *paramPort)
ogdf::dot::Ast::parseStmtList
StmtList * parseStmtList(Iterator current, Iterator &rest)
ogdf::dot::Ast::parseEdgeStmt
EdgeStmt * parseEdgeStmt(Iterator current, Iterator &rest)
ogdf::dot::Ast::AList
Definition: DotParser.h:308
ogdf::ClusterGraph
Representation of clustered graphs.
Definition: ClusterGraph.h:346
ogdf::dot::Ast::AttrList::head
AList * head
Definition: DotParser.h:301
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::dot::Ast::~Ast
~Ast()
ogdf::dot::Ast::EdgeLhs::~EdgeLhs
virtual ~EdgeLhs()=0
ogdf::dot::Ast::m_tend
const Iterator m_tend
Definition: DotParser.h:134
ogdf::dot::Ast::AttrList::AttrList
AttrList(AList *headAList, AttrList *tailAttrList)
ogdf::dot::Ast::parseCompassPt
CompassPt * parseCompassPt(Iterator current, Iterator &rest)
ogdf::dot::Ast::NodeId::read
virtual bool read(Parser &P, ogdf::Graph &G, GraphAttributes *GA, ClusterGraph *C, ClusterGraphAttributes *CA, const SubgraphData &data) override
ogdf::dot::Ast::CompassPt::Type::sw
@ sw
ogdf::dot::Ast::Graph::~Graph
~Graph()
ogdf::dot::Ast::NodeStmt::read
virtual bool read(Parser &P, ogdf::Graph &G, GraphAttributes *GA, ClusterGraph *C, ClusterGraphAttributes *CA, const SubgraphData &data) override
ogdf::dot::Ast::parseEdgeRhs
EdgeRhs * parseEdgeRhs(Iterator current, Iterator &rest)
ogdf::dot::Ast::Subgraph::read
virtual bool read(Parser &P, ogdf::Graph &G, GraphAttributes *GA, ClusterGraph *C, ClusterGraphAttributes *CA, const SubgraphData &data) override
ogdf::dot::Ast::EdgeStmt::~EdgeStmt
~EdgeStmt()
ogdf::dot::Ast::NodeStmt::attrs
AttrList * attrs
Definition: DotParser.h:202
ogdf::dot::Ast::AttrStmt::Type::edge
@ edge