Mon, 03 Feb 2014 20:12:44 +0200
- committed work so far done on expressions
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); | |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
61 | String PeekNextString (int a = 1); |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
62 | String DescribePosition(); |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
63 | |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
64 | static Lexer* GetCurrentLexer(); |
88 | 65 | |
66 | inline bool HasValidToken() const | |
67 | { | |
68 | return (mTokenPosition < mTokens.end() && mTokenPosition >= mTokens.begin()); | |
69 | } | |
70 | ||
71 | inline Token* GetToken() const | |
72 | { | |
73 | assert (HasValidToken() == true); | |
74 | return &(*mTokenPosition); | |
75 | } | |
76 | ||
77 | inline bool IsAtEnd() const | |
78 | { | |
79 | return mTokenPosition == mTokens.end(); | |
80 | } | |
81 | ||
82 | inline EToken GetTokenType() const | |
83 | { | |
84 | return GetToken()->type; | |
85 | } | |
86 | ||
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
87 | inline void Skip (int a = 1) |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
88 | { |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
89 | mTokenPosition += a; |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
90 | } |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
91 | |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
92 | inline int GetPosition() |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
93 | { |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
94 | return mTokenPosition - mTokens.begin(); |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
95 | } |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
96 | |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
97 | inline void SetPosition (int pos) |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
98 | { |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
99 | mTokenPosition = mTokens.begin() + pos; |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
100 | } |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
101 | |
88 | 102 | // If @tok is given, describes the token. If not, describes @tok_type. |
103 | static inline String DescribeTokenType (EToken toktype) | |
104 | { | |
105 | return DescribeTokenPrivate (toktype, null); | |
106 | } | |
107 | ||
108 | static inline String DescribeToken (Token* tok) | |
109 | { | |
110 | return DescribeTokenPrivate (tok->type, tok); | |
111 | } | |
112 | ||
113 | private: | |
114 | TokenList mTokens; | |
115 | Iterator mTokenPosition; | |
116 | ||
117 | // read a mandatory token from scanner | |
118 | void MustGetFromScanner (LexerScanner& sc, EToken tt = tkAny); | |
119 | void CheckFileHeader (LexerScanner& sc); | |
120 | ||
121 | static String DescribeTokenPrivate (EToken tok_type, Token* tok); | |
122 | }; | |
123 | ||
124 | #endif // BOTC_LEXER_H |