aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/jinja2cpp/src/lexer.cpp
blob: dfd33eb37eba5913d6ed1ab5c61a783870e90829 (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
#include "lexer.h"

#include <iostream>

namespace jinja2
{

bool Lexer::Preprocess()
{
    bool result = true;
    while (1)
    {
        lexertk::token token = m_tokenizer();
        if (token.is_error())
        {
            result = false;
            break;
        }

        Token newToken;
        newToken.range.startOffset = token.position;
        newToken.range.endOffset = newToken.range.startOffset + token.length;

        if (token.type == lexertk::token::e_eof)
        {
            newToken.type = Token::Eof;
            m_tokens.push_back(std::move(newToken));
            break;
        }

        switch (token.type)
        {
        case lexertk::token::e_number:
            result = ProcessNumber(token, newToken);
            break;
        case lexertk::token::e_symbol:
            result = ProcessSymbolOrKeyword(token, newToken);
            break;
        case lexertk::token::e_string:
            result = ProcessString(token, newToken);
            break;
        case lexertk::token::e_lte:
            newToken.type = Token::LessEqual;
            break;
        case lexertk::token::e_ne:
            newToken.type = Token::NotEqual;
            break;
        case lexertk::token::e_gte:
            newToken.type = Token::GreaterEqual;
            break;
        case lexertk::token::e_eq:
            newToken.type = Token::Equal;
            break;
        case lexertk::token::e_mulmul:
            newToken.type = Token::MulMul;
            break;
        case lexertk::token::e_divdiv:
            newToken.type = Token::DivDiv;
            break;
        default:
            newToken.type = static_cast<Token::Type>(token.type);
            break;
        }

        if (result)
            m_tokens.push_back(std::move(newToken));
        else
            break;
    }

    return result;
}

bool Lexer::ProcessNumber(const lexertk::token&, Token& newToken)
{
    newToken.type = Token::FloatNum;
    newToken.value = m_helper->GetAsValue(newToken.range, newToken.type);
    return true;
}

bool Lexer::ProcessSymbolOrKeyword(const lexertk::token&, Token& newToken)
{
    Keyword kwType = m_helper->GetKeyword(newToken.range);
    Token::Type tokType = Token::Unknown;
    
    switch (kwType)
    {
    case Keyword::None:
        tokType = Token::None;
        break;
    case Keyword::True:
        tokType = Token::True;
        break;
    case Keyword::False:
        tokType = Token::False;
        break;
    default:
        tokType = Token::Unknown;
        break;
    }
    
    if (tokType == Token::Unknown)
    {
        newToken.type = Token::Identifier;
        auto id = m_helper->GetAsString(newToken.range);
        newToken.value = InternalValue(id);
    }
    else
    {
        newToken.type = tokType;
    }
    return true;
}

bool Lexer::ProcessString(const lexertk::token&, Token& newToken)
{
    newToken.type = Token::String;
    newToken.value = m_helper->GetAsValue(newToken.range, newToken.type);
    return true;
}

} // namespace jinja2