-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.cpp
More file actions
114 lines (96 loc) · 2.75 KB
/
token.cpp
File metadata and controls
114 lines (96 loc) · 2.75 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
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
109
110
111
112
113
114
#include "token.hpp"
using namespace std;
internalizer<i7_string>vocabulary;
// The addition constructor.
token::token(const token&left, const token&right) :
codepoint_count{left.codepoint_count + right.codepoint_count},
line_count{left.line_count + right.line_count},
text{nullptr},
only_whitespace{left.only_whitespace && right.only_whitespace},
lexical_effect{left.lexical_effect + right.lexical_effect} {}
token::token() :
codepoint_count{0},
line_count{0},
text{nullptr},
only_whitespace{true},
lexical_effect{0} {}
token::token(unsigned codepoint_count) :
codepoint_count{codepoint_count},
line_count{0},
text{nullptr},
only_whitespace{false},
lexical_effect{0} {}
token::token(const i7_string&text, bool only_whitespace, const lexer_monoid&lexical_effect, unsigned line_count) :
codepoint_count{static_cast<unsigned>(text.size())},
line_count{line_count},
text{&vocabulary.acquire(text)},
only_whitespace{only_whitespace},
lexical_effect{lexical_effect} {}
token::token(const token©) :
codepoint_count{copy.codepoint_count},
line_count{copy.line_count},
text{copy.text ? &vocabulary.acquire(*copy.text) : nullptr},
only_whitespace{copy.only_whitespace},
lexical_effect{copy.lexical_effect} {}
token::~token() {
if (text) {
vocabulary.release(*text);
}
}
token&token::operator =(const token©) {
if (© == this) {
return *this;
}
codepoint_count = copy.codepoint_count;
line_count = copy.line_count;
if (text != copy.text) {
if (text) {
vocabulary.release(*text);
}
if (copy.text) {
text = &vocabulary.acquire(*copy.text);
} else {
text = nullptr;
}
}
only_whitespace = copy.only_whitespace;
lexical_effect = copy.lexical_effect;
return *this;
}
unsigned token::get_codepoint_count() const {
return codepoint_count;
}
unsigned token::get_line_count() const {
return line_count;
}
const i7_string*token::get_text() const {
return text;
}
bool token::is_only_whitespace() const {
return only_whitespace;
}
const lexer_monoid&token::get_lexical_effect() const {
return lexical_effect;
}
bool token::operator <(const token&other) const {
return codepoint_count < other.codepoint_count;
}
token token::operator +(const token&other) const {
return token{*this, other};
}
token&token::operator +=(const token&other) {
codepoint_count += other.codepoint_count;
line_count += other.line_count;
if (text) {
vocabulary.release(*text);
}
text = nullptr;
lexical_effect += other.lexical_effect;
return *this;
}
ostream&operator <<(ostream&out, const ::token&token) {
if (token.get_text()) {
out << "`" << ASSUME_EIGHT_BIT(*token.get_text()) << "'_";
}
return out << token.get_codepoint_count() << ": " << token.get_lexical_effect();
}