JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
parse_index.hpp
Go to the documentation of this file.
1/// \file jsonv/parse_index.hpp
2/// Parsed index of a JSON document.
3///
4/// Copyright (c) 2020 by Travis Gockel. All rights reserved.
5///
6/// This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
7/// as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
8/// version.
9///
10/// \author Travis Gockel (travis@gockelhut.com)
11#pragma once
12
13#include <jsonv/config.hpp>
14#include <jsonv/ast.hpp>
15
16namespace jsonv
17{
18
19class extract_options;
20class parse_options;
21
22/// \ingroup Value
23/// \{
24
25/// Represents the index of a parsed AST. When combined with the original text, can be used to create a \c value. See
26/// \c parse_index::parse to construct instances from JSON source text.
28{
29public:
31 {
32 public:
33 using value_type = ast_node;
34
35 public:
36 iterator() = default;
37
38 iterator& operator++();
39 iterator operator++(int)
40 {
41 iterator temp(*this);
42 ++*this;
43 return temp;
44 }
45
46 value_type operator*() const;
47
48 // Note the lack of comparison between `_prefix` -- valid `iterator`s will always have the same `_prefix`
49 bool operator==(const iterator& other) const { return _iter == other._iter; }
50 bool operator!=(const iterator& other) const { return _iter != other._iter; }
51 bool operator< (const iterator& other) const { return _iter < other._iter; }
52 bool operator<=(const iterator& other) const { return _iter <= other._iter; }
53 bool operator> (const iterator& other) const { return _iter > other._iter; }
54 bool operator>=(const iterator& other) const { return _iter >= other._iter; }
55
56 private:
57 explicit iterator(const std::uintptr_t prefix, const std::uint64_t* iter) :
58 _prefix(prefix),
59 _iter(iter)
60 { }
61
62 friend class parse_index;
63
64 private:
65 std::uintptr_t _prefix;
66 const std::uint64_t* _iter;
67 };
68
70
71public:
72 /// Creates an empty not-an-AST instance.
74
76 _impl(std::exchange(src._impl, nullptr))
77 { }
78
79 parse_index& operator=(parse_index&& src) noexcept
80 {
81 if (this == &src)
82 return *this;
83
84 using std::swap;
85 swap(_impl, src._impl);
86 src.reset();
87
88 return *this;
89 }
90
91 ~parse_index() noexcept;
92
93 /// \{
94 /// Create an \c parse_index from the given \a src JSON.
95 ///
96 /// \param options
97 /// The options used to control parsing. If unspecified, these will be \c parse_options::create_default().
98 /// \param initial_buffer_capacity
99 /// The initial capacity of the underlying buffer. By default (\c nullopt), this will size the buffer according
100 /// to the length of the \a src string.
104 );
108 /// \}
109
110 /// Clear the contents of this instance.
111 void reset();
112
113 /// Check if this instance represents a valid AST. This will be \c false if the source JSON was not valid JSON text.
114 /// This will also be \c false if this instance was default-constructed or moved-from.
115 ///
116 /// \note
117 /// Even if this returns true, it is possible that conversion to a \c jsonv::value will throw an exception. For
118 /// example, if the value of a number exceeds the range of an \c int64_t. This is because JSON does not specify an
119 /// acceptable range for numbers, but the storage of \c jsonv::value does.
120 bool success() const noexcept;
121
122 /// See \ref success.
124
125 /// Validate that the parse was a \c success.
126 ///
127 /// \throws parse_error if the parse was not successful. This will contain additional details about why the parse
128 /// failed.
129 /// \throws std::invalid_argument if this instance was default-constructed or moved-from.
130 void validate() const;
131
132 iterator begin() const;
133 iterator cbegin() const { return begin(); }
134
135 iterator end() const;
136 iterator cend() const { return end(); }
137
138 /// \{
139 /// \param options
140 /// The options used to control how values are extracted from this source. If unspecified, these will be
141 /// \c extract_options::create_default().
143 value extract_tree() const;
144 /// \}
145
146 /// \{
147 /// Get a string representation of the AST.
148 ///
149 /// +--------------------+--------+
150 /// | `ast_node_type` | Output |
151 /// +--------------------+--------+
152 /// | `document_start` | `^` |
153 /// | `document_end` | `$` |
154 /// | `object_begin` | `{` |
155 /// | `object_end` | `}` |
156 /// | `array_begin` | `[` |
157 /// | `array_end` | `]` |
158 /// | `string_canonical` | `s` |
159 /// | `string_escaped` | `S` |
160 /// | `key_canonical` | `k` |
161 /// | `key_escaped` | `K` |
162 /// | `literal_true` | `t` |
163 /// | `literal_false` | `f` |
164 /// | `literal_null` | `n` |
165 /// | `integer` | `i` |
166 /// | `decimal` | `d` |
167 /// | `error` | `!` |
168 /// +--------------------+--------+
169 ///
170 /// This exists primarily for debugging purposes.
171 friend std::ostream& operator<<(std::ostream&, const parse_index&);
172 friend std::string to_string(const parse_index&);
173 /// \}
174
175private:
176 struct impl;
177
178 explicit parse_index(impl*) noexcept;
179
180private:
181 impl* _impl = nullptr;
182};
183
184/// \}
185
186}
Utilities for directly dealing with a JSON AST.
Represents an entry in a JSON AST.
Definition ast.hpp:160
An adapter for enumeration types.
Configuration for various extraction options. This becomes part of the extraction_context.
Represents the index of a parsed AST.
parse_index() noexcept=default
Creates an empty not-an-AST instance.
friend std::ostream & operator<<(std::ostream &, const parse_index &)
value extract_tree(const extract_options &options) const
Configuration for various parsing options.
Definition parse.hpp:63
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
Definition value.hpp:107
Copyright (c) 2014-2020 by Travis Gockel.
#define JSONV_PUBLIC
This function or class is part of the public API for JSON Voorhees.
Definition config.hpp:102
JSONV_PUBLIC void swap(value &a, value &b) noexcept
Swap the values a and b.
STL namespace.
std::optional< T > optional
Represents a value that may or may not be present.
Definition optional.hpp:22
std::string_view string_view
A non-owning reference to a string.