-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.cpp
108 lines (94 loc) · 2.46 KB
/
token.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "token.hpp"
// system includes
#include <cctype>
#include <iostream>
// define constants for special characters
const char OPENCHAR = '(';
const char CLOSECHAR = ')';
const char COMMENTCHAR = ';';
const char QUOTATIONCHAR = '"';
Token::Token(TokenType t): m_type(t){}
Token::Token(const std::string & str): m_type(STRING), value(str) {}
Token::TokenType Token::type() const{
return m_type;
}
std::string Token::asString() const{
switch(m_type){
case OPEN:
return "(";
case CLOSE:
return ")";
case STRING:
return value;
case OPENQUOTATION:
return "\"";
case CLOSEQUOTATION:
return "\"";
}
return "";
}
// add token to sequence unless it is empty, clears token
void store_ifnot_empty(std::string & token, TokenSequenceType & seq){
if(!token.empty()){
// .emplace_back() is similar to push back. But emplace and put thing on top or back. More advance for string for vector
seq.emplace_back(token);
token.clear();
}
}
TokenSequenceType tokenize(std::istream & seq){
TokenSequenceType tokens;
std::string token;
bool openquotation = false;
while (true) {
char c = seq.get();
if (seq.eof()) break;
if (openquotation == true && c == QUOTATIONCHAR)
{
store_ifnot_empty(token, tokens);
tokens.push_back(Token::TokenType::CLOSEQUOTATION);
openquotation = false;
}
else if (openquotation == true)
{
token.push_back(c);
}
else
{
if (c == COMMENTCHAR) {
// chomp until the end of the line
while ((!seq.eof()) && (c != '\n')) {
c = seq.get();
}
if (seq.eof()) break;
}
else if (c == OPENCHAR) {
store_ifnot_empty(token, tokens);
tokens.push_back(Token::TokenType::OPEN);
}
else if (c == CLOSECHAR) {
store_ifnot_empty(token, tokens);
tokens.push_back(Token::TokenType::CLOSE);
}
else if (c == QUOTATIONCHAR) {
store_ifnot_empty(token, tokens);
if (openquotation == false) {
tokens.push_back(Token::TokenType::OPENQUOTATION);
openquotation = true;
}
}
// ispace is to check if there is a whitespace character
// if there is a space, clear out that space
else if (isspace(c)) {
store_ifnot_empty(token, tokens);
}
else {
token.push_back(c);
}
}
}
store_ifnot_empty(token, tokens);
return tokens;
}
// emplace vs push back
//vector <foo> name. assume full is a class.
// emplace back will automaticly create foo then pass in argument inside foo (short cut)