#ifndef LLVM_SUPPORT_YAMLPARSER_H
#define LLVM_SUPPORT_YAMLPARSER_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 <map>
#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;
bool dumpTokens(StringRef Input, raw_ostream &);
bool scanTokens(StringRef Input);
std::string escape(StringRef Input);
class Stream {
public:
Stream(StringRef Input, SourceMgr &);
Stream(MemoryBuffer *InputBuffer, 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;
};
class Node {
virtual void anchor();
public:
enum NodeKind {
NK_Null,
NK_Scalar,
NK_KeyValue,
NK_Mapping,
NK_Sequence,
NK_Alias
};
Node(unsigned int Type, OwningPtr<Document> &, StringRef Anchor,
StringRef Tag);
StringRef getAnchor() const { return Anchor; }
StringRef getRawTag() const { return Tag; }
std::string getVerbatimTag() const;
SMRange getSourceRange() const { return SourceRange; }
void setSourceRange(SMRange SR) { SourceRange = SR; }
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;
StringRef Tag;
};
class NullNode : public Node {
virtual void anchor();
public:
NullNode(OwningPtr<Document> &D)
: Node(NK_Null, D, StringRef(), StringRef()) {}
static inline bool classof(const Node *N) {
return N->getType() == NK_Null;
}
};
class ScalarNode : public Node {
virtual void anchor();
public:
ScalarNode(OwningPtr<Document> &D, StringRef Anchor, StringRef Tag,
StringRef Val)
: Node(NK_Scalar, D, Anchor, Tag), Value(Val) {
SMLoc Start = SMLoc::getFromPointer(Val.begin());
SMLoc End = SMLoc::getFromPointer(Val.end());
SourceRange = SMRange(Start, End);
}
StringRef getRawValue() const { return Value; }
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;
};
class KeyValueNode : public Node {
virtual void anchor();
public:
KeyValueNode(OwningPtr<Document> &D)
: Node(NK_KeyValue, D, StringRef(), StringRef())
, Key(0)
, Value(0)
{}
Node *getKey();
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;
};
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();
if (Base->CurrentEntry == 0)
Base = 0;
return *this;
}
private:
BaseT *Base;
};
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) {
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();
}
class MappingNode : public Node {
virtual void anchor();
public:
enum MappingType {
MT_Block,
MT_Flow,
MT_Inline };
MappingNode(OwningPtr<Document> &D, StringRef Anchor, StringRef Tag,
MappingType MT)
: Node(NK_Mapping, D, Anchor, Tag), 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();
};
class SequenceNode : public Node {
virtual void anchor();
public:
enum SequenceType {
ST_Block,
ST_Flow,
ST_Indentless
};
SequenceNode(OwningPtr<Document> &D, StringRef Anchor, StringRef Tag,
SequenceType ST)
: Node(NK_Sequence, D, Anchor, Tag), SeqType(ST), IsAtBeginning(true),
IsAtEnd(false),
WasPreviousTokenFlowEntry(true), 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;
};
class AliasNode : public Node {
virtual void anchor();
public:
AliasNode(OwningPtr<Document> &D, StringRef Val)
: Node(NK_Alias, D, StringRef(), 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;
};
class Document {
public:
Node *parseBlockNode();
Document(Stream &ParentStream);
bool skip();
Node *getRoot() {
if (Root)
return Root;
return Root = parseBlockNode();
}
const std::map<StringRef, StringRef> &getTagMap() const {
return TagMap;
}
private:
friend class Node;
friend class document_iterator;
Stream &stream;
BumpPtrAllocator NodeAllocator;
Node *Root;
std::map<StringRef, StringRef> TagMap;
Token &peekNext();
Token getNext();
void setError(const Twine &Message, Token &Location) const;
bool failed() const;
bool parseDirectives();
void parseYAMLDirective();
void parseTAGDirective();
bool expectToken(int TK);
};
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 || !*Doc;
}
OwningPtr<Document> *Doc;
};
}
}
#endif