demonstrates how to obtain the (zero-based) location of a node from a recently parsed tree
2470{
2471
2472
2473
2475 "{" "\n"
2476 "aa: contents," "\n"
2477 "foo: [one, [two, three]]" "\n"
2478 "}" "\n"
2479 "";
2480
2481
2482
2487 CHECK(parser.options().locations());
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500 parser.reserve_locations(50u);
2501
2503
2505
2506
2507
2508
2509 CHECK(parser.location_contents(loc).begins_with(
"{"));
2513
2514
2516 CHECK(parser.location_contents(loc).begins_with(
"aa"));
2520
2521 loc = tree[
"foo"].
location(parser);
2522 CHECK(parser.location_contents(loc).begins_with(
"foo"));
2526 loc = tree[
"foo"][0].
location(parser);
2527 CHECK(parser.location_contents(loc).begins_with(
"one"));
2530
2531 loc = tree[
"foo"][1].
location(parser);
2532 CHECK(parser.location_contents(loc).begins_with(
"["));
2535 loc = tree[
"foo"][1][0].
location(parser);
2536 CHECK(parser.location_contents(loc).begins_with(
"two"));
2539 loc = tree[
"foo"][1][1].
location(parser);
2540 CHECK(parser.location_contents(loc).begins_with(
"three"));
2543
2544
2545
2546
2547
2548
2550
2551
2552
2554 CHECK(parser.location_contents(loc).begins_with(
"this is a docval"));
2557
2558
2560 "" "\n"
2561 "a new: buffer" "\n"
2562 "to: be parsed" "\n"
2563 "map with key:" "\n"
2564 " first: value" "\n"
2565 " second: value" "\n"
2566 "seq with key:" "\n"
2567 " - first value" "\n"
2568 " - second value" "\n"
2569 " -" "\n"
2570 " - nested first value" "\n"
2571 " - nested second value" "\n"
2572 " -" "\n"
2573 " nested first: value" "\n"
2574 " nested second: value" "\n"
2575 "");
2576
2577
2578
2579
2580
2581
2583 CHECK(parser.location_contents(loc).begins_with(
"a new"));
2587
2588 loc = tree2[
"a new"].
location(parser);
2589 CHECK(parser.location_contents(loc).begins_with(
"a new"));
2593 loc = tree2[
"to"].
location(parser);
2594 CHECK(parser.location_contents(loc).begins_with(
"to"));
2597
2598
2599 loc = tree2[
"map with key"].
location(parser);
2600 CHECK(parser.location_contents(loc).begins_with(
"map with key"));
2603 loc = tree2[
"map with key"][
"first"].
location(parser);
2604 CHECK(parser.location_contents(loc).begins_with(
"first"));
2607 loc = tree2[
"map with key"][
"second"].
location(parser);
2608 CHECK(parser.location_contents(loc).begins_with(
"second"));
2611
2612 loc = tree2[
"seq with key"].
location(parser);
2613 CHECK(parser.location_contents(loc).begins_with(
"seq with key"));
2616 loc = tree2[
"seq with key"][0].
location(parser);
2617 CHECK(parser.location_contents(loc).begins_with(
"first value"));
2620 loc = tree2[
"seq with key"][1].
location(parser);
2621 CHECK(parser.location_contents(loc).begins_with(
"second value"));
2624
2625 loc = tree2[
"seq with key"][2].
location(parser);
2626 CHECK(parser.location_contents(loc).begins_with(
"- nested first value"));
2629 loc = tree2[
"seq with key"][2][0].
location(parser);
2630 CHECK(parser.location_contents(loc).begins_with(
"nested first value"));
2633
2634 loc = tree2[
"seq with key"][3].
location(parser);
2635 CHECK(parser.location_contents(loc).begins_with(
"nested first: "));
2638 loc = tree2[
"seq with key"][3][0].
location(parser);
2639 CHECK(parser.location_contents(loc).begins_with(
"nested first: "));
2642}
NodeRef rootref()
Get the root as a NodeRef . Note that a non-const Tree implicitly converts to NodeRef.
Location location(Parser const &p, id_type node) const
Get the location of a node from the parse used to parse this tree.
ParseEngine< EventHandlerTree > Parser
This is the main ryml parser, where the parser events are handled to create a ryml tree (see Event Ha...
The event handler to create a ryml Tree.
holds a source or yaml file position, for example when an error is detected; See also location_format...
size_t offset
number of bytes from the beginning of the source buffer
Options to give to the ParseEngine to control its behavior.
ParserOptions & locations(bool enabled) noexcept
enable/disable source location tracking.
Location location(Parser const &parser) const