Wed, 12 Feb 2014 06:15:11 +0200
- refactored enums, macros split from Main.h to Macros.h
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); | |
103
48472c0678cc
- removed tkAny as the default value of Lexer::MustGetNext to prevent problems like the one last commit fixed
Teemu Piippo <crimsondusk64@gmail.com>
parents:
99
diff
changeset
|
56 | void MustGetNext (EToken tok); |
88 | 57 | void MustGetAnyOf (const List<EToken>& toks); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
58 | int GEXPRSYM_tOne (const StringList& syms); |
88 | 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); |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
103
diff
changeset
|
63 | String DescribeCurrentPosition(); |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
103
diff
changeset
|
64 | String DescribeTokenPosition(); |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
65 | |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
66 | static Lexer* GetCurrentLexer(); |
88 | 67 | |
68 | inline bool HasValidToken() const | |
69 | { | |
70 | return (mTokenPosition < mTokens.end() && mTokenPosition >= mTokens.begin()); | |
71 | } | |
72 | ||
73 | inline Token* GetToken() const | |
74 | { | |
75 | assert (HasValidToken() == true); | |
76 | return &(*mTokenPosition); | |
77 | } | |
78 | ||
79 | inline bool IsAtEnd() const | |
80 | { | |
81 | return mTokenPosition == mTokens.end(); | |
82 | } | |
83 | ||
84 | inline EToken GetTokenType() const | |
85 | { | |
86 | return GetToken()->type; | |
87 | } | |
88 | ||
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
89 | inline void Skip (int a = 1) |
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 | mTokenPosition += a; |
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 | |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
94 | inline int GetPosition() |
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 | return mTokenPosition - mTokens.begin(); |
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 | |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
99 | inline void SetPosition (int 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 | mTokenPosition = mTokens.begin() + pos; |
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. |
105 | static inline String DescribeTokenType (EToken toktype) | |
106 | { | |
107 | return DescribeTokenPrivate (toktype, null); | |
108 | } | |
109 | ||
110 | static inline String DescribeToken (Token* tok) | |
111 | { | |
112 | return DescribeTokenPrivate (tok->type, tok); | |
113 | } | |
114 | ||
115 | private: | |
116 | TokenList mTokens; | |
117 | Iterator mTokenPosition; | |
118 | ||
119 | // read a mandatory token from scanner | |
120 | void MustGetFromScanner (LexerScanner& sc, EToken tt = tkAny); | |
121 | void CheckFileHeader (LexerScanner& sc); | |
122 | ||
123 | static String DescribeTokenPrivate (EToken tok_type, Token* tok); | |
124 | }; | |
125 | ||
126 | #endif // BOTC_LEXER_H |