Orcus
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_PARSER_BASE_HPP
9 #define INCLUDED_ORCUS_PARSER_BASE_HPP
10 
11 #include "orcus/env.hpp"
12 #include "orcus/exception.hpp"
13 
14 #include <string>
15 #include <cstdlib>
16 #include <cstddef>
17 #include <cassert>
18 
19 namespace orcus {
20 
25 class ORCUS_PSR_DLLPUBLIC parse_error : public general_error
26 {
27  std::ptrdiff_t m_offset;
28 protected:
29  parse_error(const std::string& msg, std::ptrdiff_t offset);
30  parse_error(const std::string& cls, const std::string& msg, std::ptrdiff_t offset);
31 
32  static std::string build_message(const char* msg_before, char c, const char* msg_after);
33  static std::string build_message(const char* msg_before, const char* p, size_t n, const char* msg_after);
34 
35 public:
36  std::ptrdiff_t offset() const;
37 };
38 
39 class ORCUS_PSR_DLLPUBLIC parser_base
40 {
41 protected:
42  const char* const mp_begin;
43  const char* mp_char;
44  const char* mp_end;
45  const bool m_transient_stream;
46 
47 protected:
48  parser_base(const char* p, size_t n, bool transient_stream);
49 
50  bool transient_stream() const { return m_transient_stream; }
51 
52  bool has_char() const
53  {
54  assert(mp_char <= mp_end);
55  return mp_char != mp_end;
56  }
57 
58  bool has_next() const
59  {
60  assert((mp_char+1) <= mp_end);
61  return (mp_char+1) != mp_end;
62  }
63 
64  void next(size_t inc=1) { mp_char += inc; }
65 
66  void prev(size_t dec=1);
67 
68  char cur_char() const { return *mp_char; }
69 
70  char next_char() const;
71 
72  void skip(const char* chars_to_skip, size_t n_chars_to_skip);
73 
78 
89  bool parse_expected(const char* expected, size_t n_expected);
90 
97  double parse_double();
98 
107  size_t remaining_size() const;
108 
115  size_t available_size() const
116  {
117  return std::distance(mp_char, mp_end);
118  }
119 
125  std::ptrdiff_t offset() const;
126 };
127 
128 }
129 
130 #endif
131 
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
orcus::parser_base::skip_space_and_control
void skip_space_and_control()
orcus::parser_base::offset
std::ptrdiff_t offset() const
orcus::general_error
Definition: exception.hpp:19
orcus::parse_error::parse_error
parse_error(const std::string &msg, std::ptrdiff_t offset)
offset in the stream where the error occurred.
orcus::parser_base::available_size
size_t available_size() const
Definition: parser_base.hpp:115
orcus::parser_base::parse_expected
bool parse_expected(const char *expected, size_t n_expected)
orcus::parser_base::parse_double
double parse_double()
orcus::parser_base
Definition: parser_base.hpp:40
orcus::parse_error
Definition: parser_base.hpp:26
orcus::parser_base::remaining_size
size_t remaining_size() const