Doomseeker
   
  • Doomseeker
  • Wadseeker
  • Download
  • Tracker
  • Git
  • Docs
  • Main Page
  • Classes
  • Files
  • File List
  • File Members

src/core/scanner.h

00001 //------------------------------------------------------------------------------
00002 // scanner.hpp
00003 //------------------------------------------------------------------------------
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00018 // 02110-1301, USA.
00019 //
00020 //------------------------------------------------------------------------------
00021 // Copyright (C) 2010 "Blzut3" <admin@maniacsvault.net>
00022 //------------------------------------------------------------------------------
00023 
00024 #ifndef __SCANNER_HPP__
00025 #define __SCANNER_HPP__
00026 
00027 #include <QObject>
00028 #include <QString>
00029 
00030 #include "global.h"
00031 
00032 enum ETokenType
00033 {
00034         TK_Identifier,          // Ex: SomeIdentifier
00035         TK_StringConst,         // Ex: "Some String"
00036         TK_IntConst,            // Ex: 27
00037         TK_FloatConst,          // Ex: 1.5
00038         TK_BoolConst,           // Ex: true
00039         TK_AndAnd,                      // &&
00040         TK_OrOr,                        // ||
00041         TK_EqEq,                        // ==
00042         TK_NotEq,                       // !=
00043         TK_GtrEq,                       // >=
00044         TK_LessEq,                      // <=
00045         TK_ShiftLeft,           // <<
00046         TK_ShiftRight,          // >>
00047         TK_Increment,           // ++
00048         TK_Decrement,           // --
00049         TK_PointerMember,       // ->
00050         TK_ScopeResolution,     // ::
00051         TK_MacroConcat,         // ##
00052         TK_AddEq,                       // +=
00053         TK_SubEq,                       // -=
00054         TK_MulEq,                       // *=
00055         TK_DivEq,                       // /=
00056         TK_ModEq,                       // %=
00057         TK_ShiftLeftEq,         // <<=
00058         TK_ShiftRightEq,        // >>=
00059         TK_AndEq,                       // &=
00060         TK_OrEq,                        // |=
00061         TK_XorEq,                       // ^=
00062         TK_Ellipsis,            // ...
00063 
00064         TK_NumSpecialTokens,
00065 
00066         TK_NoToken = -1
00067 };
00068 
00073 class MAIN_EXPORT Scanner
00074 {
00075         public:
00076                 struct ParserState
00077                 {
00078                         QString                 str;
00079                         unsigned int    number;
00080                         double                  decimal;
00081                         bool                    boolean;
00082                         char                    token;
00083                         unsigned int    tokenLine;
00084                         unsigned int    tokenLinePosition;
00085                         unsigned int    scanPos;
00086                 };
00087 
00088                 enum MessageLevel
00089                 {
00090                         ML_ERROR,
00091                         ML_WARNING,
00092                         ML_NOTICE
00093                 };
00094 
00095                 Scanner(const char* data, int length=-1);
00096                 ~Scanner();
00097 
00098                 void                    checkForMeta();
00103                 void                    checkForWhitespace();
00109                 bool                    checkToken(char token);
00113                 void                    expandState();
00114                 int                             currentLine() const { return state.tokenLine; }
00115                 int                             currentLinePos() const { return state.tokenLinePosition; }
00116                 int                             currentPos() const { return logicalPosition; }
00117                 unsigned int    currentScanPos() const { return scanPos; }
00118                 bool                    nextString();
00123                 bool                    nextToken(bool autoExpandState=true);
00124                 void                    mustGetToken(char token);
00125                 void                    rewind(); 
00126                 const char*             scriptData() const { return data; }
00127                 void                    scriptMessage(MessageLevel level, const char* error, ...) const;
00128                 void                    setScriptIdentifier(const QString &ident) { scriptIdentifier = ident; }
00129                 int                             skipLine();
00130 
00134                 bool                    tokensLeft() { return scanPos < length; }
00135 
00136                 const ParserState &operator*() const { return state; }
00137                 const ParserState *operator->() const { return &state; }
00138 
00139                 static const QString& escape(QString &str);
00140                 static const QString& unescape(QString &str);
00141 
00142                 static void             setMessageHandler(void (*handler)(MessageLevel, const char*, va_list)) { messageHandler = handler; }
00143 
00144                 ParserState             state;
00145 
00146         protected:
00151                 void            incrementLine();
00152 
00153         private:
00154                 ParserState             nextState, prevState;
00155 
00156                 char*                   data;
00157                 unsigned int    length;
00158 
00159                 unsigned int    line;
00160                 unsigned int    lineStart;
00161                 unsigned int    logicalPosition;
00162                 unsigned int    scanPos;
00163 
00164                 bool                    needNext; // If checkToken returns false this will be false.
00165 
00166                 QString                 scriptIdentifier;
00167 
00168                 static void             (*messageHandler)(MessageLevel, const char*, va_list);
00169 };
00170 
00171 #endif /* __SCANNER_HPP__ */
   
Doomseeker © 2009-2024 The Doomseeker Team