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