aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/parser/proto_ast/common.h
blob: 013f47228838c9c2112e5cace1bfcb3e529eec7c (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
#pragma once

#include <yql/essentials/parser/lexer_common/lexer.h>

#include <google/protobuf/message.h>
#include <util/generic/ptr.h>
#include <util/generic/vector.h>
#include <util/charset/utf8.h>

namespace NProtoAST {
    static const char* INVALID_TOKEN_NAME = "nothing";
    static const char* ABSENCE = " absence";

    template <typename InputType>
    void InvalidCharacter(IOutputStream& err, const InputType* input) {
        wchar32 rune = 0;
        size_t runeLen = 0;
        auto begin = input->get_nextChar();
        auto end = begin + input->get_sizeBuf();
        if (begin != end && SafeReadUTF8Char(rune, runeLen, begin, end) == RECODE_OK) {
            err << " '" << TStringBuf((const char*)begin, runeLen) << "' (Unicode character <" << ui32(rune) << ">)";
        }
    }


    template <typename TokenType>
    inline void InvalidToken(IOutputStream& err, const TokenType* token) {
        if (token) {
            if (token->get_input()) {
                err << " '" << token->getText() << "'";
            } else {
                err << ABSENCE;
            }
        }
    }

    class TTooManyErrors : public yexception {
    };

    class IErrorCollector {
    public:
        explicit IErrorCollector(size_t maxErrors);
        virtual ~IErrorCollector();

        // throws TTooManyErrors
        void Error(ui32 line, ui32 col, const TString& message);

    private:
        virtual void AddError(ui32 line, ui32 col, const TString& message) = 0;

    protected:
        const size_t MaxErrors;
        size_t NumErrors;
    };

    class TErrorOutput: public IErrorCollector {
    public:
        TErrorOutput(IOutputStream& err, const TString& name, size_t maxErrors);
        virtual ~TErrorOutput();

    private:
        void AddError(ui32 line, ui32 col, const TString& message) override;

    public:
        IOutputStream& Err;
        TString Name;
    };

} // namespace NProtoAST