-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.cpp
More file actions
74 lines (61 loc) · 1.14 KB
/
lexer.cpp
File metadata and controls
74 lines (61 loc) · 1.14 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "lexer.hpp"
#include <cctype>
Lexer::Lexer(std::istream &is)
: state_{Lexer::State::OUTSIDE_TOKEN}, input_{is}, tokens_{}
{
}
Lexer::Token Lexer::get()
{
// Return the front of the tokens queue if not empty
if (tokens_.empty())
{
return next();
}
else
{
Token token = tokens_.front();
tokens_.pop_front();
return token;
}
}
Lexer::Token Lexer::peek()
{
// Check if the queue of tokens is not empty
if (tokens_.empty())
{
tokens_.push_back(next());
}
return tokens_.front();
}
Lexer::Token Lexer::next()
{
// Define eof token
auto eofToken = Token{Token::END_OF_FILE, ""};
// Clear the lexeme
lexeme_ = "";
// Skip spaces
while (!input_.eof() && isspace(input_.peek()) && state_ == State::OUTSIDE_TOKEN)
{
input_.get();
}
// Check if EOF is received
if (input_.eof())
{
return eofToken;
}
// Change the state
char c = input_.peek();
/*
switch (c)
{
case '.':
input_.get();
state_ = STATE
}
*/
// Read the input stream and check if a token has been formed
while (input_.eof() && state_ != OUTSIDE_TOKEN)
{
}
return {};
}