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
19namespace orcus {
20
25class ORCUS_PSR_DLLPUBLIC parse_error : public general_error
26{
27 std::ptrdiff_t m_offset;
28protected:
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
35public:
36 std::ptrdiff_t offset() const;
37};
38
39class ORCUS_PSR_DLLPUBLIC parser_base
40{
41protected:
42 const char* const mp_begin;
43 const char* mp_char;
44 const char* mp_end;
45 const bool m_transient_stream;
46
47protected:
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: */
Definition: exception.hpp:19
Definition: parser_base.hpp:26
parse_error(const std::string &msg, std::ptrdiff_t offset)
offset in the stream where the error occurred.
Definition: parser_base.hpp:40
size_t available_size() const
Definition: parser_base.hpp:115
void skip_space_and_control()
size_t remaining_size() const
std::ptrdiff_t offset() const
bool parse_expected(const char *expected, size_t n_expected)