This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
003tokenize.cc
187 lines (158 loc) · 4.45 KB
/
003tokenize.cc
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//// split input into tokens separated by newlines, indent, and the following boundaries:
const string Punctuation_chars = "()#\""; // the skeleton of a Wart program
const string Quote_and_unquote_chars = "'`,@"; // controlling eval and macros
// Design considered the following:
// doing the minimum necessary to support macros later
// so backquote and unquote and splice are supported
// so infix ops and implicit gensyms are ignored
// supporting whitespace sensitivity
// preserve indent information because later passes can't recreate it
// skip indent in empty lines
// avoid modifying strings
// so parse them here and make them easy for later passes to detect
// line contains 1 indent and zero or more regular tokens
// and a newline token at the end
struct token {
string value;
long indent_level;
bool newline;
explicit token(string s)
:value(s), indent_level(-1), newline(false) {}
explicit token(long indent)
:value(""), indent_level(indent), newline(false) {}
static token Newline() {
token t(0); t.newline = true; return t; }
bool operator==(const string& x) const {
return value == x;
}
bool operator!=(const string& x) const {
return !(*this == x);
}
bool operator==(const token& x) const {
return value == x.value && indent_level == x.indent_level && newline == x.newline;
}
bool operator!=(const token& x) const {
return !(*this == x);
}
};
token next_token(indent_sensitive_stream& in) {
if (!in.at_start_of_line)
skip_whitespace(in.fd);
token maybe_indent("");
if (in.at_start_of_line)
maybe_indent = token(indent(in.fd));
if (in.fd.peek() == '#')
skip_comment(in.fd);
if (in.fd.peek() == '\n') {
in.fd.get();
in.at_start_of_line = true;
trace("tokenize") << token::Newline();
return token::Newline();
}
if (in.at_start_of_line) {
// still here? no comment or newline?
in.at_start_of_line = false;
trace("tokenize") << maybe_indent;
return maybe_indent;
}
ostringstream out;
if (in.fd.peek() == '"')
slurp_string(in.fd, out);
else if (find(Punctuation_chars, in.fd.peek()))
slurp_char(in.fd, out);
else if (in.fd.peek() == ',')
slurp_unquote(in.fd, out);
else if (find(Quote_and_unquote_chars, in.fd.peek())) {
slurp_char(in.fd, out);
if (isspace(in.fd.peek()) || in.fd.peek() == ')') {
if (Interactive)
RAISE << "You can't put strings in single-quotes\n";
else {
cell* context = peek_next_atom(in.fd);
RAISE << "You can't put strings in single-quotes: '" << context << '\n';
rmref(context);
}
}
}
else
slurp_word(in.fd, out);
if (out.str() == ":") {
trace("tokenize") << "skip comment token";
return next_token(in);
}
trace("tokenize") << out.str();
return token(out.str());
}
void slurp_unquote(istream& in, ostream& out) {
slurp_char(in, out); // comma
if (in.peek() == '@')
slurp_char(in, out);
}
//// internals
// slurp functions read a token when you're sure to be at it
void slurp_char(istream& in, ostream& out) {
out << (char)in.get();
}
void slurp_word(istream& in, ostream& out) {
char c;
while (in >> c) {
if (isspace(c) || find(Punctuation_chars, c) || find(Quote_and_unquote_chars, c)) {
in.putback(c);
break;
}
out << c;
}
}
void slurp_string(istream& in, ostream& out) {
slurp_char(in, out); // initial quote
char c;
while (in >> c) {
out << c;
if (c == '\\')
slurp_char(in, out); // blindly read next
else if (c == '"')
break;
}
}
long indent(istream& in) {
long indent = 0;
char c;
while (in >> c) {
if (!isspace(c) || c == '\n') {
in.putback(c);
break;
}
else if (c == ' ') ++indent;
else if (c == '\t') indent+=2;
}
return indent;
}
void skip_comment(istream& in) {
char c;
while (in >> c) {
if (c == '\n') {
in.putback(c);
break;
}
}
}
void skip_whitespace(istream& in) {
while (isspace(in.peek()) && in.peek() != '\n')
in.get();
}
cell* peek_next_atom(istream& in) { // should always undo changes to 'in'
std::streampos curr = in.tellg();
indent_sensitive_stream dummy(in);
cell* result = read(dummy);
in.seekg(curr);
in.clear();
return result;
}
bool find(string s, char c) {
return s.find(c) != NOT_FOUND;
}
ostream& operator<<(ostream& os, token y) {
if (y.newline) return os << "\\n";
if (y == "") return os << ":" << y.indent_level;
else return os << y.value;
}