/util/lexgen/generator.h

https://bitbucket.org/ultra_iter/qt-vtl · C Header · 221 lines · 145 code · 36 blank · 40 comment · 5 complexity · 9bc9922ea99b193716103a9419db5ed2 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the utils of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #ifndef GENERATOR_H
  42. #define GENERATOR_H
  43. #include <QTextStream>
  44. #include <QStringList>
  45. #include "nfa.h"
  46. class LineStream
  47. {
  48. private:
  49. struct SharedStream
  50. {
  51. int ref;
  52. QTextStream *stream;
  53. };
  54. public:
  55. LineStream(QTextStream *textStream)
  56. {
  57. shared = new SharedStream;
  58. shared->ref = 1;
  59. shared->stream = textStream;
  60. }
  61. LineStream(const LineStream &other)
  62. {
  63. shared = other.shared;
  64. shared->ref++;
  65. }
  66. LineStream &operator=(const LineStream &other)
  67. {
  68. if (this == &other)
  69. return *this;
  70. LineStream copy(other); // keep refcount up
  71. qSwap(*shared, *other.shared);
  72. return *this;
  73. }
  74. ~LineStream()
  75. {
  76. if (!--shared->ref) {
  77. (*shared->stream) << endl;
  78. delete shared;
  79. }
  80. }
  81. template <typename T>
  82. LineStream &operator<<(const T &value)
  83. { (*shared->stream) << value; return *this; }
  84. SharedStream *shared;
  85. };
  86. class CodeBlock
  87. {
  88. public:
  89. inline CodeBlock() { stream.setString(&output, QIODevice::WriteOnly); }
  90. inline void indent() { indentStr += QLatin1String(" "); }
  91. inline void outdent() { indentStr.remove(0, 4); }
  92. template <typename T>
  93. LineStream operator<<(const T &value)
  94. { stream << indentStr; stream << value; return LineStream(&stream); }
  95. inline void addNewLine() { stream << endl; }
  96. inline QString toString() const { stream.flush(); return output; }
  97. private:
  98. QString output;
  99. mutable QTextStream stream;
  100. QString indentStr;
  101. };
  102. class Function
  103. {
  104. public:
  105. inline Function(const QString &returnType, const QString &name)
  106. : rtype(returnType), fname(name), iline(false), cnst(false) {}
  107. inline Function() : iline(false), cnst(false) {}
  108. inline void setName(const QString &name) { fname = name; }
  109. inline QString name() const { return fname; }
  110. inline void setInline(bool i) { iline = i; }
  111. inline bool isInline() const { return iline; }
  112. inline void setReturnType(const QString &type) { rtype = type; }
  113. inline QString returnType() const { return rtype; }
  114. inline void addBody(const QString &_body) { body += _body; }
  115. inline void addBody(const CodeBlock &block) { body += block.toString(); }
  116. inline bool hasBody() const { return !body.isEmpty(); }
  117. inline void setConst(bool konst) { cnst = konst; }
  118. inline bool isConst() const { return cnst; }
  119. void printDeclaration(CodeBlock &block, const QString &funcNamePrefix = QString()) const;
  120. QString definition() const;
  121. private:
  122. QString signature(const QString &funcNamePrefix = QString()) const;
  123. QString rtype;
  124. QString fname;
  125. QString body;
  126. bool iline;
  127. bool cnst;
  128. };
  129. class Class
  130. {
  131. public:
  132. enum Access { PublicMember, ProtectedMember, PrivateMember };
  133. inline Class(const QString &name) : cname(name) {}
  134. inline void setName(const QString &name) { cname = name; }
  135. inline QString name() const { return cname; }
  136. inline void addMember(Access access, const QString &name)
  137. { sections[access].variables.append(name); }
  138. inline void addMember(Access access, const Function &func)
  139. { sections[access].functions.append(func); }
  140. void addConstructor(Access access, const QString &body, const QString &args = QString());
  141. inline void addConstructor(Access access, const CodeBlock &body, const QString &args = QString())
  142. { addConstructor(access, body.toString(), args); }
  143. QString declaration() const;
  144. QString definition() const;
  145. private:
  146. QString cname;
  147. struct Section
  148. {
  149. QVector<Function> functions;
  150. QStringList variables;
  151. QVector<Function> constructors;
  152. inline bool isEmpty() const
  153. { return functions.isEmpty() && variables.isEmpty() && constructors.isEmpty(); }
  154. void printDeclaration(const Class *klass, CodeBlock &block) const;
  155. QString definition(const Class *klass) const;
  156. };
  157. Section sections[3];
  158. };
  159. class Generator
  160. {
  161. public:
  162. Generator(const DFA &dfa, const Config &config);
  163. QString generate();
  164. private:
  165. void generateTransitions(CodeBlock &body, const TransitionMap &transitions);
  166. bool isSingleReferencedFinalState(int i) const;
  167. DFA dfa;
  168. Config cfg;
  169. InputType minInput;
  170. InputType maxInput;
  171. QHash<int, int> backReferenceMap;
  172. QString headerFileName;
  173. public:
  174. struct TransitionSequence
  175. {
  176. inline TransitionSequence() : first(-1), last(-1), transition(-1) {}
  177. InputType first;
  178. InputType last;
  179. int transition;
  180. QString testFunction;
  181. };
  182. private:
  183. QVector<TransitionSequence> charFunctionRanges;
  184. };
  185. #endif