src/lexer_scanner.h

Sat, 11 Jan 2014 22:36:31 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 11 Jan 2014 22:36:31 +0200
changeset 73
1ee9b312dc18
child 74
007fbadfa7f9
permissions
-rw-r--r--

- *hot stuff coming through*

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

mercurial