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
16#include <optional>
17
18namespace jsonv
19{
20
21class extract_options;
22class parse_options;
23
24/// \ingroup Value
25/// \{
26
27/// Represents the index of a parsed AST. When combined with the original text, can be used to create a \c value. See
28/// \c parse_index::parse to construct instances from JSON source text.
30{
31public:
33 {
34 public:
35 using value_type = ast_node;
36
37 public:
38 iterator() = default;
39
40 iterator& operator++();
41 iterator operator++(int)
42 {
43 iterator temp(*this);
44 ++*this;
45 return temp;
46 }
47
48 value_type operator*() const;
49
50 // Note the lack of comparison between `_prefix` -- valid `iterator`s will always have the same `_prefix`
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 bool operator> (const iterator& other) const { return _iter > other._iter; }
56 bool operator>=(const iterator& other) const { return _iter >= other._iter; }
57
58 private:
59 explicit iterator(const std::uintptr_t prefix, const std::uint64_t* iter) :
60 _prefix(prefix),
61 _iter(iter)
62 { }
63
64 friend class parse_index;
65
66 private:
67 std::uintptr_t _prefix;
68 const std::uint64_t* _iter;
69 };
70
72
73public:
74 /// Creates an empty not-an-AST instance.
76
78 _impl(std::exchange(src._impl, nullptr))
79 { }
80
81 parse_index& operator=(parse_index&& src) noexcept
82 {
83 if (this == &src)
84 return *this;
85
86 using std::swap;
87 swap(_impl, src._impl);
88 src.reset();
89
90 return *this;
91 }
92
93 ~parse_index() noexcept;
94
95 /// \{
96 /// Create an \c parse_index from the given \a src JSON.
97 ///
98 /// \param options
99 /// The options used to control parsing. If unspecified, these will be \c parse_options::create_default().
100 /// \param initial_buffer_capacity
101 /// The initial capacity of the underlying buffer. By default (\c nullopt), this will size the buffer according
102 /// to the length of the \a src string.
103 static parse_index parse(std::string_view src,
106 );
107 static parse_index parse(std::string_view src, std::optional<std::size_t> initial_buffer_capacity);
108 static parse_index parse(std::string_view src, const parse_options& options);
109 static parse_index parse(std::string_view src);
110 /// \}
111
112 /// Clear the contents of this instance.
113 void reset();
114
115 /// Check if this instance represents a valid AST. This will be \c false if the source JSON was not valid JSON text.
116 /// This will also be \c false if this instance was default-constructed or moved-from.
117 ///
118 /// \note
119 /// Even if this returns true, it is possible that conversion to a \c jsonv::value will throw an exception. For
120 /// example, if the value of a number exceeds the range of an \c int64_t. This is because JSON does not specify an
121 /// acceptable range for numbers, but the storage of \c jsonv::value does.
122 bool success() const noexcept;
123
124 /// See \ref success.
126
127 /// Validate that the parse was a \c success.
128 ///
129 /// \throws parse_error if the parse was not successful. This will contain additional details about why the parse
130 /// failed.
131 /// \throws std::invalid_argument if this instance was default-constructed or moved-from.
132 void validate() const;
133
134 iterator begin() const;
135 iterator cbegin() const { return begin(); }
136
137 iterator end() const;
138 iterator cend() const { return end(); }
139
140 /// \{
141 /// \param options
142 /// The options used to control how values are extracted from this source. If unspecified, these will be
143 /// \c extract_options::create_default().
145 value extract_tree() const;
146 /// \}
147
148 /// \{
149 /// Get a string representation of the AST.
150 ///
151 /// +--------------------+--------+
152 /// | `ast_node_type` | Output |
153 /// +--------------------+--------+
154 /// | `document_start` | `^` |
155 /// | `document_end` | `$` |
156 /// | `object_begin` | `{` |
157 /// | `object_end` | `}` |
158 /// | `array_begin` | `[` |
159 /// | `array_end` | `]` |
160 /// | `string_canonical` | `s` |
161 /// | `string_escaped` | `S` |
162 /// | `key_canonical` | `k` |
163 /// | `key_escaped` | `K` |
164 /// | `literal_true` | `t` |
165 /// | `literal_false` | `f` |
166 /// | `literal_null` | `n` |
167 /// | `integer` | `i` |
168 /// | `decimal` | `d` |
169 /// | `error` | `!` |
170 /// +--------------------+--------+
171 ///
172 /// This exists primarily for debugging purposes.
173 friend std::ostream& operator<<(std::ostream&, const parse_index&);
174 friend std::string to_string(const parse_index&);
175 /// \}
176
177private:
178 struct impl;
179
180 explicit parse_index(impl*) noexcept;
181
182private:
183 impl* _impl = nullptr;
184};
185
186/// \}
187
188}
Utilities for directly dealing with a JSON AST.
Represents an entry in a JSON AST.
Definition ast.hpp:159
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.