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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
// file : bpkg/manifest-parser.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#include <bpkg/manifest-parser>
#include <cassert>
#include <sstream>
using namespace std;
namespace bpkg
{
using parsing = manifest_parsing;
using name_value = manifest_parser::name_value_type;
name_value manifest_parser::
next ()
{
if (s_ == end)
return name_value {"", "", l_, c_, l_, c_};
xchar c (skip_spaces ());
// Here is the problem: if we are in the 'body' state (that is,
// we are parsing inside the manifest) and we see the special
// empty name, then before returning the "start" pair for the
// next manifest, we have to return the "end" pair. One way
// would be to cache the "start" pair and return it on the
// next call of next(). But that would require quite a bit
// of extra logic. The alternative is to detect the beginning
// of the empty name before parsing too far. This way, the
// next call to next() will start parsing where we left of
// and return the "start" pair naturally.
//
if (s_ == body && c == ':')
{
s_ = start;
uint64_t ln (c.line ()), cn (c.column ());
return name_value {"", "", ln, cn, ln, cn};
}
// Regardless of the state, what should come next is a name,
// potentially the special empty one.
//
name_value r;
parse_name (r);
skip_spaces ();
c = get ();
if (is_eos (c))
{
// This is ok as long as the name is empty.
//
if (!r.name.empty ())
throw parsing (name_, c.line (), c.column (),
"':' expected after name");
s_ = end;
// The "end" pair.
//
r.value_line = r.name_line;
r.value_column = r.name_column;
return r;
}
if (c != ':')
throw parsing (name_, c.line (), c.column (), "':' expected after name");
skip_spaces ();
parse_value (r);
c = peek ();
// The character after the value should be either a newline or eos.
//
assert (c == '\n' || is_eos (c));
if (c == '\n')
get ();
// Now figure out whether what we've got makes sense, depending
// on the state we are in.
//
if (s_ == start)
{
// Start of the (next) manifest. The first pair should be the
// special empty name/format version.
//
if (!r.name.empty ())
throw parsing (name_, r.name_line, r.name_column,
"format version pair expected");
// The version value is only mandatory for the first manifest in
// a sequence.
//
if (r.value.empty ())
{
if (version_.empty ())
throw parsing (name_, r.value_line, r.value_column,
"format version value expected");
r.value = version_;
}
else
{
version_ = r.value; // Update with the latest.
if (version_ != "1")
throw parsing (name_, r.value_line, r.value_column,
"unsupported format version " + version_);
}
s_ = body;
}
else
{
// Parsing the body of the manifest.
//
// Should have been handled by the special case above.
//
assert (!r.name.empty ());
}
return r;
}
void manifest_parser::
parse_name (name_value& r)
{
xchar c (peek ());
r.name_line = c.line ();
r.name_column = c.column ();
for (; !is_eos (c); c = peek ())
{
if (c == ':' || c == ' ' || c == '\t' || c == '\n')
break;
r.name += c;
get ();
}
}
void manifest_parser::
parse_value (name_value& r)
{
xchar c (peek ());
r.value_line = c.line ();
r.value_column = c.column ();
string& v (r.value);
string::size_type n (0); // Size of last non-space character (simpel mode).
// Detect the multi-line mode introductor.
//
bool ml (false);
if (c == '\\')
{
get ();
xchar p (peek ());
if (p == '\n')
{
get (); // Newline is not part of the value so skip it.
c = peek ();
ml = true;
}
else if (is_eos (p))
ml = true;
else
unget (c);
}
// The nl flag signals that the preceding character was a "special
// newline", that is, a newline that was part of the milti-line mode
// introductor or an escape sequence.
//
for (bool nl (ml); !is_eos (c); c = peek ())
{
// Detect the special "\n\\\n" sequence. In the multi-line mode,
// this is a "terminator". In the simple mode, this is a way to
// specify a newline.
//
// The key idea here is this: if we "swallowed" any characters
// (i.e., called get() without a matching unget()), then we
// have to restart the loop in order to do all the tests for
// the next character. Also, for this to work, we can only
// add one character to v, which limits us to maximum three
// characters look-ahead: one in v, one "ungot", and one
// peeked.
//
// The first block handles the special sequence that starts with
// a special newline. In multi-line mode, this is an "immediate
// termination" where we "use" the newline from the introductor.
// Note also that in the simple mode the special sequence can
// only start with a special (i.e., escaped) newline.
//
if (nl)
{
nl = false;
if (c == '\\')
{
get ();
xchar c1 (peek ());
if (c1 == '\n' || is_eos (c1))
{
if (ml)
break;
else
{
if (c1 == '\n')
get ();
v += '\n'; // Literal newline.
n++;
continue; // Restart from the next character.
}
}
else
unget (c); // Fall through.
}
}
if (c == '\n')
{
if (ml)
{
get ();
xchar c1 (peek ());
if (c1 == '\\')
{
get ();
xchar c2 (peek ());
if (c2 == '\n' || is_eos (c2))
break;
else
{
v += '\n';
unget (c1);
continue; // Restart from c1 (slash).
}
}
else
unget (c); // Fall through.
}
else
break; // Simple value terminator.
}
// Detect the newline escape sequence. The same look-ahead
// approach as above.
//
if (c == '\\')
{
get ();
xchar c1 (peek ());
if (c1 == '\n' || is_eos (c1))
{
if (c1 == '\n')
{
get ();
nl = true; // This is a special newline.
}
continue; // Restart from the next character.
}
else if (c1 == '\\')
{
get ();
xchar c2 (peek ());
if (c2 == '\n' || is_eos (c1))
{
v += '\\';
n++;
// Restart from c2 (newline/eos).
}
else
{
v += '\\';
n++;
unget (c1); // Restart from c1 (second slash).
}
continue;
}
else
unget (c); // Fall through.
}
get ();
v += c;
if (!ml)
{
if (c != ' ' && c != '\t')
n++;
}
}
// Cut off trailing whitespaces.
//
if (!ml)
v.resize (n);
}
manifest_parser::xchar manifest_parser::
skip_spaces ()
{
xchar c (peek ());
bool start (c.column () == 1);
for (; !is_eos (c); c = peek ())
{
switch (c)
{
case ' ':
case '\t':
break;
case '\n':
{
// Skip empty lines.
//
if (!start)
return c;
break;
}
case '#':
{
// We only recognize '#' as a start of a comment at the beginning
// of the line (sans leading spaces).
//
if (!start)
return c;
get ();
// Read until newline or eos.
//
for (c = peek (); !is_eos (c) && c != '\n'; c = peek ())
get ();
continue;
}
default:
return c; // Not a space.
}
get ();
}
return c;
}
// Character interface.
//
manifest_parser::xchar manifest_parser::
peek ()
{
if (unget_)
return buf_;
else
{
if (eos_)
return xchar (xchar::traits_type::eof (), l_, c_);
else
{
xchar::int_type v (is_.peek ());
if (v == xchar::traits_type::eof ())
eos_ = true;
return xchar (v, l_, c_);
}
}
}
manifest_parser::xchar manifest_parser::
get ()
{
if (unget_)
{
unget_ = false;
return buf_;
}
else
{
// When is_.get () returns eof, the failbit is also set (stupid,
// isn't?) which may trigger an exception. To work around this
// we will call peek() first and only call get() if it is not
// eof. But we can only call peek() on eof once; any subsequent
// calls will spoil the failbit (even more stupid).
//
xchar c (peek ());
if (!is_eos (c))
{
is_.get ();
if (c == '\n')
{
l_++;
c_ = 1;
}
else
c_++;
}
return c;
}
}
void manifest_parser::
unget (const xchar& c)
{
// Because iostream::unget cannot work once eos is reached,
// we have to provide our own implementation.
//
buf_ = c;
unget_ = true;
}
// manifest_parsing
//
static string
format (const string& n, uint64_t l, uint64_t c, const string& d)
{
ostringstream os;
if (!n.empty ())
os << n << ':';
os << l << ':' << c << ": error: " << d;
return os.str ();
}
manifest_parsing::
manifest_parsing (const string& n, uint64_t l, uint64_t c, const string& d)
: runtime_error (format (n, l, c, d)),
name (n), line (l), column (c), description (d)
{
}
}
|