aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/select/node.h
blob: dd6b6d48800d59b5d702c9248672151cb633ee0e (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @class document::select::Node
 * @ingroup select
 *
 * @brief Base class for all nodes in the document selection tree.
 *
 * @author Håkon Humberset
 */

#pragma once

#include "resultlist.h"
#include "context.h"
#include "parser_limits.h"

namespace document::select {

class Visitor;

class Node : public Printable
{
protected:
    vespalib::string _name;
    uint32_t _max_depth;
    bool _parentheses; // Set to true if parentheses was used around this part
                       // Set such that we can recreate original query in print.
public:
    using UP = std::unique_ptr<Node>;
    using SP = std::shared_ptr<Node>;

    Node(vespalib::stringref name, uint32_t max_depth)
        : _name(name), _max_depth(max_depth), _parentheses(false)
    {
        throw_parse_error_if_max_depth_exceeded();
    }

    explicit Node(vespalib::stringref name)
        : _name(name), _max_depth(1), _parentheses(false)
    {}
    ~Node() override = default;

    // Depth is explicitly tracked to limit recursion to a sane maximum when building and
    // processing ASTs, as the Bison framework does not have anything useful for us there.
    // The AST is built from the leaves up towards the root, so we can cheaply track depth
    // of subtrees in O(1) time per node by computing a node's own depth based on immediate
    // children at node construction time.
    [[nodiscard]] uint32_t max_depth() const noexcept { return _max_depth; }

    void setParentheses() { _parentheses = true; }
    void clearParentheses() { _parentheses = false; }
    bool hadParentheses() const { return _parentheses; }

    virtual ResultList contains(const Context&) const = 0;
    virtual ResultList trace(const Context&, std::ostream& trace) const = 0;
    virtual bool isLeafNode() const { return true; }
    virtual void visit(Visitor&) const = 0;

    virtual Node::UP clone() const = 0;
protected:
    void throw_parse_error_if_max_depth_exceeded() const {
        if (_max_depth > ParserLimits::MaxRecursionDepth) {
            throw_max_depth_exceeded_exception();
        }
    }

    Node::UP wrapParens(Node* node) const {
        Node::UP ret(node);
        if (_parentheses) {
            ret->setParentheses();
        }
        return ret;
    }
};

}