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>
36 #include <ogdf/basic/HashArray.h>
39 #include <ogdf/fileformats/DOT.h>
41 
42 #include <cstdio>
43 #include <set>
44 #include <string>
45 #include <vector>
46 
47 namespace ogdf {
48 
49 namespace dot {
50 
51 class Parser;
52 struct SubgraphData;
53 
55 
112 class Ast {
113 public:
114  struct Graph;
115  struct StmtList;
116  struct NodeStmt;
117  struct EdgeStmt;
118  struct AttrStmt;
119  struct AsgnStmt;
120  struct Subgraph;
121  struct NodeId;
122  struct EdgeRhs;
123  struct AttrList;
124  struct AList;
125  struct Port;
126  struct CompassPt;
127 
128  struct Stmt;
129  struct EdgeLhs;
130 
131 private:
132  using Tokens = std::vector<Token>;
133  using Iterator = Tokens::const_iterator;
134 
137 
139 
140  Graph* parseGraph(Iterator current, Iterator& rest);
141  Subgraph* parseSubgraph(Iterator current, Iterator& rest);
142  NodeStmt* parseNodeStmt(Iterator current, Iterator& rest);
143  EdgeStmt* parseEdgeStmt(Iterator current, Iterator& rest);
144  AttrStmt* parseAttrStmt(Iterator current, Iterator& rest);
145  AsgnStmt* parseAsgnStmt(Iterator current, Iterator& rest);
146  EdgeRhs* parseEdgeRhs(Iterator current, Iterator& rest);
147  NodeId* parseNodeId(Iterator current, Iterator& rest);
148  Stmt* parseStmt(Iterator current, Iterator& rest);
149  StmtList* parseStmtList(Iterator current, Iterator& rest);
150  AttrList* parseAttrList(Iterator current, Iterator& rest);
151  AList* parseAList(Iterator current, Iterator& rest);
152  Port* parsePort(Iterator current, Iterator& rest);
153  CompassPt* parseCompassPt(Iterator current, Iterator& rest);
154 
155 public:
157 
160  explicit Ast(const Tokens& tokens);
161  ~Ast();
162 
164 
167  bool build();
168 
170  Graph* root() const;
171 
172  struct Graph {
173  const bool strict;
174  const bool directed;
175  std::string* id;
176 
178 
179  Graph(const bool& paramStrict, const bool& dir, std::string* idString,
180  StmtList* statementList);
181  ~Graph();
182 
183  bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
185  };
186 
187  struct StmtList {
190 
191  StmtList(Stmt* headSTMT, StmtList* tailStatementList);
192  ~StmtList();
193  };
194 
195  struct Stmt {
196  virtual ~Stmt() = 0;
197 
198  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
199  ClusterGraphAttributes* CA, const SubgraphData& data) = 0;
200  };
201 
202  struct NodeStmt : public Stmt {
205 
206  NodeStmt(NodeId* nodeID, AttrList* attrList);
207  ~NodeStmt();
208 
209  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
210  ClusterGraphAttributes* CA, const SubgraphData& data) override;
211  };
212 
213  struct EdgeStmt : public Stmt {
217 
218  EdgeStmt(EdgeLhs* edgeLHS, EdgeRhs* edgeRHS, AttrList* attrList);
219  ~EdgeStmt();
220 
221  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
222  ClusterGraphAttributes* CA, const SubgraphData& data) override;
223  };
224 
225  struct AsgnStmt : public Stmt {
226  const std::string lhs;
227  const std::string rhs;
228 
229  AsgnStmt(const std::string& lhsString, const std::string& rhsString);
230  ~AsgnStmt();
231 
232  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
233  ClusterGraphAttributes* CA, const SubgraphData& data) override;
234  };
235 
236  struct AttrStmt : public Stmt {
237  enum class Type { graph, edge, node };
238 
241 
242  AttrStmt(const Type& paramType, AttrList* attrList);
243  ~AttrStmt();
244 
245  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
246  ClusterGraphAttributes* CA, const SubgraphData& data) override;
247  };
248 
249  struct EdgeLhs {
250  virtual ~EdgeLhs() = 0;
251 
252  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
253  ClusterGraphAttributes* CA, const SubgraphData& data) = 0;
254  };
255 
256  struct Subgraph : public Stmt, EdgeLhs {
257  std::string* id;
259 
260  Subgraph(std::string* idString, StmtList* statementList);
261  ~Subgraph();
262 
263  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
264  ClusterGraphAttributes* CA, const SubgraphData& data) override;
265  };
266 
267  struct NodeId : public EdgeLhs {
268  const std::string id;
270 
271  NodeId(const std::string& idString, Port* paramPort);
272  ~NodeId();
273 
274  virtual bool read(Parser& P, ogdf::Graph& G, GraphAttributes* GA, ClusterGraph* C,
275  ClusterGraphAttributes* CA, const SubgraphData& data) override;
276  };
277 
278  struct CompassPt {
279  enum class Type { n, ne, e, se, s, sw, w, nw, c, wildcard };
281 
282  CompassPt(const Type& paramType);
283  ~CompassPt();
284  };
285 
286  struct Port {
287  std::string* id;
289 
290  Port(std::string* idString, CompassPt* compassPT);
291  ~Port();
292  };
293 
294  struct EdgeRhs {
297 
298  EdgeRhs(EdgeLhs* headEdgeLHS, EdgeRhs* tailEdgeRHS);
299  ~EdgeRhs();
300  };
301 
302  struct AttrList {
305 
306  AttrList(AList* headAList, AttrList* tailAttrList);
307  ~AttrList();
308  };
309 
310  struct AList {
313 
314  AList(AsgnStmt* headAsgnStmt, AList* tailAList);
315  ~AList();
316  };
317 };
318 
320 
327 class Parser {
328 private:
329  std::istream& m_in;
330 
331  // Maps node id to Graph node.
333 
335 
336 public:
338  explicit Parser(std::istream& in);
339 
340  bool read(Graph& G);
341  bool read(Graph& G, GraphAttributes& GA);
342  bool read(Graph& G, ClusterGraph& C);
343  bool read(Graph& G, ClusterGraph& C, ClusterGraphAttributes& CA);
344 
346 
358  const std::string& id);
359 };
360 
362 struct SubgraphData {
364  std::vector<Ast::AttrList*>& nodeDefaults;
365  std::vector<Ast::AttrList*>& edgeDefaults;
366  std::set<node>& nodes;
367 
369 
375  SubgraphData(cluster root, std::vector<Ast::AttrList*>& nodeDefaultsVector,
376  std::vector<Ast::AttrList*>& edgeDefaultsVector, std::set<node>& nodeSet);
377 
378 
380  SubgraphData withCluster(cluster newRootCluster) const;
381 
383  SubgraphData withDefaults(std::vector<Ast::AttrList*>& newNodeDefaults,
384  std::vector<Ast::AttrList*>& newEdgeDefaults) const;
386  SubgraphData withNodes(std::set<node>& newNodes) const;
387 };
388 
389 }
390 }
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: AugmentationModule.h:36
ogdf::dot::SubgraphData::nodeDefaults
std::vector< Ast::AttrList * > & nodeDefaults
Definition: DotParser.h:364
ogdf::GraphAttributes
Stores additional attributes of a graph (like layout information).
Definition: GraphAttributes.h:66
GraphAttributes.h
Declaration of class GraphAttributes which extends a Graph by additional attributes.
ogdf::dot::Ast::EdgeStmt
Definition: DotParser.h:213
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:329
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:174
ogdf::dot::Ast::CompassPt::Type::n
@ n
ogdf::dot::Ast::StmtList
Definition: DotParser.h:187
ogdf::dot::Ast::Port::compassPt
CompassPt * compassPt
Definition: DotParser.h:288
ogdf::dot::Ast::AsgnStmt::rhs
const std::string rhs
Definition: DotParser.h:227
ogdf::dot::Ast::parseStmt
Stmt * parseStmt(Iterator current, Iterator &rest)
ogdf::dot::Ast::EdgeStmt::attrs
AttrList * attrs
Definition: DotParser.h:216
ogdf::dot::Ast::EdgeRhs::head
EdgeLhs * head
Definition: DotParser.h:295
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:173
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:175
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:312
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:132
ogdf::dot::Ast::EdgeRhs::tail
EdgeRhs * tail
Definition: DotParser.h:296
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:215
ogdf::dot::Ast::CompassPt
Definition: DotParser.h:278
ogdf::dot::Ast::Iterator
Tokens::const_iterator Iterator
Definition: DotParser.h:133
ogdf::dot::Ast::NodeStmt
Definition: DotParser.h:202
ogdf::dot::Ast::CompassPt::Type::s
@ s
ogdf::dot::Ast::EdgeLhs
Definition: DotParser.h:249
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:188
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:294
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:195
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:362
ogdf::ClusterElement
Representation of clusters in a clustered graph.
Definition: ClusterGraph.h:55
ogdf::dot::Ast::NodeStmt::~NodeStmt
~NodeStmt()
ogdf::dot::Ast::Subgraph::id
std::string * id
Definition: DotParser.h:257
ogdf::dot::Ast::CompassPt::~CompassPt
~CompassPt()
ogdf::ClusterGraphAttributes
Stores additional attributes of a clustered graph (like layout information).
Definition: ClusterGraphAttributes.h:46
ogdf::dot::Ast::AttrList::~AttrList
~AttrList()
ogdf::dot::Ast::CompassPt::Type
Type
Definition: DotParser.h:279
ogdf::dot::Ast::parseSubgraph
Subgraph * parseSubgraph(Iterator current, Iterator &rest)
ogdf::dot::Ast::NodeId::port
Port * port
Definition: DotParser.h:269
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:258
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:239
ogdf::dot::Ast::NodeId
Definition: DotParser.h:267
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:240
ogdf::dot::Ast::m_graph
Graph * m_graph
Definition: DotParser.h:138
ogdf::dot::Ast::Graph
Definition: DotParser.h:172
ogdf::dot::Ast::Port::~Port
~Port()
ogdf::dot::Ast::AsgnStmt
Definition: DotParser.h:225
ogdf::dot::Ast::AsgnStmt::lhs
const std::string lhs
Definition: DotParser.h:226
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:237
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:862
ogdf::dot::Ast::CompassPt::Type::ne
@ ne
ogdf::dot::Ast::CompassPt::Type::e
@ e
ogdf::dot::Ast::AttrStmt
Definition: DotParser.h:236
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:304
ogdf::dot::Ast::Port::id
std::string * id
Definition: DotParser.h:287
ogdf::dot::Ast::NodeId::id
const std::string id
Definition: DotParser.h:268
ogdf::dot::Parser
DOT format parser class.
Definition: DotParser.h:327
ogdf::dot::Ast::StmtList::tail
StmtList * tail
Definition: DotParser.h:189
ogdf::dot::Ast::AList::head
AsgnStmt * head
Definition: DotParser.h:311
ogdf::dot::Parser::m_nodeId
HashArray< std::string, node > m_nodeId
Definition: DotParser.h:332
ogdf::dot::Ast::parseGraph
Graph * parseGraph(Iterator current, Iterator &rest)
ogdf::dot::SubgraphData::nodes
std::set< node > & nodes
Definition: DotParser.h:366
ogdf::dot::Ast::AttrStmt::~AttrStmt
~AttrStmt()
ogdf::dot::SubgraphData::rootCluster
cluster rootCluster
Definition: DotParser.h:363
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:177
ClusterGraphAttributes.h
Declares ClusterGraphAttributes, an extension of class GraphAttributes, to store clustergraph layout ...
ClusterGraph.h
Derived class of GraphObserver providing additional functionality to handle clustered graphs.
ogdf::dot::Ast::EdgeStmt::lhs
EdgeLhs * lhs
Definition: DotParser.h:214
ogdf::dot::Ast::NodeStmt::nodeId
NodeId * nodeId
Definition: DotParser.h:203
ogdf::dot::Ast::CompassPt::type
Type type
Definition: DotParser.h:280
ogdf::dot::Ast::AttrList
Definition: DotParser.h:302
ogdf::dot::Ast::Port
Definition: DotParser.h:286
ogdf::dot::SubgraphData::edgeDefaults
std::vector< Ast::AttrList * > & edgeDefaults
Definition: DotParser.h:365
ogdf::dot::Ast::Subgraph
Definition: DotParser.h:256
ogdf::dot::Ast::m_tokens
const Tokens m_tokens
Definition: DotParser.h:135
ogdf::dot::Ast
DOT format abstract syntax tree class, based on official documentation.
Definition: DotParser.h:112
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)
DOT.h
DOT related enums and string conversion functions.
ogdf::dot::Ast::AList
Definition: DotParser.h:310
ogdf::ClusterGraph
Representation of clustered graphs.
Definition: ClusterGraph.h:339
ogdf::dot::Ast::AttrList::head
AList * head
Definition: DotParser.h:303
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
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:136
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:204
ogdf::dot::Ast::AttrStmt::Type::edge
@ edge