-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.cpp
212 lines (167 loc) · 6.4 KB
/
filter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
lazydot - a simple dot preprocessor
Copyright (C) 2021 Stefan Klein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "filter.h"
#include <algorithm>
#include <string>
#include <stdio.h>
#include <vector>
#include <map>
extern FILE *yyout;
typedef std::map<std::string, std::string> AttributesList;
struct FlowItem
{
std::string title;
AttributesList edgeAttributes;
};
static struct {
std::map<std::string, AttributesList> templates;
std::string currentFlowStart;
std::string currentFlowEnd;
std::string lastFlowStart;
std::string lastFlowEnd;
std::vector<std::string> alreadyDeclaredNodes;
AttributesList edgeAttributesForNextNode;
std::vector<std::vector<FlowItem>> flows;
AttributesList attributes;
size_t intendLevel;
size_t subgraphCounter;
} state;
void out(const std::string &str)
{
fwrite(str.c_str(), 1, str.size(), yyout);
}
void intendedOut(const std::string &str)
{
for(size_t i = 0; i <= state.intendLevel; i++)
fprintf(yyout, " ");
out(str);
}
std::string quoteString(const std::string &str)
{
return('"' + str + '"');
}
std::string formatAttribute(std::map<std::string, std::string> &attributes, char separator = ',', bool inBrackets = true)
{
if(attributes.empty())
return("");
std::string out = inBrackets ? " [ " : "";
for(auto it = attributes.begin(); it != attributes.end(); ++it) {
if(it != attributes.begin())
out += separator, out += " ";
out += it->first + "=\"" + it->second + "\"";
}
out += inBrackets ? " ] " : "";
return(out);
}
void onStart()
{
state.flows.emplace_back(std::vector<FlowItem>());
state.intendLevel = 0;
out("digraph G {\n");
}
void onGraph()
{
intendedOut(formatAttribute(state.attributes, ';', false) + "\n\n");
state.attributes.clear();
}
void onTemplateDefinition(std::string name)
{
if(state.templates.find(name) != state.templates.end())
fprintf(stderr, "Warning: Template '%s' redefined, discarding old attributes.\n", name.c_str());
state.templates.insert(std::make_pair(name, state.attributes));
state.attributes.clear();
}
void onNode(std::string title)
{
if(title == ".")
title = state.lastFlowStart;
else if(title == "$")
title = state.lastFlowEnd;
if(state.currentFlowStart.empty())
state.currentFlowStart = title;
state.currentFlowEnd = title;
if(std::find(state.alreadyDeclaredNodes.begin(), state.alreadyDeclaredNodes.end(), title) == state.alreadyDeclaredNodes.end()) {
intendedOut(quoteString(title) + formatAttribute(state.attributes).c_str() + "\n");
state.alreadyDeclaredNodes.emplace_back(title);
}
state.attributes.clear();
state.flows.back().emplace_back(FlowItem { title, state.edgeAttributesForNextNode });
state.edgeAttributesForNextNode.clear();
}
void onEdge()
{
state.edgeAttributesForNextNode = state.attributes;
state.attributes.clear();
}
void onNodeFlowEnd()
{
state.lastFlowStart = state.currentFlowStart;
state.lastFlowEnd = state.currentFlowEnd;
state.currentFlowStart = state.currentFlowEnd = "";
state.flows.emplace_back(std::vector<FlowItem>());
}
void onAttribute(std::string key, std::string value)
{
state.attributes.insert(std::pair<std::string, std::string>(key, value));
}
void onTemplateUse(std::string name)
{
auto source = state.templates.find(name);
if(source != state.templates.end())
state.attributes.insert(source->second.begin(), source->second.end());
else
fprintf(stderr, "Warning: Template '%s' does not exist, ignoring is-attribute.\n", name.c_str());
}
void onClusterStart(std::string title)
{
out("\n");
intendedOut("subgraph cluster_" + std::to_string(state.subgraphCounter++) + " {\n");
state.intendLevel++;
state.attributes.insert(std::pair<std::string, std::string>("label", title));
intendedOut(formatAttribute(state.attributes, ';', false) + "\n\n");
state.attributes.clear();
}
void onClusterEnd()
{
state.intendLevel--;
intendedOut("}\n");
}
void onEnd()
{
out("\n");
for(auto &flow : state.flows) {
if(flow.size() > 1) {
intendedOut("");
for(auto it = flow.begin(); !flow.empty() && it != flow.end(); ++it) {
// if we have attributes and already printed more than one node, repeat the last one
if(!it->edgeAttributes.empty() && std::distance(flow.begin(), it) > 1 && (it - 1)->edgeAttributes.empty())
out(" ; " + quoteString((it - 1)->title));
// print the arrow, if needed
if(it != flow.begin())
out(" -> ");
out(quoteString(it->title));
// if this item has attributes, output them...
if(!it->edgeAttributes.empty()) {
out(formatAttribute(it->edgeAttributes));
// ...and print this item's a second time, if it not the last item
if(it + 1 != flow.end())
out("; " + quoteString(it->title));
}
}
out("\n");
}
}
out("}\n");
}