Orcus
yaml_parser_base.hpp
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 */
7
8#ifndef INCLUDED_ORCUS_YAML_PARSER_BASE_HPP
9#define INCLUDED_ORCUS_YAML_PARSER_BASE_HPP
10
11#include "orcus/parser_base.hpp"
12#include "orcus/pstring.hpp"
13
14#include <memory>
15#include <cassert>
16
17namespace orcus { namespace yaml {
18
19class ORCUS_PSR_DLLPUBLIC parse_error : public ::orcus::parse_error
20{
21public:
22 parse_error(const std::string& msg, std::ptrdiff_t offset);
23
24 static void throw_with(const char* msg_before, char c, const char* msg_after, std::ptrdiff_t offset);
25 static void throw_with(const char* msg_before, const char* p, size_t n, const char* msg_after, std::ptrdiff_t offset);
26};
27
28namespace detail {
29
30enum class scope_t
31{
32 unset,
33 sequence,
34 map,
35 multi_line_string
36};
37
38enum class keyword_t
39{
40 unknown,
41 boolean_true,
42 boolean_false,
43 null
44};
45
46enum class parse_token_t
47{
48 unknown,
49
50 // handler tokens (tokens associated with handler events)
51
52 begin_parse,
53 end_parse,
54 begin_document,
55 end_document,
56 begin_sequence,
57 end_sequence,
58 begin_map,
59 end_map,
60 begin_map_key,
61 end_map_key,
62 string,
63 number,
64 boolean_true,
65 boolean_false,
66 null,
67
68 // non-handler tokens
69
70 begin_sequence_element
71};
72
73}
74
75class ORCUS_PSR_DLLPUBLIC parser_base : public ::orcus::parser_base
76{
77 struct impl;
78 std::unique_ptr<impl> mp_impl;
79
80protected:
81
82 // The entire line is empty.
83 static const size_t parse_indent_blank_line;
84
85 // End of stream has reached while parsing in the indent part of a line.
86 static const size_t parse_indent_end_of_stream;
87
88 static const size_t scope_empty;
89
90 struct key_value
91 {
92 pstring key;
93 pstring value;
94 };
95
96 parser_base() = delete;
97 parser_base(const parser_base&) = delete;
98 parser_base& operator=(const parser_base&) = delete;
99
100 parser_base(const char* p, size_t n);
101 ~parser_base();
102
103 void push_parse_token(detail::parse_token_t t);
104
105 detail::parse_token_t get_last_parse_token() const;
106
116
122 size_t parse_indent();
123
129
135
136 void reset_on_new_line();
137
138 size_t get_scope() const;
139
140 void push_scope(size_t scope_width);
141
142 void clear_scopes();
143
144 detail::scope_t get_scope_type() const;
145
146 void set_scope_type(detail::scope_t type);
147
153 size_t pop_scope();
154
155 void push_line_back(const char* p, size_t n);
156
157 pstring pop_line_front();
158
159 bool has_line_buffer() const;
160
161 size_t get_line_buffer_count() const;
162
163 pstring merge_line_buffer();
164
171 const char* get_doc_hash() const;
172
180 void set_doc_hash(const char* hash);
181
182 detail::keyword_t parse_keyword(const char* p, size_t len);
183
184 key_value parse_key_value(const char* p, size_t len);
185
186 pstring parse_single_quoted_string_value(const char*& p, size_t max_length);
187
188 pstring parse_double_quoted_string_value(const char*& p, size_t max_length);
189
190 void skip_blanks(const char*& p, size_t len);
191
192 void start_literal_block();
193
194 bool in_literal_block() const;
195
196 void handle_line_in_literal(size_t indent);
197
198 void handle_line_in_multi_line_string();
199};
200
201}}
202
203#endif
204
205/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: parser_base.hpp:26
Definition: parser_base.hpp:40
Definition: pstring.hpp:28
Definition: yaml_parser_base.hpp:20
Definition: yaml_parser_base.hpp:76
const char * get_doc_hash() const
void set_doc_hash(const char *hash)
size_t offset_last_char_of_line() const
Definition: yaml_parser_base.hpp:91