-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathtohtml.d
181 lines (170 loc) · 5.22 KB
/
tohtml.d
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
module pegged.tohtml;
import std.stdio;
import std.conv, std.string;
import std.algorithm.searching;
import pegged.peg;
enum Expand
{
no, ifMatch, ifNotMatch
}
/**
* Writes a parse tree to a html file.
*
* Params:
* e = Defines if the tree is expanded.
* Details = Defines the details, as a list of strings, that are expanded
* when e is equal to `Expand.ifMatch`, and not expanded when e is equal to
* `Expand.ifNotMatch`. When no details are passed, each node is expanded.
* p = The ParseTree to represent.
* file = The file where to write the tree.
*/
void toHTML(Expand e = Expand.no, Details...)(const ref ParseTree p,
File file)
{
file.write(`
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Pegged produced parse tree</title>
<style>
code {
position: relative;
font-family: monospace;
white-space: pre;
}
code.failure {
color: red;
}
/* hide tooltip */
code span {
display: none;
}
/* show and style tooltip */
code:hover span {
/* show tooltip */
display: block;
/* position relative to container div.tooltip */
position: absolute;
top: -1.6em;
left: -0.6em;
background: #ffffff;
padding: 0.5em;
color: #000000;
border: 0.1em solid #b7ddf2;
border-radius: 0.5em;
white-space: nowrap;
z-index: 1;
}
details {
margin-left:25px;
white-space: nowrap;
}
details.leaf summary::-webkit-details-marker {
opacity: 0;
}
</style>
</head>
<body>
`);
void treeToHTML(const ref ParseTree p)
{
file.write("<details");
static if (e != Expand.no)
{
static if (!Details.length)
{
file.write(" open");
}
else
{
static if (e == Expand.ifMatch)
{
foreach(detail; Details)
if (indexOf(p.name, detail) > -1L)
{
file.write(" open");
break;
}
}
else
{
bool w;
foreach(detail; Details)
if (indexOf(p.name, detail) > -1L)
{
w = true;
break;
}
if (!w)
file.write(" open");
}
}
}
if (p.children.length == 0)
file.write(` class="leaf"`);
file.write("><summary>", p.name, " ", to!string([p.begin, p.end]));
if (p.children.length == 0 && !p.successful)
{ // Failure leaf.
Position pos = position(p);
auto left = pos.index < 10 ? p.input[0 .. pos.index] : p.input[pos.index-10 .. pos.index];
auto right = pos.index + 10 < p.input.length ? p.input[pos.index .. pos.index + 10] : p.input[pos.index .. $];
file.write(" failure at line ", to!string(pos.line), ", col ", to!string(pos.col), ", ");
if (left.length > 0)
file.write("after <code>", left.stringified, "</code> ");
file.write("expected <code>");
if (p.matches.length > 0)
file.write(p.matches[$-1].stringified);
else
file.write("NO MATCH");
file.write(`</code>, but got <code class="failure">`, right.stringified, "</code>\n");
}
else
{
file.write(" <code");
if (!p.successful)
file.write(` class="failure"`);
auto firstNewLine = p.input[p.begin .. p.end].countUntil('\n');
auto firstLine = p.input[p.begin .. firstNewLine >= 0 ? p.begin + firstNewLine : p.end];
if (firstLine.length > 0)
file.write(">", firstLine, "<span><pre>", p.input[p.begin .. p.end].stripReturns, "</pre></span></code>");
else // Insert the return-symbol so the mouse has something to hover over.
file.write(">⏎<span><pre>⏎", p.input[p.begin .. p.end].stripReturns, "</pre></span></code>");
}
file.write("</summary>\n");
foreach (child; p.children)
treeToHTML(child);
file.write("</details>\n");
}
treeToHTML(p);
file.write(`
</body>
</html>
`);
}
/**
* Writes a parse tree to a html file.
*
* Params:
* e = Defines if the tree is expanded.
* Details = Defines the details, as a list of strings, that are expanded
* when e is equal to `Expand.yes`, and not expanded when e is equal to
* `Expand.invert`. When no details are passed, each node is expanded.
* p = The ParseTree to represent.
* filename = The name of file where tree is written.
*/
void toHTML(Expand e = Expand.no, Details...)(const ref ParseTree p,
string filename)
{
if (filename.endsWith(".html", ".htm") == 0)
filename ~= ".html";
toHTML!(e, Details)(p, File(filename, "w"));
}
/* Pegged should probably just read its input in text mode, so no "\r" occur and
* indices into the input are consistent across all platforms.
* https://forum.dlang.org/post/[email protected]
*/
string stripReturns(string str)
{
return str.tr("\r", "", "d");
}