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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
//===--- YAMLParser.h - Simple YAML parser --------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This is a YAML 1.2 parser.
//
// See http://www.yaml.org/spec/1.2/spec.html for the full standard.
//
// This currently does not implement the following:
// * Multi-line literal folding.
// * Tag resolution.
// * UTF-16.
// * BOMs anywhere other than the first Unicode scalar value in the file.
//
// The most important class here is Stream. This represents a YAML stream with
// 0, 1, or many documents.
//
// SourceMgr sm;
// StringRef input = getInput();
// yaml::Stream stream(input, sm);
//
// for (yaml::document_iterator di = stream.begin(), de = stream.end();
// di != de; ++di) {
// yaml::Node *n = di->getRoot();
// if (n) {
// // Do something with n...
// } else
// break;
// }
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_SUPPORT_YAML_PARSER_H
#define LLVM_SUPPORT_YAML_PARSER_H
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/SMLoc.h"
#include <limits>
#include <utility>
namespace llvm {
class MemoryBuffer;
class SourceMgr;
class raw_ostream;
class Twine;
namespace yaml {
class document_iterator;
class Document;
class Node;
class Scanner;
struct Token;
/// @brief Dump all the tokens in this stream to OS.
/// @returns true if there was an error, false otherwise.
bool dumpTokens(StringRef Input, raw_ostream &);
/// @brief Scans all tokens in input without outputting anything. This is used
/// for benchmarking the tokenizer.
/// @returns true if there was an error, false otherwise.
bool scanTokens(StringRef Input);
/// @brief Escape \a Input for a double quoted scalar.
std::string escape(StringRef Input);
/// @brief This class represents a YAML stream potentially containing multiple
/// documents.
class Stream {
public:
Stream(StringRef Input, SourceMgr &);
~Stream();
document_iterator begin();
document_iterator end();
void skip();
bool failed();
bool validate() {
skip();
return !failed();
}
void printError(Node *N, const Twine &Msg);
private:
OwningPtr<Scanner> scanner;
OwningPtr<Document> CurrentDoc;
friend class Document;
/// @brief Validate a %YAML x.x directive.
void handleYAMLDirective(const Token &);
};
/// @brief Abstract base class for all Nodes.
class Node {
public:
enum NodeKind {
NK_Null,
NK_Scalar,
NK_KeyValue,
NK_Mapping,
NK_Sequence,
NK_Alias
};
Node(unsigned int Type, OwningPtr<Document>&, StringRef Anchor);
/// @brief Get the value of the anchor attached to this node. If it does not
/// have one, getAnchor().size() will be 0.
StringRef getAnchor() const { return Anchor; }
SMRange getSourceRange() const { return SourceRange; }
void setSourceRange(SMRange SR) { SourceRange = SR; }
// These functions forward to Document and Scanner.
Token &peekNext();
Token getNext();
Node *parseBlockNode();
BumpPtrAllocator &getAllocator();
void setError(const Twine &Message, Token &Location) const;
bool failed() const;
virtual void skip() {}
unsigned int getType() const { return TypeID; }
void *operator new ( size_t Size
, BumpPtrAllocator &Alloc
, size_t Alignment = 16) throw() {
return Alloc.Allocate(Size, Alignment);
}
void operator delete(void *Ptr, BumpPtrAllocator &Alloc, size_t) throw() {
Alloc.Deallocate(Ptr);
}
protected:
OwningPtr<Document> &Doc;
SMRange SourceRange;
void operator delete(void *) throw() {}
virtual ~Node() {}
private:
unsigned int TypeID;
StringRef Anchor;
};
/// @brief A null value.
///
/// Example:
/// !!null null
class NullNode : public Node {
public:
NullNode(OwningPtr<Document> &D) : Node(NK_Null, D, StringRef()) {}
static inline bool classof(const Node *N) {
return N->getType() == NK_Null;
}
};
/// @brief A scalar node is an opaque datum that can be presented as a
/// series of zero or more Unicode scalar values.
///
/// Example:
/// Adena
class ScalarNode : public Node {
public:
ScalarNode(OwningPtr<Document> &D, StringRef Anchor, StringRef Val)
: Node(NK_Scalar, D, Anchor)
, Value(Val) {
SMLoc Start = SMLoc::getFromPointer(Val.begin());
SMLoc End = SMLoc::getFromPointer(Val.end() - 1);
SourceRange = SMRange(Start, End);
}
// Return Value without any escaping or folding or other fun YAML stuff. This
// is the exact bytes that are contained in the file (after conversion to
// utf8).
StringRef getRawValue() const { return Value; }
/// @brief Gets the value of this node as a StringRef.
///
/// @param Storage is used to store the content of the returned StringRef iff
/// it requires any modification from how it appeared in the source.
/// This happens with escaped characters and multi-line literals.
StringRef getValue(SmallVectorImpl<char> &Storage) const;
static inline bool classof(const Node *N) {
return N->getType() == NK_Scalar;
}
private:
StringRef Value;
StringRef unescapeDoubleQuoted( StringRef UnquotedValue
, StringRef::size_type Start
, SmallVectorImpl<char> &Storage) const;
};
/// @brief A key and value pair. While not technically a Node under the YAML
/// representation graph, it is easier to treat them this way.
///
/// TODO: Consider making this not a child of Node.
///
/// Example:
/// Section: .text
class KeyValueNode : public Node {
public:
KeyValueNode(OwningPtr<Document> &D)
: Node(NK_KeyValue, D, StringRef())
, Key(0)
, Value(0)
{}
/// @brief Parse and return the key.
///
/// This may be called multiple times.
///
/// @returns The key, or nullptr if failed() == true.
Node *getKey();
/// @brief Parse and return the value.
///
/// This may be called multiple times.
///
/// @returns The value, or nullptr if failed() == true.
Node *getValue();
virtual void skip() LLVM_OVERRIDE {
getKey()->skip();
getValue()->skip();
}
static inline bool classof(const Node *N) {
return N->getType() == NK_KeyValue;
}
private:
Node *Key;
Node *Value;
};
/// @brief This is an iterator abstraction over YAML collections shared by both
/// sequences and maps.
///
/// BaseT must have a ValueT* member named CurrentEntry and a member function
/// increment() which must set CurrentEntry to 0 to create an end iterator.
template <class BaseT, class ValueT>
class basic_collection_iterator
: public std::iterator<std::forward_iterator_tag, ValueT> {
public:
basic_collection_iterator() : Base(0) {}
basic_collection_iterator(BaseT *B) : Base(B) {}
ValueT *operator ->() const {
assert(Base && Base->CurrentEntry && "Attempted to access end iterator!");
return Base->CurrentEntry;
}
ValueT &operator *() const {
assert(Base && Base->CurrentEntry &&
"Attempted to dereference end iterator!");
return *Base->CurrentEntry;
}
operator ValueT*() const {
assert(Base && Base->CurrentEntry && "Attempted to access end iterator!");
return Base->CurrentEntry;
}
bool operator !=(const basic_collection_iterator &Other) const {
if(Base != Other.Base)
return true;
return (Base && Other.Base) && Base->CurrentEntry
!= Other.Base->CurrentEntry;
}
basic_collection_iterator &operator++() {
assert(Base && "Attempted to advance iterator past end!");
Base->increment();
// Create an end iterator.
if (Base->CurrentEntry == 0)
Base = 0;
return *this;
}
private:
BaseT *Base;
};
// The following two templates are used for both MappingNode and Sequence Node.
template <class CollectionType>
typename CollectionType::iterator begin(CollectionType &C) {
assert(C.IsAtBeginning && "You may only iterate over a collection once!");
C.IsAtBeginning = false;
typename CollectionType::iterator ret(&C);
++ret;
return ret;
}
template <class CollectionType>
void skip(CollectionType &C) {
// TODO: support skipping from the middle of a parsed collection ;/
assert((C.IsAtBeginning || C.IsAtEnd) && "Cannot skip mid parse!");
if (C.IsAtBeginning)
for (typename CollectionType::iterator i = begin(C), e = C.end();
i != e; ++i)
i->skip();
}
/// @brief Represents a YAML map created from either a block map for a flow map.
///
/// This parses the YAML stream as increment() is called.
///
/// Example:
/// Name: _main
/// Scope: Global
class MappingNode : public Node {
public:
enum MappingType {
MT_Block,
MT_Flow,
MT_Inline ///< An inline mapping node is used for "[key: value]".
};
MappingNode(OwningPtr<Document> &D, StringRef Anchor, MappingType MT)
: Node(NK_Mapping, D, Anchor)
, Type(MT)
, IsAtBeginning(true)
, IsAtEnd(false)
, CurrentEntry(0)
{}
friend class basic_collection_iterator<MappingNode, KeyValueNode>;
typedef basic_collection_iterator<MappingNode, KeyValueNode> iterator;
template <class T> friend typename T::iterator yaml::begin(T &);
template <class T> friend void yaml::skip(T &);
iterator begin() {
return yaml::begin(*this);
}
iterator end() { return iterator(); }
virtual void skip() LLVM_OVERRIDE {
yaml::skip(*this);
}
static inline bool classof(const Node *N) {
return N->getType() == NK_Mapping;
}
private:
MappingType Type;
bool IsAtBeginning;
bool IsAtEnd;
KeyValueNode *CurrentEntry;
void increment();
};
/// @brief Represents a YAML sequence created from either a block sequence for a
/// flow sequence.
///
/// This parses the YAML stream as increment() is called.
///
/// Example:
/// - Hello
/// - World
class SequenceNode : public Node {
public:
enum SequenceType {
ST_Block,
ST_Flow,
// Use for:
//
// key:
// - val1
// - val2
//
// As a BlockMappingEntry and BlockEnd are not created in this case.
ST_Indentless
};
SequenceNode(OwningPtr<Document> &D, StringRef Anchor, SequenceType ST)
: Node(NK_Sequence, D, Anchor)
, SeqType(ST)
, IsAtBeginning(true)
, IsAtEnd(false)
, WasPreviousTokenFlowEntry(true) // Start with an imaginary ','.
, CurrentEntry(0)
{}
friend class basic_collection_iterator<SequenceNode, Node>;
typedef basic_collection_iterator<SequenceNode, Node> iterator;
template <class T> friend typename T::iterator yaml::begin(T &);
template <class T> friend void yaml::skip(T &);
void increment();
iterator begin() {
return yaml::begin(*this);
}
iterator end() { return iterator(); }
virtual void skip() LLVM_OVERRIDE {
yaml::skip(*this);
}
static inline bool classof(const Node *N) {
return N->getType() == NK_Sequence;
}
private:
SequenceType SeqType;
bool IsAtBeginning;
bool IsAtEnd;
bool WasPreviousTokenFlowEntry;
Node *CurrentEntry;
};
/// @brief Represents an alias to a Node with an anchor.
///
/// Example:
/// *AnchorName
class AliasNode : public Node {
public:
AliasNode(OwningPtr<Document> &D, StringRef Val)
: Node(NK_Alias, D, StringRef()), Name(Val) {}
StringRef getName() const { return Name; }
Node *getTarget();
static inline bool classof(const Node *N) {
return N->getType() == NK_Alias;
}
private:
StringRef Name;
};
/// @brief A YAML Stream is a sequence of Documents. A document contains a root
/// node.
class Document {
public:
/// @brief Root for parsing a node. Returns a single node.
Node *parseBlockNode();
Document(Stream &ParentStream);
/// @brief Finish parsing the current document and return true if there are
/// more. Return false otherwise.
bool skip();
/// @brief Parse and return the root level node.
Node *getRoot() {
if (Root)
return Root;
return Root = parseBlockNode();
}
private:
friend class Node;
friend class document_iterator;
/// @brief Stream to read tokens from.
Stream &stream;
/// @brief Used to allocate nodes to. All are destroyed without calling their
/// destructor when the document is destroyed.
BumpPtrAllocator NodeAllocator;
/// @brief The root node. Used to support skipping a partially parsed
/// document.
Node *Root;
Token &peekNext();
Token getNext();
void setError(const Twine &Message, Token &Location) const;
bool failed() const;
void handleTagDirective(const Token &Tag) {
// TODO: Track tags.
}
/// @brief Parse %BLAH directives and return true if any were encountered.
bool parseDirectives();
/// @brief Consume the next token and error if it is not \a TK.
bool expectToken(int TK);
};
/// @brief Iterator abstraction for Documents over a Stream.
class document_iterator {
public:
document_iterator() : Doc(0) {}
document_iterator(OwningPtr<Document> &D) : Doc(&D) {}
bool operator ==(const document_iterator &Other) {
if (isAtEnd() || Other.isAtEnd())
return isAtEnd() && Other.isAtEnd();
return *Doc == *Other.Doc;
}
bool operator !=(const document_iterator &Other) {
return !(*this == Other);
}
document_iterator operator ++() {
assert(Doc != 0 && "incrementing iterator past the end.");
if (!(*Doc)->skip()) {
Doc->reset(0);
} else {
Stream &S = (*Doc)->stream;
Doc->reset(new Document(S));
}
return *this;
}
Document &operator *() {
return *Doc->get();
}
OwningPtr<Document> &operator ->() {
return *Doc;
}
private:
bool isAtEnd() const {
return Doc == 0 || *Doc == 0;
}
OwningPtr<Document> *Doc;
};
}
}
#endif
|