src/lexer_scanner.h

changeset 73
1ee9b312dc18
child 74
007fbadfa7f9
equal deleted inserted replaced
72:03e4d9db3fd9 73:1ee9b312dc18
1 /*
2 Copyright (c) 2013-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 are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 * 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
15 * Neither the name of the <organization> nor the
16 names of its contributors may be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef IRIS_SCANNER_H
32 #define IRIS_SCANNER_H
33
34 #include <climits>
35 #include "main.h"
36
37 class lexer_scanner
38 {
39 types:
40 struct position_info
41 {
42 char* pos;
43 char* line_break_pos;
44 int line;
45 };
46
47 // Flags for check_string()
48 enum
49 {
50 f_check_word = (1 << 0), // must be followed by whitespace
51 f_check_peek = (1 << 1), // don't advance cursor
52 };
53
54 public:
55 static inline bool is_symbol_char (char c)
56 {
57 return (c >= 'a' && c <= 'z') ||
58 (c >= 'A' && c <= 'Z') ||
59 (c == '_');
60 }
61
62 lexer_scanner (FILE* fp);
63 ~lexer_scanner();
64 bool get_next_token();
65
66 inline const string& get_token_text() const
67 {
68 return m_token_text;
69 }
70
71 inline int get_line() const
72 {
73 return m_line;
74 }
75
76 inline int get_column() const
77 {
78 return m_ptr - m_line_break_pos;
79 }
80
81 inline e_token get_e_token() const
82 {
83 return m_e_token;
84 }
85
86 static string get_token_string (e_token a);
87
88 private:
89 char* m_data,
90 * m_ptr,
91 * m_line_break_pos;
92 string m_token_text,
93 m_last_token;
94 e_token m_e_token;
95 int m_line;
96
97 bool check_string (const char* c, int flags = 0);
98
99 // Yields a copy of the current position information.
100 position_info get_position() const;
101
102 // Sets the current position based on given data.
103 void set_position (const position_info& a);
104 };
105
106 #endif // IRIS_SCANNER_H
107

mercurial