summaryrefslogtreecommitdiffstats
path: root/src/jomlib/ppexprparser.cpp
blob: 7af643bdea1699097d9cae82f50534e87eede56e (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
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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of jom.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "ppexprparser.h"
#include "macrotable.h"
#include "ppexpr-lex.inc"

PPExprParser::PPExprParser()
:   stack_size(0),
    sym_stack(0),
    state_stack(0),
    m_macroTable(0)
{
}

PPExprParser::~PPExprParser()
{
    if (stack_size) {
        free(sym_stack);
        free(state_stack);
    }
}

inline void PPExprParser::reallocateStack()
{
    if (!stack_size)
        stack_size = 128;
    else
        stack_size <<= 1;

    sym_stack = reinterpret_cast<Value*> (realloc(sym_stack, stack_size * sizeof(Value)));
    state_stack = reinterpret_cast<int*> (realloc(state_stack, stack_size * sizeof(int)));
}

bool PPExprParser::parse(const char* str)
{
    const int INITIAL_STATE = 0;
    int yytoken = -1;

    if (!state_stack)
        reallocateStack();

    tos = 0;
    m_errorMessage.clear();
    state_stack[++tos] = INITIAL_STATE;
    yyInputBuffer = yy_scan_string(str);
    if (!yyInputBuffer) {
        m_errorMessage = "Can't create lexer's input buffer.";
        return false;
    }

    while (true)
    {
        if (yytoken == -1 && - TERMINAL_COUNT != action_index [state_stack [tos]]) {
            yytoken = yylex();
#ifdef DEBUG_GRAMMAR
            printf("*** yylex %d\n", yytoken);
#endif
        }

        int act = t_action(state_stack [tos], yytoken);

        if (act == ACCEPT_STATE) {
            yy_delete_buffer((YY_BUFFER_STATE)yyInputBuffer);
            return true;
        }

        else if (act > 0) {
            if (++tos == stack_size) {
                reallocateStack();
                if (!state_stack) {
                    m_errorMessage = "stack overflow";
                    yy_delete_buffer((YY_BUFFER_STATE)yyInputBuffer);
                    return false;
                }
            }

            sym_stack [tos] = yylval;
            state_stack [tos] = act;
            yytoken = -1;
        }

        else if (act < 0)
        {
            int r = - act - 1;

#ifdef DEBUG_GRAMMAR
            int ridx = rule_index [r];
            printf ("*** reduce using rule %d %s ::=", r + 1, spell[rule_info [ridx]]);
            ++ridx;
            for (int i = ridx; i < ridx + rhs [r]; ++i)
            {
                int symbol = rule_info [i];
                if (const char *name = spell [symbol])
                    printf (" %s", name);
                else
                    printf (" #%d", symbol);
            }
            printf ("\n");
#endif

            tos -= rhs [r];
            act = state_stack [tos++];

            switch (r) {

    case 2: // term0
        sym(1).num = (sym(1).num != 0 && sym(3).num != 0) ? 1 : 0;
        break;

    case 3: // term0
        sym(1).num = (sym(1).num != 0 || sym(3).num != 0) ? 1 : 0;
        break;

    case 4: // term0
        sym(1).num &= sym(3).num;
        break;

    case 5: // term0
        sym(1).num |= sym(3).num;
        break;

    case 8: { // strterm1
        QByteArray* lhs = sym(1).str;
        QByteArray* rhs = sym(3).str;
        sym(1).num = (*lhs == *rhs) ? 1 : 0;
        delete lhs;
        delete rhs;
        break;
    }

    case 9: { // strterm1
        QByteArray* lhs = sym(1).str;
        QByteArray* rhs = sym(3).str;
        sym(1).num = (*lhs == *rhs) ? 0 : 1;
        delete lhs;
        delete rhs;
        break;
    }

    case 11: // term2
        sym(1).num = (sym(1).num == sym(3).num);
        break;

    case 12: // term2
        sym(1).num = (sym(1).num != sym(3).num);
        break;

    case 13: // term2
        sym(1).num = (sym(1).num < sym(3).num);
        break;

    case 14: // term2
        sym(1).num = (sym(1).num > sym(3).num);
        break;

    case 15: // term2
        sym(1).num = (sym(1).num <= sym(3).num);
        break;

    case 16: // term2
        sym(1).num = (sym(1).num >= sym(3).num);
        break;

    case 18: // term3
        sym(1).num <<= sym(3).num;
        break;

    case 19: // term3
        sym(1).num >>= sym(3).num;
        break;

    case 21: // term4
        sym(1).num += sym(3).num;
        break;

    case 22: // term4
        sym(1).num -= sym(3).num;
        break;

    case 24: // term5
        sym(1).num *= sym(3).num;
        break;

    case 25: { // term5
        const int rhs = sym(3).num;
        if (rhs == 0) {
            m_errorMessage = "division by zero";
            yy_delete_buffer((YY_BUFFER_STATE)yyInputBuffer);
            return false;
        }
        sym(1).num /= rhs;
        break;
    }

    case 26: // term5
        sym(1).num %= sym(3).num;
        break;

    case 28: // term6
        sym(1).num = -sym(2).num;
        break;

    case 29: // term6
        sym(1).num = ~sym(2).num;
        break;

    case 30: // term6
        sym(1).num = (sym(2).num == 0) ? 1 : 0;
        break;

    case 32: // primary
        sym(1).num = sym(2).num;
        break;

            } // switch

            state_stack [tos] = nt_action (act, lhs [r] - TERMINAL_COUNT);
        }
        else
        {
            // ### ERROR RECOVERY HERE
            break;
        }
    }

    m_errorMessage = "Syntax Error";
    yy_delete_buffer((YY_BUFFER_STATE)yyInputBuffer);
    return false;
}