-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathToken.cpp
More file actions
81 lines (67 loc) · 2.02 KB
/
Copy pathToken.cpp
File metadata and controls
81 lines (67 loc) · 2.02 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
75
76
77
78
79
80
81
#include "Token.h"
#include "FetlangManager.h"
#include "QuoteUtil.h"
Token::Token(const std::string& original_string, int line) {
this->line = line;
// Normalize original_string to this->value
this->value = "";
// Make lowercase if not a quote
if (original_string[0] != '"') {
for (char c : original_string) {
this->value += tolower(c);
}
} else {
this->value = QuoteUtil::requote(original_string);
}
this->setCategory(Token::UNSET_TOKEN);
}
Token::Token() { this->setCategory(Token::NULL_TOKEN); }
void Token::checkNull(const std::string& msg) const {
if (this->category == NULL_TOKEN) {
throw TokenException(std::string("Can't '") + msg + "' in a NULL token", *this);
}
}
void Token::setCategory(const Token::TokenCategory category) { this->category = category; }
void Token::setValue(const std::string value) { this->value = value; }
Token::TokenCategory Token::getCategory() const {
checkNull("getCategory");
return this->category;
}
std::string Token::getValue() const {
checkNull("getValue");
return this->value;
}
int Token::getLine() const { return this->line; }
bool Token::isNullToken() const { return (this->category == Token::NULL_TOKEN); }
KeyphraseCategory Token::getKeyphraseCategory() const {
checkNull("getKeyphraseCategory");
return manager.getKeyphraseCategory(this->value);
}
std::ostream& operator<<(std::ostream& out, Token const& in) {
std::string token_cat = "{{!Unknown Token!}}";
// std::string phrase_cat = "";
switch (in.category) {
case Token::NULL_TOKEN:
token_cat = "NULL_TOKEN";
break;
case Token::KEYPHRASE_TOKEN:
token_cat = "KEYPHRASE_TOKEN";
break;
case Token::FRACTION_LITERAL_TOKEN:
token_cat = "FRACTION_LITERAL_TOKEN";
break;
case Token::CHAIN_LITERAL_TOKEN:
token_cat = "CHAIN_LITERAL_TOKEN";
break;
case Token::IDENTIFIER_TOKEN:
token_cat = "IDENTIFIER_TOKEN";
break;
default:;
}
out << "{[" << in.value << "], [" << token_cat << "]}";
return out;
}
void TokenException::display() const {
displayLineAndMessage();
std::cerr << "\t" << token << "\n";
}