OGRE  1.9
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 "OgreMaterial.h"
35 #include "OgreCompositor.h"
36 #include "OgreCompositionPass.h"
37 #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 
259 
269  uint32 registerCustomWordId(const String &word);
270 
271  private: // Tree processing
272  AbstractNodeListPtr convertToAST(const ConcreteNodeListPtr &nodes);
274  void processImports(AbstractNodeListPtr &nodes);
276  AbstractNodeListPtr loadImportPath(const String &name);
278  AbstractNodeListPtr locateTarget(AbstractNodeList *nodes, const String &target);
280  void processObjects(AbstractNodeList *nodes, const AbstractNodeListPtr &top);
282  void processVariables(AbstractNodeList *nodes);
284  void overlayObject(const AbstractNodePtr &source, ObjectAbstractNode *dest);
286  bool isNameExcluded(const String &cls, AbstractNode *parent);
288  void initWordMap();
289  private:
290  // Resource group
292  // The word -> id conversion table
294 
295  // The largest registered id
297 
298  // This is an environment map
301 
303  ImportCacheMap mImports; // The set of imported scripts to avoid circular dependencies
305  ImportRequestMap mImportRequests; // This holds the target objects for each script to be imported
306 
307  // This stores the imports of the scripts, so they are separated and can be treated specially
309 
310  // Error list
312 
313  // The listener
315  private: // Internal helper classes and processors
317  {
318  private:
322  public:
324  const AbstractNodeListPtr &getResult() const;
325  void visit(ConcreteNode *node);
326  static void visit(AbstractTreeBuilder *visitor, const ConcreteNodeList &nodes);
327  };
328  friend class AbstractTreeBuilder;
329  public: // Public translator definitions
330  // This enum are built-in word id values
331  enum
332  {
333  ID_ON = 1,
334  ID_OFF = 2,
335  ID_TRUE = 1,
336  ID_FALSE = 2,
337  ID_YES = 1,
338  ID_NO = 2
339  };
340  };
341 
348  {
349  public:
351 
352  ScriptCompilerEvent(const String &type):mType(type){}
354  private: // Non-copyable
357  };
358 
364  {
365  public:
368 
370  virtual ConcreteNodeListPtr importFile(ScriptCompiler *compiler, const String &name);
372  virtual void preConversion(ScriptCompiler *compiler, ConcreteNodeListPtr nodes);
374 
380  virtual bool postConversion(ScriptCompiler *compiler, const AbstractNodeListPtr&);
382  virtual void handleError(ScriptCompiler *compiler, uint32 code, const String &file, int line, const String &msg);
384 
393  virtual bool handleEvent(ScriptCompiler *compiler, ScriptCompilerEvent *evt, void *retval);
394  };
395 
396  class ScriptTranslator;
397  class ScriptTranslatorManager;
398 
402  class _OgreExport ScriptCompilerManager : public Singleton<ScriptCompilerManager>, public ScriptLoader, public ScriptCompilerAlloc
403  {
404  private:
406 
407  // A list of patterns loaded by this compiler manager
409 
410  // A pointer to the listener used for compiling scripts
412 
413  // Stores a map from object types to the translators that handle them
415 
416  // A pointer to the built-in ScriptTranslatorManager
418 
419  // A pointer to the specific compiler instance used
420  OGRE_THREAD_POINTER(ScriptCompiler, mScriptCompiler);
421  public:
423  virtual ~ScriptCompilerManager();
424 
426  void setListener(ScriptCompilerListener *listener);
428  ScriptCompilerListener *getListener();
429 
431  void addTranslatorManager(ScriptTranslatorManager *man);
433  void removeTranslatorManager(ScriptTranslatorManager *man);
435  void clearTranslatorManagers();
437  ScriptTranslator *getTranslator(const AbstractNodePtr &node);
438 
440 
450  uint32 registerCustomWordId(const String &word);
451 
453  void addScriptPattern(const String &pattern);
455  const StringVector& getScriptPatterns(void) const;
457  void parseScript(DataStreamPtr& stream, const String& groupName);
459  Real getLoadingOrder(void) const;
460 
476  static ScriptCompilerManager& getSingleton(void);
492  static ScriptCompilerManager* getSingletonPtr(void);
493  };
494 
495  // Standard event types
497  {
498  public:
502 
504  :ScriptCompilerEvent(eventType), mMaterial(material), mAliases(aliases){}
505  };
506 
508  {
509  public:
511  {
515  COMPOSITOR
516  };
520 
522  :ScriptCompilerEvent(eventType), mResourceType(resourceType), mName(name){}
523  };
524 
526  {
527  public:
531 
533  :ScriptCompilerEvent(eventType), mClass(cls), mParent(parent){}
534  };
535 
537  {
538  public:
539  String mFile, mName, mResourceGroup;
541 
542  CreateMaterialScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
543  :ScriptCompilerEvent(eventType), mFile(file), mName(name), mResourceGroup(resourceGroup){}
544  };
545 
547  {
548  public:
549  String mFile, mName, mResourceGroup, mSource, mSyntax;
552 
553  CreateGpuProgramScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup, const String &source,
554  const String &syntax, GpuProgramType programType)
555  :ScriptCompilerEvent(eventType), mFile(file), mName(name), mResourceGroup(resourceGroup), mSource(source),
556  mSyntax(syntax), mProgramType(programType)
557  {}
558  };
559 
561  {
562  public:
563  String mFile, mName, mResourceGroup, mSource, mLanguage;
566 
567  CreateHighLevelGpuProgramScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup, const String &source,
568  const String &language, GpuProgramType programType)
569  :ScriptCompilerEvent(eventType), mFile(file), mName(name), mResourceGroup(resourceGroup), mSource(source),
570  mLanguage(language), mProgramType(programType)
571  {}
572  };
573 
575  {
576  public:
577  String mFile, mName, mResourceGroup;
579 
580  CreateGpuSharedParametersScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
581  :ScriptCompilerEvent(eventType), mFile(file), mName(name), mResourceGroup(resourceGroup){}
582  };
583 
585  {
586  public:
587  String mFile, mName, mResourceGroup;
589 
590  CreateParticleSystemScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
591  :ScriptCompilerEvent(eventType), mFile(file), mName(name), mResourceGroup(resourceGroup){}
592  };
593 
595  {
596  public:
597  String mFile, mName, mResourceGroup;
599 
600  CreateCompositorScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
601  :ScriptCompilerEvent(eventType), mFile(file), mName(name), mResourceGroup(resourceGroup){}
602  };
603 
605  enum
606  {
623 
630 
638 
645 
728 
817 
821 
825 
838  //ID_GAMMA, - already registered for material
846 
857 
864 
879 #ifdef RTSHADER_SYSTEM_BUILD_CORE_SHADERS
880  ID_RT_SHADER_SYSTEM,
881 #endif
882  // More program IDs
890  // More binding IDs
895 
896  // Support for subroutine
898 
900  };
903 }
904 
905 #include "OgreHeaderSuffix.h"
906 
907 #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:344
vector< ScriptTranslatorManager * >::type mManagers
#define _OgreExport
Definition: OgrePlatform.h:260
SharedPtr< Error > ErrorPtr
SharedPtr< ConcreteNode > ConcreteNodePtr
Variant type that can hold Any other type.
Definition: OgreAny.h:56
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.
Suport for shader model 5.0.
This abstract node represents an import statement.
Class encapsulates rendering properties of an object.
Definition: OgreMaterial.h:88
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
This is the main class for the compiler.
ConcreteNode * parent
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:553
ScriptCompilerEvent & operator=(const ScriptCompilerEvent &)