scanner.hpp 4.5 KB

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