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 
17 namespace orcus { namespace yaml {
18 
19 class ORCUS_PSR_DLLPUBLIC parse_error : public ::orcus::parse_error
20 {
21 public:
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 
28 namespace detail {
29 
30 enum class scope_t
31 {
32  unset,
33  sequence,
34  map,
35  multi_line_string
36 };
37 
38 enum class keyword_t
39 {
40  unknown,
41  boolean_true,
42  boolean_false,
43  null
44 };
45 
46 enum 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 
75 class ORCUS_PSR_DLLPUBLIC parser_base : public ::orcus::parser_base
76 {
77  struct impl;
78  std::unique_ptr<impl> mp_impl;
79 
80 protected:
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 
115  size_t offset_last_char_of_line() const;
116 
122  size_t parse_indent();
123 
128  pstring parse_to_end_of_line();
129 
134  void skip_comment();
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: yaml_parser_base.hpp:90
Definition: pstring.hpp:24
Definition: parser_base.hpp:35
Definition: yaml_parser_base.hpp:19
Definition: parser_base.hpp:21
Definition: yaml_parser_base.hpp:75