8#if defined(RYML_SINGLE_HEADER)
9 #define RYML_SINGLE_HDR_DEFINE_NOW
10 #include <ryml_ints.hpp>
11#elif defined(RYML_SINGLE_HEADER_LIB)
12 #include <ryml_ints.hpp>
29 std::vector<char>
yaml = {};
38 events.resize(
static_cast<size_t>(esz));
39 arena.resize(arena_size);
60 parser.parse_in_place_ev(filename, yaml);
99 printf(R
"(usage: %.*s [options] [<filename>]
101 if filename is missing, we default to a preset example.
105 -e <size>,--events-size <size> set size of events buffer. -1 means
106 estimate size. default=%d
107 -a <size>,--arena-size <size> set size of arena buffer. -1 means
108 take the source size as the arena size.
110 -n,--no-retry do not retry parsing if any buffer size
111 was insufficient. default=%s
112 -q,--quiet do not print events. default=%s
114 static_cast<int>(basename.
len), basename.
str,
117 defaults.
retry ?
"retry" :
"do not retry",
118 defaults.
quiet ?
"quiet" :
"print events");
120bool parse_args(
int argc,
const char **argv,
Args *args,
int &status)
124 int remaining = argc - 1;
126 auto advance = [&](
int num) { argv += num; remaining -= num; };
127 auto parse_arg = [&](
const char *name,
int *dst) {
134 printf(
"error: could not parse %s value: %.*s\n", name,
static_cast<int>(val.
len), val.
str);
141 printf(
"error: missing value: %s\n", name);
145 while(remaining > 0 && status == 0)
148 if(arg ==
"-h" || arg ==
"--help")
154 else if(arg ==
"-e" || arg ==
"--events-size")
158 else if(arg ==
"-a" || arg ==
"--arena-size")
162 else if(arg ==
"-n" || arg ==
"--no-retry")
167 else if(arg ==
"-q" || arg ==
"--quiet")
180 else if(remaining > 1)
183 printf(
"error: too many arguments\n");
194int main(
int argc,
const char *argv[])
203 bool is_demo = args.
filename ==
nullptr;
206 char demo_yaml[] =
""
207 "doe: a deer, a female deer\n"
208 "ray: a drop of golden sun\n"
209 "me : a name I call myself\n"
210 "far: a long long way to run\n"
215 const int demo_expected[] = {
253 const char *filename = args.
filename;
254 std::vector<char> yaml_file;
265 if(filename_ ==
"-" || filename_ ==
"stdin")
315 bool ok = (result.
events[pos] == demo_expected[pos]);
321 printf(
"pos=%zu\tevent[%zu]:\t%.*s = 0x%08x", pos, evt,
static_cast<int>(len), flags, result.
events[pos]);
325 int offset = result.
events[pos + 1];
326 int length = result.
events[pos + 2];
329 const char *ptr = in_arena ? result.
arena.data() : yaml.
str;
330 const char *str = ptr + offset;
332 printf(
"\tstr=(%d,%d)\t'%.*s'", offset, length, length, str);
333 ok = ok && (offset == demo_expected[pos + 1]);
334 ok = ok && (length == demo_expected[pos + 2]);
340 printf(
" ... fail!");
347 return success ? 0 : 1;
This is the main driver of parsing logic: it scans the YAML or JSON source for tokens,...
An event handler that creates an integer buffer with a very compact representation of the YAML tree i...
provides conversion and comparison facilities from/between std::vector<char> to c4::substr and c4::cs...
bool atoi(csubstr str, T *v) noexcept
Convert a trimmed string to a signed integral value.
int32_t evt_size
data type for integer events size.
void events_ints_print(csubstr parsed_yaml, csubstr arena, ievt::evt_bits const *evts, ievt::evt_bits evts_sz)
Print integer events to stdout.
evt_size estimate_events_ints_size(csubstr src)
Read YAML source and, without undergoing a full parse, estimate the size of the integer buffer requir...
void file_get_contents(const char *filename, FILE *fp, size_t filesz, void *buf, size_t bufsz)
load a file of specified size from disk into an existing contiguous buffer.
void stdin_get_contents(ContiguousContainer *cont, FILE *f=stdin)
load a file from stdin (or similar stream-like file) and return a newly created ContiguousContainer w...
substr to_substr(char(&s)[N]) noexcept
csubstr to_csubstr(const char(&s)[N]) noexcept
basic_substring< char > substr
a mutable string view
basic_substring< const char > csubstr
an immutable string view
void print_usage(c4::csubstr basename)
bool parse_args(int argc, const char **argv, Args *args, int &status)
int main(int argc, const char *argv[])
c4::yml::ParseEngine< c4::yml::extra::EventHandlerInts > parser
bool parse_in_place(const char *filename, c4::substr yaml, IntsResult *result)
c4::yml::extra::EventHandlerInts handler
bool parse_or_resize_and_then_parse(const char *filename, c4::csubstr yaml, IntsResult *result)
void resize_buffers(c4::yml::extra::EventHandlerInts const &handler)
std::vector< char > yaml
source buffer
std::vector< evt_bits > events
result buffer with events
void reset_yaml(c4::csubstr yaml_)
std::vector< char > arena
arena to place out-of-source filtered scalars or tags
void resize_buffers(c4::yml::extra::evt_size esz, size_t arena_size)
size_t len
the length of the substring
iterator begin() noexcept
size_t size() const noexcept
C * str
a restricted pointer to the first character of the substring
A parser event handler that creates a compact representation of the YAML tree in a contiguous buffer ...
evt_size 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