demonstrates how to obtain the (zero-based) location of a node from a recently parsed tree
2472{
2473
2474
2475
2477 "{" "\n"
2478 "aa: contents," "\n"
2479 "foo: [one, [two, three]]" "\n"
2480 "}" "\n"
2481 "";
2482
2483
2484
2489 CHECK(parser.options().locations());
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502 parser.reserve_locations(50u);
2503
2505
2507
2508
2509
2510
2511 CHECK(parser.location_contents(loc).begins_with(
"{"));
2515
2516
2518 CHECK(parser.location_contents(loc).begins_with(
"aa"));
2522
2523 loc = tree[
"foo"].
location(parser);
2524 CHECK(parser.location_contents(loc).begins_with(
"foo"));
2528 loc = tree[
"foo"][0].
location(parser);
2529 CHECK(parser.location_contents(loc).begins_with(
"one"));
2532
2533 loc = tree[
"foo"][1].
location(parser);
2534 CHECK(parser.location_contents(loc).begins_with(
"["));
2537 loc = tree[
"foo"][1][0].
location(parser);
2538 CHECK(parser.location_contents(loc).begins_with(
"two"));
2541 loc = tree[
"foo"][1][1].
location(parser);
2542 CHECK(parser.location_contents(loc).begins_with(
"three"));
2545
2546
2547
2548
2549
2550
2552
2553
2554
2556 CHECK(parser.location_contents(loc).begins_with(
"this is a docval"));
2559
2560
2562 "" "\n"
2563 "a new: buffer" "\n"
2564 "to: be parsed" "\n"
2565 "map with key:" "\n"
2566 " first: value" "\n"
2567 " second: value" "\n"
2568 "seq with key:" "\n"
2569 " - first value" "\n"
2570 " - second value" "\n"
2571 " -" "\n"
2572 " - nested first value" "\n"
2573 " - nested second value" "\n"
2574 " -" "\n"
2575 " nested first: value" "\n"
2576 " nested second: value" "\n"
2577 "");
2578
2579
2580
2581
2582
2583
2585 CHECK(parser.location_contents(loc).begins_with(
"a new"));
2589
2590 loc = tree2[
"a new"].
location(parser);
2591 CHECK(parser.location_contents(loc).begins_with(
"a new"));
2595 loc = tree2[
"to"].
location(parser);
2596 CHECK(parser.location_contents(loc).begins_with(
"to"));
2599
2600
2601 loc = tree2[
"map with key"].
location(parser);
2602 CHECK(parser.location_contents(loc).begins_with(
"map with key"));
2605 loc = tree2[
"map with key"][
"first"].
location(parser);
2606 CHECK(parser.location_contents(loc).begins_with(
"first"));
2609 loc = tree2[
"map with key"][
"second"].
location(parser);
2610 CHECK(parser.location_contents(loc).begins_with(
"second"));
2613
2614 loc = tree2[
"seq with key"].
location(parser);
2615 CHECK(parser.location_contents(loc).begins_with(
"seq with key"));
2618 loc = tree2[
"seq with key"][0].
location(parser);
2619 CHECK(parser.location_contents(loc).begins_with(
"first value"));
2622 loc = tree2[
"seq with key"][1].
location(parser);
2623 CHECK(parser.location_contents(loc).begins_with(
"second value"));
2626
2627 loc = tree2[
"seq with key"][2].
location(parser);
2628 CHECK(parser.location_contents(loc).begins_with(
"- nested first value"));
2631 loc = tree2[
"seq with key"][2][0].
location(parser);
2632 CHECK(parser.location_contents(loc).begins_with(
"nested first value"));
2635
2636 loc = tree2[
"seq with key"][3].
location(parser);
2637 CHECK(parser.location_contents(loc).begins_with(
"nested first: "));
2640 loc = tree2[
"seq with key"][3][0].
location(parser);
2641 CHECK(parser.location_contents(loc).begins_with(
"nested first: "));
2644}
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