Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/RenderSystems/GL/src/atifs/include/Compiler2Pass.h @ 5

Last change on this file since 5 was 5, checked in by anonymous, 17 years ago

=hoffentlich gehts jetzt

File size: 9.2 KB
Line 
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-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29
30
31#ifndef COMPILER2PASS_H
32#define COMPILER2PASS_H
33
34#include <vector>
35
36#ifdef _WIN32
37#include <windows.h>
38#endif
39
40// FIX ME - should not be hard coded
41#define BAD_TOKEN 999
42
43typedef unsigned int uint;
44
45
46
47/** Compiler2Pass is a generic compiler/assembler
48@remarks
49        provides a tokenizer in pass 1 and relies on the subclass to provide the virtual method for pass 2
50
51        PASS 1 - tokenize source: this is a simple brute force lexical scanner/analyzer that also parses
52                         the formed token for proper semantics and context in one pass
53                         it uses Look Ahead Left-Right (LALR) ruling based on Backus - Naur From notation for semantic
54                         checking and also performs     context checking allowing for language dialects
55
56        PASS 2 - generate application specific instructions ie native instructions
57
58@par
59        this class must be subclassed with the subclass providing implementation for Pass 2.  The subclass
60        is responsible for setting up the token libraries along with defining the language syntax.
61
62*/
63class Compiler2Pass {
64
65protected:
66
67        // BNF operation types
68        enum OperationType {otRULE, otAND, otOR, otOPTIONAL, otREPEAT, otEND};
69
70        /** structure used to build rule paths
71
72        */
73        struct TokenRule {
74                OperationType mOperation;
75                uint mTokenID;
76                char* mSymbol;
77                uint mErrorID;
78
79        };
80
81        /** structure used to build Symbol Type library */
82        struct SymbolDef {
83          uint mID;                                     // Token ID which is the index into the Token Type library
84          uint mPass2Data;                      // data used by pass 2 to build native instructions
85
86          uint mContextKey;                     // context key to fit the Active Context
87          uint mContextPatternSet;      // new pattern to set for Active Context bits
88          uint mContextPatternClear;// Contexts bits to clear Active Context bits
89
90          int mDefTextID;                       // index into text table for default name : set at runtime
91          uint mRuleID;                         // index into Rule database for non-terminal toke rulepath
92                                                                // if RuleID is zero the token is terminal
93
94        };
95
96
97        /** structure for Token instructions */
98        struct TokenInst {
99          uint mNTTRuleID;                      // Non-Terminal Token Rule ID that generated Token
100          uint mID;                                     // Token ID
101          int mLine;                            // line number in source code where Token was found
102          int mPos;                                     // Character position in source where Token was found
103
104        };
105
106        typedef std::vector<TokenInst> TokenInstContainer;
107        //typedef TokenInstContainer::iterator TokenInstIterator;
108
109        /// container for Tokens extracted from source
110        TokenInstContainer mTokenInstructions;
111
112        /// pointer to the source to be compiled
113        const char* mSource;
114        int mEndOfSource;
115
116        /// pointers to Text and Token Type libraries setup by subclass
117        SymbolDef* mSymbolTypeLib;
118
119        /// pointer to root rule path - has to be set by subclass constructor
120        TokenRule* mRootRulePath;
121
122        /// number of entries in Text and Token Type libraries
123        int mRulePathLibCnt;
124        int mSymbolTypeLibCnt;
125
126        /// mVauleID needs to be initialized by the subclass before compiling occurs
127        /// it defines the token ID used in the symbol type library
128        uint mValueID;
129
130
131        /// storage container for constants defined in source
132        std::vector<float> mConstants;
133
134        /// Active Contexts pattern used in pass 1 to determine which tokens are valid for a certain context
135        uint mActiveContexts;
136
137        /** check token semantics between ID1 and ID2 using left/right semantic data in Token Type library
138        @param ID1 token ID on the left
139        @param ID2 token ID on the right
140        @return
141                true if both will bind to each other
142                false if either fails the semantic bind test
143
144        */
145        //bool checkTokenSemantics(uint ID1, uint ID2);
146
147        /** perform pass 1 of compile process
148                scans source for symbols that can be tokenized and then
149                performs general semantic and context verification on each symbol before it is tokenized.
150                A tokenized instruction list is built to be used by Pass 2.
151
152        */
153        bool doPass1();
154
155        /** pure virtual method that must be set up by subclass to perform Pass 2 of compile process
156        @remark
157                Pass 2 is for the subclass to take the token instructions generated in Pass 1 and
158                build the application specific instructions along with verifying
159                symantic and context rules that could not be checked in Pass 1
160
161        */
162        virtual bool doPass2() = 0;
163
164        void findEOL();
165
166        /** get the text symbol for this token
167        @remark
168                mainly used for debugging and in test routines
169        @param sid is the token ID
170        @return a pointer to the string text
171        */
172        char* getTypeDefText(const uint sid);
173
174        /** check to see if the text at the present position in the source is a numerical constant
175        @param fvalue is a reference that will receive the float value that is in the source
176        @param charsize reference to receive number of characters that make of the value in the source
177        @return
178                true if characters form a valid float representation
179                false if a number value could not be extracted
180        */
181        bool isFloatValue(float & fvalue, int & charsize);
182
183        /** check to see if the text is in the symbol text library
184        @param symbol points to begining of text where a symbol token might exist
185        @param symbolsize reference that will receive the size value of the symbol found
186        @return
187                true if a matching token could be found in the token type library
188                false if could not be tokenized
189        */
190        bool isSymbol(const char* symbol, int & symbolsize);
191
192
193        /// position to the next possible valid sysmbol
194        bool positionToNextSymbol();
195
196
197        /** process input source text using rulepath to determine allowed tokens
198        @remarks
199                the method is reentrant and recursive
200                if a non-terminal token is encountered in the current rule path then the method is
201                called using the new rule path referenced by the non-terminal token
202                Tokens can have the following operation states which effects the flow path of the rule
203                        RULE: defines a rule path for the non-terminal token
204                        AND: the token is required for the rule to pass
205                        OR: if the previous tokens failed then try these ones
206                        OPTIONAL: the token is optional and does not cause the rule to fail if the token is not found
207                        REPEAT: the token is required but there can be more than one in a sequence
208                        END: end of the rule path - the method returns the succuss of the rule
209
210        @param rulepathIDX index into to array of Token Rules that define a rule path to be processed
211        @return
212                true if rule passed - all required tokens found
213                false if one or more tokens required to complete the rule were not found
214        */
215        bool processRulePath( uint rulepathIDX);
216
217
218        // setup ActiveContexts - should be called by subclass to setup initial language contexts
219        void setActiveContexts(const uint contexts){ mActiveContexts = contexts; }
220
221
222        /// comment specifiers are hard coded
223        void skipComments();
224
225        /// find end of line marker and move past it
226        void skipEOL();
227
228        /// skip all the white space which includes spaces and tabs
229        void skipWhiteSpace();
230
231
232        /** check if current position in source has the symbol text equivalent to the TokenID
233        @param rulepathIDX index into rule path database of token to validate
234        @param activeRuleID index of non-terminal rule that generated the token
235        @return
236                true if token was found
237                false if token symbol text does not match the source text
238                if token is non-terminal then processRulePath is called
239        */
240        bool ValidateToken(const uint rulepathIDX, const uint activeRuleID);
241
242
243public:
244        // ** these probably should not be public
245        int mCurrentLine;
246        int mCharPos;
247
248
249        /// constructor
250        Compiler2Pass();
251    virtual ~Compiler2Pass() {}
252        /** compile the source - performs 2 passes
253                first pass is to tokinize, check semantics and context
254                second pass is performed by subclass and converts tokens to application specific instructions
255        @remark
256                Pass 2 only gets executed if Pass 1 has no errors
257        @param source a pointer to the source text to be compiled
258        @return
259                true if Pass 1 and Pass 2 are successfull
260                false if any errors occur in Pass 1 or Pass 2
261        */
262        bool compile(const char* source);
263
264        /** Initialize the type library with matching symbol text found in symbol text library
265                find a default text for all Symbol Types in library
266
267                scan through all the rules and initialize TypeLib with index to text and index to rules for non-terminal tokens
268
269                must be called by subclass after libraries and rule database setup
270        */
271
272        void InitSymbolTypeLib();
273
274};
275
276#endif
277
Note: See TracBrowser for help on using the repository browser.