X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FSupport%2FYAMLParser.h;h=702044936c62680d8958bed6a57e2b805c3ec34a;hb=354362524a72b3fa43a6c09380b7ae3b2380cbba;hp=cf5706af1934fea5b7bd5ee13e0e52f6f3586a54;hpb=68f404d890cf5be94a7a818f4215bbff64598a45;p=oota-llvm.git diff --git a/include/llvm/Support/YAMLParser.h b/include/llvm/Support/YAMLParser.h index cf5706af193..702044936c6 100644 --- a/include/llvm/Support/YAMLParser.h +++ b/include/llvm/Support/YAMLParser.h @@ -35,8 +35,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_SUPPORT_YAML_PARSER_H -#define LLVM_SUPPORT_YAML_PARSER_H +#ifndef LLVM_SUPPORT_YAMLPARSER_H +#define LLVM_SUPPORT_YAMLPARSER_H #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/SmallString.h" @@ -44,6 +44,7 @@ #include "llvm/Support/Allocator.h" #include "llvm/Support/SMLoc.h" +#include #include #include @@ -77,7 +78,11 @@ std::string escape(StringRef Input); /// documents. class Stream { public: + /// @brief This keeps a reference to the string referenced by \p Input. Stream(StringRef Input, SourceMgr &); + + /// @brief This takes ownership of \p InputBuffer. + Stream(MemoryBuffer *InputBuffer, SourceMgr &); ~Stream(); document_iterator begin(); @@ -96,13 +101,11 @@ private: OwningPtr CurrentDoc; friend class Document; - - /// @brief Validate a %YAML x.x directive. - void handleYAMLDirective(const Token &); }; /// @brief Abstract base class for all Nodes. class Node { + virtual void anchor(); public: enum NodeKind { NK_Null, @@ -113,12 +116,21 @@ public: NK_Alias }; - Node(unsigned int Type, OwningPtr&, StringRef Anchor); + Node(unsigned int Type, OwningPtr &, StringRef Anchor, + StringRef Tag); /// @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; } + /// \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; + SMRange getSourceRange() const { return SourceRange; } void setSourceRange(SMRange SR) { SourceRange = SR; } @@ -130,10 +142,9 @@ public: void setError(const Twine &Message, Token &Location) const; bool failed() const; - virtual void skip() {}; + virtual void skip() {} unsigned int getType() const { return TypeID; } - static inline bool classof(const Node *) { return true; } void *operator new ( size_t Size , BumpPtrAllocator &Alloc @@ -149,11 +160,15 @@ protected: OwningPtr &Doc; SMRange SourceRange; - ~Node(); + void operator delete(void *) throw() {} + + virtual ~Node() {} private: unsigned int TypeID; StringRef Anchor; + /// \brief The tag as typed in the document. + StringRef Tag; }; /// @brief A null value. @@ -161,16 +176,14 @@ private: /// Example: /// !!null null class NullNode : public Node { + virtual void anchor(); public: - NullNode(OwningPtr &D) : Node(NK_Null, D, StringRef()) {} + NullNode(OwningPtr &D) + : Node(NK_Null, D, StringRef(), StringRef()) {} - static inline bool classof(const NullNode *) { return true; } static inline bool classof(const Node *N) { return N->getType() == NK_Null; } - -protected: - ~NullNode(); }; /// @brief A scalar node is an opaque datum that can be presented as a @@ -179,12 +192,13 @@ protected: /// Example: /// Adena class ScalarNode : public Node { + virtual void anchor(); public: - ScalarNode(OwningPtr &D, StringRef Anchor, StringRef Val) - : Node(NK_Scalar, D, Anchor) - , Value(Val) { + ScalarNode(OwningPtr &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() - 1); + SMLoc End = SMLoc::getFromPointer(Val.end()); SourceRange = SMRange(Start, End); } @@ -200,14 +214,10 @@ public: /// This happens with escaped characters and multi-line literals. StringRef getValue(SmallVectorImpl &Storage) const; - static inline bool classof(const ScalarNode *) { return true; } static inline bool classof(const Node *N) { return N->getType() == NK_Scalar; } -protected: - ~ScalarNode(); - private: StringRef Value; @@ -224,9 +234,10 @@ private: /// Example: /// Section: .text class KeyValueNode : public Node { + virtual void anchor(); public: KeyValueNode(OwningPtr &D) - : Node(NK_KeyValue, D, StringRef()) + : Node(NK_KeyValue, D, StringRef(), StringRef()) , Key(0) , Value(0) {} @@ -245,19 +256,15 @@ public: /// @returns The value, or nullptr if failed() == true. Node *getValue(); - virtual void skip() { + virtual void skip() LLVM_OVERRIDE { getKey()->skip(); getValue()->skip(); } - static inline bool classof(const KeyValueNode *) { return true; } static inline bool classof(const Node *N) { return N->getType() == NK_KeyValue; } -protected: - ~KeyValueNode(); - private: Node *Key; Node *Value; @@ -339,20 +346,18 @@ void skip(CollectionType &C) { /// Name: _main /// Scope: Global class MappingNode : public Node { + virtual void anchor(); public: enum MappingType { MT_Block, MT_Flow, - MT_Inline //< An inline mapping node is used for "[key: value]". + MT_Inline ///< An inline mapping node is used for "[key: value]". }; - MappingNode(OwningPtr &D, StringRef Anchor, MappingType MT) - : Node(NK_Mapping, D, Anchor) - , Type(MT) - , IsAtBeginning(true) - , IsAtEnd(false) - , CurrentEntry(0) - {} + MappingNode(OwningPtr &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; typedef basic_collection_iterator iterator; @@ -365,18 +370,14 @@ public: iterator end() { return iterator(); } - virtual void skip() { + virtual void skip() LLVM_OVERRIDE { yaml::skip(*this); } - static inline bool classof(const MappingNode *) { return true; } static inline bool classof(const Node *N) { return N->getType() == NK_Mapping; } -protected: - ~MappingNode(); - private: MappingType Type; bool IsAtBeginning; @@ -395,6 +396,7 @@ private: /// - Hello /// - World class SequenceNode : public Node { + virtual void anchor(); public: enum SequenceType { ST_Block, @@ -409,14 +411,12 @@ public: ST_Indentless }; - SequenceNode(OwningPtr &D, StringRef Anchor, SequenceType ST) - : Node(NK_Sequence, D, Anchor) - , SeqType(ST) - , IsAtBeginning(true) - , IsAtEnd(false) - , WasPreviousTokenFlowEntry(true) // Start with an imaginary ','. - , CurrentEntry(0) - {} + SequenceNode(OwningPtr &D, StringRef Anchor, StringRef Tag, + SequenceType ST) + : Node(NK_Sequence, D, Anchor, Tag), SeqType(ST), IsAtBeginning(true), + IsAtEnd(false), + WasPreviousTokenFlowEntry(true), // Start with an imaginary ','. + CurrentEntry(0) {} friend class basic_collection_iterator; typedef basic_collection_iterator iterator; @@ -431,18 +431,14 @@ public: iterator end() { return iterator(); } - virtual void skip() { + virtual void skip() LLVM_OVERRIDE { yaml::skip(*this); } - static inline bool classof(const SequenceNode *) { return true; } static inline bool classof(const Node *N) { return N->getType() == NK_Sequence; } -protected: - ~SequenceNode(); - private: SequenceType SeqType; bool IsAtBeginning; @@ -456,21 +452,18 @@ private: /// Example: /// *AnchorName class AliasNode : public Node { + virtual void anchor(); public: AliasNode(OwningPtr &D, StringRef Val) - : Node(NK_Alias, D, StringRef()), Name(Val) {} + : Node(NK_Alias, D, StringRef(), StringRef()), Name(Val) {} StringRef getName() const { return Name; } Node *getTarget(); - static inline bool classof(const ScalarNode *) { return true; } static inline bool classof(const Node *N) { return N->getType() == NK_Alias; } -protected: - ~AliasNode(); - private: StringRef Name; }; @@ -495,6 +488,10 @@ public: return Root = parseBlockNode(); } + const std::map &getTagMap() const { + return TagMap; + } + private: friend class Node; friend class document_iterator; @@ -510,18 +507,23 @@ private: /// document. Node *Root; + /// \brief Maps tag prefixes to their expansion. + std::map TagMap; + 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 Parse %YAML + void parseYAMLDirective(); + + /// \brief Parse %TAG + void parseTAGDirective(); + /// @brief Consume the next token and error if it is not \a TK. bool expectToken(int TK); }; @@ -529,34 +531,44 @@ private: /// @brief Iterator abstraction for Documents over a Stream. class document_iterator { public: - document_iterator() : Doc(NullDoc) {} - document_iterator(OwningPtr &D) : Doc(D) {} + document_iterator() : Doc(0) {} + document_iterator(OwningPtr &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 Doc != Other.Doc; + return !(*this == Other); } document_iterator operator ++() { - if (!Doc->skip()) { - Doc.reset(0); + assert(Doc != 0 && "incrementing iterator past the end."); + if (!(*Doc)->skip()) { + Doc->reset(0); } else { - Stream &S = Doc->stream; - Doc.reset(new Document(S)); + Stream &S = (*Doc)->stream; + Doc->reset(new Document(S)); } return *this; } Document &operator *() { - return *Doc; + return *Doc->get(); } OwningPtr &operator ->() { - return Doc; + return *Doc; } private: - static OwningPtr NullDoc; - OwningPtr &Doc; + bool isAtEnd() const { + return !Doc || !*Doc; + } + + OwningPtr *Doc; }; }