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-2013 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  {
58  CNT_COLON
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;
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  {
197  String file, message;
198  int line;
200  };
203 
204  // These are the built-in error codes
205  enum{
218  CE_REFERENCETOANONEXISTINGOBJECT
219  };
221  public:
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 = "");
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
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:
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;
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
403  public:
406 
411 
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 
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  {
592 
599 
607 
614 
697 
786 
790 
794 
807  //ID_GAMMA, - already registered for material
815 
826 
833 
848 #ifdef RTSHADER_SYSTEM_BUILD_CORE_SHADERS
849  ID_RT_SHADER_SYSTEM,
850 #endif
852  // More program IDs
859  // More binding IDs
864 
865  // Support for subroutine
867 
869  };
872 }
873 
874 #include "OgreHeaderSuffix.h"
875 
876 #endif
#define _OgreExport
Definition: OgrePlatform.h:257
AbstractNode * parent
virtual AbstractNode * clone() const =0
Returns a new AbstractNode which is a replica of this one.
virtual String getValue() const =0
Returns a string value depending on the type of the AbstractNode.
AbstractNode(AbstractNode *ptr)
AbstractNodeType type
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Variant type that can hold Any other type.
Definition: OgreAny.h:57
This is an abstract node which cannot be broken down further.
AbstractNode * clone() const
Returns a new AbstractNode which is a replica of this one.
AtomAbstractNode(AbstractNode *ptr)
String getValue() const
Returns a string value depending on the type of the AbstractNode.
void parseNumber() const
CreateCompositorScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
CreateGpuProgramScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup, const String &source, const String &syntax, GpuProgramType programType)
CreateGpuSharedParametersScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
CreateHighLevelGpuProgramScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup, const String &source, const String &language, GpuProgramType programType)
CreateMaterialScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
CreateParticleSystemScriptCompilerEvent(const String &file, const String &name, const String &resourceGroup)
This abstract node represents an import statement.
String getValue() const
Returns a string value depending on the type of the AbstractNode.
AbstractNode * clone() const
Returns a new AbstractNode which is a replica of this one.
Class encapsulates rendering properties of an object.
Definition: OgreMaterial.h:89
This specific abstract node represents a script object.
std::pair< bool, String > getVariable(const String &name) const
String getValue() const
Returns a string value depending on the type of the AbstractNode.
void addVariable(const String &name)
AbstractNode * clone() const
Returns a new AbstractNode which is a replica of this one.
const map< String, String >::type & getVariables() const
void setVariable(const String &name, const String &value)
map< String, String >::type mEnv
ObjectAbstractNode(AbstractNode *ptr)
vector< String >::type bases
PreApplyTextureAliasesScriptCompilerEvent(Material *material, AliasTextureNamePairList *aliases)
ProcessNameExclusionScriptCompilerEvent(const String &cls, AbstractNode *parent)
ProcessResourceNameScriptCompilerEvent(ResourceType resourceType, const String &name)
This abstract node represents a script property.
String getValue() const
Returns a string value depending on the type of the AbstractNode.
AbstractNode * clone() const
Returns a new AbstractNode which is a replica of this one.
PropertyAbstractNode(AbstractNode *ptr)
This struct is a base class for events which can be thrown by the compilers and caught by subscribers...
ScriptCompilerEvent & operator=(const ScriptCompilerEvent &)
ScriptCompilerEvent(const String &type)
ScriptCompilerEvent(const ScriptCompilerEvent &)
This is a listener for the compiler.
virtual ConcreteNodeListPtr importFile(ScriptCompiler *compiler, const String &name)
Returns the concrete node list from the given file.
virtual bool handleEvent(ScriptCompiler *compiler, ScriptCompilerEvent *evt, void *retval)
Called when an event occurs during translation, return true if handled.
virtual void preConversion(ScriptCompiler *compiler, ConcreteNodeListPtr nodes)
Allows for responding to and overriding behavior before a CST is translated into an AST.
virtual bool postConversion(ScriptCompiler *compiler, const AbstractNodeListPtr &)
Allows vetoing of continued compilation after the entire AST conversion process finishes.
virtual void handleError(ScriptCompiler *compiler, uint32 code, const String &file, int line, const String &msg)
Called when an error occurred.
Manages threaded compilation of scripts.
void removeTranslatorManager(ScriptTranslatorManager *man)
Removes the given translator manager from the list of managers.
static ScriptCompilerManager & getSingleton(void)
Override standard Singleton retrieval.
ScriptTranslator * getTranslator(const AbstractNodePtr &node)
Retrieves a ScriptTranslator from the supported managers.
vector< ScriptTranslatorManager * >::type mManagers
ScriptCompilerListener * getListener()
Returns the currently set listener used for compiler instances.
ScriptTranslatorManager * mBuiltinTranslatorManager
void setListener(ScriptCompilerListener *listener)
Sets the listener used for compiler instances.
void addScriptPattern(const String &pattern)
Adds a script extension that can be handled (e.g. *.material, *.pu, etc.)
const StringVector & getScriptPatterns(void) const
Gets the file patterns which should be used to find scripts for this class.
OGRE_THREAD_POINTER(ScriptCompiler, mScriptCompiler)
static ScriptCompilerManager * getSingletonPtr(void)
Override standard Singleton retrieval.
void parseScript(DataStreamPtr &stream, const String &groupName)
Parse a script file.
void addTranslatorManager(ScriptTranslatorManager *man)
Adds the given translator manager to the list of managers.
void clearTranslatorManagers()
Clears all translator managers.
ScriptCompilerListener * mListener
Real getLoadingOrder(void) const
Gets the relative loading order of scripts of this type.
static void visit(AbstractTreeBuilder *visitor, const ConcreteNodeList &nodes)
const AbstractNodeListPtr & getResult() const
AbstractTreeBuilder(ScriptCompiler *compiler)
This is the main class for the compiler.
map< String, String >::type Environment
bool _compile(AbstractNodeListPtr nodes, const String &group, bool doImports=true, bool doObjects=true, bool doVariables=true)
Compiles the given abstract syntax tree.
AbstractNodeListPtr convertToAST(const ConcreteNodeListPtr &nodes)
void processObjects(AbstractNodeList *nodes, const AbstractNodeListPtr &top)
Handles object inheritance and variable expansion.
bool compile(const ConcreteNodeListPtr &nodes, const String &group)
Compiles resources from the given concrete node list.
void processImports(AbstractNodeListPtr &nodes)
This built-in function processes import nodes.
void addError(uint32 code, const String &file, int line, const String &msg="")
Adds the given error to the compiler's list of errors.
HashMap< String, uint32 > IdMap
static String formatErrorCode(uint32 code)
void processVariables(AbstractNodeList *nodes)
Handles processing the variables.
void overlayObject(const AbstractNodePtr &source, ObjectAbstractNode *dest)
This function overlays the given object on the destination object following inheritance rules.
bool isNameExcluded(const String &cls, AbstractNode *parent)
Returns true if the given class is name excluded.
bool _fireEvent(ScriptCompilerEvent *evt, void *retval)
Internal method for firing the handleEvent method.
multimap< String, String >::type ImportRequestMap
AbstractNodeListPtr locateTarget(AbstractNodeList *nodes, const String &target)
Returns the abstract nodes from the given tree which represent the target.
AbstractNodeList mImportTable
SharedPtr< Error > ErrorPtr
ImportRequestMap mImportRequests
const String & getResourceGroup() const
Returns the resource group currently set for this compiler.
bool compile(const String &str, const String &source, const String &group)
Takes in a string of script code and compiles it into resources.
AbstractNodeListPtr _generateAST(const String &str, const String &source, bool doImports=false, bool doObjects=false, bool doVariables=false)
Generates the AST from the given string script.
list< ErrorPtr >::type ErrorList
AbstractNodeListPtr loadImportPath(const String &name)
Loads the requested script and converts it to an AST.
ScriptCompilerListener * mListener
void setListener(ScriptCompilerListener *listener)
Sets the listener used by the compiler.
map< String, AbstractNodeListPtr >::type ImportCacheMap
void removeNameExclusion(const String &type)
Removes a name exclusion.
void addNameExclusion(const String &type)
Adds a name exclusion to the map.
void initWordMap()
This function sets up the initial values in word id map.
ScriptCompilerListener * getListener()
Returns the currently set listener.
Abstract class defining the interface used by classes which wish to perform script loading to define ...
The ScriptTranslatorManager manages the lifetime and access to script translators.
This class translates script AST (abstract syntax tree) into Ogre resources.
Reference-counted shared pointer, used for objects where implicit destruction is required.
Template class for creating single-instance global classes.
Definition: OgreSingleton.h:65
This abstract node represents a variable assignment.
VariableAccessAbstractNode(AbstractNode *ptr)
String getValue() const
Returns a string value depending on the type of the AbstractNode.
AbstractNode * clone() const
Returns a new AbstractNode which is a replica of this one.
vector< String >::type StringVector
SharedPtr< AbstractNode > AbstractNodePtr
map< String, String >::type AliasTextureNamePairList
Alias / Texture name pair (first = alias, second = texture name)
Definition: OgreCommon.h:553
SharedPtr< ConcreteNode > ConcreteNodePtr
AbstractNodeType
This enum holds the types of the possible abstract nodes.
SharedPtr< ConcreteNodeList > ConcreteNodeListPtr
list< ConcreteNodePtr >::type ConcreteNodeList
SharedPtr< AbstractNodeList > AbstractNodeListPtr
ConcreteNodeType
These enums hold the types of the concrete parsed nodes.
list< AbstractNodePtr >::type AbstractNodeList
@ ANT_VARIABLE_SET
@ ANT_VARIABLE_ACCESS
@ CNT_VARIABLE_ASSIGN
@ ID_PARTICLE_SYSTEM
@ ID_ONE_MINUS_DEST_ALPHA
@ ID_COMPUTE_PROGRAM
@ ID_CAMERA_FAR_CORNERS_WORLD_SPACE
@ ID_REVERSE_SUBTRACT
@ ID_COMPOSITOR_LOGIC
@ ID_LIGHT_CLIP_PLANES
@ ID_VISIBILITY_MASK
@ ID_SHADOW_CASTER_VERTEX_PROGRAM_REF
@ ID_INVERSE_SAWTOOTH
@ ID_BLEND_TEXTURE_ALPHA
@ ID_LAST_RENDER_QUEUE
@ ID_BLEND_DIFFUSE_COLOUR
@ ID_TESSELATION_HULL_PROGRAM_REF
@ ID_SHADOW_RECEIVER_MATERIAL
@ ID_SHARED_PARAMS_REF
@ ID_SHADOWS_ENABLED
@ ID_RECEIVE_SHADOWS
@ ID_TESSELATION_DOMAIN
@ ID_TESSELATION_DOMAIN_PROGRAM_REF
@ ID_GEOMETRY_PROGRAM
@ ID_POINT_SIZE_ATTENUATION
@ ID_TARGET_WIDTH_SCALED
@ ID_FRAGMENT_PROGRAM
@ ID_SHADOW_RECEIVER_FRAGMENT_PROGRAM_REF
@ ID_SHADOW_RECEIVER_VERTEX_PROGRAM_REF
@ ID_COLOUR_OP_MULTIPASS_FALLBACK
@ ID_TEX_ADDRESS_MODE
@ ID_COMPUTE_PROGRAM_REF
@ ID_ONE_MINUS_DEST_COLOUR
@ ID_TRANSPARENT_SORTING
@ ID_VERTEX_PROGRAM_REF
@ ID_MATERIAL_SCHEME
@ ID_CAMERA_FAR_CORNERS_VIEW_SPACE
@ ID_TARGET_HEIGHT_SCALED
@ ID_BLEND_DIFFUSE_ALPHA
@ ID_GPU_VENDOR_RULE
@ ID_ALPHA_TO_COVERAGE
@ ID_SHADOW_CASTER_MATERIAL
@ ID_FIRST_RENDER_QUEUE
@ ID_SEPARATE_SCENE_BLEND_OP
@ ID_PARAM_NAMED_AUTO
@ ID_ONE_MINUS_SRC_COLOUR
@ ID_GPU_DEVICE_RULE
@ ID_SHARED_PARAM_NAMED
@ ID_GEOMETRY_PROGRAM_REF
@ ID_ONE_MINUS_SRC_ALPHA
@ ID_TESSELATION_HULL
@ ID_TRANSPARENCY_CASTS_SHADOWS
@ ID_POLYGON_MODE_OVERRIDEABLE
@ ID_TEX_BORDER_COLOUR
@ ID_ITERATION_DEPTH_BIAS
@ ID_SET_TEXTURE_ALIAS
@ ID_FRAGMENT_PROGRAM_REF
@ ID_ILLUMINATION_STAGE
@ ID_BLEND_CURRENT_ALPHA
@ ID_SHADOW_CASTER_FRAGMENT_PROGRAM_REF
@ ID_PARAM_INDEXED_AUTO
@ ID_ALPHA_REJECTION
@ ID_CUBIC_REFLECTION
@ ID_TESSELATION_HULL_PROGRAM
Suport for shader model 5.0.
@ ID_NORMALISE_NORMALS
@ ID_END_BUILTIN_IDS
@ ID_SEPARATE_SCENE_BLEND
@ ID_TESSELATION_DOMAIN_PROGRAM
GpuProgramType
Enumerates the types of programs which can run on the GPU.
float Real
Software floating point type.
unsigned int uint32
Definition: OgrePlatform.h:359
_StringBase String
ConcreteNodeType type
ConcreteNodeList children
ConcreteNode * parent
std::vector< T, A > type

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.