aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/JSONPath/Parsers/ParserJSONPathQuery.cpp
blob: d8d633a1ec90c5f4314d867ebf9fc8ba317da461 (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
#include <Functions/JSONPath/ASTs/ASTJSONPathQuery.h>
#include <Functions/JSONPath/Parsers/ParserJSONPathQuery.h>
#include <Functions/JSONPath/Parsers/ParserJSONPathRoot.h>
#include <Functions/JSONPath/Parsers/ParserJSONPathMemberAccess.h>
#include <Functions/JSONPath/Parsers/ParserJSONPathMemberSquareBracketAccess.h>
#include <Functions/JSONPath/Parsers/ParserJSONPathRange.h>
#include <Functions/JSONPath/Parsers/ParserJSONPathStar.h>

namespace DB

{
/**
 *
 * @param pos token iterator
 * @param query node of ASTJSONPathQuery
 * @param expected stuff for logging
 * @return was parse successful
 */
bool ParserJSONPathQuery::parseImpl(Pos & pos, ASTPtr & query, Expected & expected)
{
    query = std::make_shared<ASTJSONPathQuery>();
    ParserJSONPathMemberAccess parser_jsonpath_member_access;
    ParserJSONPathMemberSquareBracketAccess parser_jsonpath_member_square_bracket_access;
    ParserJSONPathRange parser_jsonpath_range;
    ParserJSONPathStar parser_jsonpath_star;
    ParserJSONPathRoot parser_jsonpath_root;

    ASTPtr path_root;
    if (!parser_jsonpath_root.parse(pos, path_root, expected))
    {
        return false;
    }
    query->children.push_back(path_root);

    ASTPtr accessor;
    while (parser_jsonpath_member_access.parse(pos, accessor, expected)
           || parser_jsonpath_member_square_bracket_access.parse(pos, accessor, expected)
           || parser_jsonpath_range.parse(pos, accessor, expected)
           || parser_jsonpath_star.parse(pos, accessor, expected))
    {
        if (accessor)
        {
            query->children.push_back(accessor);
            accessor = nullptr;
        }
    }
    /// parsing was successful if we reached the end of query by this point
    return pos->type == TokenType::EndOfStream;
}

}