blob: fb012056227d2ecd1407c72e2f8e3bc5e63a5b3e [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $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 Prijonoac9d1422006-01-18 23:32:27 +000025class Pj_Cis;
26class Pj_Cis_Buffer;
27class Pj_Scanner;
28
29class Pj_Cis_Buffer
Benny Prijono5dcb38d2005-11-21 01:55:47 +000030{
Benny Prijonoac9d1422006-01-18 23:32:27 +000031 friend class Pj_Cis;
32
Benny Prijono5dcb38d2005-11-21 01:55:47 +000033public:
Benny Prijonoac9d1422006-01-18 23:32:27 +000034 Pj_Cis_Buffer()
35 {
36 pj_cis_buf_init(&buf_);
Benny Prijono5dcb38d2005-11-21 01:55:47 +000037 }
38
39private:
Benny Prijonoac9d1422006-01-18 23:32:27 +000040 pj_cis_buf_t buf_;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000041};
42
Benny Prijonoac9d1422006-01-18 23:32:27 +000043
44class Pj_Cis
45{
46 friend class Pj_Scanner;
47
48public:
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
104private:
105 pj_cis_t cis_;
106};
107
108
109
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000110class Pj_Scanner
111{
112public:
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 Prijonoac9d1422006-01-18 23:32:27 +0000144 int peek(const Pj_Cis *cis, Pj_String *out)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000145 {
Benny Prijonoac9d1422006-01-18 23:32:27 +0000146 return pj_scan_peek(&scanner_, &cis->cis_, out);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000147 }
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 Prijonoac9d1422006-01-18 23:32:27 +0000154 int peek_until(const Pj_Cis *cis, Pj_String *out)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000155 {
Benny Prijonoac9d1422006-01-18 23:32:27 +0000156 return pj_scan_peek_until(&scanner_, &cis->cis_, out);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000157 }
158
Benny Prijonoac9d1422006-01-18 23:32:27 +0000159 void get(const Pj_Cis *cis, Pj_String *out)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000160 {
Benny Prijonoac9d1422006-01-18 23:32:27 +0000161 pj_scan_get(&scanner_, &cis->cis_, out);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000162 }
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 Prijonoac9d1422006-01-18 23:32:27 +0000184 void get_until(const Pj_Cis *cis, Pj_String *out)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000185 {
Benny Prijonoac9d1422006-01-18 23:32:27 +0000186 pj_scan_get_until(&scanner_, &cis->cis_, out);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000187 }
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 Prijonoac9d1422006-01-18 23:32:27 +0000219 void save_state(State *state) const
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000220 {
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 Prijonoac9d1422006-01-18 23:32:27 +0000236 return pj_scan_get_col(&scanner_);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000237 }
238
239
240private:
241 pj_scanner scanner_;
242};
243
244#endif /* __PJPP_SCANNER_HPP__ */
245