Debian Codegraph @

js/src/irregexp/imported/regexp-dotprinter.cc

raw
1 // Copyright 2019 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "irregexp/imported/regexp-dotprinter.h"
6
7 #include "irregexp/imported/regexp-compiler.h"
8
9 namespace v8 {
10 namespace internal {
11
12 // -------------------------------------------------------------------
13 // Dot/dotty output
14
15 class DotPrinterImpl : public NodeVisitor {
16 public:
17 explicit DotPrinterImpl(std::ostream& os) : os_(os) {}
18 void PrintNode(const char* label, RegExpNode* node);
19 void Visit(RegExpNode* node);
20 void PrintAttributes(RegExpNode* from);
21 void PrintOnFailure(RegExpNode* from, RegExpNode* to);
22 #define DECLARE_VISIT(Type) virtual void Visit##Type(Type##Node* that);
23 FOR_EACH_NODE_TYPE(DECLARE_VISIT)
24 #undef DECLARE_VISIT
25 private:
26 std::ostream& os_;
27 };
28
29 void DotPrinterImpl::PrintNode(const char* label, RegExpNode* node) {
30 os_ << "digraph G {\n graph [label=\"";
31 for (int i = 0; label[i]; i++) {
32 switch (label[i]) {
33 case '\\':
34 os_ << "\\\\";
35 break;
36 case '"':
37 os_ << "\"";
38 break;
39 default:
40 os_ << label[i];
41 break;
42 }
43 }
44 os_ << "\"];\n";
45 Visit(node);
46 os_ << "}" << std::endl;
47 }
48
49 void DotPrinterImpl::Visit(RegExpNode* node) {
50 if (node->info()->visited) return;
51 node->info()->visited = true;
52 node->Accept(this);
53 }
54
55 void DotPrinterImpl::PrintOnFailure(RegExpNode* from, RegExpNode* on_failure) {
56 os_ << " n" << from << " -> n" << on_failure << " [style=dotted];\n";
57 Visit(on_failure);
58 }
59
60 class AttributePrinter {
61 public:
62 explicit AttributePrinter(std::ostream& os) : os_(os), first_(true) {}
63 void PrintSeparator() {
64 if (first_) {
65 first_ = false;
66 } else {
67 os_ << "|";
68 }
69 }
70 void PrintBit(const char* name, bool value) {
71 if (!value) return;
72 PrintSeparator();
73 os_ << "{" << name << "}";
74 }
75 void PrintPositive(const char* name, int value) {
76 if (value < 0) return;
77 PrintSeparator();
78 os_ << "{" << name << "|" << value << "}";
79 }
80
81 private:
82 std::ostream& os_;
83 bool first_;
84 };
85
86 void DotPrinterImpl::PrintAttributes(RegExpNode* that) {
87 os_ << " a" << that << " [shape=Mrecord, color=grey, fontcolor=grey, "
88 << "margin=0.1, fontsize=10, label=\"{";
89 AttributePrinter printer(os_);
90 NodeInfo* info = that->info();
91 printer.PrintBit("NI", info->follows_newline_interest);
92 printer.PrintBit("WI", info->follows_word_interest);
93 printer.PrintBit("SI", info->follows_start_interest);
94 Label* label = that->label();
95 if (label->is_bound()) printer.PrintPositive("@", label->pos());
96 os_ << "}\"];\n"
97 << " a" << that << " -> n" << that
98 << " [style=dashed, color=grey, arrowhead=none];\n";
99 }
100
101 void DotPrinterImpl::VisitChoice(ChoiceNode* that) {
102 os_ << " n" << that << " [shape=Mrecord, label=\"?\"];\n";
103 for (int i = 0; i < that->alternatives()->length(); i++) {
104 GuardedAlternative alt = that->alternatives()->at(i);
105 os_ << " n" << that << " -> n" << alt.node();
106 }
107 for (int i = 0; i < that->alternatives()->length(); i++) {
108 GuardedAlternative alt = that->alternatives()->at(i);
109 alt.node()->Accept(this);
110 }
111 PrintAttributes(that);
112 }
113
114 void DotPrinterImpl::VisitLoopChoice(LoopChoiceNode* that) {
115 VisitChoice(that);
116 }
117
118 void DotPrinterImpl::VisitNegativeLookaroundChoice(
119 NegativeLookaroundChoiceNode* that) {
120 VisitChoice(that);
121 }
122
123 void DotPrinterImpl::VisitText(TextNode* that) {
124 Zone* zone = that->zone();
125 os_ << " n" << that << " [label=\"";
126 for (int i = 0; i < that->elements()->length(); i++) {
127 if (i > 0) os_ << " ";
128 TextElement elm = that->elements()->at(i);
129 switch (elm.text_type()) {
130 case TextElement::ATOM: {
131 base::Vector<const base::uc16> data = elm.atom()->data();
132 for (int j = 0; j < data.length(); j++) {
133 os_ << static_cast<char>(data[j]);
134 }
135 break;
136 }
137 case TextElement::CLASS_RANGES: {
138 RegExpClassRanges* node = elm.class_ranges();
139 os_ << "[";
140 if (node->is_negated()) os_ << "^";
141 for (int j = 0; j < node->ranges(zone)->length(); j++) {
142 CharacterRange range = node->ranges(zone)->at(j);
143 os_ << AsUC32(range.from()) << "-" << AsUC32(range.to());
144 }
145 os_ << "]";
146 break;
147 }
148 default:
149 UNREACHABLE();
150 }
151 }
152 os_ << "\", shape=box, peripheries=2];\n";
153 PrintAttributes(that);
154 os_ << " n" << that << " -> n" << that->on_success() << ";\n";
155 Visit(that->on_success());
156 }
157
158 void DotPrinterImpl::VisitBackReference(BackReferenceNode* that) {
159 os_ << " n" << that << " [label=\"$" << that->start_register() << "..$"
160 << that->end_register() << "\", shape=doubleoctagon];\n";
161 PrintAttributes(that);
162 os_ << " n" << that << " -> n" << that->on_success() << ";\n";
163 Visit(that->on_success());
164 }
165
166 void DotPrinterImpl::VisitEnd(EndNode* that) {
167 os_ << " n" << that << " [style=bold, shape=point];\n";
168 PrintAttributes(that);
169 }
170
171 void DotPrinterImpl::VisitAssertion(AssertionNode* that) {
172 os_ << " n" << that << " [";
173 switch (that->assertion_type()) {
174 case AssertionNode::AT_END:
175 os_ << "label=\"$\", shape=septagon";
176 break;
177 case AssertionNode::AT_START:
178 os_ << "label=\"^\", shape=septagon";
179 break;
180 case AssertionNode::AT_BOUNDARY:
181 os_ << "label=\"\\b\", shape=septagon";
182 break;
183 case AssertionNode::AT_NON_BOUNDARY:
184 os_ << "label=\"\\B\", shape=septagon";
185 break;
186 case AssertionNode::AFTER_NEWLINE:
187 os_ << "label=\"(?<=\\n)\", shape=septagon";
188 break;
189 }
190 os_ << "];\n";
191 PrintAttributes(that);
192 RegExpNode* successor = that->on_success();
193 os_ << " n" << that << " -> n" << successor << ";\n";
194 Visit(successor);
195 }
196
197 void DotPrinterImpl::VisitAction(ActionNode* that) {
198 os_ << " n" << that << " [";
199 switch (that->action_type_) {
200 case ActionNode::SET_REGISTER_FOR_LOOP:
201 os_ << "label=\"$" << that->data_.u_store_register.reg
202 << ":=" << that->data_.u_store_register.value << "\", shape=octagon";
203 break;
204 case ActionNode::INCREMENT_REGISTER:
205 os_ << "label=\"$" << that->data_.u_increment_register.reg
206 << "++\", shape=octagon";
207 break;
208 case ActionNode::STORE_POSITION:
209 os_ << "label=\"$" << that->data_.u_position_register.reg
210 << ":=$pos\", shape=octagon";
211 break;
212 case ActionNode::BEGIN_POSITIVE_SUBMATCH:
213 os_ << "label=\"$" << that->data_.u_submatch.current_position_register
214 << ":=$pos,begin-positive\", shape=septagon";
215 break;
216 case ActionNode::BEGIN_NEGATIVE_SUBMATCH:
217 os_ << "label=\"$" << that->data_.u_submatch.current_position_register
218 << ":=$pos,begin-negative\", shape=septagon";
219 break;
220 case ActionNode::POSITIVE_SUBMATCH_SUCCESS:
221 os_ << "label=\"escape\", shape=septagon";
222 break;
223 case ActionNode::EMPTY_MATCH_CHECK:
224 os_ << "label=\"$" << that->data_.u_empty_match_check.start_register
225 << "=$pos?,$" << that->data_.u_empty_match_check.repetition_register
226 << "<" << that->data_.u_empty_match_check.repetition_limit
227 << "?\", shape=septagon";
228 break;
229 case ActionNode::CLEAR_CAPTURES: {
230 os_ << "label=\"clear $" << that->data_.u_clear_captures.range_from
231 << " to $" << that->data_.u_clear_captures.range_to
232 << "\", shape=septagon";
233 break;
234 }
235 case ActionNode::MODIFY_FLAGS: {
236 os_ << "label=\"flags $" << that->flags() << "\", shape=septagon";
237 break;
238 }
239 }
240 os_ << "];\n";
241 PrintAttributes(that);
242 RegExpNode* successor = that->on_success();
243 os_ << " n" << that << " -> n" << successor << ";\n";
244 Visit(successor);
245 }
246
247 void DotPrinter::DotPrint(const char* label, RegExpNode* node) {
248 StdoutStream os;
249 DotPrinterImpl printer(os);
250 printer.PrintNode(label, node);
251 }
252
253 } // namespace internal
254 } // namespace v8