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 lexer_scanner |
|
36 { |
|
37 types: |
|
38 struct position_info |
|
39 { |
|
40 char* pos; |
|
41 char* line_break_pos; |
|
42 int line; |
|
43 }; |
|
44 |
|
45 // Flags for check_string() |
|
46 enum |
|
47 { |
|
48 f_check_word = (1 << 0), // must be followed by whitespace |
|
49 f_check_peek = (1 << 1), // don't advance cursor |
|
50 }; |
|
51 |
|
52 public: |
|
53 static inline bool is_symbol_char (char c, bool allow_numbers) |
|
54 { |
|
55 if (allow_numbers && (c >= '0' && c <= '9')) |
|
56 return true; |
|
57 |
|
58 return (c >= 'a' && c <= 'z') || |
|
59 (c >= 'A' && c <= 'Z') || |
|
60 (c == '_'); |
|
61 } |
|
62 |
|
63 lexer_scanner (FILE* fp); |
|
64 ~lexer_scanner(); |
|
65 bool get_next_token(); |
|
66 string read_line(); |
|
67 |
|
68 inline const string& get_token_text() const |
|
69 { |
|
70 return m_token_text; |
|
71 } |
|
72 |
|
73 inline int get_line() const |
|
74 { |
|
75 return m_line; |
|
76 } |
|
77 |
|
78 inline int get_column() const |
|
79 { |
|
80 return m_ptr - m_line_break_pos; |
|
81 } |
|
82 |
|
83 inline e_token get_token_type() const |
|
84 { |
|
85 return m_token_type; |
|
86 } |
|
87 |
|
88 static string get_token_string (e_token a); |
|
89 |
|
90 private: |
|
91 char* m_data; |
|
92 char* m_ptr; |
|
93 char* m_line_break_pos; |
|
94 string m_token_text, |
|
95 m_last_token; |
|
96 e_token m_token_type; |
|
97 int m_line; |
|
98 |
|
99 bool check_string (const char* c, int flags = 0); |
|
100 |
|
101 // Yields a copy of the current position information. |
|
102 position_info get_position() const; |
|
103 |
|
104 // Sets the current position based on given data. |
|
105 void set_position (const position_info& 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 |
|