Cerata
A library to generate structural hardware designs
style.h
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <memory>
18 #include <vector>
19 #include <string>
20 #include <sstream>
21 
22 #include "cerata/graph.h"
23 #include "cerata/node.h"
24 
25 namespace cerata::dot {
26 
28 inline std::string tab(uint n) {
29  return std::string(2 * n, ' ');
30 }
31 
33 inline std::string tab(int n) {
34  return tab(static_cast<uint>(n));
35 }
36 
38 inline std::string sanitize(std::string in) {
39  std::replace(in.begin(), in.end(), ':', '_');
40  std::replace(in.begin(), in.end(), '-', '_');
41  std::replace(in.begin(), in.end(), '"', '_');
42  return in;
43 }
44 
46 inline std::string awq(const std::string &attribute, const std::string &style) {
47  if (!style.empty()) {
48  return attribute + "=\"" + style + "\"";
49  } else {
50  return "";
51  }
52 }
53 
55 struct Palette {
56  int num_colors;
57  std::string black;
58  std::string white;
59  std::string gray;
60  std::string darker;
61  std::string dark;
62  std::string light;
63  std::string lighter;
64 
65  std::vector<std::string> b;
66  std::vector<std::string> m;
67  std::vector<std::string> d;
68 
70  static Palette normal();
71 };
72 
74 struct StyleBuilder {
75  std::vector<std::string> parts;
76  StyleBuilder &operator<<(const std::string &part);
79  std::string ToString();
80 };
81 
83 struct Config {
85  struct NodeConfig {
86  bool parameters = true;
87  bool literals = true;
88  bool signals = true;
89  bool ports = true;
90  bool expressions = true;
91 
93  struct ExpandConfig {
94  bool record = false;
95  bool stream = false;
96  bool expression = false;
97  } expand;
98 
100  struct TypeConfig {
101  bool bit = true;
102  bool vector = true;
103  bool record = true;
104  bool stream = true;
105  } types;
106  } nodes;
107 
109  static Config all();
111  static Config normal();
113  static Config streams();
115  bool operator()(const Node &node);
116 };
117 
121 struct Style {
123  using str = std::string;
124 
126  struct SubGraph {
130 
132  struct NodeGroup {
136 
138  struct EdgeStyle {
140  struct Colors {
142  } color;
143 
152  } edge;
153 
155  struct NodeStyle {
157  struct Colors {
164  } color;
172 
174  struct TypeStyle {
182  } type;
183  } node;
184 
187 
189  std::string GenHTMLTableCell(const Type &t,
190  const std::string &name,
191  int level = 0);
192 
194  static std::string GenDotRecordCell(const Type &t,
195  const std::string &name,
196  int level = 0);
197 
199  std::string GetLabel(const Node &n);
200 
202  std::string GetStyle(const Node &n);
203 
205  static Style normal();
206 };
207 
208 } // namespace cerata::dot
cerata::dot::Style::NodeStyle::type
struct cerata::dot::Style::NodeStyle::TypeStyle type
Styles for types.
cerata::dot::Config::NodeConfig::signals
bool signals
Show signals.
Definition: style.h:88
cerata::dot::Config::nodes
struct cerata::dot::Config::NodeConfig nodes
Node configuration.
cerata::dot::tab
std::string tab(uint n)
Return indent string.
Definition: style.h:28
cerata::dot::Style::subgraph
struct cerata::dot::Style::SubGraph subgraph
Style for sub graphs.
cerata::dot::Config::NodeConfig::ExpandConfig::stream
bool stream
Expand streams.
Definition: style.h:95
cerata::dot::Config::normal
static Config normal()
Return a configuration that will generate default constructs.
Definition: style.cc:118
cerata::dot::Style::NodeStyle::nested
str nested
Style for nested nodes.
Definition: style.h:171
cerata::dot::Style::NodeStyle::Colors::record
str record
Record node color.
Definition: style.h:161
cerata::dot::Style::EdgeStyle::param
str param
Style for parameter edges.
Definition: style.h:148
cerata::dot::Style::EdgeStyle::base
str base
Base style.
Definition: style.h:144
cerata::dot::Style::NodeStyle::Colors::stream_child
str stream_child
Stream child color.
Definition: style.h:160
cerata::dot::Style::NodeGroup
Node group configuration.
Definition: style.h:132
cerata::dot::Style::NodeStyle
Node style.
Definition: style.h:155
cerata::dot::Style::normal
static Style normal()
Default style.
Definition: style.cc:24
cerata::dot::Config::NodeConfig::expressions
bool expressions
Show expressions.
Definition: style.h:90
cerata::dot::Palette::darker
std::string darker
Darker gray color.
Definition: style.h:60
cerata::dot::Style::NodeStyle::parameter
str parameter
Style for parameters.
Definition: style.h:168
cerata::dot::Style::NodeStyle::Colors
Colors for types.
Definition: style.h:157
cerata::dot::Style::NodeGroup::base
str base
Base style for groups.
Definition: style.h:133
cerata::Type
A Type.
Definition: type.h:63
cerata::dot::Style::EdgeStyle::port_to_port
str port_to_port
Style for port-to-port.
Definition: style.h:147
cerata::dot::Config::NodeConfig::ExpandConfig::record
bool record
Expand records.
Definition: style.h:94
cerata::dot::Style::EdgeStyle::expr
str expr
Style for expressions.
Definition: style.h:151
cerata::dot::Palette::light
std::string light
Light gray color.
Definition: style.h:62
cerata::dot::Style::EdgeStyle::color
struct cerata::dot::Style::EdgeStyle::Colors color
Colors for specific edges.
cerata::dot::Palette::white
std::string white
White color.
Definition: style.h:58
cerata::dot::Style::EdgeStyle::Colors
Specific edge colors.
Definition: style.h:140
cerata::dot::Style::NodeStyle::Colors::stream
str stream
Stream node color.
Definition: style.h:158
cerata::dot::Config::NodeConfig::ExpandConfig
Expansion configuration.
Definition: style.h:93
cerata::dot::Palette::dark
std::string dark
Very dark gray color.
Definition: style.h:61
cerata::dot::Style::SubGraph::color
str color
Subgraph color.
Definition: style.h:128
cerata::dot::Style::NodeStyle::TypeStyle
Styles for specific node types.
Definition: style.h:174
cerata::dot::Style::str
std::string str
Short-hand for std::string.
Definition: style.h:123
cerata::Node
A node.
Definition: node.h:42
cerata::dot::Style::NodeGroup::color
str color
Color for groups.
Definition: style.h:134
cerata::dot::Style::NodeStyle::literal
str literal
Style for literals.
Definition: style.h:169
cerata::dot::Style::NodeStyle::TypeStyle::bit
str bit
Style for bits.
Definition: style.h:175
cerata::dot::Style::GetStyle
std::string GetStyle(const Node &n)
Get the style for a node.
Definition: style.cc:175
cerata::dot::StyleBuilder
Convenience structure to build up dot styles.
Definition: style.h:74
cerata::dot::awq
std::string awq(const std::string &attribute, const std::string &style)
Assign with quotes.
Definition: style.h:46
cerata::dot::Config::NodeConfig::parameters
bool parameters
Show parameters.
Definition: style.h:86
cerata::dot::Config::all
static Config all()
Return a configuration that will generate every construct.
Definition: style.cc:138
cerata::dot::Style::EdgeStyle
Style for edges.
Definition: style.h:138
cerata::dot::StyleBuilder::operator<<
StyleBuilder & operator<<(const std::string &part)
Append a part to the style.
Definition: style.cc:88
cerata::dot::Style::EdgeStyle::port_to_sig
str port_to_sig
Style for port-to-signal.
Definition: style.h:145
cerata::dot::Config
DOT output configuration. Determines what Cerata constructs will be used for generation.
Definition: style.h:83
cerata::dot::Config::streams
static Config streams()
Return a configuration that will generate onnly stream constructs.
Definition: style.cc:104
cerata::dot::Style::NodeStyle::TypeStyle::integer
str integer
Style for integers.
Definition: style.h:180
cerata::dot::Style::nodegroup
struct cerata::dot::Style::NodeGroup nodegroup
Style for group of nodes.
cerata::dot::Style::NodeStyle::Colors::record_border
str record_border
Record border color.
Definition: style.h:162
cerata::dot::StyleBuilder::parts
std::vector< std::string > parts
Definition: style.h:75
cerata::dot::Style::NodeStyle::port
str port
Style for ports.
Definition: style.h:166
cerata::dot::Style::EdgeStyle::stream
str stream
Style for stream edges.
Definition: style.h:149
cerata::dot
Contains everything related to the DOT back-end.
Definition: dot.cc:27
cerata::dot::Config::NodeConfig::types
struct cerata::dot::Config::NodeConfig::TypeConfig types
Type configuration.
cerata::dot::Style::GetLabel
std::string GetLabel(const Node &n)
Get the label for a node.
Definition: style.cc:215
cerata::dot::Config::operator()
bool operator()(const Node &node)
Return whether a node should be generated on the DOT graph.
Definition: style.cc:93
cerata::dot::Config::NodeConfig::TypeConfig::record
bool record
Show record types.
Definition: style.h:103
cerata::dot::Style::node
struct cerata::dot::Style::NodeStyle node
Style for nodes.
cerata::dot::Config::NodeConfig::expand
struct cerata::dot::Config::NodeConfig::ExpandConfig expand
Configures what types of nodes to expand.
cerata::dot::Config::NodeConfig::TypeConfig
Type configuration.
Definition: style.h:100
cerata::dot::Palette::gray
std::string gray
Gray color.
Definition: style.h:59
cerata::dot::Palette::lighter
std::string lighter
Lighter gray color.
Definition: style.h:63
cerata::dot::Palette
A color Palette.
Definition: style.h:55
cerata::dot::Style::NodeStyle::TypeStyle::record
str record
Style for records.
Definition: style.h:179
cerata::dot::Style::NodeStyle::TypeStyle::stream
str stream
Style for streams.
Definition: style.h:178
cerata::dot::Style::edge
struct cerata::dot::Style::EdgeStyle edge
Style for edges.
cerata::dot::Style
Dot style configuration.
Definition: style.h:121
cerata::dot::Config::NodeConfig::ExpandConfig::expression
bool expression
Expand expressions.
Definition: style.h:96
cerata::dot::Style::SubGraph::base
str base
Subgraph base style.
Definition: style.h:127
cerata::dot::Style::EdgeStyle::sig_to_port
str sig_to_port
Style for signal-to-port.
Definition: style.h:146
cerata::dot::Config::NodeConfig::TypeConfig::stream
bool stream
Show stream types.
Definition: style.h:104
cerata::dot::Palette::d
std::vector< std::string > d
Dark.
Definition: style.h:67
cerata::dot::StyleBuilder::ToString
std::string ToString()
Generate the style string.
Definition: style.cc:75
cerata::dot::Config::NodeConfig::TypeConfig::vector
bool vector
Show vector types.
Definition: style.h:102
cerata::dot::Style::config
Config config
Configuration of what types of constructs to show or hide for this style.
Definition: style.h:186
cerata::dot::Palette::b
std::vector< std::string > b
Bright.
Definition: style.h:65
cerata::dot::Style::GenDotRecordCell
static std::string GenDotRecordCell(const Type &t, const std::string &name, int level=0)
Generate a DOT record cell from a type.
cerata::dot::Config::NodeConfig::literals
bool literals
Show literals.
Definition: style.h:87
cerata::dot::Style::EdgeStyle::lit
str lit
Style for literal edges.
Definition: style.h:150
cerata::dot::Palette::black
std::string black
Black color.
Definition: style.h:57
cerata::dot::sanitize
std::string sanitize(std::string in)
Sanitize a string for usage in DOT.
Definition: style.h:38
cerata::dot::Style::NodeStyle::TypeStyle::boolean
str boolean
Style for booleans.
Definition: style.h:176
cerata::dot::Style::NodeStyle::Colors::stream_border
str stream_border
Stream border color.
Definition: style.h:159
cerata::dot::Style::NodeStyle::TypeStyle::vector
str vector
Style for vectors.
Definition: style.h:177
cerata::dot::Config::NodeConfig::ports
bool ports
Show ports.
Definition: style.h:89
cerata::dot::Config::NodeConfig
Node configuration.
Definition: style.h:85
cerata::dot::Style::NodeStyle::expression
str expression
Style for expressions.
Definition: style.h:170
cerata::dot::Config::NodeConfig::TypeConfig::bit
bool bit
Show bit types.
Definition: style.h:101
cerata::dot::Style::NodeStyle::signal
str signal
Style for signals.
Definition: style.h:167
cerata::dot::Palette::normal
static Palette normal()
Default palette.
Definition: style.cc:159
cerata::dot::Style::GenHTMLTableCell
std::string GenHTMLTableCell(const Type &t, const std::string &name, int level=0)
Generate a HTML table cell from a type.
Definition: dot.cc:140
cerata::dot::Style::SubGraph
Subgraph style.
Definition: style.h:126
cerata::dot::Style::NodeStyle::Colors::record_child
str record_child
Record child color.
Definition: style.h:163
cerata::dot::Style::NodeStyle::TypeStyle::string
str string
Style for strings.
Definition: style.h:181
cerata::dot::Palette::m
std::vector< std::string > m
Medium.
Definition: style.h:66
cerata::dot::Style::EdgeStyle::Colors::stream
str stream
Colors for stream edges.
Definition: style.h:141
cerata::dot::Style::NodeStyle::color
struct cerata::dot::Style::NodeStyle::Colors color
Colors for specific nodes.
cerata::dot::Style::NodeStyle::base
str base
Base node style.
Definition: style.h:165
cerata::dot::Palette::num_colors
int num_colors
Number of colors of this Palette.
Definition: style.h:56