blob: 9e2d528f0c6df51399dfe01266d97eb950235c9d [file] [log] [blame]
Benny Prijonoe0312a72005-11-18 00:16:43 +00001/* $Id$ */
Benny Prijonoe7224612005-11-13 19:40:44 +00002/*
Benny Prijonoe0312a72005-11-18 00:16:43 +00003 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
Benny Prijonoe7224612005-11-13 19:40:44 +00004 *
Benny Prijonoe0312a72005-11-18 00:16:43 +00005 * 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.
Benny Prijonoe7224612005-11-13 19:40:44 +00009 *
Benny Prijonoe0312a72005-11-18 00:16:43 +000010 * This program is distributed in the hope that it will be useful,
Benny Prijonoe7224612005-11-13 19:40:44 +000011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Benny Prijonoe0312a72005-11-18 00:16:43 +000012 * 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
Benny Prijonoe7224612005-11-13 19:40:44 +000018 */
19#ifndef __PJPP_SCANNER_HPP__
20#define __PJPP_SCANNER_HPP__
21
22#include <pjlib-util/scanner.h>
23#include <pj++/string.hpp>
24
25class Pj_Char_Spec
26{
27public:
28 Pj_Char_Spec() { pj_cs_init(cs__); }
29
30 void set(int c) { pj_cs_set(cs__, c); }
31 void add_range(int begin, int end) { pj_cs_add_range(cs__, begin, end); }
32 void add_alpha() { pj_cs_add_alpha(cs__); }
33 void add_num() { pj_cs_add_num(cs__); }
34 void add_str(const char *str) { pj_cs_add_str(cs__, str); }
35 void del_range(int begin, int end) { pj_cs_del_range(cs__, begin, end); }
36 void del_str(const char *str) { pj_cs_del_str(cs__, str); }
37 void invert() { pj_cs_invert(cs__); }
38 int match(int c) { return pj_cs_match(cs__, c); }
39
40 pj_char_spec_element_t *cs_()
41 {
42 return cs__;
43 }
44
45 const pj_char_spec_element_t *cs_() const
46 {
47 return cs__;
48 }
49
50private:
51 pj_char_spec cs__;
52};
53
54class Pj_Scanner
55{
56public:
57 Pj_Scanner() {}
58
59 enum
60 {
61 SYNTAX_ERROR = 101
62 };
63 static void syntax_error_handler_throw_pj(pj_scanner *);
64
65 typedef pj_scan_state State;
66
67 void init(char *buf, int len, unsigned options=PJ_SCAN_AUTOSKIP_WS,
68 pj_syn_err_func_ptr callback = &syntax_error_handler_throw_pj)
69 {
70 pj_scan_init(&scanner_, buf, len, options, callback);
71 }
72
73 void fini()
74 {
75 pj_scan_fini(&scanner_);
76 }
77
78 int eof() const
79 {
80 return pj_scan_is_eof(&scanner_);
81 }
82
83 int peek_char() const
84 {
85 return *scanner_.curptr;
86 }
87
88 int peek(const Pj_Char_Spec *cs, Pj_String *out)
89 {
90 return pj_scan_peek(&scanner_, cs->cs_(), out);
91 }
92
93 int peek_n(pj_size_t len, Pj_String *out)
94 {
95 return pj_scan_peek_n(&scanner_, len, out);
96 }
97
98 int peek_until(const Pj_Char_Spec *cs, Pj_String *out)
99 {
100 return pj_scan_peek_until(&scanner_, cs->cs_(), out);
101 }
102
103 void get(const Pj_Char_Spec *cs, Pj_String *out)
104 {
105 pj_scan_get(&scanner_, cs->cs_(), out);
106 }
107
108 void get_n(unsigned N, Pj_String *out)
109 {
110 pj_scan_get_n(&scanner_, N, out);
111 }
112
113 int get_char()
114 {
115 return pj_scan_get_char(&scanner_);
116 }
117
118 void get_quote(int begin_quote, int end_quote, Pj_String *out)
119 {
120 pj_scan_get_quote(&scanner_, begin_quote, end_quote, out);
121 }
122
123 void get_newline()
124 {
125 pj_scan_get_newline(&scanner_);
126 }
127
128 void get_until(const Pj_Char_Spec *cs, Pj_String *out)
129 {
130 pj_scan_get_until(&scanner_, cs->cs_(), out);
131 }
132
133 void get_until_ch(int until_ch, Pj_String *out)
134 {
135 pj_scan_get_until_ch(&scanner_, until_ch, out);
136 }
137
138 void get_until_chr(const char *spec, Pj_String *out)
139 {
140 pj_scan_get_until_chr(&scanner_, spec, out);
141 }
142
143 void advance_n(unsigned N, bool skip_ws=true)
144 {
145 pj_scan_advance_n(&scanner_, N, skip_ws);
146 }
147
148 int strcmp(const char *s, int len)
149 {
150 return pj_scan_strcmp(&scanner_, s, len);
151 }
152
153 int stricmp(const char *s, int len)
154 {
155 return pj_scan_stricmp(&scanner_, s, len);
156 }
157
158 void skip_ws()
159 {
160 pj_scan_skip_whitespace(&scanner_);
161 }
162
163 void save_state(State *state)
164 {
165 pj_scan_save_state(&scanner_, state);
166 }
167
168 void restore_state(State *state)
169 {
170 pj_scan_restore_state(&scanner_, state);
171 }
172
173 int get_pos_line() const
174 {
175 return scanner_.line;
176 }
177
178 int get_pos_col() const
179 {
180 return scanner_.col;
181 }
182
183
184private:
185 pj_scanner scanner_;
186};
187
188#endif /* __PJPP_SCANNER_HPP__ */
189