-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathin2.cxx
248 lines (207 loc) · 6.31 KB
/
in2.cxx
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Boost Licensed
//
module;
#include <cassert>
#include <cstdint>
#include <iostream>
#include <sstream>
#include <string>
export module maud_:in2;
import :parsing;
using std::operator""s;
export void compile_in2(std::istream &is, std::ostream &os);
export std::string compile_in2(std::string in) {
std::stringstream is{std::move(in)}, os;
compile_in2(is, os);
return std::move(os).str();
}
std::ostream *os = &std::cout;
auto find_end_of_quoted_string(auto str) {
assert(*str == '"');
while (true) {
++str;
str = find_first(OF<'"', '\\'>, str);
if (*str == 0) return str;
if (*str == '"') return ++str;
assert(*str == '\\');
++str;
}
}
auto find_end_of_block_string(auto str) {
assert(*str == '[');
auto begin = ++str;
str = find_first(not OF<'='>, str);
if (*str != '[') return str;
size_t open_len = &*str - &*begin;
while (true) {
str = find_first(OF<']'>, str);
if (*str == 0) return str;
begin = ++str;
str = find_first(not OF<'='>, str);
if (*str == 0) return str;
if (*str != ']') continue;
size_t close_len = &*str - &*begin;
if (close_len != open_len) continue;
return ++str;
}
}
auto find_end_of_string(auto str) {
return *str == '"' ? find_end_of_quoted_string(str) : find_end_of_block_string(str);
}
auto literal(auto begin) {
auto end = begin;
std::string bracket_fill;
while (true) {
end = find_first(OF<
// A literal chunk is always ended by @.
'@',
// A closing bracket followed by zero or more
// equals might require expanding the string's
// bracket.
']'>,
end);
if (*end != ']') break;
auto bracket = end++;
end = find_first(not OF<'='>, end);
if (*end != ']' and *end != '@') continue;
// `Hello ]=] ` ->
// `render([=[Hello ]=] ]=])`
// ^^^ oops not the end I want.
//
// `Hello ]=@` ->
// `render([=[Hello ]=]=])`
// ^^^ oops not the end I want.
//
// `Hello ]= ` ->
// `render([=[Hello ]= ]=])`
// ^^ it's fine that's not an end.
size_t len = &*end - &*bracket;
if (bracket_fill.size() >= len) continue;
bracket_fill.resize(len, '=');
}
// Don't bother rendering an empty literal.
if (begin == end) return end;
debug("literal", begin, end);
*os << "render([" << bracket_fill << "[" << (*begin == '\n' ? "\n" : "")
<< begin.view_to(end) << ']' << bracket_fill << "])\n";
return end;
}
auto skipping_strings_find_first(auto real_end, auto str) {
while (true) {
str = find_first(real_end or OF<'[', '"'>, str);
if (real_end(*str) or *str == 0) return str;
// A string might contain characters matching real_end,
// so skip past the string to make sure we don't return
// prematurely.
str = find_end_of_string(str);
if (*str == 0) return str;
}
}
auto pipeline(auto begin, auto end) {
debug("pipeline init", begin, end);
*os << "set(IT ";
reference(begin);
*os << ")\n";
int depth = 0;
while (true) {
++end;
begin = find_first(not SPACE, end);
// Find the end of the pipeline or the next filter
end = skipping_strings_find_first(OF<'@', '|'>, begin);
if (std::string_view v{&*begin, &*end}; v == "foreach") {
debug("pipeline foreach", begin, end);
*os << "set(foreach_IT_" << depth << ")\nforeach(IT ${IT})\n";
++depth;
} else if (v == "endforeach") {
debug("pipeline endforeach", begin, end);
*os << "list(APPEND foreach_IT_" << depth << " \"${IT}\")\nendforeach()\n"
<< "set(IT \"${foreach_IT_" << depth << "}\")\n";
--depth;
} else {
debug("pipeline filter", begin, end);
*os << "in2_pipeline_filter_" << begin.view_to(end) << "\n";
}
if (*end != '|') break;
}
// TODO assert depth == 0
debug("pipeline output", end, end);
*os << "render(\"${IT}\")\n";
if (*end == 0) return end;
return ++end;
}
void reference(auto begin) {
begin = find_first(not SPACE, begin);
auto end = find_first(SPACE or OF<'@', '|'>, begin);
*os << "\"${" << begin.view_to(end) << "}\"";
}
auto const HASH_LINE = std::string(82, '#') + "\n";
void debug(auto type, Location begin, Location end) {
*os << "\n# " << type << " " << begin.line_column() << "-" << end.line_column() << "\n"
<< HASH_LINE //
<< "# " << begin.view_line() << "\n";
if (begin.line == end.line) {
*os << "# " << std::string(begin.column, ' ');
if (int len = end.column - begin.column; len >= 2) {
*os << '^' << std::string(len - 2, '~');
}
*os << "^\n" << HASH_LINE;
return;
}
*os << "# " << std::string(begin.column, ' ') << '^'
<< std::string(begin.view_line().size() - begin.column, '~') << "\n#"
<< std::string(end.column, '~') << "v\n# " << end.view_line() << "\n"
<< HASH_LINE;
}
// str is assumed to be null terminated
void compile(auto begin) {
if (*begin == 0) return;
// we always start with a literal chunk
LITERAL:
auto end = literal(begin);
if (*end == 0) return;
// skip past the @
begin = ++end;
// check for @@, in which case we resume with a
// new literal
if (*begin == '@') {
debug("@@ -> @", begin, begin);
*os << "render(\"@\")\n";
++begin;
if (*begin == 0) return;
goto LITERAL;
}
// Now we have a var ref, a command block, or
// a pipe.
end = find_first(OF<
// The block ends with @.
'@',
// A '(' indicates a command.
'(',
// A '|' indicates a pipeline.
'|'>,
begin);
if (*end == '@' or *end == 0) {
debug("reference", begin, end);
*os << "render(";
reference(begin);
*os << ")\n";
if (*end == 0) return;
begin = ++end;
goto LITERAL;
}
if (*end == '(') {
end = skipping_strings_find_first(OF<'@'>, end);
debug("commands", begin, end);
*os << begin.view_to(end) << "\n";
if (*end == 0) return;
begin = ++end;
goto LITERAL;
}
begin = pipeline(begin, end);
goto LITERAL;
}
void compile_in2(std::istream &is, std::ostream &os) {
std::string in2(std::istreambuf_iterator{is}, {});
::os = &os;
compile(Location{in2.c_str()});
}