aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/jinja2cpp/src/lexer.h
blob: e644de2f7b63a9bdd67566346c2ce0f63aab517d (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#ifndef JINJA2CPP_SRC_LEXER_H
#define JINJA2CPP_SRC_LEXER_H

#include "lexertk.h"
#include "internal_value.h"

#include <functional>

namespace jinja2
{
struct CharRange
{
    size_t startOffset;
    size_t endOffset;
    auto size() const {return endOffset - startOffset;}
};

struct Token
{
    enum Type
    {
        Unknown,

        // One-symbol operators
        Lt = '<',
        Gt = '>',
        Plus = '+',
        Minus = '-',
        Percent = '%',
        Mul = '*',
        Div = '/',
        LBracket = '(',
        RBracket = ')',
        LSqBracket = '[',
        RSqBracket = ']',
        LCrlBracket = '{',
        RCrlBracket = '}',
        Assign = '=',
        Comma = ',',
        Eof = 256,

        // General
        Identifier,
        IntegerNum,
        FloatNum,
        String,

        // Operators
        Equal,
        NotEqual,
        LessEqual,
        GreaterEqual,
        StarStar,
        DashDash,
        MulMul,
        DivDiv,
        True,
        False,
        None,

        // Keywords
        LogicalOr,
        LogicalAnd,
        LogicalNot,
        In,
        Is,
        For,
        Endfor,
        If,
        Else,
        ElIf,
        EndIf,
        Block,
        EndBlock,
        Extends,
        Macro,
        EndMacro,
        Call,
        EndCall,
        Filter,
        EndFilter,
        Set,
        EndSet,
        Include,
        Import,
        Recursive,
        Scoped,
        With,
        EndWith,
        Without,
        Ignore,
        Missing,
        Context,
        From,
        As,
        Do,

        // Template control
        CommentBegin,
        CommentEnd,
        RawBegin,
        RawEnd,
        MetaBegin,
        MetaEnd,
        StmtBegin,
        StmtEnd,
        ExprBegin,
        ExprEnd,
    };
    
    Type type = Unknown;
    CharRange range = {0, 0};
    InternalValue value;

    bool IsEof() const
    {
        return type == Eof;
    }

    bool operator == (char ch) const
    {
        return type == ch;
    }

    bool operator == (Type t) const
    {
        return type == t;
    }

    template<typename T>
    bool operator != (T v) const
    {
        return !(*this == v);
    }
};

enum class Keyword
{
    Unknown,

    // Keywords
    LogicalOr,
    LogicalAnd,
    LogicalNot,
    True,
    False,
    None,
    In,
    Is,
    For,
    Endfor,
    If,
    Else,
    ElIf,
    EndIf,
    Block,
    EndBlock,
    Extends,
    Macro,
    EndMacro,
    Call,
    EndCall,
    Filter,
    EndFilter,
    Set,
    EndSet,
    Include,
    Import,
    Recursive,
    Scoped,
    With,
    EndWith,
    Without,
    Ignore,
    Missing,
    Context,
    From,
    As,
    Do,
};

struct LexerHelper
{
    virtual std::string GetAsString(const CharRange& range) = 0;
    virtual InternalValue GetAsValue(const CharRange& range, Token::Type type) = 0;
    virtual Keyword GetKeyword(const CharRange& range) = 0;
    virtual char GetCharAt(size_t pos) = 0;
};

class Lexer
{
public:
    using TokensList = std::vector<Token>;
    Lexer(std::function<lexertk::token ()> tokenizer, LexerHelper* helper)
        : m_tokenizer(std::move(tokenizer))
        , m_helper(helper)
    {
    }

    bool Preprocess();
    const TokensList& GetTokens() const
    {
        return m_tokens;
    }
    
    auto GetHelper() const {return m_helper;}

private:
    bool ProcessNumber(const lexertk::token& token, Token& newToken);
    bool ProcessSymbolOrKeyword(const lexertk::token& token, Token& newToken);
    bool ProcessString(const lexertk::token& token, Token& newToken);
private:
    std::function<lexertk::token ()> m_tokenizer;
    TokensList m_tokens;
    LexerHelper* m_helper;
};

class LexScanner
{
public:
    struct State
    {
        Lexer::TokensList::const_iterator m_begin;
        Lexer::TokensList::const_iterator m_end;
        Lexer::TokensList::const_iterator m_cur;
    };

    struct StateSaver
    {
        StateSaver(LexScanner& scanner)
            : m_state(scanner.m_state)
            , m_scanner(scanner)
        {
        }

        ~StateSaver()
        {
            if (!m_commited)
                m_scanner.m_state = m_state;
        }

        void Commit()
        {
            m_commited = true;
        }

        State m_state;
        LexScanner& m_scanner;
        bool m_commited = false;
    };

    LexScanner(const Lexer& lexer)
        : m_helper(lexer.GetHelper())
    {
        m_state.m_begin = lexer.GetTokens().begin();
        m_state.m_end = lexer.GetTokens().end();
        Reset();
    }

    void Reset()
    {
        m_state.m_cur = m_state.m_begin;
    }

    auto GetState() const
    {
        return m_state;
    }

    void RestoreState(const State& state)
    {
        m_state = state;
    }

    const Token& NextToken()
    {
        if (m_state.m_cur == m_state.m_end)
            return EofToken();

        return *m_state.m_cur ++;
    }

    void EatToken()
    {
        if (m_state.m_cur != m_state.m_end)
            ++ m_state.m_cur;
    }

    void ReturnToken()
    {
        if (m_state.m_cur != m_state.m_begin)
            -- m_state.m_cur;
    }

    const Token& PeekNextToken() const
    {
        if (m_state.m_cur == m_state.m_end)
            return EofToken();

        return *m_state.m_cur;
    }

    bool EatIfEqual(char type, Token* tok = nullptr)
    {
        return EatIfEqual(static_cast<Token::Type>(type), tok);
    }

    bool EatIfEqual(Token::Type type, Token* tok = nullptr)
    {
        if (m_state.m_cur == m_state.m_end)
        {
            if(type == Token::Type::Eof && tok)
                *tok = EofToken();

            return type == Token::Type::Eof;
        }

        return EatIfEqualImpl(tok, [type](const Token& t) {return t.type == type;});
    }
    
    auto GetAsKeyword(const Token& tok) const
    {
        return m_helper->GetKeyword(tok.range);
    }
    
    bool EatIfEqual(Keyword kwType, Token* tok = nullptr)
    {
        if (m_state.m_cur == m_state.m_end)
            return false;

        return EatIfEqualImpl(tok, [this, kwType](const Token& t) {return GetAsKeyword(t) == kwType;});
    }
    
private:
    template<typename Fn>
    bool EatIfEqualImpl(Token* tok, Fn&& predicate)
    {
        if (predicate(*m_state.m_cur))
        {
            if (tok)
                *tok = *m_state.m_cur;
            ++ m_state.m_cur;
            return true;
        }

        return false;
    }

private:
    State m_state;
    LexerHelper* m_helper;
    
    static const Token& EofToken()
    {
        static Token eof;
        eof.type = Token::Eof;
        return eof;
    }
};

} // namespace jinja2

namespace std
{
template<>
struct hash<jinja2::Keyword>
{
    size_t operator()(jinja2::Keyword kw) const
    {
        return std::hash<int>{}(static_cast<int>(kw));
    }
};
} // namespace std

#endif // JINJA2CPP_SRC_LEXER_H