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 public: |
|
38 struct PositionInfo |
|
39 { |
|
40 char* pos; |
|
41 int line; |
|
42 }; |
|
43 |
|
44 // Flags for check_string() |
|
45 enum |
|
46 { |
|
47 FCheckWord = (1 << 0), // must be followed by whitespace |
|
48 FCheckPeek = (1 << 1), // don't advance cursor |
|
49 }; |
|
50 |
|
51 static inline bool isSymbolChar (char c, bool allownumbers) |
|
52 { |
|
53 if (allownumbers && (c >= '0' && c <= '9')) |
|
54 return true; |
|
55 |
|
56 return (c >= 'a' && c <= 'z') || |
|
57 (c >= 'A' && c <= 'Z') || |
|
58 (c == '_'); |
|
59 } |
|
60 |
|
61 LexerScanner (FILE* fp); |
|
62 ~LexerScanner(); |
|
63 bool getNextToken(); |
|
64 String readLine(); |
|
65 |
|
66 inline const String& getTokenText() const |
|
67 { |
|
68 return m_tokenText; |
|
69 } |
|
70 |
|
71 inline int getLine() const |
|
72 { |
|
73 return m_line; |
|
74 } |
|
75 |
|
76 inline int getColumn() const |
|
77 { |
|
78 return m_position - m_lineBreakPosition; |
|
79 } |
|
80 |
|
81 inline ETokenType getTokenType() const |
|
82 { |
|
83 return m_tokenType; |
|
84 } |
|
85 |
|
86 static String getTokenString (ETokenType a); |
|
87 |
|
88 private: |
|
89 char* m_data; |
|
90 char* m_position; |
|
91 char* m_lineBreakPosition; |
|
92 String m_tokenText, |
|
93 m_lastToken; |
|
94 ETokenType m_tokenType; |
|
95 int m_line; |
|
96 |
|
97 bool checkString (const char* c, int flags = 0); |
|
98 |
|
99 // Yields a copy of the current position information. |
|
100 PositionInfo getPosition() const; |
|
101 |
|
102 // Sets the current position based on given data. |
|
103 void setPosition (const PositionInfo& a); |
|
104 |
|
105 // Skips one character |
|
106 void skip(); |
|
107 |
|
108 // Skips many characters |
|
109 void skip (int chars); |
|
110 }; |
|
111 |
|
112 #endif // BOTC_LEXER_SCANNER_H |
|