util/qlalr/cppgenerator.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
   339           fprintf (stderr, "*** cannot create %s\n", qPrintable(grammar.merged_output));
   339           fprintf (stderr, "*** cannot create %s\n", qPrintable(grammar.merged_output));
   340           return;
   340           return;
   341         }
   341         }
   342 
   342 
   343       QTextStream out (&f);
   343       QTextStream out (&f);
   344       out << "// This file was generated by qlalr - DO NOT EDIT!\n";
   344 
   345 
   345       // copyright headers must come first, otherwise the headers tests will fail
   346       if (copyright)
   346       if (copyright)
   347         {
   347         {
   348           out << copyrightHeader()
   348           out << copyrightHeader()
   349               << privateCopyrightHeader()
   349               << privateCopyrightHeader()
   350               << endl;
   350               << endl;
   351         }
   351         }
   352 
   352 
       
   353       out << "// This file was generated by qlalr - DO NOT EDIT!\n";
       
   354 
   353       out << startIncludeGuard(grammar.merged_output) << endl;
   355       out << startIncludeGuard(grammar.merged_output) << endl;
       
   356 
       
   357       if (copyright) {
       
   358           out << "#if defined(Q_OS_VXWORKS) && defined(ERROR)" << endl
       
   359               << "#  undef ERROR" << endl
       
   360               << "#endif" << endl << endl;
       
   361       }
   354 
   362 
   355       generateDecl (out);
   363       generateDecl (out);
   356       generateImpl (out);
   364       generateImpl (out);
   357       out << p.decls();
   365       out << p.decls();
   358       out << p.impls();
   366       out << p.impls();
   369 
   377 
   370   { // decls...
   378   { // decls...
   371     QFile f (declFileName);
   379     QFile f (declFileName);
   372     f.open (QFile::WriteOnly);
   380     f.open (QFile::WriteOnly);
   373     QTextStream out (&f);
   381     QTextStream out (&f);
   374     out << "// This file was generated by qlalr - DO NOT EDIT!\n";
       
   375 
   382 
   376     QString prot = declFileName.toUpper ().replace (QLatin1Char ('.'), QLatin1Char ('_'));
   383     QString prot = declFileName.toUpper ().replace (QLatin1Char ('.'), QLatin1Char ('_'));
   377 
   384 
       
   385     // copyright headers must come first, otherwise the headers tests will fail
   378     if (copyright)
   386     if (copyright)
   379       {
   387       {
   380         out << copyrightHeader()
   388         out << copyrightHeader()
   381             << privateCopyrightHeader()
   389             << privateCopyrightHeader()
   382             << endl;
   390             << endl;
   383       }
   391       }
   384 
   392 
       
   393     out << "// This file was generated by qlalr - DO NOT EDIT!\n";
       
   394 
   385     out << "#ifndef " << prot << endl
   395     out << "#ifndef " << prot << endl
   386         << "#define " << prot << endl
   396         << "#define " << prot << endl
   387         << endl;
   397         << endl;
   388 
   398 
       
   399     if (copyright) {
       
   400         out << "#include <QtCore/qglobal.h>" << endl << endl;
       
   401         out << "QT_BEGIN_NAMESPACE" << endl << endl;
       
   402     }
   389     generateDecl (out);
   403     generateDecl (out);
       
   404     if (copyright)
       
   405         out << "QT_END_NAMESPACE" << endl;
   390 
   406 
   391     out << "#endif // " << prot << endl << endl;
   407     out << "#endif // " << prot << endl << endl;
   392   } // end decls
   408   } // end decls
   393 
   409 
   394   { // bits...
   410   { // bits...
   395     QFile f (bitsFileName);
   411     QFile f (bitsFileName);
   396     f.open (QFile::WriteOnly);
   412     f.open (QFile::WriteOnly);
   397     QTextStream out (&f);
   413     QTextStream out (&f);
   398     out << "// This file was generated by qlalr - DO NOT EDIT!\n";
   414 
   399 
   415     // copyright headers must come first, otherwise the headers tests will fail
   400     if (copyright)
   416     if (copyright)
   401       out << copyrightHeader();
   417       out << copyrightHeader();
   402 
   418 
       
   419     out << "// This file was generated by qlalr - DO NOT EDIT!\n";
       
   420 
   403     out << "#include \"" << declFileName << "\"" << endl << endl;
   421     out << "#include \"" << declFileName << "\"" << endl << endl;
       
   422     if (copyright)
       
   423         out << "QT_BEGIN_NAMESPACE" << endl << endl;
   404     generateImpl(out);
   424     generateImpl(out);
       
   425     if (copyright)
       
   426         out << "QT_END_NAMESPACE" << endl;
   405 
   427 
   406   } // end bits
   428   } // end bits
   407 
   429 
   408   if (! grammar.decl_file_name.isEmpty ())
   430   if (! grammar.decl_file_name.isEmpty ())
   409     {
   431     {
   410       QFile f (grammar.decl_file_name);
   432       QFile f (grammar.decl_file_name);
   411       f.open (QFile::WriteOnly);
   433       f.open (QFile::WriteOnly);
   412       QTextStream out (&f);
   434       QTextStream out (&f);
   413       out << "// This file was generated by qlalr - DO NOT EDIT!\n";
       
   414       out << p.decls();
   435       out << p.decls();
   415     }
   436     }
   416 
   437 
   417   if (! grammar.impl_file_name.isEmpty ())
   438   if (! grammar.impl_file_name.isEmpty ())
   418     {
   439     {
   419       QFile f (grammar.impl_file_name);
   440       QFile f (grammar.impl_file_name);
   420       f.open (QFile::WriteOnly);
   441       f.open (QFile::WriteOnly);
   421       QTextStream out (&f);
   442       QTextStream out (&f);
   422       out << "// This file was generated by qlalr - DO NOT EDIT!\n";
       
   423       out << p.impls();
   443       out << p.impls();
   424     }
   444     }
   425 }
   445 }
   426 
   446 
   427 QString CppGenerator::debugInfoProt() const
   447 QString CppGenerator::debugInfoProt() const
   466       << "    GOTO_INDEX_OFFSET = " << compressed_action.index.size () << "," << endl
   486       << "    GOTO_INDEX_OFFSET = " << compressed_action.index.size () << "," << endl
   467       << "    GOTO_INFO_OFFSET = " << compressed_action.info.size () << "," << endl
   487       << "    GOTO_INFO_OFFSET = " << compressed_action.info.size () << "," << endl
   468       << "    GOTO_CHECK_OFFSET = " << compressed_action.check.size () << endl
   488       << "    GOTO_CHECK_OFFSET = " << compressed_action.check.size () << endl
   469       << "  };" << endl
   489       << "  };" << endl
   470       << endl
   490       << endl
   471       << "  static const char  *const spell [];" << endl
   491       << "  static const char  *const    spell [];" << endl
   472       << "  static const int            lhs [];" << endl
   492       << "  static const short             lhs [];" << endl
   473       << "  static const int            rhs [];" << endl;
   493       << "  static const short             rhs [];" << endl;
   474 
   494 
   475   if (debug_info)
   495   if (debug_info)
   476     {
   496     {
   477       QString prot = debugInfoProt();
   497       QString prot = debugInfoProt();
   478 
   498 
   480           << "  static const int     rule_index [];" << endl
   500           << "  static const int     rule_index [];" << endl
   481           << "  static const int      rule_info [];" << endl
   501           << "  static const int      rule_info [];" << endl
   482           << "#endif // " << prot << endl << endl;
   502           << "#endif // " << prot << endl << endl;
   483     }
   503     }
   484 
   504 
   485   out << "  static const int   goto_default [];" << endl
   505   out << "  static const short    goto_default [];" << endl
   486       << "  static const int action_default [];" << endl
   506       << "  static const short  action_default [];" << endl
   487       << "  static const int   action_index [];" << endl
   507       << "  static const short    action_index [];" << endl
   488       << "  static const int    action_info [];" << endl
   508       << "  static const short     action_info [];" << endl
   489       << "  static const int   action_check [];" << endl
   509       << "  static const short    action_check [];" << endl
   490       << endl
   510       << endl
   491       << "  static inline int nt_action (int state, int nt)" << endl
   511       << "  static inline int nt_action (int state, int nt)" << endl
   492       << "  {" << endl
   512       << "  {" << endl
   493       << "    const int *const goto_index = &action_index [GOTO_INDEX_OFFSET];" << endl
   513       << "    const int yyn = action_index [GOTO_INDEX_OFFSET + state] + nt;" << endl
   494       << "    const int *const goto_check = &action_check [GOTO_CHECK_OFFSET];" << endl
   514       << "    if (yyn < 0 || action_check [GOTO_CHECK_OFFSET + yyn] != nt)" << endl
   495       << endl
       
   496       << "    const int yyn = goto_index [state] + nt;" << endl
       
   497       << endl
       
   498       << "    if (yyn < 0 || goto_check [yyn] != nt)" << endl
       
   499       << "      return goto_default [nt];" << endl
   515       << "      return goto_default [nt];" << endl
   500       << endl
   516       << endl
   501       << "    const int *const goto_info = &action_info [GOTO_INFO_OFFSET];" << endl
   517       << "    return action_info [GOTO_INFO_OFFSET + yyn];" << endl
   502       << "    return goto_info [yyn];" << endl
       
   503       << "  }" << endl
   518       << "  }" << endl
   504       << endl
   519       << endl
   505       << "  static inline int t_action (int state, int token)" << endl
   520       << "  static inline int t_action (int state, int token)" << endl
   506       << "  {" << endl
   521       << "  {" << endl
   507       << "    const int yyn = action_index [state] + token;" << endl
   522       << "    const int yyn = action_index [state] + token;" << endl
   565   if (debug_info)
   580   if (debug_info)
   566     out << endl << "#endif // " << debugInfoProt() << endl;
   581     out << endl << "#endif // " << debugInfoProt() << endl;
   567 
   582 
   568   out << "};" << endl << endl;
   583   out << "};" << endl << endl;
   569 
   584 
   570   out << "const int " << grammar.table_name << "::lhs [] = {";
   585   out << "const short " << grammar.table_name << "::lhs [] = {";
   571   idx = 0;
   586   idx = 0;
   572   for (RulePointer rule = grammar.rules.begin (); rule != grammar.rules.end (); ++rule, ++idx)
   587   for (RulePointer rule = grammar.rules.begin (); rule != grammar.rules.end (); ++rule, ++idx)
   573     {
   588     {
   574       if (idx)
   589       if (idx)
   575         out << ", ";
   590         out << ", ";
   579 
   594 
   580       out << aut.id (rule->lhs);
   595       out << aut.id (rule->lhs);
   581     }
   596     }
   582   out << "};" << endl << endl;
   597   out << "};" << endl << endl;
   583 
   598 
   584   out << "const int " << grammar.table_name << ":: rhs[] = {";
   599   out << "const short " << grammar.table_name << "::rhs [] = {";
   585   idx = 0;
   600   idx = 0;
   586   for (RulePointer rule = grammar.rules.begin (); rule != grammar.rules.end (); ++rule, ++idx)
   601   for (RulePointer rule = grammar.rules.begin (); rule != grammar.rules.end (); ++rule, ++idx)
   587     {
   602     {
   588       if (idx)
   603       if (idx)
   589         out << ", ";
   604         out << ", ";
   634         }
   649         }
   635       out << "};" << endl
   650       out << "};" << endl
   636           << "#endif // " << prot << endl << endl;
   651           << "#endif // " << prot << endl << endl;
   637     }
   652     }
   638 
   653 
   639   out << "const int " << grammar.table_name << "::action_default [] = {";
   654   out << "const short " << grammar.table_name << "::action_default [] = {";
   640   idx = 0;
   655   idx = 0;
   641   for (StatePointer state = aut.states.begin (); state != aut.states.end (); ++state, ++idx)
   656   for (StatePointer state = aut.states.begin (); state != aut.states.end (); ++state, ++idx)
   642     {
   657     {
   643       if (state != aut.states.begin ())
   658       if (state != aut.states.begin ())
   644         out << ", ";
   659         out << ", ";
   651       else
   666       else
   652         out << "0";
   667         out << "0";
   653     }
   668     }
   654   out << "};" << endl << endl;
   669   out << "};" << endl << endl;
   655 
   670 
   656   out << "const int " << grammar.table_name << "::goto_default [] = {";
   671   out << "const short " << grammar.table_name << "::goto_default [] = {";
   657   for (int i = 0; i < defgoto.size (); ++i)
   672   for (int i = 0; i < defgoto.size (); ++i)
   658     {
   673     {
   659       if (i)
   674       if (i)
   660         out << ", ";
   675         out << ", ";
   661 
   676 
   664 
   679 
   665       out << defgoto [i];
   680       out << defgoto [i];
   666     }
   681     }
   667   out << "};" << endl << endl;
   682   out << "};" << endl << endl;
   668 
   683 
   669   out << "const int " << grammar.table_name << "::action_index [] = {";
   684   out << "const short " << grammar.table_name << "::action_index [] = {";
   670   for (int i = 0; i < compressed_action.index.size (); ++i)
   685   for (int i = 0; i < compressed_action.index.size (); ++i)
   671     {
   686     {
   672       if (! (i % 10))
   687       if (! (i % 10))
   673         out << endl << "  ";
   688         out << endl << "  ";
   674 
   689 
   685 
   700 
   686       out << compressed_goto.index [i];
   701       out << compressed_goto.index [i];
   687     }
   702     }
   688   out << "};" << endl << endl;
   703   out << "};" << endl << endl;
   689 
   704 
   690   out << "const int " << grammar.table_name << "::action_info [] = {";
   705   out << "const short " << grammar.table_name << "::action_info [] = {";
   691   for (int i = 0; i < compressed_action.info.size (); ++i)
   706   for (int i = 0; i < compressed_action.info.size (); ++i)
   692     {
   707     {
   693       if (! (i % 10))
   708       if (! (i % 10))
   694         out << endl << "  ";
   709         out << endl << "  ";
   695 
   710 
   706 
   721 
   707       out << compressed_goto.info [i];
   722       out << compressed_goto.info [i];
   708     }
   723     }
   709   out << "};" << endl << endl;
   724   out << "};" << endl << endl;
   710 
   725 
   711   out << "const int " << grammar.table_name << "::action_check [] = {";
   726   out << "const short " << grammar.table_name << "::action_check [] = {";
   712   for (int i = 0; i < compressed_action.check.size (); ++i)
   727   for (int i = 0; i < compressed_action.check.size (); ++i)
   713     {
   728     {
   714       if (! (i % 10))
   729       if (! (i % 10))
   715         out << endl << "  ";
   730         out << endl << "  ";
   716 
   731