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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
|
/* This file is part of kdev-pg-qt
Copyright (C) 2005 Roberto Raggi <roberto@kdevelop.org>
Copyright (C) 2006 Jakob Petsovits <jpetso@gmx.at>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "kdev-pg.h"
#include "kdev-pg-ast.h"
#include "kdev-pg-visitor.h"
#include "kdev-pg-default-visitor.h"
#include "kdev-pg-pretty-printer.h"
#include "kdev-pg-environment.h"
#include "kdev-pg-first.h"
#include "kdev-pg-follow.h"
#include "kdev-pg-checker.h"
#include "kdev-pg-inline-checker.h"
#include "kdev-pg-generate.h"
#include "kdev-pg-regexp.h"
#include <QtCore/QFile>
#include <QtCore/QStringList>
#include <QtCore/QCoreApplication>
#include <QtCore/QFileInfo>
namespace KDevPG
{
extern QTextStream checkOut;
}
int yyparse();
void usage()
{
KDevPG::checkOut << "Usage: kdev-pg-qt [further-options] --output=<name> [further-options] <file.g>" << endl;
}
void help()
{
usage();
KDevPG::checkOut
<< "Options:" << endl
<< "\t--output=<name> - Specify a prefix for all generated files" << endl
<< "\t--namespace=<NameSpaceName> - Specify the namespace for all generated classes (default: the prefix)" << endl
<< "\t--no-output - Do not actually generate files" << endl
<< "Components:" << endl
<< "\t--debug-visitor - Generate a visitor to dump the parse-tree" << endl
<< "\t--serialize-visitor - Generate a visitor to store the parse-tree in a QTextStream" << endl
<< "\t--no-ast - Do not generate any AST-files" << endl
<< "\t--with-ast - Generate AST (default)" << endl
<< "\t--no-parser - Do not create the parser, built-in-visitors etc." << endl
<< "\t--with-parser - The default, a parser will be generated" << endl
<< "\t--no-lexer - Do not generate the lexer" << endl
<< "\t--with-lexer - Ensure lexer generation" << endl
<< "\t--token-text - Generate a function converting the number of a token into its name" << endl
<< "Informative output:" << endl
<< "\t--terminals - Save a list of all terminals in a file named \"kdev-pg-terminals\"" << endl
<< "\t--symbols - Save a list of all non-terminals in a file named \"kdev-pg-symbols\"" << endl
<< "\t--rules - Save debugging-information for all rules in a file named \"kdev-pg-rules\"" << endl
<< "\t--automata - Save DFAs for all used named regexps in \"kdev-pg-lexer-name-<name>.dot\" and for all states in \"kdev-pg-lexer-state-<name>.dot\"" << endl
<< "Error-handling:" << endl
<< "\t--permissive-conflicts - The default, conflicts are shown, but kdev-pg-qt will continue (default)" << endl
<< "\t--strict-conflicts - Quit after having detected conflicts" << endl
<< "\t--ignore-conflicts - Do not perform conflict-checking" << endl
<< "Visitor generation:" << endl
<< "\t--new-visitor=VisitorName - Create a new empty visitor" << endl
<< "\t--inherit-default-visitor - Use the DefaultVisitor to visit sub-nodes" << endl
<< "\t--inherit-abstract-visitor - Reimplement the functionality of the DefaultVisitor" << endl
<< "Output format:" << endl
<< "\t--error-aware-code - Line-numbers in parser.cpp related compiler-messages will correspond to line-numbers in the grammar-file (default)" << endl
<< "\t--beautiful-code - Line-numbers in compiler-messages will be arbitrary, but the code will look more beautiful and it is probably more compiler-independent" << endl
<< "\t--compatible-error-aware-code - Like --error-aware-code, but using #line instead of GCC-specific directives, thus not providing included-from-info to display the line number in the generated file" << endl
<< "\t--visitor-table - Visit::visitNode will be implemented by using a lookup-array (default)" << endl
<< "\t--visitor-switch - Visitor::visitNode will use a switch-statement" << endl
<< "About:" << endl
<< "\t--help - Show this messages" << endl
<< "\t--usage - Show usage" << endl
<< "\t--version - Show version" << endl
<< "\t--author - Show authors" << endl << endl
<< "See http://techbase.kde.org/Development/KDevelop-PG-Qt_Introduction for further aid." << endl;
exit(EXIT_SUCCESS);
}
void version()
{
KDevPG::checkOut << "KDevelop-PG-Qt: 1.0.0" << endl;
exit(EXIT_SUCCESS);
}
void author()
{
KDevPG::checkOut << QString::fromUtf8("KDevelop-PG-Qt: Copyright © 2005-2012 by the KDevelop-PG-Qt developers:") << endl
<< QString::fromUtf8("\tRoberto Raggi\n"
"\tJakob Petsovits\n"
"\tAndreas Pakulat\n"
"\tJonathan Schmidt-Dominé\n"
"\t...and others") << endl;
exit(EXIT_SUCCESS);
}
class DebugRule
{
public:
QTextStream& out;
DebugRule(QTextStream& o): out(o)
{}
void operator()(KDevPG::Model::Node *node)
{
KDevPG::Model::EvolveItem *e = KDevPG::nodeCast<KDevPG::Model::EvolveItem*>(node);
out << endl;
KDevPG::PrettyPrinter p(out);
p(e);
bool initial = true;
KDevPG::World::NodeSet::iterator it;
out << endl;
out << " FIRST:[";
for (it = KDevPG::globalSystem.first(e).begin(); it != KDevPG::globalSystem.first(e).end(); ++it)
{
if (!initial)
out << ",";
p(*it);
initial = false;
}
out << "]";
initial = true;
out << endl;
out << " FOLLOW:[";
for (it = KDevPG::globalSystem.follow(e->mSymbol).begin();
it != KDevPG::globalSystem.follow(e->mSymbol).end(); ++it)
{
if (!initial)
out << ",";
p(*it);
initial = false;
}
out << "]";
out << endl;
}
};
namespace KDevPG
{
QFileInfo fileInfo;
}
int main(int argc, char **argv)
{
bool dump_terminals = false;
bool dump_symbols = false;
bool generate_parser = true;
bool generate_lexer = true;
bool ensure_generate_lexer_or_not = false;
bool ensure_generate_parser_or_not = false;
bool debug_rules = false;
bool debug_automata = false;
bool inherit_default = false;
bool output = true;
QString new_visitor;
QCoreApplication app(argc, argv);
QStringList args = QCoreApplication::arguments();
args.pop_front();
foreach(const QString& arg, args)
{
#define SW(str) arg.startsWith(QLatin1String(#str))
if (SW(--output=))
{
KDevPG::globalSystem.language = arg.mid( 9 );
output = true;
}
else if (SW(--namespace=))
{
KDevPG::globalSystem.ns = arg.mid( 12 );
}
else if (arg == "--no-output")
{
output = false;
}
else if (arg == "--no-ast")
{
KDevPG::globalSystem.generateAst = false;
}
else if (arg == "--with-ast")
{
KDevPG::globalSystem.generateAst = true;
}
else if (arg == "--serialize-visitor")
{
KDevPG::globalSystem.generateSerializeVisitor = true;
}
else if (arg == "--ignore-conflicts")
{
KDevPG::globalSystem.conflictHandling = KDevPG::World::Ignore;
}
else if (arg == "--strict-conflicts")
{
KDevPG::globalSystem.conflictHandling = KDevPG::World::Strict;
}
else if (arg == "--permissive-conflicts")
{
KDevPG::globalSystem.conflictHandling = KDevPG::World::Permissive;
}
else if (arg == "--debug-visitor")
{
KDevPG::globalSystem.generateDebugVisitor = true;
}
else if (arg == "--token-text")
{
KDevPG::globalSystem.generateTokenText = true;
}
else if (arg == "--no-parser")
{
generate_parser = false;
ensure_generate_parser_or_not = true;
}
else if (arg == "--with-parser")
{
generate_parser = true;
ensure_generate_parser_or_not = true;
}
else if (arg == "--no-lexer")
{
generate_lexer = false;
ensure_generate_lexer_or_not = true;
}
else if (arg == "--with-lexer")
{
generate_lexer = true;
ensure_generate_lexer_or_not = true;
}
else if (arg == "--beautiful-code")
{
KDevPG::globalSystem.lineNumberPolicy = KDevPG::World::BeautifulCode;
}
else if (arg == "--error-aware-code")
{
KDevPG::globalSystem.lineNumberPolicy = KDevPG::World::FullLineNumbers;
}
else if (arg == "--compatible-error-aware-code")
{
KDevPG::globalSystem.lineNumberPolicy = KDevPG::World::CompatibilityLineNumbers;
}
else if (arg == "--visitor-table")
{
KDevPG::globalSystem.visitorTable = true;
}
else if (arg == "--visitor-switch")
{
KDevPG::globalSystem.visitorTable = false;
}
else if (SW(--new-visitor=))
{
new_visitor = arg.mid( 14 );
KDevPG::globalSystem.conflictHandling = KDevPG::World::Ignore;
output = false;
}
else if (arg == "--inherit-default-visitor")
{
inherit_default = true;
}
else if (arg == "--inherit-abstract-visitor")
{
inherit_default = false;
}
else if (arg == "--help")
{
help();
}
else if (arg == "--version")
{
version();
}
else if (arg == "--author")
{
author();
}
else if (arg == "--usage")
{
usage();
exit(EXIT_SUCCESS);
}
else if (arg == "--terminals")
{
dump_terminals = true;
}
else if (arg == "--symbols")
{
dump_symbols = true;
}
else if (arg == "--rules")
{
debug_rules = true;
}
else if (arg == "--automata")
{
debug_automata = true;
}
else if (KDevPG::file.fileName().isEmpty())
{
KDevPG::file.setFileName(arg);
if (!KDevPG::file.open(QIODevice::ReadOnly|QIODevice::Text))
{
KDevPG::checkOut << "kdev-pg-qt: file ``" << arg
<< "'' not found!" << endl;
KDevPG::file.setFileName("");
}
else
KDevPG::fileInfo.setFile(KDevPG::file);
}
else
{
KDevPG::checkOut << "kdev-pg-qt: unknown option ``" << arg << "''"
<< endl;
}
}
if( KDevPG::globalSystem.ns.isEmpty() )
KDevPG::globalSystem.ns = KDevPG::globalSystem.language;
if( !KDevPG::file.isOpen() )
{
exit(1);
}
yyparse();
KDevPG::file.close();
KDevPG::globalSystem.finishedParsing();
if(!ensure_generate_parser_or_not)
generate_parser = !KDevPG::globalSystem.rules.empty();
if(generate_parser)
{
if(KDevPG::globalSystem.rules.empty())
{
KDevPG::checkOut << "** ERROR no parser rules" << endl;
KDevPG::ProblemSummaryPrinter::reportError();
}
else
{
if(KDevPG::globalSystem.start.empty())
KDevPG::checkOut << "** WARNING could not detect a start-symbol, every symbol gets reused, you have to care about EOFs yourself!" << endl;
for(QList<KDevPG::Model::EvolveItem*>::iterator it = KDevPG::globalSystem.rules.begin(); it != KDevPG::globalSystem.rules.end(); ++it)
{
KDevPG::InlineChecker check;
check(*it);
}
for(QList<KDevPG::Model::EvolveItem*>::iterator it = KDevPG::globalSystem.rules.begin(); it != KDevPG::globalSystem.rules.end(); ++it)
{
KDevPG::InitializeEnvironment initenv;
initenv(*it);
}
for(QList<KDevPG::Model::EvolveItem*>::iterator it = KDevPG::globalSystem.rules.begin(); it != KDevPG::globalSystem.rules.end(); ++it)
{
KDevPG::EmptyOperatorChecker check;
check(*it);
}
KDevPG::computeFirst();
KDevPG::computeFollow();
if(KDevPG::globalSystem.conflictHandling != KDevPG::World::Ignore)
{
for(QList<KDevPG::Model::EvolveItem*>::iterator it = KDevPG::globalSystem.rules.begin(); it != KDevPG::globalSystem.rules.end(); ++it)
{
KDevPG::FirstFollowConflictChecker check;
check(*it);
}
for(QList<KDevPG::Model::EvolveItem*>::iterator it = KDevPG::globalSystem.rules.begin(); it != KDevPG::globalSystem.rules.end(); ++it)
{
KDevPG::FirstFirstConflictChecker check;
check(*it);
}
}
for(QList<KDevPG::Model::EvolveItem*>::iterator it = KDevPG::globalSystem.rules.begin(); it != KDevPG::globalSystem.rules.end(); ++it)
{
KDevPG::EmptyFirstChecker check;
check(*it);
}
for(QList<KDevPG::Model::EvolveItem*>::iterator it = KDevPG::globalSystem.rules.begin(); it != KDevPG::globalSystem.rules.end(); ++it)
{
KDevPG::EmptyOperatorChecker check;
check(*it);
}
for(QList<KDevPG::Model::EvolveItem*>::iterator it = KDevPG::globalSystem.rules.begin(); it != KDevPG::globalSystem.rules.end(); ++it)
{
KDevPG::UndefinedSymbolChecker check;
check(*it);
}
for(QList<KDevPG::Model::EvolveItem*>::iterator it = KDevPG::globalSystem.rules.begin(); it != KDevPG::globalSystem.rules.end(); ++it)
{
KDevPG::UndefinedTokenChecker check;
check(*it);
}
}
}
if (ensure_generate_lexer_or_not && generate_lexer && !KDevPG::globalSystem.hasLexer)
{
KDevPG::checkOut << "** ERROR no lexer definiton" << endl;
KDevPG::ProblemSummaryPrinter::reportError();
generate_lexer = false;
}
if (!ensure_generate_lexer_or_not)
generate_lexer = KDevPG::globalSystem.hasLexer;
if (generate_lexer)
{
if(!KDevPG::globalSystem.lexerEnvs.contains("start"))
{
KDevPG::checkOut << "** ERROR missing start-state in the lexer" << endl;
KDevPG::ProblemSummaryPrinter::reportError();
}
if(!QRegExp("[a-zA-Z_][a-zA-Z_0-9]*").exactMatch(KDevPG::globalSystem.tokenStream))
{ // primarily to exclude KDevPG::TokenStream (the default value)
KDevPG::checkOut << "** ERROR You have to specify a valid name for your lexer (%token_stream)" << endl;
KDevPG::ProblemSummaryPrinter::reportError();
}
foreach(QString state, KDevPG::globalSystem.lexerEnvs.keys())
{
KDevPG::GNFA &alltogether = **KDevPG::globalSystem.lexerEnvResults.insert(state, new KDevPG::GNFA(KDevPG::globalSystem.lexerEnvs[state]));
KDevPG::GDFA &deterministic = **KDevPG::globalSystem.dfaForNfa.insert(&alltogether, new KDevPG::GDFA(alltogether.dfa()));
deterministic.minimize();
deterministic.setActions(KDevPG::globalSystem.lexerActions[state]);
}
}
if(dump_terminals)
{
QFile ft("kdev-pg-terminals");
ft.open( QIODevice::WriteOnly | QIODevice::Truncate );
QTextStream strm(&ft);
for (KDevPG::World::TerminalSet::iterator it = KDevPG::globalSystem.terminals.begin();
it != KDevPG::globalSystem.terminals.end(); ++it)
{
strm << it.key() << endl;
}
}
if (dump_symbols)
{
QFile ft("kdev-pg-symbols");
ft.open( QIODevice::WriteOnly | QIODevice::Truncate );
QTextStream strm(&ft);
for (KDevPG::World::SymbolSet::iterator it = KDevPG::globalSystem.symbols.begin();
it != KDevPG::globalSystem.symbols.end(); ++it)
{
strm << it.key() << endl;
}
}
if (debug_rules)
{
QFile ft("kdev-pg-rules");
ft.open( QIODevice::WriteOnly | QIODevice::Truncate );
QTextStream strm(&ft);
for(QList<KDevPG::Model::EvolveItem*>::iterator it = KDevPG::globalSystem.rules.begin(); it != KDevPG::globalSystem.rules.end(); ++it)
{
DebugRule dr(strm);
dr(*it);
}
}
if (debug_automata)
{
foreach(QString state, KDevPG::globalSystem.lexerEnvs.keys())
{
QFile ft(QString("kdev-pg-lexer-state-") + state + ".dot");
ft.open(QIODevice::WriteOnly);
QTextStream str(&ft);
KDevPG::globalSystem.dfaForNfa[KDevPG::globalSystem.lexerEnvResults[state]]->dotOutput(str, state);
ft.close();
}
foreach(QString name, KDevPG::globalSystem.regexpById.keys())
{
if(KDevPG::globalSystem.dfaForNfa.contains(KDevPG::globalSystem.regexpById[name]))
{
QFile ft(QString("kdev-pg-lexer-name-") + name + ".dot");
ft.open(QIODevice::WriteOnly);
QTextStream str(&ft);
KDevPG::globalSystem.dfaForNfa[KDevPG::globalSystem.regexpById[name]]->dotOutput(str, name);
ft.close();
}
}
}
KDevPG::ProblemSummaryPrinter()();
if (!(dump_terminals || dump_symbols || debug_rules) && output && KDevPG::globalSystem.language.isEmpty())
{
usage();
exit(EXIT_FAILURE);
}
if (output && generate_parser)
KDevPG::generateOutput();
if (output && generate_lexer)
KDevPG::generateLexer();
if (!new_visitor.isEmpty())
KDevPG::generateVisitor(new_visitor, inherit_default);
return EXIT_SUCCESS;
}
|