-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLnPpLexer.cpp
269 lines (249 loc) · 6.83 KB
/
LnPpLexer.cpp
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
/*
** Copyright (C) 2024 Rochus Keller ([email protected])
**
** This file is part of the Luon language project.
**
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
*/
// adopted from FreePascal and merged with preprocessing from Oberon+ parser
#include "LnPpLexer.h"
#include "LnTokenType.h"
#include <QBuffer>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QtDebug>
using namespace Ln;
PpLexer::PpLexer():d_sloc(0)
{
}
PpLexer::~PpLexer()
{
}
bool PpLexer::reset(const QByteArrayList& options)
{
d_buffer.clear();
d_sloc = 0;
d_options.clear();
foreach( const QByteArray& o, options )
d_options[ Token::getSymbol(o).constData() ] = true;
return true;
}
void PpLexer::setStream(QIODevice*d, const QString& sourcePath, const QDateTime& ts)
{
d_lex.setStream(d, sourcePath, ts);
}
bool PpLexer::setStream(const QString& sourcePath)
{
d_lex.setStream(sourcePath);
return true;
}
Token PpLexer::nextToken()
{
Token t;
if( !d_buffer.isEmpty() )
{
t = d_buffer.first();
d_buffer.pop_front();
}else
t = nextTokenImp();
Q_ASSERT( t.d_type != Tok_LtStar && t.d_type != Tok_Comment );
return t;
}
Token PpLexer::peekToken(quint8 lookAhead)
{
Q_ASSERT( lookAhead > 0 );
while( d_buffer.size() < lookAhead )
{
Token t = nextTokenImp();
Q_ASSERT( t.d_type != Tok_Comment && t.d_type != Tok_Comment );
d_buffer.push_back( t );
}
return d_buffer[ lookAhead - 1 ];
}
void PpLexer::ppcmd()
{
Token t = d_lex.peekToken();
switch(t.d_type)
{
case Tok_ident:
{
Token name = d_lex.nextToken();
t = d_lex.nextToken();
switch( t.d_type )
{
case Tok_Plus:
d_options[name.d_val.constData()] = true;
t = d_lex.nextToken();
break;
case Tok_Minus:
d_options[name.d_val.constData()] = false;
t = d_lex.nextToken();
break;
default:
raise(name, "expecting '+' or '-' after identifier" );
while( t.d_type != Tok_StarGt && t.d_type != Tok_Eof )
t = d_lex.nextToken();
break;
}
}
break;
case Tok_IF:
d_lex.nextToken();
{
const bool cond = ppexpr();
d_conditionStack.append( ppstatus(false) );
ppsetthis( ppouter().open && cond );
t = d_lex.nextToken();
if( t.d_type != Tok_THEN )
raise( t, "expecting 'THEN'" );
else
t = d_lex.nextToken();
}
break;
case Tok_ELSIF:
d_lex.nextToken();
if( ppthis().elseSeen || d_conditionStack.isEmpty() )
{
raise(t, "ELSIF directive not expected here");
while( t.d_type != Tok_StarGt && t.d_type != Tok_Eof )
t = d_lex.nextToken();
}else
{
const bool cond = ppexpr();
ppsetthis( ppouter().open && cond && !ppthis().openSeen );
t = d_lex.nextToken();
if( t.d_type != Tok_THEN )
raise( t, "expecting 'THEN'" );
else
t = d_lex.nextToken();
}
break;
case Tok_ELSE:
d_lex.nextToken();
if( ppthis().elseSeen || d_conditionStack.isEmpty() )
{
raise(t, "ELSE directive not expected here");
while( t.d_type != Tok_StarGt && t.d_type != Tok_Eof )
t = d_lex.nextToken();
}else
{
ppsetthis( ppouter().open && !ppthis().openSeen, true );
t = d_lex.nextToken();
}
break;
case Tok_END:
d_lex.nextToken();
if( d_conditionStack.isEmpty() )
raise(t, "spurious END directive");
else
d_conditionStack.pop_back();
t = d_lex.nextToken();
break;
}
if( t.d_type != Tok_StarGt )
{
raise(t, "expecting '*>'" );
while( t.d_type != Tok_StarGt && t.d_type != Tok_Eof )
t = d_lex.nextToken();
}
}
bool PpLexer::ppexpr()
{
bool res = ppterm();
Token t = d_lex.peekToken();
while( t.d_type == Tok_OR ) // check all, otherwise not all tokens are eaten
{
t = d_lex.nextToken();
res = ppterm() || res; // order significant
t = d_lex.peekToken();
}
return res;
}
bool PpLexer::ppterm()
{
bool res = ppfactor();
Token t = d_lex.peekToken();
while( t.d_type == Tok_Amp || t.d_type == Tok_AND )
{
t = d_lex.nextToken();
res = ppfactor() && res;
t = d_lex.peekToken();
}
return res;
}
bool PpLexer::ppfactor()
{
Token t = d_lex.nextToken();
switch( t.d_type )
{
case Tok_ident:
return d_options.value(t.d_val.constData());
case Tok_Lpar:
{
const bool res = ppexpr();
t = d_lex.nextToken();
if( t.d_type != Tok_Rpar )
error( t, "expecting ')'");
return res;
}
case Tok_Tilde:
case Tok_NOT:
return !ppfactor();
}
return false;
}
Token PpLexer::nextTokenImp()
{
Token t = d_lex.peekToken();
while( t.d_type == Tok_LtStar )
{
while( t.d_type == Tok_LtStar )
{
const Token start = d_lex.nextToken();
t = d_lex.peekToken();
if( t.d_type != Tok_StarGt && t.d_type != Tok_Eof )
{
try{ ppcmd(); }
catch(...){ return d_err; }
}
if( t.d_type == Tok_Eof )
return error( start, "non-terminated source code directive" );
else
t = d_lex.peekToken();
}
if( !ppthis().open )
{
t = d_lex.peekToken();
while( t.d_type != Tok_LtStar && t.d_type != Tok_Eof )
{
t = d_lex.nextToken();
t = d_lex.peekToken();
}
}
}
t = d_lex.nextToken();
if( t.d_type == Tok_Eof && !d_conditionStack.isEmpty() )
return error( t, "expecting END directive" );
return t;
}
Token PpLexer::error(const Token& t, const QByteArray& msg)
{
d_err = t;
d_err.d_type = Tok_Invalid;
d_err.d_val = msg;
return d_err;
}
void PpLexer::raise(const Token& t, const QByteArray& msg)
{
error(t,msg);
throw "";
}