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