summaryrefslogtreecommitdiffstats
path: root/src/libixion/lexer_tokens.cpp
blob: 2f3bef340d1bf315aa2670afa29023babcc9ff00 (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "lexer_tokens.hpp"

#include <iostream>
#include <sstream>
#include <algorithm>

using namespace std;

namespace ixion {

std::string print_tokens(const lexer_tokens_t& tokens, bool verbose)
{
    std::ostringstream os;

    for (const auto& t : tokens)
    {
        if (verbose)
            os << "(" << get_opcode_name(t.opcode) << ")'" << t << "' ";
        else
            os << t;
    }

    return os.str();
}

const char* get_opcode_name(lexer_opcode_t oc)
{
    switch (oc)
    {
        case lexer_opcode_t::value:
            return "value";
        case lexer_opcode_t::string:
            return "string";
        case lexer_opcode_t::name:
            return "name";
        case lexer_opcode_t::divide:
            return "divide";
        case lexer_opcode_t::minus:
            return "minus";
        case lexer_opcode_t::multiply:
            return "multiply";
        case lexer_opcode_t::exponent:
            return "exponent";
        case lexer_opcode_t::concat:
            return "concat";
        case lexer_opcode_t::equal:
            return "equal";
        case lexer_opcode_t::less:
            return "less";
        case lexer_opcode_t::greater:
            return "greater";
        case lexer_opcode_t::plus:
            return "plus";
        case lexer_opcode_t::open:
            return "open";
        case lexer_opcode_t::close:
            return "close";
        case lexer_opcode_t::sep:
            return "sep";
        case lexer_opcode_t::array_row_sep:
            return "array-row-sep";
        case lexer_opcode_t::array_open:
            return "array-open";
        case lexer_opcode_t::array_close:
            return "array-close";
    }
    return "";
}

lexer_token::lexer_token(lexer_opcode_t _opcode) :
    opcode(_opcode)
{
}

lexer_token::lexer_token(lexer_opcode_t _opcode, std::string_view _value) :
    opcode(_opcode), value(_value)
{
}

lexer_token::lexer_token(double _value) :
    opcode(lexer_opcode_t::value), value(_value)
{
}


std::ostream& operator<<(std::ostream& os, const lexer_token& t)
{
    switch (t.opcode)
    {
        case lexer_opcode_t::plus:
            os << '+';
            break;
        case lexer_opcode_t::minus:
            os << '-';
            break;
        case lexer_opcode_t::divide:
            os << '/';
            break;
        case lexer_opcode_t::multiply:
            os << '*';
            break;
        case lexer_opcode_t::exponent:
            os << '^';
            break;
        case lexer_opcode_t::concat:
            os << '&';
            break;
        case lexer_opcode_t::equal:
            os << '=';
            break;
        case lexer_opcode_t::less:
            os << '<';
            break;
        case lexer_opcode_t::greater:
            os << '>';
            break;
        case lexer_opcode_t::open:
            os << '(';
            break;
        case lexer_opcode_t::close:
            os << ')';
            break;
        case lexer_opcode_t::array_open:
            os << '{';
            break;
        case lexer_opcode_t::array_close:
            os << '}';
            break;
        case lexer_opcode_t::sep:
            os << ',';
            break;
        case lexer_opcode_t::array_row_sep:
            os << ';';
            break;
        case lexer_opcode_t::name:
        case lexer_opcode_t::string:
            os << std::get<std::string_view>(t.value);
            break;
        case lexer_opcode_t::value:
            os << std::get<double>(t.value);
            break;
    }

    return os;
}

} // namespace ixion

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */