Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #ifndef __PJPP_SCANNER_HPP__ |
| 20 | #define __PJPP_SCANNER_HPP__ |
| 21 | |
| 22 | #include <pjlib-util/scanner.h> |
| 23 | #include <pj++/string.hpp> |
| 24 | |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 25 | class Pj_Cis; |
| 26 | class Pj_Cis_Buffer; |
| 27 | class Pj_Scanner; |
| 28 | |
| 29 | class Pj_Cis_Buffer |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 30 | { |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 31 | friend class Pj_Cis; |
| 32 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 33 | public: |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 34 | Pj_Cis_Buffer() |
| 35 | { |
| 36 | pj_cis_buf_init(&buf_); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | private: |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 40 | pj_cis_buf_t buf_; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 43 | |
| 44 | class Pj_Cis |
| 45 | { |
| 46 | friend class Pj_Scanner; |
| 47 | |
| 48 | public: |
| 49 | Pj_Cis(Pj_Cis_Buffer *buf) |
| 50 | { |
| 51 | pj_cis_init(&buf->buf_, &cis_); |
| 52 | } |
| 53 | |
| 54 | Pj_Cis(const Pj_Cis &rhs) |
| 55 | { |
| 56 | pj_cis_dup(&cis_, (pj_cis_t*)&rhs.cis_); |
| 57 | } |
| 58 | |
| 59 | void add_range(int start, int end) |
| 60 | { |
| 61 | pj_cis_add_range(&cis_, start, end); |
| 62 | } |
| 63 | |
| 64 | void add_alpha() |
| 65 | { |
| 66 | pj_cis_add_alpha(&cis_); |
| 67 | } |
| 68 | |
| 69 | void add_num() |
| 70 | { |
| 71 | pj_cis_add_num(&cis_); |
| 72 | } |
| 73 | |
| 74 | void add_str(const char *str) |
| 75 | { |
| 76 | pj_cis_add_str(&cis_, str); |
| 77 | } |
| 78 | |
| 79 | void add_cis(const Pj_Cis &rhs) |
| 80 | { |
| 81 | pj_cis_add_cis(&cis_, &rhs.cis_); |
| 82 | } |
| 83 | |
| 84 | void del_range(int start, int end) |
| 85 | { |
| 86 | pj_cis_del_range(&cis_, start, end); |
| 87 | } |
| 88 | |
| 89 | void del_str(const char *str) |
| 90 | { |
| 91 | pj_cis_del_str(&cis_, str); |
| 92 | } |
| 93 | |
| 94 | void invert() |
| 95 | { |
| 96 | pj_cis_invert(&cis_); |
| 97 | } |
| 98 | |
| 99 | bool match(int c) const |
| 100 | { |
| 101 | return pj_cis_match(&cis_, c) != 0; |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | pj_cis_t cis_; |
| 106 | }; |
| 107 | |
| 108 | |
| 109 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 110 | class Pj_Scanner |
| 111 | { |
| 112 | public: |
| 113 | Pj_Scanner() {} |
| 114 | |
| 115 | enum |
| 116 | { |
| 117 | SYNTAX_ERROR = 101 |
| 118 | }; |
| 119 | static void syntax_error_handler_throw_pj(pj_scanner *); |
| 120 | |
| 121 | typedef pj_scan_state State; |
| 122 | |
| 123 | void init(char *buf, int len, unsigned options=PJ_SCAN_AUTOSKIP_WS, |
| 124 | pj_syn_err_func_ptr callback = &syntax_error_handler_throw_pj) |
| 125 | { |
| 126 | pj_scan_init(&scanner_, buf, len, options, callback); |
| 127 | } |
| 128 | |
| 129 | void fini() |
| 130 | { |
| 131 | pj_scan_fini(&scanner_); |
| 132 | } |
| 133 | |
| 134 | int eof() const |
| 135 | { |
| 136 | return pj_scan_is_eof(&scanner_); |
| 137 | } |
| 138 | |
| 139 | int peek_char() const |
| 140 | { |
| 141 | return *scanner_.curptr; |
| 142 | } |
| 143 | |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 144 | int peek(const Pj_Cis *cis, Pj_String *out) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 145 | { |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 146 | return pj_scan_peek(&scanner_, &cis->cis_, out); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int peek_n(pj_size_t len, Pj_String *out) |
| 150 | { |
| 151 | return pj_scan_peek_n(&scanner_, len, out); |
| 152 | } |
| 153 | |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 154 | int peek_until(const Pj_Cis *cis, Pj_String *out) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 155 | { |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 156 | return pj_scan_peek_until(&scanner_, &cis->cis_, out); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 159 | void get(const Pj_Cis *cis, Pj_String *out) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 160 | { |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 161 | pj_scan_get(&scanner_, &cis->cis_, out); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void get_n(unsigned N, Pj_String *out) |
| 165 | { |
| 166 | pj_scan_get_n(&scanner_, N, out); |
| 167 | } |
| 168 | |
| 169 | int get_char() |
| 170 | { |
| 171 | return pj_scan_get_char(&scanner_); |
| 172 | } |
| 173 | |
| 174 | void get_quote(int begin_quote, int end_quote, Pj_String *out) |
| 175 | { |
| 176 | pj_scan_get_quote(&scanner_, begin_quote, end_quote, out); |
| 177 | } |
| 178 | |
| 179 | void get_newline() |
| 180 | { |
| 181 | pj_scan_get_newline(&scanner_); |
| 182 | } |
| 183 | |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 184 | void get_until(const Pj_Cis *cis, Pj_String *out) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 185 | { |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 186 | pj_scan_get_until(&scanner_, &cis->cis_, out); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void get_until_ch(int until_ch, Pj_String *out) |
| 190 | { |
| 191 | pj_scan_get_until_ch(&scanner_, until_ch, out); |
| 192 | } |
| 193 | |
| 194 | void get_until_chr(const char *spec, Pj_String *out) |
| 195 | { |
| 196 | pj_scan_get_until_chr(&scanner_, spec, out); |
| 197 | } |
| 198 | |
| 199 | void advance_n(unsigned N, bool skip_ws=true) |
| 200 | { |
| 201 | pj_scan_advance_n(&scanner_, N, skip_ws); |
| 202 | } |
| 203 | |
| 204 | int strcmp(const char *s, int len) |
| 205 | { |
| 206 | return pj_scan_strcmp(&scanner_, s, len); |
| 207 | } |
| 208 | |
| 209 | int stricmp(const char *s, int len) |
| 210 | { |
| 211 | return pj_scan_stricmp(&scanner_, s, len); |
| 212 | } |
| 213 | |
| 214 | void skip_ws() |
| 215 | { |
| 216 | pj_scan_skip_whitespace(&scanner_); |
| 217 | } |
| 218 | |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 219 | void save_state(State *state) const |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 220 | { |
| 221 | pj_scan_save_state(&scanner_, state); |
| 222 | } |
| 223 | |
| 224 | void restore_state(State *state) |
| 225 | { |
| 226 | pj_scan_restore_state(&scanner_, state); |
| 227 | } |
| 228 | |
| 229 | int get_pos_line() const |
| 230 | { |
| 231 | return scanner_.line; |
| 232 | } |
| 233 | |
| 234 | int get_pos_col() const |
| 235 | { |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 236 | return pj_scan_get_col(&scanner_); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
| 240 | private: |
| 241 | pj_scanner scanner_; |
| 242 | }; |
| 243 | |
| 244 | #endif /* __PJPP_SCANNER_HPP__ */ |
| 245 | |