Debian Codegraph @

prboom2/src/scanner.h

raw
1 // Copyright (c) 2010, Braden "Blzut3" Obrzut <[email protected]>
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the <organization> nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 #ifndef __SCANNER_H__
27 #define __SCANNER_H__
28
29 #include <cstdlib>
30
31 enum
32 {
33 TK_Identifier, // Ex: SomeIdentifier
34 TK_StringConst, // Ex: "Some String"
35 TK_IntConst, // Ex: 27
36 TK_FloatConst, // Ex: 1.5
37 TK_BoolConst, // Ex: true
38 TK_AndAnd, // &&
39 TK_OrOr, // ||
40 TK_EqEq, // ==
41 TK_NotEq, // !=
42 TK_GtrEq, // >=
43 TK_LessEq, // <=
44 TK_ShiftLeft, // <<
45 TK_ShiftRight, // >>
46
47 TK_NumSpecialTokens,
48
49 TK_NoToken = -1
50 };
51
52 struct ParserState
53 {
54 char *string;
55 int number;
56 double decimal;
57 bool boolean;
58 char token;
59 unsigned int tokenLine;
60 unsigned int tokenLinePosition;
61
62 ParserState()
63 {
64 string = NULL;
65 }
66 ~ParserState()
67 {
68 if (string != NULL) free(string);
69 }
70 };
71
72 class Scanner
73 {
74 public:
75 Scanner(const char* data, int length=-1);
76 ~Scanner();
77
78 void SetString(char **ptr, const char *src, unsigned int length);
79 void CheckForWhitespace();
80 bool CheckToken(char token);
81 bool CheckInteger();
82 bool CheckFloat();
83 bool CheckString();
84 bool StringMatch(const char *target);
85 void MustGetInteger();
86 void MustGetFloat();
87 void MustGetString();
88 void ExpandState();
89 void SkipLine();
90 int GetLine() const { return tokenLine; }
91 int GetLinePos() const { return tokenLinePosition; }
92 bool GetNextToken(bool expandState=true);
93 void MustGetToken(char token);
94 void MustGetIdentifier(const char *ident);
95 bool TokensLeft() const;
96 void Error(int token);
97 void Error(const char *mustget);
98 void ErrorF(const char *msg, ...);
99 void Unget() { needNext = true; }
100 static void SetErrorCallback(void (*cb)(const char*, ...)) { error = cb; }
101
102 static void Unescape(char *str);
103
104 static const char* const TokenNames[TK_NumSpecialTokens];
105
106 char *string;
107 int number;
108 double decimal;
109 bool boolean;
110 char token;
111
112 protected:
113 Scanner()
114 {
115 data = NULL;
116 string = NULL;
117 nextState.string = NULL;
118 }
119
120 void IncrementLine();
121 void SaveState(Scanner &saved);
122 void RestoreState(Scanner &savd);
123 bool ScanInteger();
124 bool ScanFloat();
125
126 private:
127 ParserState nextState;
128
129 char* data;
130 unsigned int length;
131
132 unsigned int line;
133 unsigned int lineStart;
134 unsigned int logicalPosition;
135 unsigned int tokenLine;
136 unsigned int tokenLinePosition;
137 unsigned int scanPos;
138
139 bool needNext; // If checkToken returns false this will be false.
140
141 static void (*error)(const char* message, ...);
142 };
143
144 #endif /* __SCANNER_H__ */