Sun, 02 Feb 2014 17:06:39 +0200
- reformatting
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 | { | |
37 | types: | |
38 | struct Token | |
39 | { | |
40 | EToken type; | |
41 | String text; | |
42 | String file; | |
43 | int line; | |
44 | int column; | |
45 | }; | |
46 | ||
47 | using TokenList = List<Token>; | |
48 | using Iterator = TokenList::Iterator; | |
49 | ||
50 | public: | |
51 | Lexer(); | |
52 | ~Lexer(); | |
53 | ||
54 | void ProcessFile (String file_name); | |
55 | bool GetNext (EToken req = tkAny); | |
56 | void MustGetNext (EToken tok = tkAny); | |
57 | void MustGetAnyOf (const List<EToken>& toks); | |
58 | int GetOneSymbol (const StringList& syms); | |
59 | void TokenMustBe (EToken tok); | |
60 | bool PeekNext (Token* tk = null); | |
61 | ||
62 | inline bool HasValidToken() const | |
63 | { | |
64 | return (mTokenPosition < mTokens.end() && mTokenPosition >= mTokens.begin()); | |
65 | } | |
66 | ||
67 | inline Token* GetToken() const | |
68 | { | |
69 | assert (HasValidToken() == true); | |
70 | return &(*mTokenPosition); | |
71 | } | |
72 | ||
73 | inline bool IsAtEnd() const | |
74 | { | |
75 | return mTokenPosition == mTokens.end(); | |
76 | } | |
77 | ||
78 | inline EToken GetTokenType() const | |
79 | { | |
80 | return GetToken()->type; | |
81 | } | |
82 | ||
83 | // If @tok is given, describes the token. If not, describes @tok_type. | |
84 | static inline String DescribeTokenType (EToken toktype) | |
85 | { | |
86 | return DescribeTokenPrivate (toktype, null); | |
87 | } | |
88 | ||
89 | static inline String DescribeToken (Token* tok) | |
90 | { | |
91 | return DescribeTokenPrivate (tok->type, tok); | |
92 | } | |
93 | ||
94 | static Lexer* GetCurrentLexer(); | |
95 | ||
96 | inline void Skip (int a = 1) | |
97 | { | |
98 | mTokenPosition += a; | |
99 | } | |
100 | ||
101 | String PeekNextString (int a = 1); | |
102 | ||
103 | private: | |
104 | TokenList mTokens; | |
105 | Iterator mTokenPosition; | |
106 | ||
107 | // read a mandatory token from scanner | |
108 | void MustGetFromScanner (LexerScanner& sc, EToken tt = tkAny); | |
109 | void CheckFileHeader (LexerScanner& sc); | |
110 | ||
111 | static String DescribeTokenPrivate (EToken tok_type, Token* tok); | |
112 | }; | |
113 | ||
114 | #endif // BOTC_LEXER_H |