Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

DotParser.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Graph.h>
38 #include <ogdf/basic/HashArray.h>
39 
40 #include <ogdf/fileformats/DOT.h>
42 
43 #include <cstdio>
44 #include <vector>
45 #include <set>
46 #include <string>
47 
48 
49 namespace ogdf {
50 
51 namespace dot {
52 
53 class Parser;
54 struct SubgraphData;
55 
57 
114 class Ast {
115 public:
116  struct Graph;
117  struct StmtList;
118  struct NodeStmt;
119  struct EdgeStmt;
120  struct AttrStmt;
121  struct AsgnStmt;
122  struct Subgraph;
123  struct NodeId;
124  struct EdgeRhs;
125  struct AttrList;
126  struct AList;
127  struct Port;
128  struct CompassPt;
129 
130  struct Stmt;
131  struct EdgeLhs;
132 
133 private:
134  using Tokens = std::vector<Token>;
135  using Iterator = Tokens::const_iterator;
136 
139 
141 
142  Graph *parseGraph(
143  Iterator current, Iterator &rest);
145  Iterator current, Iterator &rest);
147  Iterator current, Iterator &rest);
149  Iterator current, Iterator &rest);
151  Iterator current, Iterator &rest);
153  Iterator current, Iterator &rest);
155  Iterator current, Iterator &rest);
157  Iterator current, Iterator &rest);
158  Stmt *parseStmt(
159  Iterator current, Iterator &rest);
161  Iterator current, Iterator &rest);
163  Iterator current, Iterator &rest);
164  AList *parseAList(
165  Iterator current, Iterator &rest);
166  Port *parsePort(
167  Iterator current, Iterator &rest);
169  Iterator current, Iterator &rest);
170 
171 public:
173 
176  explicit Ast(const Tokens &tokens);
177  ~Ast();
178 
180 
183  bool build();
184 
186  Graph *root() const;
187 
188  struct Graph {
189  const bool strict;
190  const bool directed;
191  std::string *id;
192 
194 
195  Graph(
196  const bool &paramStrict,
197  const bool &dir,
198  std::string *idString,
199  StmtList *statementList);
200  ~Graph();
201 
202  bool read(
203  Parser &P,
206  };
207 
208  struct StmtList {
211 
212  StmtList(
213  Stmt *headSTMT,
214  StmtList *tailStatementList);
215  ~StmtList();
216  };
217 
218  struct Stmt {
219  virtual ~Stmt() = 0;
220 
221  virtual bool read(
222  Parser &P,
225  const SubgraphData &data) = 0;
226  };
227 
228  struct NodeStmt : public Stmt {
231 
232  NodeStmt(
233  NodeId *nodeID,
234  AttrList *attrList);
235  ~NodeStmt();
236 
237  virtual bool read(
238  Parser &P,
241  const SubgraphData &data) override;
242  };
243 
244  struct EdgeStmt : public Stmt {
248 
249  EdgeStmt(
250  EdgeLhs *edgeLHS,
251  EdgeRhs *edgeRHS,
252  AttrList *attrList);
253  ~EdgeStmt();
254 
255  virtual bool read(
256  Parser &P,
259  const SubgraphData &data) override;
260  };
261 
262  struct AsgnStmt : public Stmt {
263  const std::string lhs;
264  const std::string rhs;
265 
266  AsgnStmt(
267  const std::string &lhsString,
268  const std::string &rhsString);
269  ~AsgnStmt();
270 
271  virtual bool read(
272  Parser &P,
275  const SubgraphData &data) override;
276  };
277 
278  struct AttrStmt : public Stmt {
279  enum class Type { graph, edge, node };
280 
283 
284  AttrStmt(
285  const Type &paramType,
286  AttrList *attrList);
287  ~AttrStmt();
288 
289  virtual bool read(
290  Parser &P,
293  const SubgraphData &data) override;
294  };
295 
296  struct EdgeLhs {
297  virtual ~EdgeLhs() = 0;
298 
299  virtual bool read(
300  Parser &P,
303  const SubgraphData &data) = 0;
304  };
305 
306  struct Subgraph : public Stmt, EdgeLhs {
307  std::string *id;
309 
310  Subgraph(
311  std::string *idString,
312  StmtList *statementList);
313  ~Subgraph();
314 
315  virtual bool read(
316  Parser &P,
319  const SubgraphData &data) override;
320  };
321 
322  struct NodeId : public EdgeLhs {
323  const std::string id;
325 
326  NodeId(
327  const std::string &idString,
328  Port *paramPort);
329  ~NodeId();
330 
331  virtual bool read(
332  Parser &P,
335  const SubgraphData &data) override;
336  };
337 
338  struct CompassPt {
339  enum class Type { n, ne, e, se, s, sw, w, nw, c, wildcard };
341 
342  CompassPt(
343  const Type &paramType);
344  ~CompassPt();
345  };
346 
347  struct Port {
348  std::string *id;
350 
351  Port(
352  std::string *idString,
353  CompassPt *compassPT);
354  ~Port();
355  };
356 
357  struct EdgeRhs {
360 
361  EdgeRhs(
362  EdgeLhs *headEdgeLHS,
363  EdgeRhs *tailEdgeRHS);
364  ~EdgeRhs();
365  };
366 
367  struct AttrList {
370 
371  AttrList(
372  AList *headAList,
373  AttrList *tailAttrList);
374  ~AttrList();
375  };
376 
377  struct AList {
380 
381  AList(
382  AsgnStmt *headAsgnStmt,
383  AList *tailAList);
384  ~AList();
385  };
386 };
387 
388 
390 
397 class Parser {
398 private:
399  std::istream &m_in;
400 
401  // Maps node id to Graph node.
403 
404  bool readGraph(
405  Graph &G, GraphAttributes *GA,
407 
408 public:
410  explicit Parser(std::istream &in);
411 
412  bool read(Graph &G);
413  bool read(Graph &G, GraphAttributes &GA);
414  bool read(Graph &G, ClusterGraph &C);
415  bool read(Graph &G, ClusterGraph &C, ClusterGraphAttributes &CA);
416 
418 
429  node requestNode(
430  Graph &G, GraphAttributes *GA, ClusterGraph *C,
431  const SubgraphData &data,
432  const std::string &id);
433 };
434 
435 
437 struct SubgraphData {
439  std::vector<Ast::AttrList *> &nodeDefaults;
440  std::vector<Ast::AttrList *> &edgeDefaults;
441  std::set<node> &nodes;
442 
444 
450  SubgraphData(
451  cluster root,
452  std::vector<Ast::AttrList *> &nodeDefaultsVector,
453  std::vector<Ast::AttrList *> &edgeDefaultsVector,
454  std::set<node> &nodeSet);
455 
456 
458  SubgraphData withCluster(
459  cluster newRootCluster) const;
460 
462  SubgraphData withDefaults(
463  std::vector<Ast::AttrList *> &newNodeDefaults,
464  std::vector<Ast::AttrList *> &newEdgeDefaults) const;
466  SubgraphData withNodes(
467  std::set<node> &newNodes) const;
468 };
469 
470 
471 } // end namespace dot
472 
473 } // end namespace ogdf
Stmt * parseStmt(Iterator current, Iterator &rest)
EdgeElement * edge
The type of edges.
Definition: Graph_d.h:66
Declaration and implementation of HashArray class.
Declaration of class GraphAttributes which extends a Graph by additional attributes.
Derived class of GraphObserver providing additional functionality to handle clustered graphs...
StmtList * statements
Definition: DotParser.h:193
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:472
std::vector< Ast::AttrList * > & nodeDefaults
Definition: DotParser.h:439
DOT related enums and string conversion functions.
Tokens::const_iterator Iterator
Definition: DotParser.h:135
Indexed arrays using hashing for element access.
Definition: HashArray.h:94
const std::string id
Definition: DotParser.h:323
AttrList * parseAttrList(Iterator current, Iterator &rest)
Graph * parseGraph(Iterator current, Iterator &rest)
DOT format abstract syntax tree class, based on official documentation.
Definition: DotParser.h:114
const std::string rhs
Definition: DotParser.h:264
Graph * root() const
Returns the root of the AST (nullptr if none).
std::string * id
Definition: DotParser.h:191
std::set< node > & nodes
Definition: DotParser.h:441
CompassPt * compassPt
Definition: DotParser.h:349
std::vector< Ast::AttrList * > & edgeDefaults
Definition: DotParser.h:440
Subgraph * parseSubgraph(Iterator current, Iterator &rest)
StmtList * parseStmtList(Iterator current, Iterator &rest)
Representation of clustered graphs.
Definition: ClusterGraph.h:291
AList * parseAList(Iterator current, Iterator &rest)
HashArray< std::string, node > m_nodeId
Definition: DotParser.h:402
NodeStmt * parseNodeStmt(Iterator current, Iterator &rest)
Stores additional attributes of a graph (like layout information).
CompassPt * parseCompassPt(Iterator current, Iterator &rest)
Stores additional attributes of a clustered graph (like layout information).
Declares stuff related to DOT format lexical analysis.
Class for the representation of nodes.
Definition: Graph_d.h:167
const Tokens m_tokens
Definition: DotParser.h:137
Declares ClusterGraphAttributes, an extension of class GraphAttributes, to store clustergraph layout ...
Representation of clusters in a clustered graph.
Definition: ClusterGraph.h:53
Includes declaration of graph class.
bool build()
Builds the DOT format AST.
Port * parsePort(Iterator current, Iterator &rest)
DOT format parser class.
Definition: DotParser.h:397
const std::string lhs
Definition: DotParser.h:263
bool read(Parser &P, ogdf::Graph &G, GraphAttributes *GA, ClusterGraph *C, ClusterGraphAttributes *CA)
NodeId * parseNodeId(Iterator current, Iterator &rest)
Graph * m_graph
Definition: DotParser.h:140
std::string * id
Definition: DotParser.h:348
std::istream & m_in
Definition: DotParser.h:399
Ast(const Tokens &tokens)
Initializes AST building but does not trigger the process itself.
std::vector< Token > Tokens
Definition: DotParser.h:134
The namespace for all OGDF objects.
A helper structure containing information for recursive graph reading.
Definition: DotParser.h:437
EdgeStmt * parseEdgeStmt(Iterator current, Iterator &rest)
Graph(const bool &paramStrict, const bool &dir, std::string *idString, StmtList *statementList)
AsgnStmt * parseAsgnStmt(Iterator current, Iterator &rest)
const bool directed
Definition: DotParser.h:190
AttrStmt * parseAttrStmt(Iterator current, Iterator &rest)
EdgeRhs * parseEdgeRhs(Iterator current, Iterator &rest)
const Iterator m_tend
Definition: DotParser.h:138