rapidyaml  0.13.0
parse and emit YAML, and do it fast
quickstart-ints.cpp
Go to the documentation of this file.
1 // This file shows a quick example of parsing YAML to an int events
2 // buffer. Since this functionality is meant to implement in other
3 // programming languages, the code is kept very simple, and using only
4 // C-like idioms.
5 
6 // ryml can be used as a single header, or as a simple library:
7 #if defined(RYML_SINGLE_HEADER) // using the single header directly in the executable
8  #define RYML_SINGLE_HDR_DEFINE_NOW
9  #include <ryml_ints.hpp>
10 #elif defined(RYML_SINGLE_HEADER_LIB) // using the single header from a library
11  #include <ryml_ints.hpp>
12 #else
15 #include <c4/yml/extra/ints_utils.hpp> // to print
16 #endif
17 
18 
19 // NOLINTBEGIN(hicpp-signed-bitwise)
20 
21 int main(int, const char *[])
22 {
23  using namespace c4::yml::extra::ievt;
24  auto PSTR_ = c4::yml::extra::ievt::PSTR; // PSTR does not work in windows
25  // YAML code to be parsed in place
26  char yaml[] = "do: a deer, a female deer\n"
27  "re: a drop of golden sun\n"
28  "mi: a name I call myself\n"
29  "fa: a long long way to run\n";
30  // these are the event values we expect
31  const int expected_events[] = {
32  BSTR,
33  BDOC,
34  VAL_|BMAP|BLCK,
35  //
36  KEY_|SCLR|PLAI, 0, 2, // "do"
37  VAL_|SCLR|PLAI|PSTR_, 4, 21, // "a deer, a female deer"
38  //
39  KEY_|SCLR|PLAI|PSTR_, 26, 2, // "re"
40  VAL_|SCLR|PLAI|PSTR_, 30, 20, // "a drop of golden sun"
41  //
42  KEY_|SCLR|PLAI|PSTR_, 51, 2, // "mi"
43  VAL_|SCLR|PLAI|PSTR_, 55, 20, // "a name I call myself"
44  //
45  KEY_|SCLR|PLAI|PSTR_, 76, 2, // "fa"
46  VAL_|SCLR|PLAI|PSTR_, 80, 22, // "a long long way to run"
47  //
48  EMAP|PSTR_,
49  EDOC,
50  ESTR,
51  };
52 
53  /* the output should be this:
54  *
55  * success! YAML requires event size 30, estimated=49 (required_arena=0 actual=99)
56  * pos=0 event[0]: BSTR = 0x00000001
57  * pos=1 event[1]: BDOC = 0x00000004
58  * pos=2 event[2]: VAL_|BMAP|BLCK = 0x00140010
59  * pos=3 event[3]: KEY_|SCLR|PLAI = 0x00081100 str=(0,2) 'do'
60  * pos=6 event[4]: VAL_|SCLR|PLAI|PSTR = 0x04101100 str=(4,21) 'a deer, a female deer'
61  * pos=9 event[5]: KEY_|SCLR|PLAI|PSTR = 0x04081100 str=(26,2) 're'
62  * pos=12 event[6]: VAL_|SCLR|PLAI|PSTR = 0x04101100 str=(30,20) 'a drop of golden sun'
63  * pos=15 event[7]: KEY_|SCLR|PLAI|PSTR = 0x04081100 str=(51,2) 'mi'
64  * pos=18 event[8]: VAL_|SCLR|PLAI|PSTR = 0x04101100 str=(55,20) 'a name I call myself'
65  * pos=21 event[9]: KEY_|SCLR|PLAI|PSTR = 0x04081100 str=(76,2) 'fa'
66  * pos=24 event[10]: VAL_|SCLR|PLAI|PSTR = 0x04101100 str=(80,22) 'a long long way to run'
67  * pos=27 event[11]: EMAP|PSTR = 0x04000020
68  * pos=28 event[12]: EDOC = 0x00000008
69  * pos=29 event[13]: ESTR = 0x00000002
70  */
71 
72  // buffer to where we will write the events
73  constexpr const int events_size = 100;
74  int events[events_size] = {};
75  static_assert(events_size >= sizeof(expected_events)/sizeof(expected_events[0]), "buffer too small");
76  // buffer for placing any scalars/tags that cannot be filtered
77  // in-place
78  char arena[100] = {};
79 
80 
81  // ensure the estimation will succeed vs required size
82  int estimated_size = c4::yml::extra::estimate_events_ints_size(yaml);
83  if (estimated_size > events_size)
84  {
85  printf("the estimated size (%d) will not fit the events array (%d)\n", estimated_size, events_size); // LCOV_EXCL_LINE
86  return 1; // LCOV_EXCL_LINE
87  }
88 
89  // parse now. the parse should succeed (because the YAML above is
90  // legit), but if there were would be a parse error, we would get
91  // the default behavior which is abort on error, since we did not
92  // set up the error callbacks
95  handler.reset(yaml, arena, events, estimated_size); // note we pass the estimated size!
96  parser.parse_in_place_ev("filename", yaml);
97 
98  // the YAML was successfully parsed, but it may happen that it
99  // requires more events than may fit in the buffers. so we need to
100  // check that it actually fits (this is mandatory):
101  if(!handler.fits_buffers())
102  {
103  printf("error: buffers too small: required_evt=%d actual_evt=%d\n required_arena=%zu actual_arena=%zu\n", // LCOV_EXCL_LINE
104  handler.required_size_events(), estimated_size, handler.required_size_arena(), c4::to_csubstr(arena).len); // LCOV_EXCL_LINE
105  // WATCHOUT: if you want to retry the parse, you need to set
106  // up the source buffer again, because it is invalidated from
107  // being parsed in place. refer to the doxygen documentation
108  // for more details.
109  return 1; // LCOV_EXCL_LINE
110  }
111 
112  // done!
113  printf("success! YAML requires event size %d, estimated=%d (required_arena=%zu actual_arena=%zu)\n", // LCOV_EXCL_LINE
114  handler.required_size_events(), estimated_size, handler.required_size_arena(), c4::to_csubstr(arena).len); // LCOV_EXCL_LINE
115 
116  // ensure the result is as expected
117  bool success = true;
118 
119  // example iterating through the events array: compare and print
120  // the result
121  char flags[100];
122  for (int pos = 0, evt = 0; pos < handler.required_size_events(); ++pos, ++evt)
123  {
124  bool status = (events[pos] == expected_events[pos]);
125  // let's format the event flags to print them as string.
126  // we need to zero-terminate them to be able to align using printf.
127  memset(flags, 0, sizeof(flags)); // ensure flags are zero-terminated
128  size_t len = c4::yml::extra::ievt::to_chars(flags, events[pos]);
129  if(len + 1 >= sizeof(flags)) { printf("error: could not format flags"); return 1; } // ensure flags are zero-terminated
130  // print the event
131  printf("pos=%d\tevent[%d]:\t%20s = 0x%08x", pos, evt, flags, events[pos]);
132  if(events[pos] & WSTR) // the event has a string following it
133  {
134  int offset = events[pos + 1];
135  int length = events[pos + 2];
136  bool in_arena = (events[pos] & AREN);
137  // WATCHOUT! the string is NOT ZERO TERMINATED!
138  const char *ptr = in_arena ? arena : yaml;
139  const char *str = ptr + offset;
140  printf("\tstr=(%d,%d)\t'%.*s'", offset, length, length, str);
141  status = status && (offset == expected_events[pos + 1]);
142  status = status && (length == expected_events[pos + 2]);
143  pos += 2; // advance the two ints from the string
144  }
145  if(!status)
146  {
147  printf(" ... fail!"); // LCOV_EXCL_LINE
148  success = false; // LCOV_EXCL_LINE
149  }
150  printf("\n");
151  }
152 
153  return success ? 0 : 1;
154 }
155 
156 // NOLINTEND(hicpp-signed-bitwise)
This is the main driver of parsing logic: it scans the YAML or JSON source for tokens,...
void parse_in_place_ev(csubstr filename, substr src)
parse YAML in place, emitting events to the current handler
An event handler that creates an integer buffer with a very compact representation of the YAML tree i...
int32_t estimate_events_ints_size(csubstr src)
Read YAML source and, without undergoing a full parse, estimate the size of the integer buffer requir...
csubstr to_csubstr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2204
@ SCLR
scalar (=VAL in test suite events)
@ EMAP
end map (-MAP in test suite events)
@ BMAP
begin map (+MAP in test suite events)
@ ESTR
end stream (-STR in test suite events)
@ BSTR
begin stream (+STR in test suite events)
@ WSTR
WithSTRing: mask of all the events that encode a string following the event. For such events,...
@ VAL_
as value special flag to enable look-back in the event array. it signifies that the previous event ha...
@ BDOC
begin doc (+DOC in test suite events)
@ BLCK
container: block
@ AREN
IMPORTANT. Marks events whose string was placed in the arena. This happens when the filtered string i...
@ EDOC
end doc (-DOC in test suite events)
size_t to_chars(substr buf, ievt::DataType flags)
Convert bit mask of ievt::EventFlags to text.
Definition: ints_utils.cpp:68
int main(int, const char *[])
A parser event handler that creates a compact representation of the YAML tree in a contiguous buffer ...
bool fits_buffers() const
Predicate to test if the event and arena buffers successfully accomodated all the parse events.
int required_size_events() const
get the size needed for the event buffer from the previous parse
size_t required_size_arena() const
get the size needed for the arena from the previous parse
void reset(substr str, substr arena, ievt::DataType *dst, int32_t dst_size)