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 _BNFClause_h_
23 #define _BNFClause_h_
24 
25 #include <vector>
26 #include <string>
27 
28 #include "GrfBlock.h"
29 #include "ExprScriptFunction.h"
30 
31 namespace CodeWorker {
32 	class DtaBNFScript;
33 	class BNFClause;
34 
35 	struct BNFClauseMatchingArea {
36 		BNFClause* clause;
37 		int beginPosition;
38 		int endPosition;
39 		std::list<BNFClauseMatchingArea*> childs;
40 
BNFClauseMatchingAreaBNFClauseMatchingArea41 		BNFClauseMatchingArea(BNFClause* p, int i) : clause(p), beginPosition(i), endPosition(-1) {}
42 		~BNFClauseMatchingArea();
43 
pushChildBNFClauseMatchingArea44 		inline void pushChild(BNFClauseMatchingArea* pChild) { childs.push_back(pChild); }
45 
46 		void purgeChildsAfterPosition(int iPosition);
47 	};
48 
49 
50 	class BNFClause : public GrfBlock {
51 	private:
52 		DtaBNFScript* _pBNFScript;
53 		std::string _sName;
54 		std::string _sTemplateKey;
55 		bool _bGenericKey;
56 		BNFClause* _pTemplateClause;
57 		std::map<std::string, BNFClause*> _mapOfTemplateInstantiations;
58 		BNFClause* _pGenericTemplateClause;
59 		int _iReturnType;
60 		std::vector<EXPRESSION_TYPE> _parameterTypes;
61 		std::vector<std::string> _parameters;
62 		bool _bPropagatedParameters;
63 		int _iPreprocessingIgnoreMode;
64 		BNFClause* _pPreprocessingIgnoreClause;
65 		BNFClause* _pOverloadClause;
66 
67 	public:
68 		static int NO_RETURN_TYPE;
69 		static int LIST_RETURN_TYPE;
70 		static int NODE_RETURN_TYPE;
71 		static int VALUE_RETURN_TYPE;
72 
73 	public:
74 		BNFClause(DtaBNFScript* pBNFScript, GrfBlock* pParent, const std::string& sName, const std::string& sTemplateKey, bool bGenericKey, const std::vector<std::string>& listOfParameters, const std::vector<EXPRESSION_TYPE>& listOfParameterTypes);
75 		BNFClause(DtaBNFScript* pBNFScript, GrfBlock* pParent, const std::string& sName, int iArity);
76 		virtual ~BNFClause();
77 
78 		virtual void accept(DtaVisitor& visitor, DtaVisitorEnvironment& env);
79 
80 		virtual bool isABNFCommand() const;
81 
getName()82 		inline const std::string& getName() const { return _sName; }
getTemplateKey()83 		inline const std::string& getTemplateKey() const { return _sTemplateKey; }
getTemplateClause()84 		inline BNFClause* getTemplateClause() const { return _pTemplateClause; }
isGenericKey()85 		inline bool isGenericKey() const { return _bGenericKey; }
86 		void setTemplateClause(BNFClause* pTemplateClause);
87 		BNFClause* getInstantiatedClause(const std::string& sInstantiationKey) const;
isATemplateInstantiation()88 		inline bool isATemplateInstantiation() const { return (_pTemplateClause != NULL) || !_mapOfTemplateInstantiations.empty() || (_pGenericTemplateClause != NULL); }
getGenericTemplateClause()89 		inline BNFClause* getGenericTemplateClause() const { return _pGenericTemplateClause; }
getReturnType()90 		inline int getReturnType() const { return _iReturnType; }
setReturnType(int iReturnType)91 		inline void setReturnType(int iReturnType) { _iReturnType = iReturnType; }
92 
getArity()93 		inline unsigned int getArity() const { return _parameters.size(); }
getParameters()94 		inline const std::vector<std::string>& getParameters() const { return _parameters; }
getParameterTypes()95 		inline const std::vector<EXPRESSION_TYPE>& getParameterTypes() const { return _parameterTypes; }
getParameter(int i)96 		inline const std::string& getParameter(int i) const { return _parameters[i]; }
getParameterType(int i)97 		inline const EXPRESSION_TYPE getParameterType(int i) const { return _parameterTypes[i]; }
98 		void setParameter(int i, const std::string& sParameter, EXPRESSION_TYPE iType);
99 
100 		virtual bool addBNFLocalVariable(const std::string& sVarName, EXPRESSION_TYPE varType);
101 		virtual EXPRESSION_TYPE getLocalVariable(const std::string& sVarName) const;
102 		virtual EXPRESSION_TYPE getVariable(const std::string& sVarName) const;
103 
isPropagatedParameter()104 		inline bool isPropagatedParameter() const { return _bPropagatedParameters; }
getPreprocessingIgnoreMode()105 		inline int/*IGNORE_MODE*/ getPreprocessingIgnoreMode() const { return _iPreprocessingIgnoreMode; }
106 		void setPreprocessingIgnoreMode(int/*IGNORE_MODE*/ iPreprocessingIgnoreMode, BNFClause* pIgnoreClause);
getOverloadClause()107 		inline BNFClause* getOverloadClause() const { return _pOverloadClause; }
108 		void setOverloadClause(BNFClause* pOverloadClause);
109 
110 		std::string getSignature() const;
111 		bool propagateParameters(ExprScriptExpression& theFilter, const std::string& sFunctionQuantity, const std::vector<std::string>& listOfParameters, const std::vector<EXPRESSION_TYPE>& listOfParameterTypes);
112 
113 		virtual SEQUENCE_INTERRUPTION_LIST execute(DtaScriptVariable& visibility);
114 		virtual SEQUENCE_INTERRUPTION_LIST executeClause(DtaScriptVariable& visibility, int iSuperCallDepth);
115 
116 		virtual std::string toString() const;
117 		virtual void compileCppFunction(CppCompilerEnvironment& theCompilerEnvironment) const;
118 
119 	private:
120 		SEQUENCE_INTERRUPTION_LIST executeInternalSuperClause(DtaScriptVariable& visibility, int &iSuperCallDepth);
121 	};
122 }
123 
124 #endif
125