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 
13 #include <memory>
14 #include <cassert>
15 
16 namespace orcus { namespace yaml {
17 
18 class ORCUS_PSR_DLLPUBLIC parse_error : public ::orcus::parse_error
19 {
20 public:
21  parse_error(const std::string& msg, std::ptrdiff_t offset);
22 
23  static void throw_with(const char* msg_before, char c, const char* msg_after, std::ptrdiff_t offset);
24  static void throw_with(const char* msg_before, const char* p, size_t n, const char* msg_after, std::ptrdiff_t offset);
25 };
26 
27 namespace detail {
28 
29 enum class scope_t
30 {
31  unset,
32  sequence,
33  map,
34  multi_line_string
35 };
36 
37 enum class keyword_t
38 {
39  unknown,
40  boolean_true,
41  boolean_false,
42  null
43 };
44 
45 enum class parse_token_t
46 {
47  unknown,
48 
49  // handler tokens (tokens associated with handler events)
50 
51  begin_parse,
52  end_parse,
53  begin_document,
54  end_document,
55  begin_sequence,
56  end_sequence,
57  begin_map,
58  end_map,
59  begin_map_key,
60  end_map_key,
61  string,
62  number,
63  boolean_true,
64  boolean_false,
65  null,
66 
67  // non-handler tokens
68 
69  begin_sequence_element
70 };
71 
72 }
73 
74 class ORCUS_PSR_DLLPUBLIC parser_base : public ::orcus::parser_base
75 {
76  struct impl;
77  std::unique_ptr<impl> mp_impl;
78 
79 protected:
80 
81  // The entire line is empty.
82  static const size_t parse_indent_blank_line;
83 
84  // End of stream has reached while parsing in the indent part of a line.
85  static const size_t parse_indent_end_of_stream;
86 
87  static const size_t scope_empty;
88 
89  struct key_value
90  {
91  std::string_view key;
92  std::string_view value;
93  };
94 
95  parser_base() = delete;
96  parser_base(const parser_base&) = delete;
97  parser_base& operator=(const parser_base&) = delete;
98 
99  parser_base(const char* p, size_t n);
100  ~parser_base();
101 
102  void push_parse_token(detail::parse_token_t t);
103 
104  detail::parse_token_t get_last_parse_token() const;
105 
114  size_t offset_last_char_of_line() const;
115 
121  size_t parse_indent();
122 
127  std::string_view parse_to_end_of_line();
128 
133  void skip_comment();
134 
135  void reset_on_new_line();
136 
137  size_t get_scope() const;
138 
139  void push_scope(size_t scope_width);
140 
141  void clear_scopes();
142 
143  detail::scope_t get_scope_type() const;
144 
145  void set_scope_type(detail::scope_t type);
146 
152  size_t pop_scope();
153 
154  void push_line_back(const char* p, size_t n);
155 
156  std::string_view pop_line_front();
157 
158  bool has_line_buffer() const;
159 
160  size_t get_line_buffer_count() const;
161 
162  std::string_view merge_line_buffer();
163 
170  const char* get_doc_hash() const;
171 
179  void set_doc_hash(const char* hash);
180 
181  detail::keyword_t parse_keyword(const char* p, size_t len);
182 
183  key_value parse_key_value(const char* p, size_t len);
184 
185  std::string_view parse_single_quoted_string_value(const char*& p, size_t max_length);
186 
187  std::string_view parse_double_quoted_string_value(const char*& p, size_t max_length);
188 
189  void skip_blanks(const char*& p, size_t len);
190 
191  void start_literal_block();
192 
193  bool in_literal_block() const;
194 
195  void handle_line_in_literal(size_t indent);
196 
197  void handle_line_in_multi_line_string();
198 };
199 
200 }}
201 
202 #endif
203 
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: parser_base.hpp:27
Definition: parser_base.hpp:41
Definition: yaml_parser_base.hpp:19
Definition: yaml_parser_base.hpp:75
std::string_view parse_to_end_of_line()
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:90