File indexing completed on 2024-04-21 04:36:14

0001 /* This file is part of kdev-pg-qt
0002    Copyright (C) 2005 Roberto Raggi <roberto@kdevelop.org>
0003    Copyright (C) 2006 Jakob Petsovits <jpetso@gmx.at>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #ifndef KDEV_PG_CODEGEN_H
0022 #define KDEV_PG_CODEGEN_H
0023 
0024 #include "kdev-pg.h"
0025 #include "kdev-pg-default-visitor.h"
0026 
0027 namespace KDevPG
0028 {
0029 
0030 /**
0031  * This class is the LL/shunting yard-parser code generator. It generates the actual parsing code.
0032  * @todo Investigate whether parts of it are not code-generation, but in fact realy computations which should be handled independently from the output-language.
0033  */
0034 class CodeGenerator: protected DefaultVisitor
0035 {
0036 public:
0037   QTextStream& out;
0038   Model::EvolveItem *mEvolve;
0039   QSet<QString> *mNames;
0040   Model::SymbolItem *mSym;
0041 
0042 public:
0043   CodeGenerator(QTextStream& o, QSet<QString> *names, Model::SymbolItem *sym)
0044     : out(o), mNames(names), mSym(sym), mCurrentCatchId(0)
0045   {}
0046 
0047   void operator()(Model::Node *node);
0048 
0049 protected:
0050   void visitZero(Model::ZeroItem *node) override;
0051   void visitSymbol(Model::SymbolItem *node) override;
0052   void visitNonTerminal(Model::NonTerminalItem *node) override;
0053   void visitTerminal(Model::TerminalItem *node) override;
0054   void visitPlus(Model::PlusItem *node) override;
0055   void visitStar(Model::StarItem *node) override;
0056   void visitAction(Model::ActionItem *node) override;
0057   void visitAlternative(Model::AlternativeItem *node) override;
0058   void visitCons(Model::ConsItem *node) override;
0059   void visitEvolve(Model::EvolveItem *node) override;
0060   void visitTryCatch(Model::TryCatchItem *node) override;
0061   void visitAlias(Model::AliasItem *node) override;
0062   void visitAnnotation(Model::AnnotationItem *node) override;
0063   void visitCondition(Model::ConditionItem *node) override;
0064   void visitOperator(Model::OperatorItem *node) override;
0065 
0066 private:
0067   int mCurrentCatchId;
0068   int setCatchId(int catch_id);
0069 };
0070 
0071 class GenerateForwardParserRule
0072 {
0073 public:
0074   QTextStream& out;
0075 
0076 public:
0077   GenerateForwardParserRule(QTextStream& o): out(o)
0078   {}
0079 
0080   void operator()(QPair<QString, Model::SymbolItem*> const &__it);
0081 };
0082 
0083 class GenerateParserRule
0084 {
0085 public:
0086   QTextStream& out;
0087   QSet<QString> mNames;
0088 
0089 public:
0090   GenerateParserRule(QTextStream& o): out(o)
0091   {}
0092 
0093   void operator()(QPair<QString, Model::SymbolItem*> const &__it);
0094 };
0095 
0096 class GenerateLocalDeclarations: protected DefaultVisitor
0097 {
0098 public:
0099   QTextStream& out;
0100   QSet<QString> *mNames;
0101 
0102 public:
0103   GenerateLocalDeclarations(QTextStream& o, QSet<QString> *names)
0104     : out(o), mNames(names)
0105   {}
0106 
0107   void operator()(Model::Node *node);
0108   void visitVariableDeclaration(Model::VariableDeclarationItem *node) override;
0109 };
0110 
0111 class GenerateParseMethodSignature: protected DefaultVisitor
0112 {
0113 public:
0114   QTextStream& out;
0115   bool firstParameter;
0116   QSet<QString> *mNames;
0117 
0118 public:
0119   GenerateParseMethodSignature(QTextStream& o, QSet<QString> *names)
0120     : out(o), firstParameter(true), mNames(names)
0121   {}
0122 
0123   void operator()(Model::SymbolItem *node);
0124   void visitVariableDeclaration(Model::VariableDeclarationItem *node) override;
0125 };
0126 
0127 class GenerateRecursiveDelegation
0128 {
0129 public:
0130   QTextStream& out;
0131   
0132   GenerateRecursiveDelegation(QTextStream& o)
0133     : out(o)
0134   {}
0135   
0136   void operator()(Model::SymbolItem *node);
0137 };
0138 
0139 namespace GenerateVariableDeclarationImpl
0140 {
0141   void printType(QTextStream &out, Model::VariableDeclarationItem *node);
0142   void printName(QTextStream &out, Model::VariableDeclarationItem *node);
0143 };
0144 
0145 template<bool printType, bool printName>
0146 class GenerateVariableDeclaration
0147 {
0148 public:
0149   QTextStream& out;
0150 
0151 public:
0152   GenerateVariableDeclaration(QTextStream& o): out(o)
0153   {}
0154 
0155   void operator()(Model::VariableDeclarationItem *node)
0156   {
0157     if (printType)
0158       GenerateVariableDeclarationImpl::printType(out, node);
0159     if (printName)
0160       GenerateVariableDeclarationImpl::printName(out, node);
0161   }
0162 };
0163 
0164 class GenerateTokenVariableInitialization : public DefaultVisitor
0165 {
0166 public:
0167   QTextStream& out;
0168   GenerateTokenVariableInitialization( QTextStream& o) : out(o)
0169   {
0170   }
0171 
0172   void operator()(Model::SymbolItem* node);
0173   void visitVariableDeclaration(Model::VariableDeclarationItem *node) override;
0174 };
0175 
0176 class GenerateMemberCode
0177 {
0178 public:
0179   QTextStream& out;
0180   int mKindMask;
0181 
0182 public:
0183   GenerateMemberCode(QTextStream& o, int kind_mask)
0184   : out(o), mKindMask(kind_mask)
0185   {}
0186 
0187   void operator()(Settings::MemberItem* m);
0188 };
0189 
0190 class GenerateParserDeclarations
0191 {
0192 public:
0193   QTextStream& out;
0194 
0195 public:
0196   GenerateParserDeclarations(QTextStream& o): out(o)
0197   {}
0198 
0199   void operator()();
0200 };
0201 
0202 class GenerateParserBits
0203 {
0204 public:
0205   QTextStream& out;
0206 
0207 public:
0208   GenerateParserBits(QTextStream& o): out(o)
0209   {}
0210 
0211   void operator()();
0212 };
0213 
0214 class GenerateTokenTexts
0215 {
0216 public:
0217   QTextStream& out;
0218 
0219 public:
0220   GenerateTokenTexts(QTextStream& o): out(o)
0221   {}
0222 
0223   void operator()();
0224 };
0225 
0226 }
0227 
0228 #endif // KDEV_PG_CODEGEN_H