summaryrefslogtreecommitdiffstats
path: root/src/tools/qlalr/cppgenerator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/qlalr/cppgenerator.cpp')
-rw-r--r--src/tools/qlalr/cppgenerator.cpp209
1 files changed, 105 insertions, 104 deletions
diff --git a/src/tools/qlalr/cppgenerator.cpp b/src/tools/qlalr/cppgenerator.cpp
index 508db696b1..95f70dc988 100644
--- a/src/tools/qlalr/cppgenerator.cpp
+++ b/src/tools/qlalr/cppgenerator.cpp
@@ -36,6 +36,8 @@
#include <QtCore/qfile.h>
#include <QtCore/qmap.h>
+#include <iterator>
+
namespace {
void generateSeparator(int i, QTextStream &out)
@@ -43,7 +45,7 @@ void generateSeparator(int i, QTextStream &out)
if (!(i % 10)) {
if (i)
out << ",";
- out << endl << " ";
+ out << Qt::endl << " ";
} else {
out << ", ";
}
@@ -126,9 +128,9 @@ QString CppGenerator::endIncludeGuard(const QString &fileName)
void CppGenerator::operator () ()
{
// action table...
- state_count = aut.states.size ();
- terminal_count = grammar.terminals.size ();
- non_terminal_count = grammar.non_terminals.size ();
+ state_count = static_cast<int>(aut.states.size());
+ terminal_count = static_cast<int>(grammar.terminals.size());
+ non_terminal_count = static_cast<int>(grammar.non_terminals.size());
#define ACTION(i, j) table [(i) * terminal_count + (j)]
#define GOTO(i, j) pgoto [(i) * non_terminal_count + (j)]
@@ -156,7 +158,7 @@ void CppGenerator::operator () ()
if (grammar.isNonTerminal (a.key ()))
{
- Q_ASSERT (symbol >= terminal_count && symbol < grammar.names.size ());
+ Q_ASSERT(symbol >= terminal_count && symbol < static_cast<int>(grammar.names.size()));
GOTO (q, symbol - terminal_count) = r;
}
@@ -187,14 +189,14 @@ void CppGenerator::operator () ()
{
if (verbose)
qout() << "*** Warning. Found a reduce/reduce conflict in state " << q << " on token ``" << s << "'' between rule "
- << r << " and " << -u << endl;
+ << r << " and " << -u << Qt::endl;
++reduce_reduce_conflict_count;
u = qMax (u, -r);
if (verbose)
- qout() << "\tresolved using rule " << -u << endl;
+ qout() << "\tresolved using rule " << -u << Qt::endl;
}
else if (u > 0)
@@ -227,7 +229,7 @@ void CppGenerator::operator () ()
++shift_reduce_conflict_count;
if (verbose)
- qout() << "*** Warning. Found a shift/reduce conflict in state " << q << " on token ``" << s << "'' with rule " << r << endl;
+ qout() << "*** Warning. Found a shift/reduce conflict in state " << q << " on token ``" << s << "'' with rule " << r << Qt::endl;
}
}
}
@@ -238,14 +240,14 @@ void CppGenerator::operator () ()
{
if (shift_reduce_conflict_count != grammar.expected_shift_reduce
|| reduce_reduce_conflict_count != grammar.expected_reduce_reduce)
- qerr() << "*** Conflicts: " << shift_reduce_conflict_count << " shift/reduce, " << reduce_reduce_conflict_count << " reduce/reduce" << endl;
+ qerr() << "*** Conflicts: " << shift_reduce_conflict_count << " shift/reduce, " << reduce_reduce_conflict_count << " reduce/reduce" << Qt::endl;
if (verbose)
- qout() << endl << "*** Conflicts: " << shift_reduce_conflict_count << " shift/reduce, " << reduce_reduce_conflict_count << " reduce/reduce" << endl
- << endl;
+ qout() << Qt::endl << "*** Conflicts: " << shift_reduce_conflict_count << " shift/reduce, " << reduce_reduce_conflict_count << " reduce/reduce" << Qt::endl
+ << Qt::endl;
}
- QBitArray used_rules (grammar.rules.count ());
+ QBitArray used_rules{static_cast<int>(grammar.rules.size())};
int q = 0;
for (StatePointer state = aut.states.begin (); state != aut.states.end (); ++state, ++q)
@@ -259,14 +261,13 @@ void CppGenerator::operator () ()
}
}
- for (int i = 0; i < used_rules.count (); ++i)
+ auto rule = grammar.rules.begin();
+ for (int i = 0; i < used_rules.count (); ++i, ++rule)
{
if (! used_rules.testBit (i))
{
- RulePointer rule = grammar.rules.begin () + i;
-
if (rule != grammar.goal)
- qerr() << "*** Warning: Rule ``" << *rule << "'' is useless!" << endl;
+ qerr() << "*** Warning: Rule ``" << *rule << "'' is useless!" << Qt::endl;
}
}
@@ -280,7 +281,7 @@ void CppGenerator::operator () ()
if (u >= 0)
continue;
- RulePointer rule = grammar.rules.begin () + (- u - 1);
+ RulePointer rule = std::next(grammar.rules.begin(), - u - 1);
if (state->defaultReduce == rule)
u = 0;
@@ -348,26 +349,26 @@ void CppGenerator::operator () ()
{
out << copyrightHeader()
<< privateCopyrightHeader()
- << endl;
+ << Qt::endl;
}
out << "// This file was generated by qlalr - DO NOT EDIT!\n";
- out << startIncludeGuard(grammar.merged_output) << endl;
+ out << startIncludeGuard(grammar.merged_output) << Qt::endl;
if (copyright) {
- out << "#if defined(ERROR)" << endl
- << "# undef ERROR" << endl
- << "#endif" << endl << endl;
+ out << "#if defined(ERROR)" << Qt::endl
+ << "# undef ERROR" << Qt::endl
+ << "#endif" << Qt::endl << Qt::endl;
}
generateDecl (out);
generateImpl (out);
out << p.decls();
out << p.impls();
- out << endl;
+ out << Qt::endl;
- out << endIncludeGuard(grammar.merged_output) << endl;
+ out << endIncludeGuard(grammar.merged_output) << Qt::endl;
return;
}
@@ -388,24 +389,24 @@ void CppGenerator::operator () ()
{
out << copyrightHeader()
<< privateCopyrightHeader()
- << endl;
+ << Qt::endl;
}
out << "// This file was generated by qlalr - DO NOT EDIT!\n";
- out << "#ifndef " << prot << endl
- << "#define " << prot << endl
- << endl;
+ out << "#ifndef " << prot << Qt::endl
+ << "#define " << prot << Qt::endl
+ << Qt::endl;
if (copyright) {
- out << "#include <QtCore/qglobal.h>" << endl << endl;
- out << "QT_BEGIN_NAMESPACE" << endl << endl;
+ out << "#include <QtCore/qglobal.h>" << Qt::endl << Qt::endl;
+ out << "QT_BEGIN_NAMESPACE" << Qt::endl << Qt::endl;
}
generateDecl (out);
if (copyright)
- out << "QT_END_NAMESPACE" << endl;
+ out << "QT_END_NAMESPACE" << Qt::endl;
- out << "#endif // " << prot << endl << endl;
+ out << "#endif // " << prot << Qt::endl << Qt::endl;
} // end decls
{ // bits...
@@ -419,12 +420,12 @@ void CppGenerator::operator () ()
out << "// This file was generated by qlalr - DO NOT EDIT!\n";
- out << "#include \"" << declFileName << "\"" << endl << endl;
+ out << "#include \"" << declFileName << "\"" << Qt::endl << Qt::endl;
if (copyright)
- out << "QT_BEGIN_NAMESPACE" << endl << endl;
+ out << "QT_BEGIN_NAMESPACE" << Qt::endl << Qt::endl;
generateImpl(out);
if (copyright)
- out << "QT_END_NAMESPACE" << endl;
+ out << "QT_END_NAMESPACE" << Qt::endl;
} // end bits
@@ -455,10 +456,10 @@ QString CppGenerator::debugInfoProt() const
void CppGenerator::generateDecl (QTextStream &out)
{
- out << "class " << grammar.table_name << endl
- << "{" << endl
- << "public:" << endl
- << " enum VariousConstants {" << endl;
+ out << "class " << grammar.table_name << Qt::endl
+ << "{" << Qt::endl
+ << "public:" << Qt::endl
+ << " enum VariousConstants {" << Qt::endl;
for (const Name &t : qAsConst(grammar.terminals))
{
@@ -474,62 +475,62 @@ void CppGenerator::generateDecl (QTextStream &out)
else
name.prepend (grammar.token_prefix);
- out << " " << name << " = " << value << "," << endl;
+ out << " " << name << " = " << value << "," << Qt::endl;
}
- out << endl
- << " ACCEPT_STATE = " << accept_state << "," << endl
- << " RULE_COUNT = " << grammar.rules.size () << "," << endl
- << " STATE_COUNT = " << state_count << "," << endl
- << " TERMINAL_COUNT = " << terminal_count << "," << endl
- << " NON_TERMINAL_COUNT = " << non_terminal_count << "," << endl
- << endl
- << " GOTO_INDEX_OFFSET = " << compressed_action.index.size () << "," << endl
- << " GOTO_INFO_OFFSET = " << compressed_action.info.size () << "," << endl
- << " GOTO_CHECK_OFFSET = " << compressed_action.check.size () << endl
- << " };" << endl
- << endl
- << " static const char *const spell[];" << endl
- << " static const short lhs[];" << endl
- << " static const short rhs[];" << endl;
+ out << Qt::endl
+ << " ACCEPT_STATE = " << accept_state << "," << Qt::endl
+ << " RULE_COUNT = " << grammar.rules.size () << "," << Qt::endl
+ << " STATE_COUNT = " << state_count << "," << Qt::endl
+ << " TERMINAL_COUNT = " << terminal_count << "," << Qt::endl
+ << " NON_TERMINAL_COUNT = " << non_terminal_count << "," << Qt::endl
+ << Qt::endl
+ << " GOTO_INDEX_OFFSET = " << compressed_action.index.size () << "," << Qt::endl
+ << " GOTO_INFO_OFFSET = " << compressed_action.info.size () << "," << Qt::endl
+ << " GOTO_CHECK_OFFSET = " << compressed_action.check.size () << Qt::endl
+ << " };" << Qt::endl
+ << Qt::endl
+ << " static const char *const spell[];" << Qt::endl
+ << " static const short lhs[];" << Qt::endl
+ << " static const short rhs[];" << Qt::endl;
if (debug_info)
{
QString prot = debugInfoProt();
- out << endl << "#ifndef " << prot << endl
- << " static const int rule_index[];" << endl
- << " static const int rule_info[];" << endl
- << "#endif // " << prot << endl << endl;
+ out << Qt::endl << "#ifndef " << prot << Qt::endl
+ << " static const int rule_index[];" << Qt::endl
+ << " static const int rule_info[];" << Qt::endl
+ << "#endif // " << prot << Qt::endl << Qt::endl;
}
- out << " static const short goto_default[];" << endl
- << " static const short action_default[];" << endl
- << " static const short action_index[];" << endl
- << " static const short action_info[];" << endl
- << " static const short action_check[];" << endl
- << endl
- << " static inline int nt_action (int state, int nt)" << endl
- << " {" << endl
- << " const int yyn = action_index [GOTO_INDEX_OFFSET + state] + nt;" << endl
- << " if (yyn < 0 || action_check [GOTO_CHECK_OFFSET + yyn] != nt)" << endl
- << " return goto_default [nt];" << endl
- << endl
- << " return action_info [GOTO_INFO_OFFSET + yyn];" << endl
- << " }" << endl
- << endl
- << " static inline int t_action (int state, int token)" << endl
- << " {" << endl
- << " const int yyn = action_index [state] + token;" << endl
- << endl
- << " if (yyn < 0 || action_check [yyn] != token)" << endl
- << " return - action_default [state];" << endl
- << endl
- << " return action_info [yyn];" << endl
- << " }" << endl
- << "};" << endl
- << endl
- << endl;
+ out << " static const short goto_default[];" << Qt::endl
+ << " static const short action_default[];" << Qt::endl
+ << " static const short action_index[];" << Qt::endl
+ << " static const short action_info[];" << Qt::endl
+ << " static const short action_check[];" << Qt::endl
+ << Qt::endl
+ << " static inline int nt_action (int state, int nt)" << Qt::endl
+ << " {" << Qt::endl
+ << " const int yyn = action_index [GOTO_INDEX_OFFSET + state] + nt;" << Qt::endl
+ << " if (yyn < 0 || action_check [GOTO_CHECK_OFFSET + yyn] != nt)" << Qt::endl
+ << " return goto_default [nt];" << Qt::endl
+ << Qt::endl
+ << " return action_info [GOTO_INFO_OFFSET + yyn];" << Qt::endl
+ << " }" << Qt::endl
+ << Qt::endl
+ << " static inline int t_action (int state, int token)" << Qt::endl
+ << " {" << Qt::endl
+ << " const int yyn = action_index [state] + token;" << Qt::endl
+ << Qt::endl
+ << " if (yyn < 0 || action_check [yyn] != token)" << Qt::endl
+ << " return - action_default [state];" << Qt::endl
+ << Qt::endl
+ << " return action_info [yyn];" << Qt::endl
+ << " }" << Qt::endl
+ << "};" << Qt::endl
+ << Qt::endl
+ << Qt::endl;
}
void CppGenerator::generateImpl (QTextStream &out)
@@ -568,16 +569,16 @@ void CppGenerator::generateImpl (QTextStream &out)
{
first_nt = false;
QString prot = debugInfoProt();
- out << endl << "#ifndef " << prot << endl;
+ out << Qt::endl << "#ifndef " << prot << Qt::endl;
}
out << "\"" << *t << "\"";
}
}
if (debug_info)
- out << endl << "#endif // " << debugInfoProt() << endl;
+ out << Qt::endl << "#endif // " << debugInfoProt() << Qt::endl;
- out << endl << "};" << endl << endl;
+ out << Qt::endl << "};" << Qt::endl << Qt::endl;
out << "const short " << grammar.table_name << "::lhs [] = {";
idx = 0;
@@ -587,7 +588,7 @@ void CppGenerator::generateImpl (QTextStream &out)
out << aut.id (rule->lhs);
}
- out << endl << "};" << endl << endl;
+ out << Qt::endl << "};" << Qt::endl << Qt::endl;
out << "const short " << grammar.table_name << "::rhs [] = {";
idx = 0;
@@ -597,13 +598,13 @@ void CppGenerator::generateImpl (QTextStream &out)
out << rule->rhs.size ();
}
- out << endl << "};" << endl << endl;
+ out << Qt::endl << "};" << Qt::endl << Qt::endl;
if (debug_info)
{
QString prot = debugInfoProt();
- out << endl << "#ifndef " << prot << endl;
+ out << Qt::endl << "#ifndef " << prot << Qt::endl;
out << "const int " << grammar.table_name << "::rule_info [] = {";
idx = 0;
for (auto rule = grammar.rules.cbegin (); rule != grammar.rules.cend (); ++rule, ++idx)
@@ -615,11 +616,11 @@ void CppGenerator::generateImpl (QTextStream &out)
for (const Name &n : rule->rhs)
out << ", " << name_ids.value (n);
}
- out << endl << "};" << endl << endl;
+ out << Qt::endl << "};" << Qt::endl << Qt::endl;
out << "const int " << grammar.table_name << "::rule_index [] = {";
idx = 0;
- int offset = 0;
+ size_t offset = 0;
for (RulePointer rule = grammar.rules.begin (); rule != grammar.rules.end (); ++rule, ++idx)
{
generateSeparator(idx, out);
@@ -627,8 +628,8 @@ void CppGenerator::generateImpl (QTextStream &out)
out << offset;
offset += rule->rhs.size () + 1;
}
- out << endl << "};" << endl
- << "#endif // " << prot << endl << endl;
+ out << Qt::endl << "};" << Qt::endl
+ << "#endif // " << prot << Qt::endl << Qt::endl;
}
out << "const short " << grammar.table_name << "::action_default [] = {";
@@ -642,27 +643,27 @@ void CppGenerator::generateImpl (QTextStream &out)
else
out << "0";
}
- out << endl << "};" << endl << endl;
+ out << Qt::endl << "};" << Qt::endl << Qt::endl;
out << "const short " << grammar.table_name << "::goto_default [] = {";
generateList(defgoto, out);
- out << endl << "};" << endl << endl;
+ out << Qt::endl << "};" << Qt::endl << Qt::endl;
out << "const short " << grammar.table_name << "::action_index [] = {";
generateList(compressed_action.index, out);
- out << "," << endl;
+ out << "," << Qt::endl;
generateList(compressed_goto.index, out);
- out << endl << "};" << endl << endl;
+ out << Qt::endl << "};" << Qt::endl << Qt::endl;
out << "const short " << grammar.table_name << "::action_info [] = {";
generateList(compressed_action.info, out);
- out << "," << endl;
+ out << "," << Qt::endl;
generateList(compressed_goto.info, out);
- out << endl << "};" << endl << endl;
+ out << Qt::endl << "};" << Qt::endl << Qt::endl;
out << "const short " << grammar.table_name << "::action_check [] = {";
generateList(compressed_action.check, out);
- out << "," << endl;
+ out << "," << Qt::endl;
generateList(compressed_goto.check, out);
- out << endl << "};" << endl << endl;
+ out << Qt::endl << "};" << Qt::endl << Qt::endl;
}