Mon, 03 Mar 2014 01:04:16 +0200
- reformatting... again
88 | 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_H | |
30 | #define BOTC_LEXER_H | |
31 | ||
32 | #include "Main.h" | |
33 | #include "LexerScanner.h" | |
34 | ||
35 | class Lexer | |
36 | { | |
112
def56932f938
- changed the PROPERTY macro to a simper version and brought some refactoring with it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
110
diff
changeset
|
37 | public: |
def56932f938
- changed the PROPERTY macro to a simper version and brought some refactoring with it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
110
diff
changeset
|
38 | struct TokenInfo |
88 | 39 | { |
112
def56932f938
- changed the PROPERTY macro to a simper version and brought some refactoring with it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
110
diff
changeset
|
40 | ETokenType type; |
88 | 41 | String text; |
42 | String file; | |
43 | int line; | |
44 | int column; | |
45 | }; | |
46 | ||
112
def56932f938
- changed the PROPERTY macro to a simper version and brought some refactoring with it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
110
diff
changeset
|
47 | using TokenList = List<TokenInfo>; |
88 | 48 | using Iterator = TokenList::Iterator; |
49 | ||
50 | public: | |
51 | Lexer(); | |
52 | ~Lexer(); | |
53 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
54 | void processFile (String fileName); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
55 | bool next (ETokenType req = TK_Any); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
56 | void mustGetNext (ETokenType tok); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
57 | void mustGetAnyOf (const List<ETokenType>& toks); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
58 | int getOneSymbol (const StringList& syms); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
59 | void tokenMustBe (ETokenType tok); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
60 | bool peekNext (TokenInfo* tk = null); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
61 | bool peekNextType (ETokenType req); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
62 | String peekNextString (int a = 1); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
63 | String describeCurrentPosition(); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
64 | String describeTokenPosition(); |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
65 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
66 | static Lexer* getCurrentLexer(); |
88 | 67 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
68 | inline bool hasValidToken() const |
88 | 69 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
70 | return (m_tokenPosition < m_tokens.end() && m_tokenPosition >= m_tokens.begin()); |
88 | 71 | } |
72 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
73 | inline TokenInfo* token() const |
88 | 74 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
75 | assert (hasValidToken() == true); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
76 | return &(*m_tokenPosition); |
88 | 77 | } |
78 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
79 | inline bool isAtEnd() const |
88 | 80 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
81 | return m_tokenPosition == m_tokens.end(); |
88 | 82 | } |
83 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
84 | inline ETokenType tokenType() const |
88 | 85 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
86 | return token()->type; |
88 | 87 | } |
88 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
89 | inline void skip (int a = 1) |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
90 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
91 | m_tokenPosition += a; |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
92 | } |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
93 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
94 | inline int position() |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
95 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
96 | return m_tokenPosition - m_tokens.begin(); |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
97 | } |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
98 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
99 | inline void setPosition (int pos) |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
100 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
101 | m_tokenPosition = m_tokens.begin() + pos; |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
102 | } |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
103 | |
88 | 104 | // If @tok is given, describes the token. If not, describes @tok_type. |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
105 | static inline String describeTokenType (ETokenType toktype) |
88 | 106 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
107 | return describeTokenPrivate (toktype, null); |
88 | 108 | } |
109 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
110 | static inline String describeToken (TokenInfo* tok) |
88 | 111 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
112 | return describeTokenPrivate (tok->type, tok); |
88 | 113 | } |
114 | ||
115 | private: | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
116 | TokenList m_tokens; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
117 | Iterator m_tokenPosition; |
88 | 118 | |
119 | // read a mandatory token from scanner | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
120 | void mustGetFromScanner (LexerScanner& sc, ETokenType tt =TK_Any); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
121 | void checkFileHeader (LexerScanner& sc); |
88 | 122 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
123 | static String describeTokenPrivate (ETokenType tok_type, TokenInfo* tok); |
88 | 124 | }; |
125 | ||
126 | #endif // BOTC_LEXER_H |