OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreScriptCompiler.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4  (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 
29 #ifndef __SCRIPTCOMPILER_H_
30 #define __SCRIPTCOMPILER_H_
31 
32 #include "OgreSharedPtr.h"
33 #include "OgreSingleton.h"
34 #include "OgreScriptLoader.h"
35 #include "OgreGpuProgram.h"
36 #include "OgreAny.h"
38 #include "OgreHeaderPrefix.h"
39 
40 namespace Ogre
41 {
50  {
59  };
60 
62  struct ConcreteNode;
67  {
69  unsigned int line;
73  };
74 
77  {
85  };
86  class AbstractNode;
90 
92  {
93  public:
95  unsigned int line;
98  Any context; // A holder for translation context data
99  public:
101  virtual ~AbstractNode(){}
103  virtual AbstractNode *clone() const = 0;
105  virtual String getValue() const = 0;
106  };
107 
110  {
111  public:
114  public:
116  AbstractNode *clone() const;
117  String getValue() const;
118  private:
119  void parseNumber() const;
120  };
121 
124  {
125  private:
127  public:
128  String name, cls;
131  bool abstract;
134  AbstractNodeList overrides; // For use when processing object inheritance and overriding
135  public:
137  AbstractNode *clone() const;
138  String getValue() const;
139 
140  void addVariable(const String &name);
141  void setVariable(const String &name, const String &value);
142  std::pair<bool,String> getVariable(const String &name) const;
143  const map<String,String>::type &getVariables() const;
144  };
145 
148  {
149  public:
153  public:
155  AbstractNode *clone() const;
156  String getValue() const;
157  };
158 
161  {
162  public:
163  String target, source;
164  public:
166  AbstractNode *clone() const;
167  String getValue() const;
168  };
169 
172  {
173  public:
175  public:
177  AbstractNode *clone() const;
178  String getValue() const;
179  };
180 
181  class ScriptCompilerEvent;
183 
189  {
190  public: // Externally accessible types
191  //typedef map<String,uint32>::type IdMap;
192  typedef HashMap<String,uint32> IdMap;
193 
194  // The container for errors
195  struct Error : public ScriptCompilerAlloc
196  {
198  int line;
200  };
203 
204  // These are the built-in error codes
205  enum{
218  CE_REFERENCETOANONEXISTINGOBJECT
219  };
220  static String formatErrorCode(uint32 code);
221  public:
222  ScriptCompiler();
223  virtual ~ScriptCompiler() {}
224 
226 
231  bool compile(const String &str, const String &source, const String &group);
233  bool compile(const ConcreteNodeListPtr &nodes, const String &group);
235  AbstractNodeListPtr _generateAST(const String &str, const String &source, bool doImports = false, bool doObjects = false, bool doVariables = false);
237  bool _compile(AbstractNodeListPtr nodes, const String &group, bool doImports = true, bool doObjects = true, bool doVariables = true);
239  void addError(uint32 code, const String &file, int line, const String &msg = "");
241  void setListener(ScriptCompilerListener *listener);
243  ScriptCompilerListener *getListener();
245  const String &getResourceGroup() const;
247 
252  void addNameExclusion(const String &type);
254  void removeNameExclusion(const String &type);
256  bool _fireEvent(ScriptCompilerEvent *evt, void *retval);
257  private: // Tree processing
258  AbstractNodeListPtr convertToAST(const ConcreteNodeListPtr &nodes);
260  void processImports(AbstractNodeListPtr &nodes);
262  AbstractNodeListPtr loadImportPath(const String &name);
264  AbstractNodeListPtr locateTarget(AbstractNodeList *nodes, const String &target);
266  void processObjects(AbstractNodeList *nodes, const AbstractNodeListPtr &top);
268  void processVariables(AbstractNodeList *nodes);
270  void overlayObject(const AbstractNodePtr &source, ObjectAbstractNode *dest);
272  bool isNameExcluded(const String &cls, AbstractNode *parent);
274  void initWordMap();
275  private:
276  // Resource group
278  // The word -> id conversion table
280  // This is an environment map
283 
285  ImportCacheMap mImports; // The set of imported scripts to avoid circular dependencies
287  ImportRequestMap mImportRequests; // This holds the target objects for each script to be imported
288 
289  // This stores the imports of the scripts, so they are separated and can be treated specially
291 
292  // Error list
294 
295  // The listener
297  private: // Internal helper classes and processors
299  {
300  private:
304  public:
306  const AbstractNodeListPtr &getResult() const;
307  void visit(ConcreteNode *node);
308  static void visit(AbstractTreeBuilder *visitor, const ConcreteNodeList &nodes);
309  };
310  friend class AbstractTreeBuilder;
311  public: // Public translator definitions
312  // This enum are built-in word id values
313  enum
314  {
315  ID_ON = 1,
316  ID_OFF = 2,
317  ID_TRUE = 1,
318  ID_FALSE = 2,
319  ID_YES = 1,
320  ID_NO = 2
321  };
322  };
323 
330  {
331  public:
333 
334  ScriptCompilerEvent(const String &type):mType(type){}
336  private: // Non-copyable
339  };
340 
346  {
347  public:
350 
352  virtual ConcreteNodeListPtr importFile(ScriptCompiler *compiler, const String &name);
354  virtual void preConversion(ScriptCompiler *compiler, ConcreteNodeListPtr nodes);
356 
362  virtual bool postConversion(ScriptCompiler *compiler, const AbstractNodeListPtr&);
364  virtual void handleError(ScriptCompiler *compiler, uint32 code, const String &file, int line, const String &msg);
366 
375  virtual bool handleEvent(ScriptCompiler *compiler, ScriptCompilerEvent *evt, void *retval);
376  };
377 
378  class ScriptTranslator;
379  class ScriptTranslatorManager;
380 
384  class _OgreExport ScriptCompilerManager : public Singleton<ScriptCompilerManager>, public ScriptLoader, public ScriptCompilerAlloc
385  {
386  private:
388 
389  // A list of patterns loaded by this compiler manager
391 
392  // A pointer to the listener used for compiling scripts
394 
395  // Stores a map from object types to the translators that handle them
397 
398  // A pointer to the built-in ScriptTranslatorManager
400 
401  // A pointer to the specific compiler instance used
402  OGRE_THREAD_POINTER(ScriptCompiler, mScriptCompiler);
403  public:
405  virtual ~ScriptCompilerManager();
406 
408  void setListener(ScriptCompilerListener *listener);
410  ScriptCompilerListener *getListener();
411 
413  void addTranslatorManager(ScriptTranslatorManager *man);
415  void removeTranslatorManager(ScriptTranslatorManager *man);
417  void clearTranslatorManagers();
419  ScriptTranslator *getTranslator(const AbstractNodePtr &node);
420 
422  void addScriptPattern(const String &pattern);
424  const StringVector& getScriptPatterns(void) const;
426  void parseScript(DataStreamPtr& stream, const String& groupName);
428  Real getLoadingOrder(void) const;
429 
445  static ScriptCompilerManager& getSingleton(void);
461  static ScriptCompilerManager* getSingletonPtr(void);
462  };
463 
464  // Standard event types
466  {
467  public:
471 
473  :ScriptCompilerEvent(eventType), mMaterial(material), mAliases(aliases){}
474  };
475 
477  {
478  public:
480  {
484  COMPOSITOR
485  };
489 
491  :ScriptCompilerEvent(eventType), mResourceType(resourceType), mName(name){}
492  };
493 
495  {
496  public:
500 
502  :ScriptCompilerEvent(eventType), mClass(cls), mParent(parent){}
503  };
504 
506  {
507  public:
508  String mFile, mName, mResourceGroup;
510 
511  CreateMaterialScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
512  :ScriptCompilerEvent(eventType), mFile(file), mName(name), mResourceGroup(resourceGroup){}
513  };
514 
516  {
517  public:
518  String mFile, mName, mResourceGroup, mSource, mSyntax;
521 
522  CreateGpuProgramScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup, const String &source,
523  const String &syntax, GpuProgramType programType)
524  :ScriptCompilerEvent(eventType), mFile(file), mName(name), mResourceGroup(resourceGroup), mSource(source),
525  mSyntax(syntax), mProgramType(programType)
526  {}
527  };
528 
530  {
531  public:
532  String mFile, mName, mResourceGroup, mSource, mLanguage;
535 
536  CreateHighLevelGpuProgramScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup, const String &source,
537  const String &language, GpuProgramType programType)
538  :ScriptCompilerEvent(eventType), mFile(file), mName(name), mResourceGroup(resourceGroup), mSource(source),
539  mLanguage(language), mProgramType(programType)
540  {}
541  };
542 
544  {
545  public:
546  String mFile, mName, mResourceGroup;
548 
549  CreateGpuSharedParametersScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
550  :ScriptCompilerEvent(eventType), mFile(file), mName(name), mResourceGroup(resourceGroup){}
551  };
552 
554  {
555  public:
556  String mFile, mName, mResourceGroup;
558 
559  CreateParticleSystemScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
560  :ScriptCompilerEvent(eventType), mFile(file), mName(name), mResourceGroup(resourceGroup){}
561  };
562 
564  {
565  public:
566  String mFile, mName, mResourceGroup;
568 
569  CreateCompositorScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
570  :ScriptCompilerEvent(eventType), mFile(file), mName(name), mResourceGroup(resourceGroup){}
571  };
572 
574  enum
575  {
589 
596 
604 
611 
694 
784 
788 
797  // ID_TEXTURE,
802  // ID_GAMMA,
808  // ID_PASS,
817 
818  //Used by PASS_SCENE
829 
830  //Used by PASS_QUAD
836 
837  //Used by PASS_CLEAR
841  // ID_STENCIL,
846 
862 
872 
873 #ifdef RTSHADER_SYSTEM_BUILD_CORE_SHADERS
874  ID_RT_SHADER_SYSTEM,
875 #endif
876  // More program IDs
884  // More binding IDs
889 
890  // Support for subroutine
892 
894  };
897 }
898 
899 #include "OgreHeaderSuffix.h"
900 
901 #endif
multimap< String, String >::type ImportRequestMap
list< ConcreteNodePtr >::type ConcreteNodeList
map< String, String >::type mEnv
The ScriptTranslatorManager manages the lifetime and access to script translators.
float Real
Software floating point type.
unsigned int uint32
Definition: OgrePlatform.h:420
vector< ScriptTranslatorManager * >::type mManagers
#define _OgreExport
Definition: OgrePlatform.h:255
SharedPtr< Error > ErrorPtr
SharedPtr< ConcreteNode > ConcreteNodePtr
Variant type that can hold Any other type.
Definition: OgreAny.h:54
std::vector< T, A > type
#define OGRE_THREAD_POINTER(T, var)
CreateHighLevelGpuProgramScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup, const String &source, const String &language, GpuProgramType programType)
Template class for creating single-instance global classes.
Definition: OgreSingleton.h:64
CreateGpuSharedParametersScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
GpuProgramType
Enumerates the types of programs which can run on the GPU.
ConcreteNodeType
These enums hold the types of the concrete parsed nodes.
This abstract node represents an import statement.
Class encapsulates rendering properties of an object.
Definition: OgreMaterial.h:84
This is a listener for the compiler.
map< String, String >::type Environment
This abstract node represents a variable assignment.
AbstractNodeType type
CreateGpuProgramScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup, const String &source, const String &syntax, GpuProgramType programType)
This class translates script AST (abstract syntax tree) into Ogre resources.
ProcessNameExclusionScriptCompilerEvent(const String &cls, AbstractNode *parent)
SharedPtr< ConcreteNodeList > ConcreteNodeListPtr
This abstract node represents a script property.
SharedPtr< AbstractNodeList > AbstractNodeListPtr
Abstract class defining the interface used by classes which wish to perform script loading to define ...
This specific abstract node represents a script object.
CreateCompositorScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
This struct is a base class for events which can be thrown by the compilers and caught by subscribers...
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
list< AbstractNodePtr >::type AbstractNodeList
PreApplyTextureAliasesScriptCompilerEvent(Material *material, AliasTextureNamePairList *aliases)
vector< String >::type bases
ScriptCompilerEvent(const String &type)
ImportRequestMap mImportRequests
Manages threaded compilation of scripts.
CreateParticleSystemScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
vector< String >::type StringVector
map< String, AbstractNodeListPtr >::type ImportCacheMap
list< ErrorPtr >::type ErrorList
ScriptCompilerListener * mListener
ProcessResourceNameScriptCompilerEvent(ResourceType resourceType, const String &name)
ScriptCompilerListener * mListener
AbstractNode * parent
This is an abstract node which cannot be broken down further.
HashMap< String, uint32 > IdMap
SharedPtr< AbstractNode > AbstractNodePtr
ScriptTranslatorManager * mBuiltinTranslatorManager
AbstractNodeList mImportTable
ConcreteNodeType type
Reference-counted shared pointer, used for objects where implicit destruction is required.
ConcreteNodeList children
_StringBase String
Definition: OgreCommon.h:53
This is the main class for the compiler.
ConcreteNode * parent
Suport for shader model 5.0.
AbstractNodeType
This enum holds the types of the possible abstract nodes.
CreateMaterialScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
map< String, String >::type AliasTextureNamePairList
Alias / Texture name pair (first = alias, second = texture name)
Definition: OgreCommon.h:583
ScriptCompilerEvent & operator=(const ScriptCompilerEvent &)