@@ -8,10 +8,10 @@ YAML I/O
8
8
Introduction to YAML
9
9
====================
10
10
11
- YAML is a human readable data serialization language. The full YAML language
11
+ YAML is a human- readable data serialization language. The full YAML language
12
12
spec can be read at `yaml.org
13
13
<http://www.yaml.org/spec/1.2/spec.html#Introduction> `_. The simplest form of
14
- yaml is just "scalars", "mappings", and "sequences". A scalar is any number
14
+ YAML is just "scalars", "mappings", and "sequences". A scalar is any number
15
15
or string. The pound/hash symbol (#) begins a comment line. A mapping is
16
16
a set of key-value pairs where the key ends with a colon. For example:
17
17
@@ -49,10 +49,10 @@ of mappings in which one of the mapping values is itself a sequence:
49
49
- PowerPC
50
50
- x86
51
51
52
- Sometime sequences are known to be short and the one entry per line is too
53
- verbose, so YAML offers an alternate syntax for sequences called a "Flow
52
+ Sometimes sequences are known to be short and the one entry per line is too
53
+ verbose, so YAML offers an alternative syntax for sequences called a "Flow
54
54
Sequence" in which you put comma separated sequence elements into square
55
- brackets. The above example could then be simplified to :
55
+ brackets. The above example could then be simplified to:
56
56
57
57
58
58
.. code-block :: yaml
@@ -78,21 +78,21 @@ YAML I/O assumes you have some "native" data structures which you want to be
78
78
able to dump as YAML and recreate from YAML. The first step is to try
79
79
writing example YAML for your data structures. You may find after looking at
80
80
possible YAML representations that a direct mapping of your data structures
81
- to YAML is not very readable. Often the fields are not in the order that
81
+ to YAML is not very readable. Often, the fields are not in an order that
82
82
a human would find readable. Or the same information is replicated in multiple
83
83
locations, making it hard for a human to write such YAML correctly.
84
84
85
85
In relational database theory there is a design step called normalization in
86
86
which you reorganize fields and tables. The same considerations need to
87
87
go into the design of your YAML encoding. But, you may not want to change
88
- your existing native data structures. Therefore, when writing out YAML
88
+ your existing native data structures. Therefore, when writing out YAML,
89
89
there may be a normalization step, and when reading YAML there would be a
90
90
corresponding denormalization step.
91
91
92
- YAML I/O uses a non-invasive, traits based design. YAML I/O defines some
92
+ YAML I/O uses a non-invasive, traits- based design. YAML I/O defines some
93
93
abstract base templates. You specialize those templates on your data types.
94
- For instance, if you have an enumerated type FooBar you could specialize
95
- ScalarEnumerationTraits on that type and define the ``enumeration() `` method:
94
+ For instance, if you have an enumerated type `` FooBar `` you could specialize
95
+ `` ScalarEnumerationTraits `` on that type and define the ``enumeration() `` method:
96
96
97
97
.. code-block :: c++
98
98
@@ -107,12 +107,12 @@ ScalarEnumerationTraits on that type and define the ``enumeration()`` method:
107
107
};
108
108
109
109
110
- As with all YAML I/O template specializations, the ScalarEnumerationTraits is used for
110
+ As with all YAML I/O template specializations, the `` ScalarEnumerationTraits `` is used for
111
111
both reading and writing YAML. That is, the mapping between in-memory enum
112
112
values and the YAML string representation is only in one place.
113
113
This assures that the code for writing and parsing of YAML stays in sync.
114
114
115
- To specify a YAML mappings, you define a specialization on
115
+ To specify YAML mappings, you define a specialization on
116
116
``llvm::yaml::MappingTraits ``.
117
117
If your native data structure happens to be a struct that is already normalized,
118
118
then the specialization is simple. For example:
@@ -131,7 +131,7 @@ then the specialization is simple. For example:
131
131
};
132
132
133
133
134
- A YAML sequence is automatically inferred if you data type has ``begin() ``/``end() ``
134
+ A YAML sequence is automatically inferred if your data type has ``begin() ``/``end() ``
135
135
iterators and a ``push_back() `` method. Therefore any of the STL containers
136
136
(such as ``std::vector<> ``) will automatically translate to YAML sequences.
137
137
@@ -197,7 +197,7 @@ Error Handling
197
197
When parsing a YAML document, if the input does not match your schema (as
198
198
expressed in your ``XxxTraits<> `` specializations). YAML I/O
199
199
will print out an error message and your Input object's ``error() `` method will
200
- return true. For instance the following document:
200
+ return true. For instance, the following document:
201
201
202
202
.. code-block :: yaml
203
203
@@ -244,8 +244,8 @@ The following types have built-in support in YAML I/O:
244
244
* uint16_t
245
245
* uint8_t
246
246
247
- That is, you can use those types in fields of MappingTraits or as element type
248
- in sequence. When reading, YAML I/O will validate that the string found
247
+ That is, you can use those types in fields of `` MappingTraits `` or as the element type
248
+ in a sequence. When reading, YAML I/O will validate that the string found
249
249
is convertible to that type and error out if not.
250
250
251
251
@@ -255,7 +255,7 @@ Given that YAML I/O is trait based, the selection of how to convert your data
255
255
to YAML is based on the type of your data. But in C++ type matching, typedefs
256
256
do not generate unique type names. That means if you have two typedefs of
257
257
unsigned int, to YAML I/O both types look exactly like unsigned int. To
258
- facilitate make unique type names, YAML I/O provides a macro which is used
258
+ facilitate making unique type names, YAML I/O provides a macro which is used
259
259
like a typedef on built-in types, but expands to create a class with conversion
260
260
operators to and from the base type. For example:
261
261
@@ -271,7 +271,7 @@ is that you can now specify traits on them to get different YAML conversions.
271
271
272
272
Hex types
273
273
---------
274
- An example use of a unique type is that YAML I/O provides fixed sized unsigned
274
+ An example use of a unique type is that YAML I/O provides fixed- sized unsigned
275
275
integers that are written with YAML I/O as hexadecimal instead of the decimal
276
276
format used by the built-in integer types:
277
277
@@ -280,15 +280,15 @@ format used by the built-in integer types:
280
280
* Hex16
281
281
* Hex8
282
282
283
- You can use ``llvm::yaml::Hex32 `` instead of ``uint32_t `` and the only different will
283
+ You can use ``llvm::yaml::Hex32 `` instead of ``uint32_t `` and the only difference will
284
284
be that when YAML I/O writes out that type it will be formatted in hexadecimal.
285
285
286
286
287
287
ScalarEnumerationTraits
288
288
-----------------------
289
289
YAML I/O supports translating between in-memory enumerations and a set of string
290
290
values in YAML documents. This is done by specializing ``ScalarEnumerationTraits<> ``
291
- on your enumeration type and define an ``enumeration() `` method.
291
+ on your enumeration type and defining an ``enumeration() `` method.
292
292
For instance, suppose you had an enumeration of CPUs and a struct with it as
293
293
a field:
294
294
@@ -306,7 +306,7 @@ a field:
306
306
};
307
307
308
308
To support reading and writing of this enumeration, you can define a
309
- ScalarEnumerationTraits specialization on CPUs, which can then be used
309
+ `` ScalarEnumerationTraits `` specialization on CPUs, which can then be used
310
310
as a field type:
311
311
312
312
.. code-block :: c++
@@ -356,7 +356,7 @@ had the following bit flags defined:
356
356
357
357
LLVM_YAML_STRONG_TYPEDEF(uint32_t, MyFlags)
358
358
359
- To support reading and writing of MyFlags, you specialize ScalarBitSetTraits<>
359
+ To support reading and writing of MyFlags, you specialize `` ScalarBitSetTraits<> ``
360
360
on MyFlags and provide the bit values and their names.
361
361
362
362
.. code-block :: c++
@@ -399,7 +399,7 @@ the above schema, a same valid YAML document is:
399
399
name : Tom
400
400
flags : [ pointy, flat ]
401
401
402
- Sometimes a "flags" field might contains an enumeration part
402
+ Sometimes a "flags" field might contain an enumeration part
403
403
defined by a bit-mask.
404
404
405
405
.. code-block :: c++
@@ -415,7 +415,7 @@ defined by a bit-mask.
415
415
flagsCPU2 = 16
416
416
};
417
417
418
- To support reading and writing such fields, you need to use the maskedBitSet()
418
+ To support reading and writing such fields, you need to use the `` maskedBitSet() ``
419
419
method and provide the bit values, their names and the enumeration mask.
420
420
421
421
.. code-block :: c++
@@ -438,8 +438,8 @@ to the flow sequence.
438
438
439
439
Custom Scalar
440
440
-------------
441
- Sometimes for readability a scalar needs to be formatted in a custom way. For
442
- instance your internal data structure may use an integer for time (seconds since
441
+ Sometimes, for readability, a scalar needs to be formatted in a custom way. For
442
+ instance, your internal data structure may use an integer for time (seconds since
443
443
some epoch), but in YAML it would be much nicer to express that integer in
444
444
some time format (e.g. 4-May-2012 10:30pm). YAML I/O has a way to support
445
445
custom formatting and parsing of scalar types by specializing ``ScalarTraits<> `` on
@@ -487,13 +487,13 @@ your data type. The library doesn't provide any built-in support for block
487
487
scalar I/O for types like ``std::string `` and ``llvm::StringRef `` as they are already
488
488
supported by YAML I/O and use the ordinary scalar notation by default.
489
489
490
- BlockScalarTraits specializations are very similar to the
491
- ScalarTraits specialization - YAML I/O will provide the native type and your
490
+ `` BlockScalarTraits `` specializations are very similar to the
491
+ `` ScalarTraits `` specialization - YAML I/O will provide the native type and your
492
492
specialization must create a temporary ``llvm::StringRef `` when writing, and
493
493
it will also provide an ``llvm::StringRef `` that has the value of that block scalar
494
494
and your specialization must convert that to your native data type when reading.
495
495
An example of a custom type with an appropriate specialization of
496
- BlockScalarTraits is shown below:
496
+ `` BlockScalarTraits `` is shown below:
497
497
498
498
.. code-block :: c++
499
499
@@ -621,7 +621,7 @@ Polar which specifies a position in polar coordinates (distance, angle):
621
621
float angle;
622
622
};
623
623
624
- but you've decided the normalized YAML for should be in x,y coordinates. That
624
+ but you've decided the normalized YAML form should be in x,y coordinates. That
625
625
is, you want the yaml to look like:
626
626
627
627
.. code-block :: yaml
@@ -762,19 +762,19 @@ tag on a map. Using this functionality it is even possible to support different
762
762
mappings, as long as they are convertible.
763
763
764
764
To check a tag, inside your ``mapping() `` method you can use ``io.mapTag() `` to specify
765
- what the tag should be. This will also add that tag when writing yaml .
765
+ what the tag should be. This will also add that tag when writing YAML .
766
766
767
767
Validation
768
768
----------
769
769
770
770
Sometimes in a YAML map, each key/value pair is valid, but the combination is
771
771
not. This is similar to something having no syntax errors, but still having
772
- semantic errors. To support semantic level checking, YAML I/O allows
772
+ semantic errors. To support semantic- level checking, YAML I/O allows
773
773
an optional ``validate() `` method in a MappingTraits template specialization.
774
774
775
775
When parsing YAML, the ``validate() `` method is call *after * all key/values in
776
776
the map have been processed. Any error message returned by the ``validate() ``
777
- method during input will be printed just a like a syntax error would be printed.
777
+ method during input will be printed just like a syntax error would be printed.
778
778
When writing YAML, the ``validate() `` method is called *before * the YAML
779
779
key/values are written. Any error during output will trigger an ``assert() ``
780
780
because it is a programming error to have invalid struct values.
@@ -827,7 +827,7 @@ add "static const bool flow = true;". For instance:
827
827
static const bool flow = true;
828
828
}
829
829
830
- Flow mappings are subject to line wrapping according to the Output object
830
+ Flow mappings are subject to line wrapping according to the `` Output `` object
831
831
configuration.
832
832
833
833
Sequence
@@ -850,7 +850,7 @@ The ``size()`` method returns how many elements are currently in your sequence.
850
850
The ``element() `` method returns a reference to the i'th element in the sequence.
851
851
When parsing YAML, the ``element() `` method may be called with an index one bigger
852
852
than the current size. Your ``element() `` method should allocate space for one
853
- more element (using default constructor if element is a C++ object) and returns
853
+ more element (using default constructor if element is a C++ object) and return
854
854
a reference to that new allocated space.
855
855
856
856
@@ -919,8 +919,8 @@ trait for you document list type. The trait has the same methods as
919
919
920
920
User Context Data
921
921
=================
922
- When an ``llvm::yaml::Input `` or ``llvm::yaml::Output `` object is created their
923
- constructors take an optional "context" parameter. This is a pointer to
922
+ When an ``llvm::yaml::Input `` or ``llvm::yaml::Output `` object is created, its
923
+ constructor takes an optional "context" parameter. This is a pointer to
924
924
whatever state information you might need.
925
925
926
926
For instance, in a previous example we showed how the conversion type for a
@@ -930,18 +930,18 @@ of an outer mapping? That is where the "context" parameter comes in. You
930
930
can set values in the context in the outer map's ``mapping() `` method and
931
931
retrieve those values in the inner map's ``mapping() `` method.
932
932
933
- The context value is just a void*. All your traits which use the context
933
+ The context value is just a `` void* `` . All your traits which use the context
934
934
and operate on your native data types, need to agree what the context value
935
935
actually is. It could be a pointer to an object or struct which your various
936
- traits use to shared context sensitive information.
936
+ traits use to share context sensitive information.
937
937
938
938
939
939
Output
940
940
======
941
941
942
942
The ``llvm::yaml::Output `` class is used to generate a YAML document from your
943
943
in-memory data structures, using traits defined on your data types.
944
- To instantiate an Output object you need an ``llvm::raw_ostream ``, an optional
944
+ To instantiate an `` Output `` object you need an ``llvm::raw_ostream ``, an optional
945
945
context pointer and an optional wrapping column:
946
946
947
947
.. code-block :: c++
@@ -950,10 +950,10 @@ context pointer and an optional wrapping column:
950
950
public:
951
951
Output(llvm::raw_ostream &, void *context = NULL, int WrapColumn = 70);
952
952
953
- Once you have an Output object, you can use the C++ stream operator on it
953
+ Once you have an `` Output `` object, you can use the C++ stream operator on it
954
954
to write your native data as YAML. One thing to recall is that a YAML file
955
955
can contain multiple "documents". If the top level data structure you are
956
- streaming as YAML is a mapping, scalar, or sequence, then Output assumes you
956
+ streaming as YAML is a mapping, scalar, or sequence, then `` Output `` assumes you
957
957
are generating one document and wraps the mapping output
958
958
with "``--- ``" and trailing "``... ``".
959
959
@@ -1009,7 +1009,7 @@ Input
1009
1009
=====
1010
1010
1011
1011
The ``llvm::yaml::Input `` class is used to parse YAML document(s) into your native
1012
- data structures. To instantiate an Input
1012
+ data structures. To instantiate an `` Input ``
1013
1013
object you need a ``StringRef `` to the entire YAML file, and optionally a context
1014
1014
pointer:
1015
1015
@@ -1019,12 +1019,12 @@ pointer:
1019
1019
public:
1020
1020
Input(StringRef inputContent, void *context=NULL);
1021
1021
1022
- Once you have an Input object, you can use the C++ stream operator to read
1022
+ Once you have an `` Input `` object, you can use the C++ stream operator to read
1023
1023
the document(s). If you expect there might be multiple YAML documents in
1024
- one file, you'll need to specialize DocumentListTraits on a list of your
1024
+ one file, you'll need to specialize `` DocumentListTraits `` on a list of your
1025
1025
document type and stream in that document list type. Otherwise you can
1026
1026
just stream in the document type. Also, you can check if there was
1027
- any syntax errors in the YAML be calling the ``error() `` method on the Input
1027
+ any syntax errors in the YAML by calling the ``error() `` method on the `` Input ``
1028
1028
object. For example:
1029
1029
1030
1030
.. code-block :: c++
0 commit comments