summaryrefslogtreecommitdiffstats
path: root/src/pretty_printer.cc
blob: 2ff3e11f2902e8229279689e3d881f2a73c2750d (plain)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
 * Copyright (c) 2015, Timothy Stack
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * * Neither the name of Timothy Stack nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "pretty_printer.hh"

#include "base/string_util.hh"
#include "config.h"

void
pretty_printer::append_to(attr_line_t& al)
{
    if (this->pp_scanner->get_init_offset() > 0) {
        data_scanner::capture_t leading_cap = {
            0,
            this->pp_scanner->get_init_offset(),
        };

        // this->pp_stream << pi.get_substr(&leading_cap);
        this->pp_values.emplace_back(DT_WORD, leading_cap);
    }

    this->pp_scanner->reset();
    while (true) {
        auto tok_res = this->pp_scanner->tokenize2();
        if (!tok_res) {
            break;
        }

        element el(tok_res->tr_token, tok_res->tr_capture);

        switch (el.e_token) {
            case DT_XML_DECL_TAG:
            case DT_XML_EMPTY_TAG:
                if (this->pp_is_xml && this->pp_line_length > 0) {
                    this->start_new_line();
                }
                this->pp_values.emplace_back(el);
                if (this->pp_is_xml) {
                    this->start_new_line();
                }
                continue;
            case DT_XML_OPEN_TAG:
                if (this->pp_is_xml) {
                    this->start_new_line();
                    this->write_element(el);
                    this->pp_interval_state.back().is_start
                        = this->pp_stream.tellp();
                    this->pp_interval_state.back().is_name
                        = tok_res->to_string();
                    this->descend();
                } else {
                    this->pp_values.emplace_back(el);
                }
                continue;
            case DT_XML_CLOSE_TAG:
                this->flush_values();
                this->ascend();
                this->append_child_node();
                this->write_element(el);
                this->start_new_line();
                continue;
            case DT_LCURLY:
            case DT_LSQUARE:
            case DT_LPAREN:
                this->flush_values(true);
                this->pp_values.emplace_back(el);
                this->descend();
                this->pp_interval_state.back().is_start
                    = this->pp_stream.tellp();
                continue;
            case DT_RCURLY:
            case DT_RSQUARE:
            case DT_RPAREN:
                this->flush_values();
                if (this->pp_body_lines.top()) {
                    this->start_new_line();
                }
                this->ascend();
                this->write_element(el);
                continue;
            case DT_COMMA:
                if (this->pp_depth > 0) {
                    this->flush_values(true);
                    if (!this->pp_is_xml) {
                        this->append_child_node();
                    }
                    this->write_element(el);
                    this->start_new_line();
                    this->pp_interval_state.back().is_start
                        = this->pp_stream.tellp();
                    continue;
                }
                break;
            case DT_WHITE:
                if (this->pp_values.empty() && this->pp_depth == 0
                    && this->pp_line_length == 0)
                {
                    this->pp_leading_indent = el.e_capture.length();
                    continue;
                }
                break;
            default:
                break;
        }
        this->pp_values.emplace_back(el);
    }
    while (this->pp_depth > 0) {
        this->ascend();
    }
    this->flush_values();

    attr_line_t combined;
    combined.get_string() = this->pp_stream.str();
    combined.get_attrs() = this->pp_attrs;

    if (!al.empty()) {
        al.append("\n");
    }
    al.append(combined);

    if (this->pp_hier_stage != nullptr) {
        this->pp_hier_stage->hn_parent = this->pp_hier_nodes.back().get();
        this->pp_hier_nodes.back()->hn_children.push_back(
            std::move(this->pp_hier_stage));
    }
    this->pp_hier_stage = std::move(this->pp_hier_nodes.back());
    this->pp_hier_nodes.pop_back();
    if (this->pp_hier_stage->hn_children.size() == 1
        && this->pp_hier_stage->hn_named_children.empty())
    {
        this->pp_hier_stage
            = std::move(this->pp_hier_stage->hn_children.front());
        this->pp_hier_stage->hn_parent = nullptr;
    }
}

void
pretty_printer::write_element(const pretty_printer::element& el)
{
    if (this->pp_leading_indent == 0 && this->pp_line_length == 0
        && el.e_token == DT_WHITE)
    {
        if (this->pp_depth == 0) {
            this->pp_soft_indent += el.e_capture.length();
        }
        return;
    }
    if (((this->pp_leading_indent == 0)
         || (this->pp_line_length <= this->pp_leading_indent))
        && el.e_token == DT_LINE)
    {
        this->pp_soft_indent = 0;
        if (this->pp_line_length > 0) {
            this->pp_line_length = 0;
            this->pp_stream << std::endl;
            this->pp_body_lines.top() += 1;
        }
        return;
    }
    if (this->pp_line_length == 0) {
        this->append_indent();
    }
    ssize_t start_size = this->pp_stream.tellp();
    if (el.e_token == DT_QUOTED_STRING) {
        auto_mem<char> unquoted_str((char*) malloc(el.e_capture.length() + 1));
        const char* start
            = this->pp_scanner->to_string_fragment(el.e_capture).data();
        auto unq_len = unquote(unquoted_str.in(), start, el.e_capture.length());
        data_scanner ds(
            string_fragment::from_bytes(unquoted_str.in(), unq_len));
        string_attrs_t sa;
        pretty_printer str_pp(
            &ds, sa, this->pp_leading_indent + this->pp_depth * 4);
        attr_line_t result;
        str_pp.append_to(result);
        if (result.get_string().find('\n') != std::string::npos) {
            switch (start[0]) {
                case 'r':
                case 'u':
                    this->pp_stream << start[0];
                    this->pp_stream << start[1] << start[1];
                    break;
                default:
                    this->pp_stream << start[0] << start[0];
                    break;
            }
            this->pp_stream << std::endl << result.get_string();
            if (result.empty() || result.get_string().back() != '\n') {
                this->pp_stream << std::endl;
            }
            this->pp_stream << start[el.e_capture.length() - 1]
                            << start[el.e_capture.length() - 1];
        } else {
            this->pp_stream
                << this->pp_scanner->to_string_fragment(el.e_capture);
        }
    } else {
        this->pp_stream << this->pp_scanner->to_string_fragment(el.e_capture);
        int shift_amount
            = start_size - el.e_capture.c_begin - this->pp_shift_accum;
        shift_string_attrs(this->pp_attrs, el.e_capture.c_begin, shift_amount);
        this->pp_shift_accum = start_size - el.e_capture.c_begin;
    }
    this->pp_line_length += el.e_capture.length();
    if (el.e_token == DT_LINE) {
        this->pp_line_length = 0;
        this->pp_body_lines.top() += 1;
    }
}

void
pretty_printer::append_indent()
{
    this->pp_stream << std::string(
        this->pp_leading_indent + this->pp_soft_indent, ' ');
    this->pp_soft_indent = 0;
    if (this->pp_stream.tellp() == this->pp_leading_indent) {
        return;
    }
    for (int lpc = 0; lpc < this->pp_depth; lpc++) {
        this->pp_stream << "    ";
    }
}

bool
pretty_printer::flush_values(bool start_on_depth)
{
    nonstd::optional<data_scanner::capture_t> last_key;
    bool retval = false;

    while (!this->pp_values.empty()) {
        {
            auto& el = this->pp_values.front();
            this->write_element(this->pp_values.front());
            switch (el.e_token) {
                case DT_SYMBOL:
                case DT_CONSTANT:
                case DT_WORD:
                case DT_QUOTED_STRING:
                    last_key = el.e_capture;
                    break;
                case DT_COLON:
                case DT_EQUALS:
                    if (last_key) {
                        this->pp_interval_state.back().is_name
                            = this->pp_scanner
                                  ->to_string_fragment(last_key.value())
                                  .to_string();
                        if (!this->pp_interval_state.back().is_name.empty()) {
                            this->pp_interval_state.back().is_start
                                = static_cast<ssize_t>(this->pp_stream.tellp());
                        }
                        last_key = nonstd::nullopt;
                    }
                    break;
                default:
                    break;
            }
            if (start_on_depth
                && (el.e_token == DT_LSQUARE || el.e_token == DT_LCURLY))
            {
                if (this->pp_line_length > 0) {
                    this->pp_stream << std::endl;
                }
                this->pp_line_length = 0;
            }
        }
        this->pp_values.pop_front();
        retval = true;
    }
    return retval;
}

void
pretty_printer::start_new_line()
{
    bool has_output;

    if (this->pp_line_length > 0) {
        this->pp_stream << std::endl;
        this->pp_line_length = 0;
    }
    has_output = this->flush_values();
    if (has_output && this->pp_line_length > 0) {
        this->pp_stream << std::endl;
    }
    this->pp_line_length = 0;
    this->pp_body_lines.top() += 1;
}

void
pretty_printer::ascend()
{
    if (this->pp_depth > 0) {
        int lines = this->pp_body_lines.top();
        this->pp_depth -= 1;
        this->pp_body_lines.pop();
        this->pp_body_lines.top() += lines;

        if (!this->pp_is_xml) {
            this->append_child_node();
        }
        this->pp_interval_state.pop_back();
        this->pp_hier_stage = std::move(this->pp_hier_nodes.back());
        this->pp_hier_nodes.pop_back();
    } else {
        this->pp_body_lines.top() = 0;
    }
}

void
pretty_printer::descend()
{
    this->pp_depth += 1;
    this->pp_body_lines.push(0);
    this->pp_interval_state.resize(this->pp_depth + 1);
    this->pp_hier_nodes.push_back(
        std::make_unique<lnav::document::hier_node>());
}

void
pretty_printer::append_child_node()
{
    auto& ivstate = this->pp_interval_state.back();
    if (!ivstate.is_start) {
        return;
    }

    auto* top_node = this->pp_hier_nodes.back().get();
    auto new_key = ivstate.is_name.empty()
        ? lnav::document::section_key_t{top_node->hn_children.size()}
        : lnav::document::section_key_t{ivstate.is_name};
    this->pp_intervals.emplace_back(
        ivstate.is_start.value(),
        static_cast<ssize_t>(this->pp_stream.tellp()),
        new_key);
    auto new_node = this->pp_hier_stage != nullptr
        ? std::move(this->pp_hier_stage)
        : std::make_unique<lnav::document::hier_node>();
    auto* retval = new_node.get();
    new_node->hn_parent = top_node;
    new_node->hn_start = this->pp_intervals.back().start;
    if (!ivstate.is_name.empty()) {
        top_node->hn_named_children.insert({
            ivstate.is_name,
            retval,
        });
    }
    top_node->hn_children.emplace_back(std::move(new_node));
    ivstate.is_start = nonstd::nullopt;
    ivstate.is_name.clear();
}