| |
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 { |
| |
37 public: |
| |
38 struct TokenInfo |
| |
39 { |
| |
40 ETokenType type; |
| |
41 String text; |
| |
42 String file; |
| |
43 int line; |
| |
44 int column; |
| |
45 }; |
| |
46 |
| |
47 using TokenList = List<TokenInfo>; |
| |
48 using Iterator = TokenList::Iterator; |
| |
49 |
| |
50 public: |
| |
51 Lexer(); |
| |
52 ~Lexer(); |
| |
53 |
| |
54 void processFile (String fileName); |
| |
55 bool next (ETokenType req = TK_Any); |
| |
56 void mustGetNext (ETokenType tok); |
| |
57 void mustGetAnyOf (const List<ETokenType>& toks); |
| |
58 void mustGetSymbol (const String& a); |
| |
59 int getOneSymbol (const StringList& syms); |
| |
60 void tokenMustBe (ETokenType tok); |
| |
61 bool peekNext (TokenInfo* tk = null); |
| |
62 bool peekNextType (ETokenType req); |
| |
63 String peekNextString (int a = 1); |
| |
64 String describeCurrentPosition(); |
| |
65 String describeTokenPosition(); |
| |
66 |
| |
67 static Lexer* getCurrentLexer(); |
| |
68 |
| |
69 inline bool hasValidToken() const |
| |
70 { |
| |
71 return (m_tokenPosition < m_tokens.end() && m_tokenPosition >= m_tokens.begin()); |
| |
72 } |
| |
73 |
| |
74 inline TokenInfo* token() const |
| |
75 { |
| |
76 assert (hasValidToken() == true); |
| |
77 return &(*m_tokenPosition); |
| |
78 } |
| |
79 |
| |
80 inline bool isAtEnd() const |
| |
81 { |
| |
82 return m_tokenPosition == m_tokens.end(); |
| |
83 } |
| |
84 |
| |
85 inline ETokenType tokenType() const |
| |
86 { |
| |
87 return token()->type; |
| |
88 } |
| |
89 |
| |
90 inline void skip (int a = 1) |
| |
91 { |
| |
92 m_tokenPosition += a; |
| |
93 } |
| |
94 |
| |
95 inline int position() |
| |
96 { |
| |
97 return m_tokenPosition - m_tokens.begin(); |
| |
98 } |
| |
99 |
| |
100 inline void setPosition (int pos) |
| |
101 { |
| |
102 m_tokenPosition = m_tokens.begin() + pos; |
| |
103 } |
| |
104 |
| |
105 // If @tok is given, describes the token. If not, describes @tok_type. |
| |
106 static inline String describeTokenType (ETokenType toktype) |
| |
107 { |
| |
108 return describeTokenPrivate (toktype, null); |
| |
109 } |
| |
110 |
| |
111 static inline String describeToken (TokenInfo* tok) |
| |
112 { |
| |
113 return describeTokenPrivate (tok->type, tok); |
| |
114 } |
| |
115 |
| |
116 private: |
| |
117 TokenList m_tokens; |
| |
118 Iterator m_tokenPosition; |
| |
119 |
| |
120 // read a mandatory token from scanner |
| |
121 void mustGetFromScanner (LexerScanner& sc, ETokenType tt =TK_Any); |
| |
122 void checkFileHeader (LexerScanner& sc); |
| |
123 |
| |
124 static String describeTokenPrivate (ETokenType tok_type, TokenInfo* tok); |
| |
125 }; |
| |
126 |
| |
127 #endif // BOTC_LEXER_H |