2012-04-04 01:09:22 +02:00
|
|
|
//===--- 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;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 01:45:19 +01:00
|
|
|
#ifndef LLVM_SUPPORT_YAMLPARSER_H
|
|
|
|
#define LLVM_SUPPORT_YAMLPARSER_H
|
2012-04-04 01:09:22 +02:00
|
|
|
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
#include "llvm/Support/SMLoc.h"
|
|
|
|
#include <limits>
|
2014-01-07 12:48:04 +01:00
|
|
|
#include <map>
|
2012-04-04 01:09:22 +02:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace llvm {
|
2015-03-23 19:07:13 +01:00
|
|
|
class MemoryBufferRef;
|
2012-04-04 01:09:22 +02:00
|
|
|
class SourceMgr;
|
|
|
|
class Twine;
|
2015-03-23 19:07:13 +01:00
|
|
|
class raw_ostream;
|
2012-04-04 01:09:22 +02:00
|
|
|
|
|
|
|
namespace yaml {
|
|
|
|
|
|
|
|
class document_iterator;
|
|
|
|
class Document;
|
|
|
|
class Node;
|
|
|
|
class Scanner;
|
|
|
|
struct Token;
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Dump all the tokens in this stream to OS.
|
|
|
|
/// \returns true if there was an error, false otherwise.
|
2012-04-04 01:09:22 +02:00
|
|
|
bool dumpTokens(StringRef Input, raw_ostream &);
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Scans all tokens in input without outputting anything. This is used
|
2012-04-04 01:09:22 +02:00
|
|
|
/// for benchmarking the tokenizer.
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \returns true if there was an error, false otherwise.
|
2012-04-04 01:09:22 +02:00
|
|
|
bool scanTokens(StringRef Input);
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Escape \a Input for a double quoted scalar.
|
2012-04-04 01:09:22 +02:00
|
|
|
std::string escape(StringRef Input);
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief This class represents a YAML stream potentially containing multiple
|
2012-04-04 01:09:22 +02:00
|
|
|
/// documents.
|
|
|
|
class Stream {
|
|
|
|
public:
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief This keeps a reference to the string referenced by \p Input.
|
2015-05-07 20:08:46 +02:00
|
|
|
Stream(StringRef Input, SourceMgr &, bool ShowColors = true);
|
2012-11-20 00:21:47 +01:00
|
|
|
|
2015-05-07 20:08:46 +02:00
|
|
|
Stream(MemoryBufferRef InputBuffer, SourceMgr &, bool ShowColors = true);
|
2012-04-04 10:53:34 +02:00
|
|
|
~Stream();
|
2012-04-04 01:09:22 +02:00
|
|
|
|
|
|
|
document_iterator begin();
|
|
|
|
document_iterator end();
|
|
|
|
void skip();
|
|
|
|
bool failed();
|
|
|
|
bool validate() {
|
|
|
|
skip();
|
|
|
|
return !failed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void printError(Node *N, const Twine &Msg);
|
|
|
|
|
|
|
|
private:
|
2014-03-06 06:51:42 +01:00
|
|
|
std::unique_ptr<Scanner> scanner;
|
|
|
|
std::unique_ptr<Document> CurrentDoc;
|
2012-04-04 01:09:22 +02:00
|
|
|
|
|
|
|
friend class Document;
|
|
|
|
};
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Abstract base class for all Nodes.
|
2012-04-04 01:09:22 +02:00
|
|
|
class Node {
|
2014-04-14 23:12:15 +02:00
|
|
|
virtual void anchor();
|
|
|
|
|
2012-04-04 01:09:22 +02:00
|
|
|
public:
|
|
|
|
enum NodeKind {
|
|
|
|
NK_Null,
|
|
|
|
NK_Scalar,
|
2015-05-14 01:10:51 +02:00
|
|
|
NK_BlockScalar,
|
2012-04-04 01:09:22 +02:00
|
|
|
NK_KeyValue,
|
|
|
|
NK_Mapping,
|
|
|
|
NK_Sequence,
|
|
|
|
NK_Alias
|
|
|
|
};
|
|
|
|
|
2014-03-06 06:51:42 +01:00
|
|
|
Node(unsigned int Type, std::unique_ptr<Document> &, StringRef Anchor,
|
2013-10-19 00:38:04 +02:00
|
|
|
StringRef Tag);
|
2012-04-04 01:09:22 +02:00
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Get the value of the anchor attached to this node. If it does not
|
2012-04-04 01:09:22 +02:00
|
|
|
/// have one, getAnchor().size() will be 0.
|
|
|
|
StringRef getAnchor() const { return Anchor; }
|
|
|
|
|
2013-10-19 00:38:04 +02:00
|
|
|
/// \brief Get the tag as it was written in the document. This does not
|
|
|
|
/// perform tag resolution.
|
|
|
|
StringRef getRawTag() const { return Tag; }
|
|
|
|
|
|
|
|
/// \brief Get the verbatium tag for a given Node. This performs tag resoluton
|
|
|
|
/// and substitution.
|
|
|
|
std::string getVerbatimTag() const;
|
|
|
|
|
2012-04-04 01:09:22 +02:00
|
|
|
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;
|
|
|
|
|
2012-04-21 00:07:50 +02:00
|
|
|
virtual void skip() {}
|
2012-04-04 01:09:22 +02:00
|
|
|
|
|
|
|
unsigned int getType() const { return TypeID; }
|
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
void *operator new(size_t Size, BumpPtrAllocator &Alloc,
|
2015-03-03 22:18:16 +01:00
|
|
|
size_t Alignment = 16) throw() {
|
2012-04-04 01:09:22 +02:00
|
|
|
return Alloc.Allocate(Size, Alignment);
|
|
|
|
}
|
|
|
|
|
2015-03-03 22:18:16 +01:00
|
|
|
void operator delete(void *Ptr, BumpPtrAllocator &Alloc, size_t Size) throw() {
|
2014-04-15 10:59:52 +02:00
|
|
|
Alloc.Deallocate(Ptr, Size);
|
2012-04-04 01:09:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-03-06 06:51:42 +01:00
|
|
|
std::unique_ptr<Document> &Doc;
|
2012-04-04 01:09:22 +02:00
|
|
|
SMRange SourceRange;
|
|
|
|
|
2015-03-03 22:18:16 +01:00
|
|
|
void operator delete(void *) throw() {}
|
2012-04-06 01:06:17 +02:00
|
|
|
|
2015-05-20 17:38:29 +02:00
|
|
|
~Node() = default;
|
2012-04-05 00:34:55 +02:00
|
|
|
|
2012-04-04 01:09:22 +02:00
|
|
|
private:
|
|
|
|
unsigned int TypeID;
|
|
|
|
StringRef Anchor;
|
2013-10-19 00:38:04 +02:00
|
|
|
/// \brief The tag as typed in the document.
|
|
|
|
StringRef Tag;
|
2012-04-04 01:09:22 +02:00
|
|
|
};
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief A null value.
|
2012-04-04 01:09:22 +02:00
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
/// !!null null
|
2015-05-20 17:38:29 +02:00
|
|
|
class NullNode final : public Node {
|
2014-03-04 07:24:11 +01:00
|
|
|
void anchor() override;
|
2014-04-14 23:12:15 +02:00
|
|
|
|
2012-04-04 01:09:22 +02:00
|
|
|
public:
|
2014-03-06 06:51:42 +01:00
|
|
|
NullNode(std::unique_ptr<Document> &D)
|
2013-10-19 00:38:04 +02:00
|
|
|
: Node(NK_Null, D, StringRef(), StringRef()) {}
|
2012-04-04 01:09:22 +02:00
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
static inline bool classof(const Node *N) { return N->getType() == NK_Null; }
|
2012-04-04 01:09:22 +02:00
|
|
|
};
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief A scalar node is an opaque datum that can be presented as a
|
2012-04-04 01:09:22 +02:00
|
|
|
/// series of zero or more Unicode scalar values.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
/// Adena
|
2015-05-20 17:38:29 +02:00
|
|
|
class ScalarNode final : public Node {
|
2014-03-04 07:24:11 +01:00
|
|
|
void anchor() override;
|
2014-04-14 23:12:15 +02:00
|
|
|
|
2012-04-04 01:09:22 +02:00
|
|
|
public:
|
2014-03-06 06:51:42 +01:00
|
|
|
ScalarNode(std::unique_ptr<Document> &D, StringRef Anchor, StringRef Tag,
|
2013-10-19 00:38:04 +02:00
|
|
|
StringRef Val)
|
|
|
|
: Node(NK_Scalar, D, Anchor, Tag), Value(Val) {
|
2012-04-04 01:09:22 +02:00
|
|
|
SMLoc Start = SMLoc::getFromPointer(Val.begin());
|
2013-01-07 20:00:49 +01:00
|
|
|
SMLoc End = SMLoc::getFromPointer(Val.end());
|
2012-04-04 01:09:22 +02:00
|
|
|
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; }
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Gets the value of this node as a StringRef.
|
2012-04-04 01:09:22 +02:00
|
|
|
///
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \param Storage is used to store the content of the returned StringRef iff
|
2012-04-04 01:09:22 +02:00
|
|
|
/// 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;
|
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
StringRef unescapeDoubleQuoted(StringRef UnquotedValue,
|
|
|
|
StringRef::size_type Start,
|
|
|
|
SmallVectorImpl<char> &Storage) const;
|
2012-04-04 01:09:22 +02:00
|
|
|
};
|
|
|
|
|
2015-05-14 01:10:51 +02:00
|
|
|
/// \brief A block scalar node is an opaque datum that can be presented as a
|
|
|
|
/// series of zero or more Unicode scalar values.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
/// |
|
|
|
|
/// Hello
|
|
|
|
/// World
|
2015-05-20 17:38:29 +02:00
|
|
|
class BlockScalarNode final : public Node {
|
2015-05-14 01:10:51 +02:00
|
|
|
void anchor() override;
|
|
|
|
|
|
|
|
public:
|
|
|
|
BlockScalarNode(std::unique_ptr<Document> &D, StringRef Anchor, StringRef Tag,
|
2015-05-18 23:11:27 +02:00
|
|
|
StringRef Value, StringRef RawVal)
|
|
|
|
: Node(NK_BlockScalar, D, Anchor, Tag), Value(Value) {
|
2015-05-14 01:10:51 +02:00
|
|
|
SMLoc Start = SMLoc::getFromPointer(RawVal.begin());
|
|
|
|
SMLoc End = SMLoc::getFromPointer(RawVal.end());
|
|
|
|
SourceRange = SMRange(Start, End);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Gets the value of this node as a StringRef.
|
|
|
|
StringRef getValue() const { return Value; }
|
|
|
|
|
|
|
|
static inline bool classof(const Node *N) {
|
|
|
|
return N->getType() == NK_BlockScalar;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-05-18 23:11:27 +02:00
|
|
|
StringRef Value;
|
2015-05-14 01:10:51 +02:00
|
|
|
};
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief A key and value pair. While not technically a Node under the YAML
|
2012-04-04 01:09:22 +02:00
|
|
|
/// representation graph, it is easier to treat them this way.
|
|
|
|
///
|
|
|
|
/// TODO: Consider making this not a child of Node.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
/// Section: .text
|
2015-05-20 17:38:29 +02:00
|
|
|
class KeyValueNode final : public Node {
|
2014-03-04 07:24:11 +01:00
|
|
|
void anchor() override;
|
2014-04-14 23:12:15 +02:00
|
|
|
|
2012-04-04 01:09:22 +02:00
|
|
|
public:
|
2014-03-06 06:51:42 +01:00
|
|
|
KeyValueNode(std::unique_ptr<Document> &D)
|
2014-04-14 23:12:15 +02:00
|
|
|
: Node(NK_KeyValue, D, StringRef(), StringRef()), Key(nullptr),
|
|
|
|
Value(nullptr) {}
|
2012-04-04 01:09:22 +02:00
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Parse and return the key.
|
2012-04-04 01:09:22 +02:00
|
|
|
///
|
|
|
|
/// This may be called multiple times.
|
|
|
|
///
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \returns The key, or nullptr if failed() == true.
|
2012-04-04 01:09:22 +02:00
|
|
|
Node *getKey();
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Parse and return the value.
|
2012-04-04 01:09:22 +02:00
|
|
|
///
|
|
|
|
/// This may be called multiple times.
|
|
|
|
///
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \returns The value, or nullptr if failed() == true.
|
2012-04-04 01:09:22 +02:00
|
|
|
Node *getValue();
|
|
|
|
|
2014-03-10 04:53:12 +01:00
|
|
|
void skip() override {
|
2012-04-04 01:09:22 +02:00
|
|
|
getKey()->skip();
|
2015-05-07 01:21:29 +02:00
|
|
|
if (Node *Val = getValue())
|
|
|
|
Val->skip();
|
2012-04-04 01:09:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool classof(const Node *N) {
|
|
|
|
return N->getType() == NK_KeyValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Node *Key;
|
|
|
|
Node *Value;
|
|
|
|
};
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief This is an iterator abstraction over YAML collections shared by both
|
2012-04-04 01:09:22 +02:00
|
|
|
/// 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
|
2014-04-14 23:12:15 +02:00
|
|
|
: public std::iterator<std::forward_iterator_tag, ValueT> {
|
2012-04-04 01:09:22 +02:00
|
|
|
public:
|
2014-04-07 06:17:22 +02:00
|
|
|
basic_collection_iterator() : Base(nullptr) {}
|
2012-04-04 01:09:22 +02:00
|
|
|
basic_collection_iterator(BaseT *B) : Base(B) {}
|
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
ValueT *operator->() const {
|
2012-04-04 01:09:22 +02:00
|
|
|
assert(Base && Base->CurrentEntry && "Attempted to access end iterator!");
|
|
|
|
return Base->CurrentEntry;
|
|
|
|
}
|
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
ValueT &operator*() const {
|
2012-04-04 01:09:22 +02:00
|
|
|
assert(Base && Base->CurrentEntry &&
|
|
|
|
"Attempted to dereference end iterator!");
|
|
|
|
return *Base->CurrentEntry;
|
|
|
|
}
|
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
operator ValueT *() const {
|
2012-04-04 01:09:22 +02:00
|
|
|
assert(Base && Base->CurrentEntry && "Attempted to access end iterator!");
|
|
|
|
return Base->CurrentEntry;
|
|
|
|
}
|
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
bool operator!=(const basic_collection_iterator &Other) const {
|
|
|
|
if (Base != Other.Base)
|
2012-04-04 01:09:22 +02:00
|
|
|
return true;
|
2014-04-14 23:12:15 +02:00
|
|
|
return (Base && Other.Base) &&
|
|
|
|
Base->CurrentEntry != Other.Base->CurrentEntry;
|
2012-04-04 01:09:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
basic_collection_iterator &operator++() {
|
|
|
|
assert(Base && "Attempted to advance iterator past end!");
|
|
|
|
Base->increment();
|
|
|
|
// Create an end iterator.
|
2014-04-09 06:20:00 +02:00
|
|
|
if (!Base->CurrentEntry)
|
2014-04-07 06:17:22 +02:00
|
|
|
Base = nullptr;
|
2012-04-04 01:09:22 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
template <class CollectionType> void skip(CollectionType &C) {
|
2012-04-04 01:09:22 +02:00
|
|
|
// TODO: support skipping from the middle of a parsed collection ;/
|
|
|
|
assert((C.IsAtBeginning || C.IsAtEnd) && "Cannot skip mid parse!");
|
|
|
|
if (C.IsAtBeginning)
|
2014-04-14 23:12:15 +02:00
|
|
|
for (typename CollectionType::iterator i = begin(C), e = C.end(); i != e;
|
|
|
|
++i)
|
2012-04-04 01:09:22 +02:00
|
|
|
i->skip();
|
|
|
|
}
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Represents a YAML map created from either a block map for a flow map.
|
2012-04-04 01:09:22 +02:00
|
|
|
///
|
|
|
|
/// This parses the YAML stream as increment() is called.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
/// Name: _main
|
|
|
|
/// Scope: Global
|
2015-05-20 17:38:29 +02:00
|
|
|
class MappingNode final : public Node {
|
2014-03-04 07:24:11 +01:00
|
|
|
void anchor() override;
|
2014-04-14 23:12:15 +02:00
|
|
|
|
2012-04-04 01:09:22 +02:00
|
|
|
public:
|
|
|
|
enum MappingType {
|
|
|
|
MT_Block,
|
|
|
|
MT_Flow,
|
2012-06-09 02:01:45 +02:00
|
|
|
MT_Inline ///< An inline mapping node is used for "[key: value]".
|
2012-04-04 01:09:22 +02:00
|
|
|
};
|
|
|
|
|
2014-03-06 06:51:42 +01:00
|
|
|
MappingNode(std::unique_ptr<Document> &D, StringRef Anchor, StringRef Tag,
|
2013-10-19 00:38:04 +02:00
|
|
|
MappingType MT)
|
|
|
|
: Node(NK_Mapping, D, Anchor, Tag), Type(MT), IsAtBeginning(true),
|
2014-04-07 06:17:22 +02:00
|
|
|
IsAtEnd(false), CurrentEntry(nullptr) {}
|
2012-04-04 01:09:22 +02:00
|
|
|
|
|
|
|
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 &);
|
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
iterator begin() { return yaml::begin(*this); }
|
2012-04-04 01:09:22 +02:00
|
|
|
|
|
|
|
iterator end() { return iterator(); }
|
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
void skip() override { yaml::skip(*this); }
|
2012-04-04 01:09:22 +02:00
|
|
|
|
|
|
|
static inline bool classof(const Node *N) {
|
|
|
|
return N->getType() == NK_Mapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MappingType Type;
|
|
|
|
bool IsAtBeginning;
|
|
|
|
bool IsAtEnd;
|
|
|
|
KeyValueNode *CurrentEntry;
|
|
|
|
|
|
|
|
void increment();
|
|
|
|
};
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Represents a YAML sequence created from either a block sequence for a
|
2012-04-04 01:09:22 +02:00
|
|
|
/// flow sequence.
|
|
|
|
///
|
|
|
|
/// This parses the YAML stream as increment() is called.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
/// - Hello
|
|
|
|
/// - World
|
2015-05-20 17:38:29 +02:00
|
|
|
class SequenceNode final : public Node {
|
2014-03-04 07:24:11 +01:00
|
|
|
void anchor() override;
|
2014-04-14 23:12:15 +02:00
|
|
|
|
2012-04-04 01:09:22 +02:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2014-03-06 06:51:42 +01:00
|
|
|
SequenceNode(std::unique_ptr<Document> &D, StringRef Anchor, StringRef Tag,
|
2013-10-19 00:38:04 +02:00
|
|
|
SequenceType ST)
|
|
|
|
: Node(NK_Sequence, D, Anchor, Tag), SeqType(ST), IsAtBeginning(true),
|
|
|
|
IsAtEnd(false),
|
|
|
|
WasPreviousTokenFlowEntry(true), // Start with an imaginary ','.
|
2014-04-07 06:17:22 +02:00
|
|
|
CurrentEntry(nullptr) {}
|
2012-04-04 01:09:22 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
iterator begin() { return yaml::begin(*this); }
|
2012-04-04 01:09:22 +02:00
|
|
|
|
|
|
|
iterator end() { return iterator(); }
|
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
void skip() override { yaml::skip(*this); }
|
2012-04-04 01:09:22 +02:00
|
|
|
|
|
|
|
static inline bool classof(const Node *N) {
|
|
|
|
return N->getType() == NK_Sequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SequenceType SeqType;
|
|
|
|
bool IsAtBeginning;
|
|
|
|
bool IsAtEnd;
|
|
|
|
bool WasPreviousTokenFlowEntry;
|
|
|
|
Node *CurrentEntry;
|
|
|
|
};
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Represents an alias to a Node with an anchor.
|
2012-04-04 01:09:22 +02:00
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
/// *AnchorName
|
2015-05-20 17:38:29 +02:00
|
|
|
class AliasNode final : public Node {
|
2014-03-04 07:24:11 +01:00
|
|
|
void anchor() override;
|
2014-04-14 23:12:15 +02:00
|
|
|
|
2012-04-04 01:09:22 +02:00
|
|
|
public:
|
2014-03-06 06:51:42 +01:00
|
|
|
AliasNode(std::unique_ptr<Document> &D, StringRef Val)
|
|
|
|
: Node(NK_Alias, D, StringRef(), StringRef()), Name(Val) {}
|
2012-04-04 01:09:22 +02:00
|
|
|
|
|
|
|
StringRef getName() const { return Name; }
|
|
|
|
Node *getTarget();
|
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
static inline bool classof(const Node *N) { return N->getType() == NK_Alias; }
|
2012-04-04 01:09:22 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
StringRef Name;
|
|
|
|
};
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief A YAML Stream is a sequence of Documents. A document contains a root
|
2012-04-04 01:09:22 +02:00
|
|
|
/// node.
|
|
|
|
class Document {
|
|
|
|
public:
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Root for parsing a node. Returns a single node.
|
2012-04-04 01:09:22 +02:00
|
|
|
Node *parseBlockNode();
|
|
|
|
|
|
|
|
Document(Stream &ParentStream);
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Finish parsing the current document and return true if there are
|
2012-04-04 01:09:22 +02:00
|
|
|
/// more. Return false otherwise.
|
|
|
|
bool skip();
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Parse and return the root level node.
|
2012-04-04 01:09:22 +02:00
|
|
|
Node *getRoot() {
|
|
|
|
if (Root)
|
|
|
|
return Root;
|
|
|
|
return Root = parseBlockNode();
|
|
|
|
}
|
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
const std::map<StringRef, StringRef> &getTagMap() const { return TagMap; }
|
2013-10-19 00:38:04 +02:00
|
|
|
|
2012-04-04 01:09:22 +02:00
|
|
|
private:
|
|
|
|
friend class Node;
|
|
|
|
friend class document_iterator;
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Stream to read tokens from.
|
2012-04-04 01:09:22 +02:00
|
|
|
Stream &stream;
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Used to allocate nodes to. All are destroyed without calling their
|
2012-04-04 01:09:22 +02:00
|
|
|
/// destructor when the document is destroyed.
|
|
|
|
BumpPtrAllocator NodeAllocator;
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief The root node. Used to support skipping a partially parsed
|
2012-04-04 01:09:22 +02:00
|
|
|
/// document.
|
|
|
|
Node *Root;
|
|
|
|
|
2013-10-19 00:38:04 +02:00
|
|
|
/// \brief Maps tag prefixes to their expansion.
|
|
|
|
std::map<StringRef, StringRef> TagMap;
|
|
|
|
|
2012-04-04 01:09:22 +02:00
|
|
|
Token &peekNext();
|
|
|
|
Token getNext();
|
|
|
|
void setError(const Twine &Message, Token &Location) const;
|
|
|
|
bool failed() const;
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Parse %BLAH directives and return true if any were encountered.
|
2012-04-04 01:09:22 +02:00
|
|
|
bool parseDirectives();
|
|
|
|
|
2013-10-19 00:38:04 +02:00
|
|
|
/// \brief Parse %YAML
|
|
|
|
void parseYAMLDirective();
|
|
|
|
|
|
|
|
/// \brief Parse %TAG
|
|
|
|
void parseTAGDirective();
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Consume the next token and error if it is not \a TK.
|
2012-04-04 01:09:22 +02:00
|
|
|
bool expectToken(int TK);
|
|
|
|
};
|
|
|
|
|
2014-04-14 08:22:03 +02:00
|
|
|
/// \brief Iterator abstraction for Documents over a Stream.
|
2012-04-04 01:09:22 +02:00
|
|
|
class document_iterator {
|
|
|
|
public:
|
2014-04-07 06:17:22 +02:00
|
|
|
document_iterator() : Doc(nullptr) {}
|
2014-03-06 06:51:42 +01:00
|
|
|
document_iterator(std::unique_ptr<Document> &D) : Doc(&D) {}
|
2012-04-04 01:09:22 +02:00
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
bool operator==(const document_iterator &Other) {
|
2012-05-01 12:19:59 +02:00
|
|
|
if (isAtEnd() || Other.isAtEnd())
|
|
|
|
return isAtEnd() && Other.isAtEnd();
|
|
|
|
|
2013-05-15 09:36:59 +02:00
|
|
|
return Doc == Other.Doc;
|
2012-04-17 18:38:41 +02:00
|
|
|
}
|
2014-04-14 23:12:15 +02:00
|
|
|
bool operator!=(const document_iterator &Other) { return !(*this == Other); }
|
2012-04-04 01:09:22 +02:00
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
document_iterator operator++() {
|
2014-04-13 06:57:38 +02:00
|
|
|
assert(Doc && "incrementing iterator past the end.");
|
2012-05-01 12:19:59 +02:00
|
|
|
if (!(*Doc)->skip()) {
|
2014-04-07 06:17:22 +02:00
|
|
|
Doc->reset(nullptr);
|
2012-04-04 01:09:22 +02:00
|
|
|
} else {
|
2012-05-01 12:19:59 +02:00
|
|
|
Stream &S = (*Doc)->stream;
|
|
|
|
Doc->reset(new Document(S));
|
2012-04-04 01:09:22 +02:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
Document &operator*() { return *Doc->get(); }
|
2012-04-04 01:09:22 +02:00
|
|
|
|
2014-03-06 06:51:42 +01:00
|
|
|
std::unique_ptr<Document> &operator->() { return *Doc; }
|
2012-04-04 01:09:22 +02:00
|
|
|
|
|
|
|
private:
|
2014-04-14 23:12:15 +02:00
|
|
|
bool isAtEnd() const { return !Doc || !*Doc; }
|
2012-05-01 12:19:59 +02:00
|
|
|
|
2014-03-06 06:51:42 +01:00
|
|
|
std::unique_ptr<Document> *Doc;
|
2012-04-04 01:09:22 +02:00
|
|
|
};
|
|
|
|
|
2014-04-14 23:12:15 +02:00
|
|
|
} // End namespace yaml.
|
|
|
|
|
|
|
|
} // End namespace llvm.
|
2012-04-04 01:09:22 +02:00
|
|
|
|
|
|
|
#endif
|