aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/select/valuenode.h
blob: 47980ca039d09bdf108fc8a2c3863c83ee7b9926 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @class document::select::ValueNode
 * @ingroup select
 *
 * @brief Node representing a value in the tree
 *
 * @author Håkon Humberset
 */

#pragma once

#include "value.h"
#include "parser_limits.h"

namespace document::select {

class Context;
class Visitor;

class ValueNode : public Printable
{
public:
    using UP = std::unique_ptr<ValueNode>;

    explicit ValueNode(uint32_t max_depth)
        : _max_depth(max_depth), _parentheses(false)
    {
        throw_parse_error_if_max_depth_exceeded();
    }
    ValueNode() : _max_depth(1), _parentheses(false) {}
    ~ValueNode() override = default;

    // See comments for same function in node.h for a description on how and why
    // we track this. Since Node and ValueNode live in completely separate type
    // hierarchies, this particular bit of code duplication is unfortunate but
    // incurs the least cognitive overhead.
    [[nodiscard]] uint32_t max_depth() const noexcept { return _max_depth; }

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

    virtual std::unique_ptr<Value> getValue(const Context& context) const  = 0;
    virtual void visit(Visitor&) const = 0;
    virtual ValueNode::UP clone() const = 0;
    virtual std::unique_ptr<Value> traceValue(const Context &context, std::ostream &out) const;
private:
    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.

protected:
    void throw_parse_error_if_max_depth_exceeded() const {
        if (_max_depth > ParserLimits::MaxRecursionDepth) {
            throw_max_depth_exceeded_exception();
        }
    }

    std::unique_ptr<ValueNode> wrapParens(std::unique_ptr<ValueNode> node) const {
        if (_parentheses) {
            node->setParentheses();
        }
        return node;
    }

    std::unique_ptr<Value> defaultTrace(std::unique_ptr<Value> val, std::ostream& out) const;
};

}