aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/annotation/spannode.cpp
blob: 99d8cec7ee856ecb6942dc7fd225cc398edb4a4a (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "spannode.h"
#include "spantreevisitor.h"
#include "alternatespanlist.h"
#include <vespa/vespalib/stllike/asciistream.h>

namespace document {

namespace {

class ToStringVisitor : public SpanTreeVisitor {
public:
    ToStringVisitor();
    ~ToStringVisitor();
    vespalib::stringref str() const { return _os.str(); }
private:
    vespalib::asciistream _os;
    vespalib::string _indent;

    void newline() {
        _os << "\n" << _indent;
    }
    void visitChildren(const SpanList & list) {
        for (const SpanNode * node : list) {
            newline();
            node->accept(*this);
        }
    }

    void visitChildren(const SimpleSpanList & list) {
        for (const Span & node : list) {
            newline();
            node.accept(*this);
        }
    }

    void visit(const Span & span) override {
        _os << "Span(" << span.from() << ", " << span.length() << ")";
    }

    void visit(const SpanList & list) override {
        _os << "SpanList(";
        if (list.size() > 1) {
            vespalib::string oldIndent(_indent);
            _indent += "  ";
            visitChildren(list);
            _indent = oldIndent;
            newline();
        } else {
            (*list.begin())->accept(*this);
        }
        _os << ")";
    }
    void visit(const SimpleSpanList & list) override {
        _os << "SimpleSpanList(";
        if (list.size() > 1) {
            vespalib::string oldIndent(_indent);
            _indent += "  ";
            visitChildren(list);
            _indent = oldIndent;
            newline();
        } else {
            visit(*list.begin());
        }
        _os << ")";
    }
    void visit(const AlternateSpanList & list) override {
        _os << "AlternateSpanList(";
        vespalib::string oldIndent(_indent);
        _indent += "  ";
        for (size_t i = 0; i < list.getNumSubtrees(); ++i) {
            newline();
            _os << "Probability " << list.getProbability(i) << " : ";
            visit(list.getSubtree(i));
        }
        _indent = oldIndent;
        newline();
        _os << ")";
    }
};

ToStringVisitor::ToStringVisitor() : _os(), _indent() { }
ToStringVisitor::~ToStringVisitor() { }

}

vespalib::string
SpanNode::toString() const {
    ToStringVisitor os;
    accept(os);
    return os.str();
}

std::ostream & operator << (std::ostream & os, const SpanNode & node) {
    return os << node.toString();
}

}