-
Notifications
You must be signed in to change notification settings - Fork 3
/
LnParserTest.cpp
222 lines (205 loc) · 5.96 KB
/
LnParserTest.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
/*
* Copyright 2024 Rochus Keller <mailto:[email protected]>
*
* This file is part of the Luon language project.
*
* The following is the license that applies to this copy of the
* file. For a license to use the file under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include <QCoreApplication>
#include <QFile>
#include <QStringList>
#include <QtDebug>
#include <QFileInfo>
#include <QDir>
#include <QElapsedTimer>
#include <LnPpLexer.h>
#include <LnParser.h>
#include <LnParser2.h>
#include <LnSynTree.h>
#include <LnValidator.h>
using namespace Ln;
QStringList collectFiles( const QDir& dir, const QStringList& suffix )
{
QStringList res;
QStringList files = dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name );
foreach( const QString& f, files )
res += collectFiles( QDir( dir.absoluteFilePath(f) ), suffix );
files = dir.entryList( suffix, QDir::Files, QDir::Name );
foreach( const QString& f, files )
{
res.append(dir.absoluteFilePath(f));
}
return res;
}
static QString root;
#if 0
static void dump(QTextStream& out, const SynTree* node, int level)
{
QByteArray str;
if( node->d_tok.d_type == Tok_Invalid )
level--;
else if( node->d_tok.d_type < SynTree::R_First )
{
if( tokenTypeIsKeyword( node->d_tok.d_type ) )
str = tokenTypeString(node->d_tok.d_type);
else if( node->d_tok.d_type > TT_Specials )
str = QByteArray("\"") + node->d_tok.d_val + QByteArray("\"");
else
str = QByteArray("\"") + tokenTypeString(node->d_tok.d_type) + QByteArray("\"");
}else
str = SynTree::rToStr( node->d_tok.d_type );
if( !str.isEmpty() )
{
str += QByteArray("\t") + QFileInfo(node->d_tok.d_sourcePath).baseName().toUtf8() +
":" + QByteArray::number(node->d_tok.d_lineNr) +
":" + QByteArray::number(node->d_tok.d_colNr);
QByteArray ws;
for( int i = 0; i < level; i++ )
ws += "| ";
str = ws + str;
out << str.data() << endl;
}
foreach( SynTree* sub, node->d_children )
dump( out, sub, level + 1 );
}
#endif
class Lex : public Scanner
{
public:
PpLexer lex;
Token next()
{
return lex.nextToken();
}
Token peek(int offset)
{
return lex.peekToken(offset);
}
};
class Lex2 : public Scanner2
{
public:
PpLexer lex;
Token next()
{
return lex.nextToken();
}
Token peek(int offset)
{
return lex.peekToken(offset);
}
QString source() const { return lex.getSource(); }
};
static void checkParser(const QStringList& files)
{
int ok = 0;
QElapsedTimer timer;
timer.start();
foreach( const QString& file, files )
{
Lex lex;
lex.lex.setStream(file);
Parser p(&lex);
qDebug() << "**** parsing" << file.mid(root.size()+1);
p.RunParser();
if( !p.errors.isEmpty() )
{
foreach( const Parser::Error& e, p.errors )
qCritical() << e.path.mid(root.size()+1) << e.row << e.col << e.msg;
// break;
}else
{
ok++;
qDebug() << "ok";
}
#if 0
QFile out(file + ".st");
out.open(QIODevice::WriteOnly);
QTextStream s(&out);
dump(s,&p.root,0);
#else
QTextStream s(stdout);
//dump(s,&p.root,0);
#endif
}
qDebug() << "#### finished with" << ok << "files ok of total" << files.size() << "files" << "in" << timer.elapsed() << " [ms]";
}
static void checkParser2(const QStringList& files)
{
int ok = 0;
QElapsedTimer timer;
timer.start();
foreach( const QString& file, files )
{
Lex2 lex;
lex.lex.setStream(file);
QFile out;
out.open(stdout, QIODevice::WriteOnly);
//IlAsmRenderer ar(&out);
//MilEmitter e(&ar);
AstModel mdl;
Parser2 p(&mdl,&lex);
qDebug() << "**** parsing" << file.mid(root.size()+1);
p.RunParser();
if( !p.errors.isEmpty() )
{
foreach( const Parser2::Error& e, p.errors )
qCritical() << e.path.mid(root.size()+1) << e.pos.d_row << e.pos.d_col << e.msg;
// break;
}else
{
Validator v(&mdl);
Declaration* m = p.takeResult();
if( !v.validate(m) )
{
foreach( const Validator::Error& e, v.errors )
qCritical() << e.path.mid(root.size()+1) << e.pos.d_row << e.pos.d_col << e.msg;
}
ok++;
qDebug() << "ok";
}
}
qDebug() << "#### finished with" << ok << "files ok of total" << files.size() << "files" << "in" << timer.elapsed() << " [ms]";
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if( a.arguments().size() <= 1 )
return -1;
QStringList files;
QFileInfo info(a.arguments()[1]);
if( info.isDir() )
{
files = collectFiles(info.filePath(), QStringList() << "*.luon" << "*.ln");
root = info.filePath();
}else
files.append(info.filePath());
#if 1
foreach( const QString& f, files )
{
Lex lex;
lex.lex.setStream(f);
Token t = lex.lex.nextToken();
while( !t.isEof() )
{
qDebug() << t.getString() << t.d_val.constData() << t.d_lineNr << t.d_colNr;
t = lex.lex.nextToken();
}
}
#else
// checkParser(files);
checkParser2(files);
#endif
return 0;
}