scanner.h
1 //------------------------------------------------------------------------------
2 // scanner.hpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; 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 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 
24 #ifndef __SCANNER_HPP__
25 #define __SCANNER_HPP__
26 
27 #include <QObject>
28 #include <QString>
29 
30 #include <cstdarg>
31 
32 #include "dptr.h"
33 #include "global.h"
34 
35 enum ETokenType
36 {
37  TK_Identifier, // Ex: SomeIdentifier
38  TK_StringConst, // Ex: "Some String"
39  TK_IntConst, // Ex: 27
40  TK_FloatConst, // Ex: 1.5
41  TK_BoolConst, // Ex: true
42  TK_AndAnd, // &&
43  TK_OrOr, // ||
44  TK_EqEq, // ==
45  TK_NotEq, // !=
46  TK_GtrEq, // >=
47  TK_LessEq, // <=
48  TK_ShiftLeft, // <<
49  TK_ShiftRight, // >>
50  TK_Increment, // ++
51  TK_Decrement, // --
52  TK_PointerMember, // ->
53  TK_ScopeResolution, // ::
54  TK_MacroConcat, // ##
55  TK_AddEq, // +=
56  TK_SubEq, // -=
57  TK_MulEq, // *=
58  TK_DivEq, // /=
59  TK_ModEq, // %=
60  TK_ShiftLeftEq, // <<=
61  TK_ShiftRightEq, // >>=
62  TK_AndEq, // &=
63  TK_OrEq, // |=
64  TK_XorEq, // ^=
65  TK_Ellipsis, // ...
66 
67  TK_NumSpecialTokens,
68 
69  TK_NoToken = -1
70 };
71 
77 class MAIN_EXPORT Scanner
78 {
79 private:
80  Scanner(const Scanner &other);
81 
82 public:
84  {
85  public:
86  ParserState();
87  virtual ~ParserState();
88 
89  const QString &str() const;
90  void setStr(const QString &v);
91 
92  unsigned int number() const;
93  void setNumber(unsigned int v);
94 
95  double decimal() const;
96  void setDecimal(double v);
97 
98  bool boolean() const;
99  void setBoolean(bool v);
100 
101  char token() const;
102  void setToken(char v);
103 
104  unsigned int tokenLine() const;
105  void setTokenLine(unsigned int v);
106 
107  unsigned int tokenLinePosition() const;
108  void setTokenLinePosition(unsigned int v);
109 
110  unsigned int scanPos() const;
111  void setScanPos(unsigned int v);
112 
113  private:
115  };
116 
117  enum MessageLevel
118  {
119  ML_ERROR,
120  ML_WARNING,
121  ML_NOTICE
122  };
123 
124  Scanner(const char *data, int length = -1);
125  virtual ~Scanner();
126 
127  void checkForMeta();
132  void checkForWhitespace();
138  bool checkToken(char token);
142  void expandState();
143  int currentLine() const;
144  int currentLinePos() const;
145  int currentPos() const;
146  unsigned int currentScanPos() const;
147  bool nextString();
152  bool nextToken(bool autoExpandState = true);
153  void mustGetToken(unsigned char token);
154  void rewind();
155  const char *scriptData() const;
156  void scriptMessage(MessageLevel level, const char *error, ...) const;
157  void setScriptIdentifier(const QString &ident);
158  int skipLine();
159  ParserState &state();
160  const ParserState &state() const;
161 
165  bool tokensLeft() const;
166 
167  const ParserState &operator*() const
168  {
169  return state();
170  }
171  const ParserState *operator->() const
172  {
173  return &state();
174  }
175 
176  static const QString &escape(QString &str);
177  static const QString &unescape(QString &str);
178 
179  static void setMessageHandler(void (*handler)(MessageLevel, const char *, va_list))
180  {
181  messageHandler = handler;
182  }
183 
184 protected:
189  void incrementLine();
190 
191 private:
192  DPtr<Scanner> d;
193 
194  static void (*messageHandler)(MessageLevel, const char *, va_list);
195 };
196 
197 #endif /* __SCANNER_HPP__ */