22{
25
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
31 const int expected_events[] = {
35
38
41
44
47
51 };
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
77
78 char arena[100] = {};
79
80
81
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);
86 return 1;
87 }
88
89
90
91
92
95 handler.
reset(yaml, arena, events, estimated_size);
96 parser.parse_in_place_ev("filename", yaml);
97
98
99
100
102 {
103 printf("error: buffers too small: required_evt=%d actual_evt=%d\n required_arena=%zu actual_arena=%zu\n",
105
106
107
108
109 return 1;
110 }
111
112
113 printf("success! YAML requires event size %d, estimated=%d (required_arena=%zu actual_arena=%zu)\n",
115
116
117 bool success = true;
118
119
120
121 char flags[100];
123 {
124 bool status = (events[pos] == expected_events[pos]);
125
126
127 memset(flags, 0, sizeof(flags));
129 if(len + 1 >= sizeof(flags)) { printf("error: could not format flags"); return 1; }
130
131 printf("pos=%d\tevent[%d]:\t%20s = 0x%08x", pos, evt, flags, events[pos]);
132 if(events[pos] &
WSTR)
133 {
134 int offset = events[pos + 1];
135 int length = events[pos + 2];
136 bool in_arena = (events[pos] &
AREN);
137
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;
144 }
145 if(!status)
146 {
147 printf(" ... fail!");
148 success = false;
149 }
150 printf("\n");
151 }
152
153 return success ? 0 : 1;
154}
This is the main driver of parsing logic: it scans the YAML or JSON source for tokens,...
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(const char(&s)[N]) noexcept
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)