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 
21 class ORCUS_PSR_DLLPUBLIC parse_error : public general_error
22 {
23  std::ptrdiff_t m_offset;
24 protected:
25  parse_error(const std::string& msg, std::ptrdiff_t offset);
26  parse_error(const std::string& cls, const std::string& msg, std::ptrdiff_t offset);
27 
28  static std::string build_message(const char* msg_before, char c, const char* msg_after);
29  static std::string build_message(const char* msg_before, const char* p, size_t n, const char* msg_after);
30 
31 public:
32  std::ptrdiff_t offset() const;
33 };
34 
35 class ORCUS_PSR_DLLPUBLIC parser_base
36 {
37 protected:
38  const char* const mp_begin;
39  const char* mp_char;
40  const char* mp_end;
41 
42 protected:
43  parser_base(const char* p, size_t n);
44 
45  bool has_char() const
46  {
47  assert(mp_char <= mp_end);
48  return mp_char != mp_end;
49  }
50  bool has_next() const { return (mp_char+1) != mp_end; }
51 
52  void next(size_t inc=1);
53  void prev(size_t dec=1);
54  char cur_char() const;
55  char next_char() const;
56 
57  void skip(const char* chars_to_skip);
58 
68  bool parse_expected(const char* expected);
69 
76  double parse_double();
77 
86  size_t remaining_size() const;
87 
93  std::ptrdiff_t offset() const;
94 };
95 
96 }
97 
98 #endif
99 
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
orcus::general_error
Definition: exception.hpp:18
orcus::parser_base
Definition: parser_base.hpp:35
orcus::parse_error
Definition: parser_base.hpp:21