OGRE  1.9.0
OgreScriptCompiler.h
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2014 Torus Knot Software Ltd
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE 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
40namespace Ogre
41{
60
62 struct ConcreteNode;
74
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:
118 private:
119 void parseNumber() const;
120 };
121
124 {
125 private:
127 public:
128 String name, cls;
134 AbstractNodeList overrides; // For use when processing object inheritance and overriding
135 public:
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
158
161 {
162 public:
163 String target, source;
164 public:
168 };
169
180
183
189 {
190 public: // Externally accessible types
191 //typedef map<String,uint32>::type IdMap;
193
194 // The container for errors
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
259
270
271 private: // Tree processing
286 bool isNameExcluded(const String &cls, AbstractNode *parent);
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
329 public: // Public translator definitions
330 // This enum are built-in word id values
331 enum
332 {
333 ID_ON = 1,
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;
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
421 public:
424
429
438
440
451
453 void addScriptPattern(const String &pattern);
455 const StringVector& getScriptPatterns(void) const;
457 void parseScript(DataStreamPtr& stream, const String& groupName);
460
493 };
494
495 // Standard event types
506
508 {
509 public:
520
522 :ScriptCompilerEvent(eventType), mResourceType(resourceType), mName(name){}
523 };
524
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
883 // More program IDs
890 // More binding IDs
895
896 // Support for subroutine
898
900 };
903}
904
905#include "OgreHeaderSuffix.h"
906
907#endif
#define _OgreExport
#define HashMap
virtual String getValue() const =0
Returns a string value depending on the type of the AbstractNode.
virtual AbstractNode * clone() const =0
Returns a new AbstractNode which is a replica of this one.
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.
AtomAbstractNode(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.
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.
This specific abstract node represents a script object.
String getValue() const
Returns a string value depending on the type of the AbstractNode.
void addVariable(const String &name)
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
AbstractNode * clone() const
Returns a new AbstractNode which is a replica of this one.
std::pair< bool, String > getVariable(const String &name) const
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.
vector< ScriptTranslatorManager * >::type mManagers
static ScriptCompilerManager & getSingleton(void)
Override standard Singleton retrieval.
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.)
OGRE_THREAD_POINTER(ScriptCompiler, mScriptCompiler)
void parseScript(DataStreamPtr &stream, const String &groupName)
Parse a script file.
const StringVector & getScriptPatterns(void) const
Gets the file patterns which should be used to find scripts for this class.
void addTranslatorManager(ScriptTranslatorManager *man)
Adds the given translator manager to the list of managers.
void clearTranslatorManagers()
Clears all translator managers.
ScriptCompilerListener * getListener()
Returns the currently set listener used for compiler instances.
ScriptTranslator * getTranslator(const AbstractNodePtr &node)
Retrieves a ScriptTranslator from the supported managers.
static ScriptCompilerManager * getSingletonPtr(void)
Override standard Singleton retrieval.
ScriptCompilerListener * mListener
Real getLoadingOrder(void) const
Gets the relative loading order of scripts of this type.
uint32 registerCustomWordId(const String &word)
Adds a custom word id which can be used for custom script translators.
static void visit(AbstractTreeBuilder *visitor, const ConcreteNodeList &nodes)
AbstractTreeBuilder(ScriptCompiler *compiler)
const AbstractNodeListPtr & getResult() const
This is the main class for the compiler.
map< String, AbstractNodeListPtr >::type ImportCacheMap
bool _compile(AbstractNodeListPtr nodes, const String &group, bool doImports=true, bool doObjects=true, bool doVariables=true)
Compiles the given abstract syntax tree.
const String & getResourceGroup() const
Returns the resource group currently set for this compiler.
uint32 registerCustomWordId(const String &word)
Adds a custom word id which can be used for custom script translators.
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.
ScriptCompilerListener * getListener()
Returns the currently set listener.
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.
map< String, String >::type Environment
SharedPtr< Error > ErrorPtr
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.
AbstractNodeListPtr locateTarget(AbstractNodeList *nodes, const String &target)
Returns the abstract nodes from the given tree which represent the target.
AbstractNodeList mImportTable
ImportRequestMap mImportRequests
multimap< String, String >::type ImportRequestMap
HashMap< String, uint32 > IdMap
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.
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.
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.
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_ACCESS
@ ID_ONE_MINUS_DEST_ALPHA
@ ID_CAMERA_FAR_CORNERS_WORLD_SPACE
@ ID_SHADOW_CASTER_VERTEX_PROGRAM_REF
@ ID_BLEND_TEXTURE_ALPHA
@ ID_BLEND_DIFFUSE_COLOUR
@ ID_TESSELATION_HULL_PROGRAM_REF
@ ID_SHADOW_RECEIVER_MATERIAL
@ ID_TESSELATION_DOMAIN
@ ID_TESSELATION_DOMAIN_PROGRAM_REF
@ ID_POINT_SIZE_ATTENUATION
@ ID_TARGET_WIDTH_SCALED
@ ID_SHADOW_RECEIVER_FRAGMENT_PROGRAM_REF
@ ID_SHADOW_RECEIVER_VERTEX_PROGRAM_REF
@ ID_COLOUR_OP_MULTIPASS_FALLBACK
@ ID_COMPUTE_PROGRAM_REF
@ ID_ONE_MINUS_DEST_COLOUR
@ ID_TRANSPARENT_SORTING
@ ID_VERTEX_PROGRAM_REF
@ ID_CAMERA_FAR_CORNERS_VIEW_SPACE
@ ID_TARGET_HEIGHT_SCALED
@ ID_BLEND_DIFFUSE_ALPHA
@ ID_SHADOW_CASTER_MATERIAL
@ ID_FIRST_RENDER_QUEUE
@ ID_SEPARATE_SCENE_BLEND_OP
@ ID_ONE_MINUS_SRC_COLOUR
@ ID_SHARED_PARAM_NAMED
@ ID_GEOMETRY_PROGRAM_REF
@ ID_ONE_MINUS_SRC_ALPHA
@ ID_TRANSPARENCY_CASTS_SHADOWS
@ ID_POLYGON_MODE_OVERRIDEABLE
@ ID_ITERATION_DEPTH_BIAS
@ ID_FRAGMENT_PROGRAM_REF
@ ID_ILLUMINATION_STAGE
@ ID_BLEND_CURRENT_ALPHA
@ ID_SHADOW_CASTER_FRAGMENT_PROGRAM_REF
@ ID_PARAM_INDEXED_AUTO
@ ID_TESSELATION_HULL_PROGRAM
Suport for shader model 5.0.
@ ID_SEPARATE_SCENE_BLEND
@ ID_TESSELATION_DOMAIN_PROGRAM
@ CNT_VARIABLE_ASSIGN
GpuProgramType
Enumerates the types of programs which can run on the GPU.
float Real
Software floating point type.
unsigned int uint32
_StringBase String
ConcreteNodeType type
ConcreteNodeList children
std::list< T, A > type
std::map< K, V, P, A > type
std::multimap< K, V, P, A > type
std::vector< T, A > type