summaryrefslogtreecommitdiffstats
path: root/yql/essentials/parser/proto_ast/antlr4/proto_ast_antlr4.h
blob: 13cbbc6e73b68186f17e9d23f720d568a69b3cff (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
#pragma once

#include <yql/essentials/parser/common/error.h>
#include <yql/essentials/parser/common/antlr4/error_listener.h>
#include <yql/essentials/parser/common/antlr4/lexer_tokens_collector.h>

#include <yql/essentials/parser/proto_ast/common.h>

#ifdef ERROR
#undef ERROR
#endif
#include <contrib/libs/antlr4_cpp_runtime/src/antlr4-runtime.h>

namespace NProtoAST {
    using namespace NAST;

    template <typename InputType>
    void InvalidCharacter(IOutputStream& err, const InputType* input);

    template <typename TokenType>
    inline void InvalidToken(IOutputStream& err, const TokenType* token);

    template <>
    inline void InvalidToken<antlr4::Token>(IOutputStream& err, const antlr4::Token* token) {
        if (token) {
            if (token->getInputStream()) {
                err << " '" << token->getText() << "'";
            } else {
                err << ABSENCE;
            }
        }
    }

    template <typename TParser, typename TLexer>
    class TProtoASTBuilder4 {

    public:
        TProtoASTBuilder4(TStringBuf data, const TString& queryName = "query", google::protobuf::Arena* arena = nullptr)
            : QueryName_(queryName)
            , InputStream_(data)
            , Lexer_(&InputStream_)
            , TokenStream_(&Lexer_)
            , Parser_(&TokenStream_, arena)
        {
        }

        google::protobuf::Message* BuildAST(IErrorCollector& errors) {
            // TODO: find a better way to break on lexer errors
            typename antlr4::YqlErrorListener listener(&errors, &Parser_.error);
            Parser_.removeErrorListeners();
            Parser_.addErrorListener(&listener);
            try {
                auto result = Parser_.Parse(&errors);
                Parser_.removeErrorListener(&listener);
                Parser_.error = false;
                return result;
            } catch (const TTooManyErrors&) {
                Parser_.removeErrorListener(&listener);
                Parser_.error = false;
                return nullptr;
            } catch (...) {
                errors.Error(0, 0, CurrentExceptionMessage());
                Parser_.removeErrorListener(&listener);
                Parser_.error = false;
                return nullptr;
            }
        }

    private:
        TString QueryName_;

        antlr4::ANTLRInputStream InputStream_;
        TLexer Lexer_;

        antlr4::CommonTokenStream TokenStream_;
        TParser Parser_;
    };

} // namespace NProtoAST