37#ifndef LLVM_SUPPORT_YAMLPARSER_H
38#define LLVM_SUPPORT_YAMLPARSER_H
52#include <system_error>
63class document_iterator;
81LLVM_ABI std::string
escape(StringRef Input,
bool EscapePrintable =
true);
92 std::error_code *EC =
nullptr);
95 bool ShowColors =
true, std::error_code *EC =
nullptr);
116 std::unique_ptr<Scanner> scanner;
117 std::unique_ptr<Document> CurrentDoc;
122 virtual void anchor();
144 size_t Alignment = 16)
noexcept {
145 return Alloc.Allocate(
Size, Alignment);
149 size_t Size)
noexcept {
150 Alloc.Deallocate(
Ptr,
Size, 0);
153 void operator delete(
void *)
noexcept =
delete;
165 std::string getVerbatimTag()
const;
173 Node *parseBlockNode();
175 void setError(
const Twine &Message,
Token &Location)
const;
183 std::unique_ptr<Document> &
Doc;
200 void anchor()
override;
215 void anchor()
override;
220 :
Node(NK_Scalar,
D, Anchor, Tag),
Value(Val) {
221 SMLoc Start = SMLoc::getFromPointer(Val.
begin());
239 return N->getType() == NK_Scalar;
263 void anchor()
override;
269 SMLoc Start = SMLoc::getFromPointer(RawVal.
begin());
278 return N->getType() == NK_BlockScalar;
293 void anchor()
override;
314 if (
Node *Key = getKey()) {
316 if (
Node *Val = getValue())
322 return N->getType() == NK_KeyValue;
347 assert(Base && Base->CurrentEntry &&
"Attempted to access end iterator!");
348 return Base->CurrentEntry;
352 assert(Base && Base->CurrentEntry &&
353 "Attempted to dereference end iterator!");
354 return *Base->CurrentEntry;
358 assert(Base && Base->CurrentEntry &&
"Attempted to access end iterator!");
359 return Base->CurrentEntry;
370 if (Base && (Base ==
Other.Base)) {
371 assert((Base->CurrentEntry ==
Other.Base->CurrentEntry)
372 &&
"Equal Bases expected to point to equal Entries");
375 return Base ==
Other.Base;
379 return !(Base ==
Other.Base);
383 assert(Base &&
"Attempted to advance iterator past end!");
386 if (!Base->CurrentEntry)
392 BaseT *Base =
nullptr;
396template <
class CollectionType>
397typename CollectionType::iterator begin(CollectionType &
C) {
398 assert(
C.IsAtBeginning &&
"You may only iterate over a collection once!");
399 C.IsAtBeginning =
false;
400 typename CollectionType::iterator ret(&
C);
405template <
class CollectionType>
void skip(CollectionType &
C) {
407 assert((
C.IsAtBeginning ||
C.IsAtEnd) &&
"Cannot skip mid parse!");
409 for (
typename CollectionType::iterator i = begin(
C), e =
C.end(); i != e;
422 void anchor()
override;
433 :
Node(NK_Mapping,
D, Anchor, Tag),
Type(MT) {}
439 template <
class T>
friend typename T::iterator yaml::begin(
T &);
440 template <
class T>
friend void yaml::skip(
T &);
446 void skip()
override { yaml::skip(*
this); }
449 return N->getType() == NK_Mapping;
454 bool IsAtBeginning =
true;
455 bool IsAtEnd =
false;
470 void anchor()
override;
488 :
Node(NK_Sequence,
D, Anchor, Tag), SeqType(ST) {}
494 template <
class T>
friend typename T::iterator yaml::begin(
T &);
495 template <
class T>
friend void yaml::skip(
T &);
503 void skip()
override { yaml::skip(*
this); }
506 return N->getType() == NK_Sequence;
510 SequenceType SeqType;
511 bool IsAtBeginning =
true;
512 bool IsAtEnd =
false;
513 bool WasPreviousTokenFlowEntry =
true;
514 Node *CurrentEntry =
nullptr;
522 void anchor()
override;
556 const std::map<StringRef, StringRef> &
getTagMap()
const {
return TagMap; }
574 std::map<StringRef, StringRef> TagMap;
578 void setError(
const Twine &Message,
Token &Location)
const;
582 bool parseDirectives();
585 void parseYAMLDirective();
588 void parseTAGDirective();
591 bool expectToken(
int TK);
601 if (isAtEnd() ||
Other.isAtEnd())
602 return isAtEnd() &&
Other.isAtEnd();
604 return Doc ==
Other.Doc;
607 return !(*
this ==
Other);
611 assert(Doc &&
"incrementing iterator past the end.");
612 if (!(*Doc)->skip()) {
615 Stream &S = (*Doc)->stream;
626 bool isAtEnd()
const {
return !Doc || !*Doc; }
628 std::unique_ptr<Document> *Doc =
nullptr;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
Allocate memory in an ever growing pool, as if by bump-pointer.
Represents a location in source code.
Represents a range in source code.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
StringRef - Represent a constant reference to a string, i.e.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
Represents an alias to a Node with an anchor.
AliasNode(std::unique_ptr< Document > &D, StringRef Val)
static bool classof(const Node *N)
StringRef getName() const
A block scalar node is an opaque datum that can be presented as a series of zero or more Unicode scal...
static bool classof(const Node *N)
BlockScalarNode(std::unique_ptr< Document > &D, StringRef Anchor, StringRef Tag, StringRef Value, StringRef RawVal)
StringRef getValue() const
Gets the value of this node as a StringRef.
A YAML Stream is a sequence of Documents.
LLVM_ABI Node * parseBlockNode()
Root for parsing a node. Returns a single node.
const std::map< StringRef, StringRef > & getTagMap() const
LLVM_ABI bool skip()
Finish parsing the current document and return true if there are more.
Node * getRoot()
Parse and return the root level node.
static bool classof(const Node *N)
KeyValueNode(std::unique_ptr< Document > &D)
Represents a YAML map created from either a block map for a flow map.
static bool classof(const Node *N)
Abstract base class for all Nodes.
StringRef getRawTag() const
Get the tag as it was written in the document.
unsigned int getType() const
std::unique_ptr< Document > & Doc
Node(const Node &)=delete
StringRef getAnchor() const
Get the value of the anchor attached to this node.
void setSourceRange(SMRange SR)
Node(unsigned int Type, std::unique_ptr< Document > &, StringRef Anchor, StringRef Tag)
SMRange getSourceRange() const
void operator=(const Node &)=delete
NullNode(std::unique_ptr< Document > &D)
static bool classof(const Node *N)
A scalar node is an opaque datum that can be presented as a series of zero or more Unicode scalar val...
static bool classof(const Node *N)
ScalarNode(std::unique_ptr< Document > &D, StringRef Anchor, StringRef Tag, StringRef Val)
StringRef getRawValue() const
Represents a YAML sequence created from either a block sequence for a flow sequence.
static bool classof(const Node *N)
This class represents a YAML stream potentially containing multiple documents.
LLVM_ABI document_iterator end()
LLVM_ABI document_iterator begin()
LLVM_ABI void printError(Node *N, const Twine &Msg, SourceMgr::DiagKind Kind=SourceMgr::DK_Error)
This is an iterator abstraction over YAML collections shared by both sequences and maps.
bool operator==(const basic_collection_iterator &Other) const
Note on EqualityComparable:
basic_collection_iterator & operator++()
ValueT & operator*() const
bool operator!=(const basic_collection_iterator &Other) const
std::input_iterator_tag iterator_category
std::ptrdiff_t difference_type
basic_collection_iterator(BaseT *B)
ValueT * operator->() const
basic_collection_iterator()=default
Iterator abstraction for Documents over a Stream.
document_iterator operator++()
document_iterator(std::unique_ptr< Document > &D)
bool operator==(const document_iterator &Other) const
bool operator!=(const document_iterator &Other) const
document_iterator()=default
std::unique_ptr< Document > & operator->()
@ C
The default llvm calling convention, compatible with C.
LLVM_ABI bool dumpTokens(StringRef Input, raw_ostream &)
Dump all the tokens in this stream to OS.
LLVM_ABI std::optional< bool > parseBool(StringRef S)
Parse S as a bool according to https://yaml.org/type/bool.html.
LLVM_ABI bool scanTokens(StringRef Input)
Scans all tokens in input without outputting anything.
void skip(CollectionType &C)
LLVM_ABI std::string escape(StringRef Input, bool EscapePrintable=true)
Escape Input for a double quoted scalar; if EscapePrintable is true, all UTF8 sequences will be escap...
This is an optimization pass for GlobalISel generic memory operations.
bool failed(LogicalResult Result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Determine the kind of a node from its type.
Token - A single YAML token.