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