| 1 | // match_results.hpp |
| 2 | // Copyright (c) 2015 Ben Hanson (http://www.benhanson.net/) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | #ifndef LEXERTL_MATCH_RESULTS_HPP |
| 7 | #define LEXERTL_MATCH_RESULTS_HPP |
| 8 | |
| 9 | #include "char_traits.hpp" |
| 10 | #include "enums.hpp" |
| 11 | #include <iterator> |
| 12 | #include <stack> |
| 13 | #include <string> |
| 14 | |
| 15 | namespace lexertl |
| 16 | { |
| 17 | template<typename iter, typename id_type = std::size_t, |
| 18 | std::size_t flags = bol_bit | eol_bit | skip_bit | again_bit | |
| 19 | multi_state_bit | advance_bit> |
| 20 | struct match_results |
| 21 | { |
| 22 | typedef iter iter_type; |
| 23 | typedef typename std::iterator_traits<iter_type>::value_type char_type; |
| 24 | typedef typename basic_char_traits<char_type>::index_type index_type; |
| 25 | typedef std::basic_string<char_type> string; |
| 26 | |
| 27 | id_type id; |
| 28 | id_type user_id; |
| 29 | iter_type start; |
| 30 | iter_type end; |
| 31 | iter_type eoi; |
| 32 | bool bol; |
| 33 | id_type state; |
| 34 | |
| 35 | match_results() : |
| 36 | id(0), |
| 37 | user_id(npos()), |
| 38 | start(iter_type()), |
| 39 | end(iter_type()), |
| 40 | eoi(iter_type()), |
| 41 | bol(true), |
| 42 | state(0) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | match_results(const iter_type &start_, const iter_type &end_) : |
| 47 | id(0), |
| 48 | user_id(npos()), |
| 49 | start(start_), |
| 50 | end(start_), |
| 51 | eoi(end_), |
| 52 | bol(true), |
| 53 | state(0) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | virtual ~match_results() |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | string str() const |
| 62 | { |
| 63 | return string(start, end); |
| 64 | } |
| 65 | |
| 66 | string substr(const std::size_t soffset_, const std::size_t eoffset_) const |
| 67 | { |
| 68 | return string(start + soffset_, end - eoffset_); |
| 69 | } |
| 70 | |
| 71 | virtual void clear() |
| 72 | { |
| 73 | id = 0; |
| 74 | user_id = npos(); |
| 75 | start = eoi; |
| 76 | end = eoi; |
| 77 | bol = true; |
| 78 | state = 0; |
| 79 | } |
| 80 | |
| 81 | virtual void reset(const iter_type &start_, const iter_type &end_) |
| 82 | { |
| 83 | id = 0; |
| 84 | user_id = npos(); |
| 85 | start = start_; |
| 86 | end = start_; |
| 87 | eoi = end_; |
| 88 | bol = true; |
| 89 | state = 0; |
| 90 | } |
| 91 | |
| 92 | static id_type npos() |
| 93 | { |
| 94 | return ~static_cast<id_type>(0); |
| 95 | } |
| 96 | |
| 97 | static id_type skip() |
| 98 | { |
| 99 | return ~static_cast<id_type>(1); |
| 100 | } |
| 101 | |
| 102 | bool operator ==(const match_results &rhs_) const |
| 103 | { |
| 104 | return id == rhs_.id && |
| 105 | user_id == rhs_.user_id && |
| 106 | start == rhs_.start && |
| 107 | end == rhs_.end && |
| 108 | eoi == rhs_.eoi && |
| 109 | bol == rhs_.bol && |
| 110 | state == rhs_.state; |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | template<typename iter, typename id_type = std::size_t, |
| 115 | std::size_t flags = bol_bit | eol_bit | skip_bit | again_bit | |
| 116 | multi_state_bit | recursive_bit | advance_bit> |
| 117 | struct recursive_match_results : public match_results<iter, id_type, flags> |
| 118 | { |
| 119 | typedef std::pair<id_type, id_type> id_type_pair; |
| 120 | std::stack<id_type_pair> stack; |
| 121 | |
| 122 | recursive_match_results() : |
| 123 | match_results<iter, id_type, flags>(), |
| 124 | stack() |
| 125 | { |
| 126 | } |
| 127 | |
| 128 | recursive_match_results(const iter &start_, const iter &end_) : |
| 129 | match_results<iter, id_type, flags>(start_, end_), |
| 130 | stack() |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | virtual ~recursive_match_results() |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | virtual void clear() |
| 139 | { |
| 140 | match_results<iter, id_type, flags>::clear(); |
| 141 | |
| 142 | while (!stack.empty()) stack.pop(); |
| 143 | } |
| 144 | |
| 145 | virtual void reset(const iter &start_, const iter &end_) |
| 146 | { |
| 147 | match_results<iter, id_type, flags>::reset(start_, end_); |
| 148 | |
| 149 | while (!stack.empty()) stack.pop(); |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | typedef match_results<std::string::const_iterator> smatch; |
| 154 | typedef match_results<const char *> cmatch; |
| 155 | typedef match_results<std::wstring::const_iterator> wsmatch; |
| 156 | typedef match_results<const wchar_t *> wcmatch; |
| 157 | |
| 158 | typedef recursive_match_results<std::string::const_iterator> |
| 159 | srmatch; |
| 160 | typedef recursive_match_results<const char *> crmatch; |
| 161 | typedef recursive_match_results<std::wstring::const_iterator> |
| 162 | wsrmatch; |
| 163 | typedef recursive_match_results<const wchar_t *> wcrmatch; |
| 164 | } |
| 165 | |
| 166 | #endif |