src/lexer.h

Sun, 19 Jan 2014 20:39:30 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 19 Jan 2014 20:39:30 +0200
changeset 85
264a61e9eba0
parent 82
841562f5a32f
child 86
43fe4be38a58
permissions
-rw-r--r--

- use the Skulltag 97c2 license which is slightly differently worded

73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
85
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
2 Copyright 2012-2014 Santeri Piippo
73
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
85
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
6 modification, are permitted provided that the following conditions
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
7 are met:
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8
85
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
9 1. Redistributions of source code must retain the above copyright
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
10 notice, this list of conditions and the following disclaimer.
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
12 notice, this list of conditions and the following disclaimer in the
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
13 documentation and/or other materials provided with the distribution.
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
14 3. The name of the author may not be used to endorse or promote products
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
15 derived from this software without specific prior written permission.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16
85
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
264a61e9eba0 - use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents: 82
diff changeset
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 */
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28
80
2ed3430fdd08 - forgot to rename these header guards when i ported the lexer from an earlier project of mine
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
29 #ifndef BOTC_LEXER_H
2ed3430fdd08 - forgot to rename these header guards when i ported the lexer from an earlier project of mine
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
30 #define BOTC_LEXER_H
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 #include "main.h"
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 #include "lexer_scanner.h"
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 class lexer
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 types:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 struct token
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 {
75
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 74
diff changeset
40 e_token type;
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 string text;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 string file;
75
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 74
diff changeset
43 int line;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 74
diff changeset
44 int column;
73
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
82
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
47 using token_list = list<token>;
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
48 using iterator = token_list::iterator;
73
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 public:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 lexer();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 ~lexer();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53
82
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
54 void process_file (string file_name);
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
55 bool get_next (e_token req = tk_any);
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
56 void must_get_next (e_token tok = tk_any);
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
57 void must_get_any_of (const list<e_token>& toks);
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
58 int get_one_symbol (const string_list& syms);
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
59 void must_be (e_token tok);
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
60 bool peek_next (token* tk = null);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61
75
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 74
diff changeset
62 inline bool has_valid_token() const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 74
diff changeset
63 {
81
071715c17296 - further lexer corrections
Teemu Piippo <crimsondusk64@gmail.com>
parents: 80
diff changeset
64 return (m_token_position < m_tokens.end() && m_token_position >= m_tokens.begin());
75
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 74
diff changeset
65 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 74
diff changeset
66
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 inline token* get_token() const
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 {
75
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 74
diff changeset
69 assert (has_valid_token() == true);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 74
diff changeset
70 return &(*m_token_position);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 }
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 inline bool is_at_end() const
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 return m_token_position == m_tokens.end();
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77
75
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 74
diff changeset
78 inline e_token get_token_type() const
73
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 return get_token()->type;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 }
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 // If @tok is given, describes the token. If not, describes @tok_type.
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 static inline string describe_token_type (e_token tok_type)
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 return describe_token_private (tok_type, null);
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
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 static inline string describe_token (token* tok)
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 {
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 return describe_token_private (tok->type, tok);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 }
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 static lexer* get_main_lexer();
74
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
95
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
96 inline void skip (int a = 1)
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
97 {
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
98 m_token_position += a;
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
99 }
007fbadfa7f9 more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
100
75
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 74
diff changeset
101 string peek_next_string (int a = 1);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 private:
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 token_list m_tokens;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 iterator m_token_position;
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 // read a mandatory token from scanner
82
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
108 void must_get_next_from_scanner (lexer_scanner& sc, e_token tt = tk_any);
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
109 void check_file_header (lexer_scanner& sc);
73
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 static string describe_token_private (e_token tok_type, token* tok);
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 };
1ee9b312dc18 - *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113
80
2ed3430fdd08 - forgot to rename these header guards when i ported the lexer from an earlier project of mine
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
114 #endif // BOTC_LEXER_H

mercurial