-
Notifications
You must be signed in to change notification settings - Fork 3
/
sexpression.cc
322 lines (276 loc) · 8.55 KB
/
sexpression.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2006 Google Inc. All Rights Reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// Author: [email protected] (Phil Sung)
//
// (See description and comments in header file.)
#include <list>
#include "sexpression.h"
// ***** SExpression::const_iterator
SExpressionConstIterator::SExpressionConstIterator(const SExpression* psexp) {
CHECK(psexp->IsList());
if (psexp->IsNil()) {
done_ = true;
next_ = NULL;
} else {
done_ = false;
// Since psexp is a list and not nil, it has to be a pair
next_ = down_cast<const SExpressionPair*>(psexp);
}
}
const SExpression& SExpressionConstIterator::operator*() const {
return *(next_->car());
}
const SExpression* SExpressionConstIterator::operator->() const {
return next_->car();
}
void SExpressionConstIterator::operator++() {
if (!done_) {
if (next_->cdr()->IsPair()) {
next_ = down_cast<const SExpressionPair*>(
implicit_cast<const SExpression*>(next_->cdr()));
} else {
done_ = true;
next_ = NULL;
}
}
}
// ***** SExpression
// Returns the printed representation of the s-exp by calling
// WriteRepr on an empty string.
string SExpression::Repr() const {
string answer;
WriteRepr(&answer);
return answer;
}
SExpression* SExpression::Parse(const char* sexp) {
StringCharacterIterator char_iter(sexp);
return ParseSexp(&char_iter);
}
SExpression::const_iterator SExpression::Begin() const {
return SExpression::const_iterator(this);
}
SExpression::const_iterator SExpression::End() const {
return SExpression::const_iterator();
}
// A valid integer contains an optional +/- followed by at least one
// digit.
bool SExpression::TokenIsInteger(const char* str) {
const char* first_char = str;
// Check that there is no leading whitespace
SkipWhitespace(&str);
if (str != first_char)
return false;
// Check that the first character past the end of the int is
// \0.
return IsIntToken(str);
}
SExpression* SExpression::ParseSexp(CharacterIterator* psexp) {
SkipWhitespace(psexp);
switch (**psexp) {
case '(': return ParseList(psexp);
case '"': return ParseString(psexp);
case '|': return ParseSymbolInBars(psexp);
case '\0': return NULL;
default: return ParseUnquotedToken(psexp);
}
}
SExpression* SExpression::ParseList(CharacterIterator* psexp) {
CHECK_EQ('(', **psexp);
++(*psexp);
SkipWhitespace(psexp);
bool is_improper_list = false;
list<SExpression*> items;
while (**psexp != ')') {
// TODO(psung): Do some smarter checking for improper lists, to
// make sure that the '.' only appears in allowed places, i.e. as
// the second-to-last token, preceded by at least one other s-exp.
// True whenever the next token is not a "."
bool push_item = false;
if (**psexp == '.') {
++(*psexp);
if (ascii_isspace(**psexp)) {
is_improper_list = true;
++(*psexp);
} else {
push_item = true;
}
} else {
push_item = true;
}
if (push_item) {
// Push elements onto items as we find them.
SExpression* next_item = ParseSexp(psexp);
if (next_item == NULL) {
// The input ends before the list is complete; we need to bail
// out and delete all previously created s-exps.
while (items.size() > 0) {
SExpression* item = items.back();
items.pop_back();
delete item;
}
return NULL;
} else {
items.push_back(next_item);
}
}
SkipWhitespace(psexp);
}
++(*psexp);
// Build up cons cells for list iteratively, starting from tail.
SExpression* answer;
if (is_improper_list && items.size() >= 2) {
answer = items.back();
items.pop_back();
} else {
answer = new SExpressionNil();
}
while (items.size() > 0) {
answer = new SExpressionPair(items.back(), answer);
items.pop_back();
}
return answer;
}
SExpression* SExpression::ParseString(CharacterIterator* psexp) {
string str_value;
if (FillWithDelimitedString('"', psexp, &str_value))
return new SExpressionString(str_value);
else
return false;
}
SExpression* SExpression::ParseSymbolInBars(CharacterIterator* psexp) {
string symbol_name;
if (FillWithDelimitedString('|', psexp, &symbol_name))
return new SExpressionSymbol(symbol_name);
else
return false;
}
SExpression* SExpression::ParseUnquotedToken(CharacterIterator* psexp) {
string token;
bool has_escaped_char = false;
while (!(ascii_isspace(**psexp) || **psexp == ')' || **psexp == '\0')) {
if (**psexp == '\\') {
has_escaped_char = true;
++(*psexp);
}
token.push_back(**psexp);
++(*psexp);
}
const char* token_cstr = token.c_str();
CHECK_NE(*token_cstr, '\0');
// A token turns into a integer if it can be interpreted as such AND
// it has no escaped characters.
if (!has_escaped_char && TokenIsInteger(token_cstr))
return new SExpressionInteger(IntegerValue(token_cstr));
else if (token == "nil")
return new SExpressionNil();
else if (TokenIsAllPeriods(token_cstr))
return NULL; // Token containing all periods can't be turned into
// a symbol
else
return new SExpressionSymbol(token_cstr);
}
bool SExpression::FillWithDelimitedString(char delimiter,
CharacterIterator* psexp,
string* str) {
CHECK_EQ(delimiter, **psexp);
++(*psexp);
while (**psexp != delimiter) {
if (**psexp == '\0') // Unterminated string
return false;
if (**psexp == '\\') {
++(*psexp);
if (**psexp == '\0') // Nothing after escape char
return false;
}
str->push_back(**psexp);
++(*psexp);
}
++(*psexp);
return true;
}
int SExpression::IntegerValue(const char* str) {
int value;
int values_read = sscanf(str, "%d", &value);
CHECK_EQ(1, values_read);
return value;
}
// ***** SExpressionPair
void SExpressionPair::WriteRepr(string* str) const {
str->push_back('(');
WriteReprWithoutParens(str);
str->push_back(')');
}
// Writes the printed representation. This function correctly writes
// lists and improper lists.
void SExpressionPair::WriteReprWithoutParens(string* str) const {
car_->WriteRepr(str);
// If cdr is nil, this is the end of the list.
if (!cdr_->IsNil()) {
if (cdr_->IsPair()) {
// This is a list or improper list with at least one pair
// remaining.
str->append(" ");
down_cast<SExpressionPair*>(cdr_)->WriteReprWithoutParens(str);
} else {
// This is the last pair in an improper list.
str->append(" . ");
cdr_->WriteRepr(str);
}
}
}
// ***** SExpressionSymbol
SExpressionSymbol::SExpressionSymbol(const string& name_str)
: name_(name_str.c_str()) {
const char* name = name_str.c_str();
needs_quoting_ = false;
// Symbol can't be printed literally if:
// (1) It could be misconstrued for an integer
// (2) It contains only periods
// (3) It contains characters which are neither alphabetic nor
// acceptable punctuation (as specified by IsSymbolChar)
if (TokenIsInteger(name) || TokenIsAllPeriods(name)) {
needs_quoting_ = true;
} else {
while (*name) {
if (!IsSymbolChar(*name))
needs_quoting_ = true;
++name;
}
}
}
void SExpressionSymbol::WriteRepr(string* str) const {
if (needs_quoting_) {
const char* symbol_cstr = name_.c_str();
str->push_back('|');
while (*symbol_cstr) {
if (*symbol_cstr == '|')
str->push_back('\\');
str->push_back(*symbol_cstr);
++symbol_cstr;
}
str->push_back('|');
} else {
str->append(name_);
}
}
// We only fill in positions 0..128 but the array is static so C++
// guarantees that positions 128..255 are \0, which is acceptable.
const char SExpressionSymbol::kAsciiSymbol[256]
= "................................"
".x..xxx...xx.xxxxxxxxxxxxx..xxxx"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxx"
".xxxxxxxxxxxxxxxxxxxxxxxxxxx.xx.";