scanner.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 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. #include <pjlib-util/scanner.h>
  22. #include <pj++/string.hpp>
  23. class Pj_Cis;
  24. class Pj_Cis_Buffer;
  25. class Pj_Scanner;
  26. class Pj_Cis_Buffer
  27. {
  28. friend class Pj_Cis;
  29. public:
  30. Pj_Cis_Buffer()
  31. {
  32. pj_cis_buf_init(&buf_);
  33. }
  34. private:
  35. pj_cis_buf_t buf_;
  36. };
  37. class Pj_Cis
  38. {
  39. friend class Pj_Scanner;
  40. public:
  41. Pj_Cis(Pj_Cis_Buffer *buf)
  42. {
  43. pj_cis_init(&buf->buf_, &cis_);
  44. }
  45. Pj_Cis(const Pj_Cis &rhs)
  46. {
  47. pj_cis_dup(&cis_, (pj_cis_t*)&rhs.cis_);
  48. }
  49. void add_range(int start, int end)
  50. {
  51. pj_cis_add_range(&cis_, start, end);
  52. }
  53. void add_alpha()
  54. {
  55. pj_cis_add_alpha(&cis_);
  56. }
  57. void add_num()
  58. {
  59. pj_cis_add_num(&cis_);
  60. }
  61. void add_str(const char *str)
  62. {
  63. pj_cis_add_str(&cis_, str);
  64. }
  65. void add_cis(const Pj_Cis &rhs)
  66. {
  67. pj_cis_add_cis(&cis_, &rhs.cis_);
  68. }
  69. void del_range(int start, int end)
  70. {
  71. pj_cis_del_range(&cis_, start, end);
  72. }
  73. void del_str(const char *str)
  74. {
  75. pj_cis_del_str(&cis_, str);
  76. }
  77. void invert()
  78. {
  79. pj_cis_invert(&cis_);
  80. }
  81. bool match(int c) const
  82. {
  83. return pj_cis_match(&cis_, c) != 0;
  84. }
  85. private:
  86. pj_cis_t cis_;
  87. };
  88. class Pj_Scanner
  89. {
  90. public:
  91. Pj_Scanner() {}
  92. enum
  93. {
  94. SYNTAX_ERROR = 101
  95. };
  96. static void syntax_error_handler_throw_pj(pj_scanner *);
  97. typedef pj_scan_state State;
  98. void init(char *buf, int len, unsigned options=PJ_SCAN_AUTOSKIP_WS,
  99. pj_syn_err_func_ptr callback = &syntax_error_handler_throw_pj)
  100. {
  101. pj_scan_init(&scanner_, buf, len, options, callback);
  102. }
  103. void fini()
  104. {
  105. pj_scan_fini(&scanner_);
  106. }
  107. int eof() const
  108. {
  109. return pj_scan_is_eof(&scanner_);
  110. }
  111. int peek_char() const
  112. {
  113. return *scanner_.curptr;
  114. }
  115. int peek(const Pj_Cis *cis, Pj_String *out)
  116. {
  117. return pj_scan_peek(&scanner_, &cis->cis_, out);
  118. }
  119. int peek_n(pj_size_t len, Pj_String *out)
  120. {
  121. return pj_scan_peek_n(&scanner_, len, out);
  122. }
  123. int peek_until(const Pj_Cis *cis, Pj_String *out)
  124. {
  125. return pj_scan_peek_until(&scanner_, &cis->cis_, out);
  126. }
  127. void get(const Pj_Cis *cis, Pj_String *out)
  128. {
  129. pj_scan_get(&scanner_, &cis->cis_, out);
  130. }
  131. void get_n(unsigned N, Pj_String *out)
  132. {
  133. pj_scan_get_n(&scanner_, N, out);
  134. }
  135. int get_char()
  136. {
  137. return pj_scan_get_char(&scanner_);
  138. }
  139. void get_quote(int begin_quote, int end_quote, Pj_String *out)
  140. {
  141. pj_scan_get_quote(&scanner_, begin_quote, end_quote, out);
  142. }
  143. void get_newline()
  144. {
  145. pj_scan_get_newline(&scanner_);
  146. }
  147. void get_until(const Pj_Cis *cis, Pj_String *out)
  148. {
  149. pj_scan_get_until(&scanner_, &cis->cis_, out);
  150. }
  151. void get_until_ch(int until_ch, Pj_String *out)
  152. {
  153. pj_scan_get_until_ch(&scanner_, until_ch, out);
  154. }
  155. void get_until_chr(const char *spec, Pj_String *out)
  156. {
  157. pj_scan_get_until_chr(&scanner_, spec, out);
  158. }
  159. void advance_n(unsigned N, bool skip_ws=true)
  160. {
  161. pj_scan_advance_n(&scanner_, N, skip_ws);
  162. }
  163. int strcmp(const char *s, int len)
  164. {
  165. return pj_scan_strcmp(&scanner_, s, len);
  166. }
  167. int stricmp(const char *s, int len)
  168. {
  169. return pj_scan_stricmp(&scanner_, s, len);
  170. }
  171. void skip_ws()
  172. {
  173. pj_scan_skip_whitespace(&scanner_);
  174. }
  175. void save_state(State *state) const
  176. {
  177. pj_scan_save_state(&scanner_, state);
  178. }
  179. void restore_state(State *state)
  180. {
  181. pj_scan_restore_state(&scanner_, state);
  182. }
  183. int get_pos_line() const
  184. {
  185. return scanner_.line;
  186. }
  187. int get_pos_col() const
  188. {
  189. return pj_scan_get_col(&scanner_);
  190. }
  191. private:
  192. pj_scanner scanner_;
  193. };
  194. #endif /* __PJPP_SCANNER_HPP__ */