scanner.h
1 //------------------------------------------------------------------------------
2 // scanner.hpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Blzut3" <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 
24 #ifndef __SCANNER_HPP__
25 #define __SCANNER_HPP__
26 
27 #include <QObject>
28 #include <QString>
29 
30 #include "dptr.h"
31 #include "global.h"
32 
33 enum ETokenType
34 {
35  TK_Identifier, // Ex: SomeIdentifier
36  TK_StringConst, // Ex: "Some String"
37  TK_IntConst, // Ex: 27
38  TK_FloatConst, // Ex: 1.5
39  TK_BoolConst, // Ex: true
40  TK_AndAnd, // &&
41  TK_OrOr, // ||
42  TK_EqEq, // ==
43  TK_NotEq, // !=
44  TK_GtrEq, // >=
45  TK_LessEq, // <=
46  TK_ShiftLeft, // <<
47  TK_ShiftRight, // >>
48  TK_Increment, // ++
49  TK_Decrement, // --
50  TK_PointerMember, // ->
51  TK_ScopeResolution, // ::
52  TK_MacroConcat, // ##
53  TK_AddEq, // +=
54  TK_SubEq, // -=
55  TK_MulEq, // *=
56  TK_DivEq, // /=
57  TK_ModEq, // %=
58  TK_ShiftLeftEq, // <<=
59  TK_ShiftRightEq, // >>=
60  TK_AndEq, // &=
61  TK_OrEq, // |=
62  TK_XorEq, // ^=
63  TK_Ellipsis, // ...
64 
65  TK_NumSpecialTokens,
66 
67  TK_NoToken = -1
68 };
69 
75 class MAIN_EXPORT Scanner
76 {
77  private:
78  Scanner(const Scanner &other);
79 
80  public:
82  {
83  public:
84  ParserState();
85  virtual ~ParserState();
86 
87  const QString& str() const;
88  void setStr(const QString& v);
89 
90  unsigned int number() const;
91  void setNumber(unsigned int v);
92 
93  double decimal() const;
94  void setDecimal(double v);
95 
96  bool boolean() const;
97  void setBoolean(bool v);
98 
99  char token() const;
100  void setToken(char v);
101 
102  unsigned int tokenLine() const;
103  void setTokenLine(unsigned int v);
104 
105  unsigned int tokenLinePosition() const;
106  void setTokenLinePosition(unsigned int v);
107 
108  unsigned int scanPos() const;
109  void setScanPos(unsigned int v);
110 
111  private:
113  };
114 
115  enum MessageLevel
116  {
117  ML_ERROR,
118  ML_WARNING,
119  ML_NOTICE
120  };
121 
122  Scanner(const char* data, int length=-1);
123  virtual ~Scanner();
124 
125  void checkForMeta();
130  void checkForWhitespace();
136  bool checkToken(char token);
140  void expandState();
141  int currentLine() const;
142  int currentLinePos() const;
143  int currentPos() const;
144  unsigned int currentScanPos() const;
145  bool nextString();
150  bool nextToken(bool autoExpandState=true);
151  void mustGetToken(char token);
152  void rewind();
153  const char* scriptData() const;
154  void scriptMessage(MessageLevel level, const char* error, ...) const;
155  void setScriptIdentifier(const QString &ident);
156  int skipLine();
157  ParserState& state();
158  const ParserState& state() const;
159 
163  bool tokensLeft() const;
164 
165  const ParserState &operator*() const { return state(); }
166  const ParserState *operator->() const { return &state(); }
167 
168  static const QString& escape(QString &str);
169  static const QString& unescape(QString &str);
170 
171  static void setMessageHandler(void (*handler)(MessageLevel, const char*, va_list)) { messageHandler = handler; }
172 
173  protected:
178  void incrementLine();
179 
180  private:
181  DPtr<Scanner> d;
182 
183  static void (*messageHandler)(MessageLevel, const char*, va_list);
184 };
185 
186 #endif /* __SCANNER_HPP__ */
Scanner reads scripts by checking individual tokens.
Definition: scanner.h:75
Definition: dptr.h:54