-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTokenStream.h
More file actions
46 lines (38 loc) · 1.28 KB
/
TokenStream.h
File metadata and controls
46 lines (38 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#pragma once
#include "Lexer.h"
/// The Lexer returns a single token. This class wraps the Lexer to provide a
/// stream-like interface to the Parser. A single token of lookahead is
/// provided (via operator*), and the token stream can be advanced using the
/// increment operator. For example, a token is typically consumed via
/// "Token token(*tokens++);" (Note that the dereference operator has higher
/// precedence than the increment operator.)
class TokenStream
{
public:
/// Construct token stream for the given source code, which must be null
/// terminated.
TokenStream( const char* source )
: m_source( source )
, m_token( kTokenEOF )
{
++*this; // Lex the first token
}
/// Inspect the next token, without advancing the token stream.
Token operator*() { return m_token; }
/// Advance the token stream, calling the Lexer to obtain the next token.
TokenStream& operator++()
{
m_token = Lexer( m_source );
return *this;
}
/// Postfix increment operator.
TokenStream operator++( int )
{
TokenStream before( *this );
this->operator++();
return before;
}
private:
const char* m_source;
Token m_token;
};