1 /* "CodeWorker": a scripting language for parsing and generating text. 2 3 Copyright (C) 1996-1997, 1999-2002 C�dric Lemaire 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 19 To contact the author: codeworker@free.fr 20 */ 21 22 #ifndef _CppCompilerEnvironment_h_ 23 #define _CppCompilerEnvironment_h_ 24 25 #include <string> 26 #include <list> 27 #include <set> 28 29 namespace CodeWorker { 30 #define CW_BODY_STREAM theCompilerEnvironment.getBody() 31 #define CW_BODY_INDENT theCompilerEnvironment.getBody() << theCompilerEnvironment.getIndentation() 32 #define CW_BODY_ENDL theCompilerEnvironment.getBody().endl() 33 34 class ScpStream; 35 class DtaPatternScript; 36 class GrfFunction; 37 class BNFClause; 38 class CppVariableScope; 39 class UtlException; 40 class GrfForeach; 41 42 43 struct CppScriptSession { 44 std::string _sIndentation; 45 std::set<std::string> _setOfProjectFunctions; 46 std::set<std::string> _setOfProjectClauses; 47 std::string _sFilename; 48 std::string _sCppRadical; 49 mutable DtaPatternScript* _header; 50 mutable DtaPatternScript* _body; 51 mutable std::list<CppVariableScope*> _stackOfScopes; 52 std::list<GrfForeach*> _listOfForeachStatements; 53 54 CppScriptSession(const std::string& sFilename, const std::string& sCppRadical); 55 CppScriptSession(const CppScriptSession& copy); 56 ~CppScriptSession(); 57 58 CppScriptSession& operator =(const CppScriptSession& copy); 59 60 void incrementIndentation(); 61 void decrementIndentation(); 62 void pushVariableScope() const; 63 void popVariableScope() const; 64 void pushForeach(GrfForeach* pForeach); 65 GrfForeach* getLastForeach() const; 66 void popForeach(); 67 void catchFilename(); 68 }; 69 70 class CppCompilerEnvironment { 71 private: 72 std::string _sCppProjectDirectory; 73 std::string _sCodeWorkerDirectory; 74 int _iInlineScriptCounter; 75 int _iSwitchNumber; 76 int _iCursorNumber; 77 bool _bBracketsToNextBlock; 78 bool _bCarriageReturnAfterBlock; 79 const GrfFunction* _pCurrentFunction; 80 const BNFClause* _pCurrentClause; 81 int _iPointerToDeclarations; 82 int _iBNFStepperCursor; 83 std::list<int> _listOfLastRepeatCursors; 84 std::list<bool> _listOfLastRepeatCursorUsed; 85 std::list<std::string> _listOfProjectModules; 86 std::list<CppScriptSession> _listOfScriptSessions; 87 std::set<std::string> _globalVariables; 88 bool _bErrorEncountered; 89 90 public: 91 CppCompilerEnvironment(const std::string& sCppProjectDirectory); 92 ~CppCompilerEnvironment(); 93 94 bool pushFilename(const std::string& sFilename); 95 96 std::string getRadical() const; 97 std::string getMainRadical() const; 98 static std::string filename2Module(const std::string& sFilename); 99 static std::string getRadical(const std::string& sFile); 100 static std::string getRelativePath(const std::string& sFile); 101 static std::string convertTemplateKey(const std::string& sKey); 102 static std::string convertToCppVariable(const std::string& sVariable); 103 104 std::string getIncludeParentScript() const; getProjectModules()105 inline const std::list<std::string>& getProjectModules() const { return _listOfProjectModules; } 106 ScpStream& getHeader() const; 107 ScpStream& getMainHeader() const; 108 ScpStream& getBody() const; getIndentation()109 inline const std::string& getIndentation() const { return getCurrentScriptSession()._sIndentation; } setIndentation(const std::string & sIndentation)110 inline void setIndentation(const std::string& sIndentation) { _listOfScriptSessions.front()._sIndentation = sIndentation; } 111 void incrementIndentation(); 112 void pushVariableScope(); getCurrentScriptSession()113 inline const CppScriptSession& getCurrentScriptSession() const { return _listOfScriptSessions.front(); } getCurrentFunction()114 inline const GrfFunction* getCurrentFunction() const { return _pCurrentFunction; } setCurrentFunction(const GrfFunction * pFunction)115 inline void setCurrentFunction(const GrfFunction* pFunction) { _pCurrentFunction = pFunction; } getCurrentClause()116 inline const BNFClause* getCurrentClause() const { return _pCurrentClause; } setCurrentClause(const BNFClause * pClause)117 inline void setCurrentClause(const BNFClause* pClause) { _pCurrentClause = pClause; } getPointerToDeclarations()118 inline int getPointerToDeclarations() const { return _iPointerToDeclarations; } setPointerToDeclarations(int iPointerToDeclarations)119 inline void setPointerToDeclarations(int iPointerToDeclarations) { _iPointerToDeclarations = iPointerToDeclarations; } getBNFStepperCursor()120 inline int getBNFStepperCursor() const { return _iBNFStepperCursor; } setBNFStepperCursor(int iCursor)121 inline void setBNFStepperCursor(int iCursor) { _iBNFStepperCursor = iCursor; } 122 void pushLastRepeatCursor(int iCursor); 123 int getLastRepeatCursor(bool bUsed = false); 124 bool popLastRepeatCursor(); getGlobalVariables()125 inline const std::set<std::string>& getGlobalVariables() const { return _globalVariables; } 126 127 std::string newInlineScriptFilename(); 128 void newFunction(const GrfFunction* pFunction); 129 void newClause(const BNFClause* pClause); 130 std::string getFunctionModule(const std::string& sFunction) const; 131 std::string getClauseModule(const std::string& sClause) const; 132 bool addVariable(const std::string& sVariable); 133 bool existVariable(const std::string& sVariable) const; 134 void addGlobalVariable(const std::string& sVariable); 135 void setClauseReturnValue(const std::string& sClauseName); 136 bool isClauseReturnValue(const std::string& sVariableName) const; 137 void hasEvaluatedExpressionInScope(bool bEvaluated); 138 bool hasEvaluatedExpressionInScope() const; 139 int newSwitch(); 140 int newCursor(); 141 void popVariableScope(); 142 void decrementIndentation(); bracketsToNextBlock()143 inline bool bracketsToNextBlock() const { return _bBracketsToNextBlock; } bracketsToNextBlock(bool bBrackets)144 inline void bracketsToNextBlock(bool bBrackets) { _bBracketsToNextBlock = bBrackets; } carriageReturnAfterBlock()145 inline bool carriageReturnAfterBlock() const { return _bCarriageReturnAfterBlock; } carriageReturnAfterBlock(bool bCarriage)146 inline void carriageReturnAfterBlock(bool bCarriage) { _bCarriageReturnAfterBlock = bCarriage; } 147 void pushForeach(GrfForeach* pForeach); 148 GrfForeach* getLastForeach() const; 149 void popForeach(); 150 void catchFilename(UtlException& exception); 151 void popFilename(); 152 153 void optimizeSources(); 154 void translateToTargetLanguage(const std::string& sTargetLanguage); 155 156 private: 157 CppCompilerEnvironment(); 158 CppCompilerEnvironment(const CppCompilerEnvironment&); 159 CppCompilerEnvironment& operator =(const CppCompilerEnvironment&); 160 bool operator ==(const CppCompilerEnvironment&); 161 bool operator !=(const CppCompilerEnvironment&); 162 163 void generateDynamicPackages(); 164 void generateDSP(); 165 void generateMakefile(); 166 }; 167 168 169 class CppNewVariableScopeEnvironment { 170 private: 171 const CppScriptSession& _session; 172 std::list<CppVariableScope*> _stackOfScopes; 173 174 public: 175 CppNewVariableScopeEnvironment(CppCompilerEnvironment& env); 176 ~CppNewVariableScopeEnvironment(); 177 }; 178 179 } 180 181 #endif 182