|
1 /* |
|
2 Copyright 2012-2014 Santeri Piippo |
|
3 All rights reserved. |
|
4 |
|
5 Redistribution and use in source and binary forms, with or without |
|
6 modification, are permitted provided that the following conditions |
|
7 are met: |
|
8 |
|
9 1. Redistributions of source code must retain the above copyright |
|
10 notice, this list of conditions and the following disclaimer. |
|
11 2. Redistributions in binary form must reproduce the above copyright |
|
12 notice, this list of conditions and the following disclaimer in the |
|
13 documentation and/or other materials provided with the distribution. |
|
14 3. The name of the author may not be used to endorse or promote products |
|
15 derived from this software without specific prior written permission. |
|
16 |
|
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #ifndef BOTC_LEXER_SCANNER_H |
|
30 #define BOTC_LEXER_SCANNER_H |
|
31 |
|
32 #include <climits> |
|
33 #include "Main.h" |
|
34 |
|
35 class LexerScanner |
|
36 { |
|
37 types: |
|
38 struct PositionInfo |
|
39 { |
|
40 char* pos; |
|
41 char* line_break_pos; |
|
42 int line; |
|
43 }; |
|
44 |
|
45 // Flags for check_string() |
|
46 enum |
|
47 { |
|
48 FCheckWord = (1 << 0), // must be followed by whitespace |
|
49 FCheckPeek = (1 << 1), // don't advance cursor |
|
50 }; |
|
51 |
|
52 public: |
|
53 static inline bool IsSymbolChar (char c, bool allownumbers) |
|
54 { |
|
55 if (allownumbers && (c >= '0' && c <= '9')) |
|
56 return true; |
|
57 |
|
58 return (c >= 'a' && c <= 'z') || |
|
59 (c >= 'A' && c <= 'Z') || |
|
60 (c == '_'); |
|
61 } |
|
62 |
|
63 LexerScanner (FILE* fp); |
|
64 ~LexerScanner(); |
|
65 bool GetNextToken(); |
|
66 String ReadLine(); |
|
67 |
|
68 inline const String& GetTokenText() const |
|
69 { |
|
70 return mTokenText; |
|
71 } |
|
72 |
|
73 inline int GetLine() const |
|
74 { |
|
75 return mLine; |
|
76 } |
|
77 |
|
78 inline int GetColumn() const |
|
79 { |
|
80 return mPosition - mLineBreakPosition; |
|
81 } |
|
82 |
|
83 inline EToken GetTokenType() const |
|
84 { |
|
85 return mTokenType; |
|
86 } |
|
87 |
|
88 static String GetTokenString (EToken a); |
|
89 |
|
90 private: |
|
91 char* mData; |
|
92 char* mPosition; |
|
93 char* mLineBreakPosition; |
|
94 String mTokenText, |
|
95 mLastToken; |
|
96 EToken mTokenType; |
|
97 int mLine; |
|
98 |
|
99 bool CheckString (const char* c, int flags = 0); |
|
100 |
|
101 // Yields a copy of the current position information. |
|
102 PositionInfo GetPosition() const; |
|
103 |
|
104 // Sets the current position based on given data. |
|
105 void SetPosition (const PositionInfo& a); |
|
106 |
|
107 // Skips one character |
|
108 void Skip(); |
|
109 |
|
110 // Skips many characters |
|
111 void Skip (int chars); |
|
112 }; |
|
113 |
|
114 #endif // BOTC_LEXER_SCANNER_H |