32 #include "Main.h" |
32 #include "Main.h" |
33 #include "LexerScanner.h" |
33 #include "LexerScanner.h" |
34 |
34 |
35 class Lexer |
35 class Lexer |
36 { |
36 { |
37 types: |
37 public: |
38 struct Token |
38 struct TokenInfo |
39 { |
39 { |
40 TokenType type; |
40 ETokenType type; |
41 String text; |
41 String text; |
42 String file; |
42 String file; |
43 int line; |
43 int line; |
44 int column; |
44 int column; |
45 }; |
45 }; |
46 |
46 |
47 using TokenList = List<Token>; |
47 using TokenList = List<TokenInfo>; |
48 using Iterator = TokenList::Iterator; |
48 using Iterator = TokenList::Iterator; |
49 |
49 |
50 public: |
50 public: |
51 Lexer(); |
51 Lexer(); |
52 ~Lexer(); |
52 ~Lexer(); |
53 |
53 |
54 void ProcessFile (String file_name); |
54 void ProcessFile (String file_name); |
55 bool GetNext (TokenType req = TK_Any); |
55 bool Next (ETokenType req = TK_Any); |
56 void MustGetNext (TokenType tok); |
56 void MustGetNext (ETokenType tok); |
57 void MustGetAnyOf (const List<TokenType>& toks); |
57 void MustGetAnyOf (const List<ETokenType>& toks); |
58 int GetOneSymbol (const StringList& syms); |
58 int GetOneSymbol (const StringList& syms); |
59 void TokenMustBe (TokenType tok); |
59 void TokenMustBe (ETokenType tok); |
60 bool PeekNext (Token* tk = null); |
60 bool PeekNext (TokenInfo* tk = null); |
61 bool PeekNextType (TokenType req); |
61 bool PeekNextType (ETokenType req); |
62 String PeekNextString (int a = 1); |
62 String PeekNextString (int a = 1); |
63 String DescribeCurrentPosition(); |
63 String DescribeCurrentPosition(); |
64 String DescribeTokenPosition(); |
64 String DescribeTokenPosition(); |
65 |
65 |
66 static Lexer* GetCurrentLexer(); |
66 static Lexer* GetCurrentLexer(); |
68 inline bool HasValidToken() const |
68 inline bool HasValidToken() const |
69 { |
69 { |
70 return (mTokenPosition < mTokens.end() && mTokenPosition >= mTokens.begin()); |
70 return (mTokenPosition < mTokens.end() && mTokenPosition >= mTokens.begin()); |
71 } |
71 } |
72 |
72 |
73 inline Token* GetToken() const |
73 inline TokenInfo* Token() const |
74 { |
74 { |
75 assert (HasValidToken() == true); |
75 assert (HasValidToken() == true); |
76 return &(*mTokenPosition); |
76 return &(*mTokenPosition); |
77 } |
77 } |
78 |
78 |
79 inline bool IsAtEnd() const |
79 inline bool IsAtEnd() const |
80 { |
80 { |
81 return mTokenPosition == mTokens.end(); |
81 return mTokenPosition == mTokens.end(); |
82 } |
82 } |
83 |
83 |
84 inline TokenType GetTokenType() const |
84 inline ETokenType TokenType() const |
85 { |
85 { |
86 return GetToken()->type; |
86 return Token()->type; |
87 } |
87 } |
88 |
88 |
89 inline void Skip (int a = 1) |
89 inline void Skip (int a = 1) |
90 { |
90 { |
91 mTokenPosition += a; |
91 mTokenPosition += a; |
92 } |
92 } |
93 |
93 |
94 inline int GetPosition() |
94 inline int Position() |
95 { |
95 { |
96 return mTokenPosition - mTokens.begin(); |
96 return mTokenPosition - mTokens.begin(); |
97 } |
97 } |
98 |
98 |
99 inline void SetPosition (int pos) |
99 inline void SetPosition (int pos) |
100 { |
100 { |
101 mTokenPosition = mTokens.begin() + pos; |
101 mTokenPosition = mTokens.begin() + pos; |
102 } |
102 } |
103 |
103 |
104 // If @tok is given, describes the token. If not, describes @tok_type. |
104 // If @tok is given, describes the token. If not, describes @tok_type. |
105 static inline String DescribeTokenType (TokenType toktype) |
105 static inline String DescribeTokenType (ETokenType toktype) |
106 { |
106 { |
107 return DescribeTokenPrivate (toktype, null); |
107 return DescribeTokenPrivate (toktype, null); |
108 } |
108 } |
109 |
109 |
110 static inline String DescribeToken (Token* tok) |
110 static inline String DescribeToken (TokenInfo* tok) |
111 { |
111 { |
112 return DescribeTokenPrivate (tok->type, tok); |
112 return DescribeTokenPrivate (tok->type, tok); |
113 } |
113 } |
114 |
114 |
115 private: |
115 private: |
116 TokenList mTokens; |
116 TokenList mTokens; |
117 Iterator mTokenPosition; |
117 Iterator mTokenPosition; |
118 |
118 |
119 // read a mandatory token from scanner |
119 // read a mandatory token from scanner |
120 void MustGetFromScanner (LexerScanner& sc, TokenType tt =TK_Any); |
120 void MustGetFromScanner (LexerScanner& sc, ETokenType tt =TK_Any); |
121 void CheckFileHeader (LexerScanner& sc); |
121 void CheckFileHeader (LexerScanner& sc); |
122 |
122 |
123 static String DescribeTokenPrivate (TokenType tok_type, Token* tok); |
123 static String DescribeTokenPrivate (ETokenType tok_type, TokenInfo* tok); |
124 }; |
124 }; |
125 |
125 |
126 #endif // BOTC_LEXER_H |
126 #endif // BOTC_LEXER_H |