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 "env.hpp"
12 #include "exception.hpp"
13 
14 #include <string>
15 #include <cstdlib>
16 #include <cstddef>
17 #include <cassert>
18 #include <functional>
19 
20 namespace orcus {
21 
22 class ORCUS_PSR_DLLPUBLIC parser_base
23 {
24 protected:
25  using numeric_parser_type = std::function<const char*(const char*, const char*, double&)>;
26 
27  const char* const mp_begin;
28  const char* mp_char;
29  const char* mp_end;
30 
31 private:
32  numeric_parser_type m_func_parse_numeric;
33 
34 protected:
35  parser_base(const char* p, size_t n);
36 
37  void set_numeric_parser(const numeric_parser_type& func)
38  {
39  m_func_parse_numeric = func;
40  }
41 
42  bool has_char() const
43  {
44  assert(mp_char <= mp_end);
45  return mp_char != mp_end;
46  }
47 
48  bool has_next() const
49  {
50  assert((mp_char+1) <= mp_end);
51  return (mp_char+1) != mp_end;
52  }
53 
54  void next(size_t inc=1) { mp_char += inc; }
55 
56  void prev(size_t dec=1);
57 
58  char cur_char() const { return *mp_char; }
59 
73  char peek_char(std::size_t offset=1) const;
74 
87  std::string_view peek_chars(std::size_t length) const;
88 
94  void skip_bom();
95 
96  void skip(std::string_view chars_to_skip);
97 
102 
112  bool parse_expected(std::string_view expected);
113 
120  double parse_double();
121 
130  size_t remaining_size() const;
131 
138  size_t available_size() const
139  {
140  return std::distance(mp_char, mp_end);
141  }
142 
148  std::ptrdiff_t offset() const;
149 };
150 
151 }
152 
153 #endif
154 
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: parser_base.hpp:23
size_t available_size() const
Definition: parser_base.hpp:138
void skip_space_and_control()
size_t remaining_size() const
std::ptrdiff_t offset() const
char peek_char(std::size_t offset=1) const
bool parse_expected(std::string_view expected)
std::string_view peek_chars(std::size_t length) const