lispreader.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* $Id: lispreader.h,v 1.9 2004/05/11 22:16:12 sik0fewl Exp $ */
  2. /*
  3. * lispreader.h
  4. *
  5. * Copyright (C) 1998-2000 Mark Probst
  6. * Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Library General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Library General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Library General Public
  19. * License along with this library; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. */
  23. #ifndef __LISPREADER_H__
  24. #define __LISPREADER_H__
  25. #include <stdio.h>
  26. #include <zlib.h>
  27. #include <string>
  28. #include <vector>
  29. #define LISP_STREAM_FILE 1
  30. #define LISP_STREAM_STRING 2
  31. #define LISP_STREAM_ANY 3
  32. #define LISP_TYPE_INTERNAL -3
  33. #define LISP_TYPE_PARSE_ERROR -2
  34. #define LISP_TYPE_EOF -1
  35. #define LISP_TYPE_NIL 0
  36. #define LISP_TYPE_SYMBOL 1
  37. #define LISP_TYPE_INTEGER 2
  38. #define LISP_TYPE_STRING 3
  39. #define LISP_TYPE_REAL 4
  40. #define LISP_TYPE_CONS 5
  41. #define LISP_TYPE_PATTERN_CONS 6
  42. #define LISP_TYPE_BOOLEAN 7
  43. #define LISP_TYPE_PATTERN_VAR 8
  44. #define LISP_PATTERN_ANY 1
  45. #define LISP_PATTERN_SYMBOL 2
  46. #define LISP_PATTERN_STRING 3
  47. #define LISP_PATTERN_INTEGER 4
  48. #define LISP_PATTERN_REAL 5
  49. #define LISP_PATTERN_BOOLEAN 6
  50. #define LISP_PATTERN_LIST 7
  51. #define LISP_PATTERN_OR 8
  52. class LispReaderException : public std::exception
  53. {
  54. public:
  55. LispReaderException(const char* _message, const char* _file = "", const unsigned int _line = 0)
  56. : message(_message), file(_file), line(_line) { };
  57. virtual ~LispReaderException() throw() { };
  58. const char* what() const throw() { return message; };
  59. const char* what_file() const throw() { return file; };
  60. const unsigned int what_line() const throw() { return line; };
  61. private:
  62. const char* message;
  63. const char* file;
  64. const unsigned int line;
  65. };
  66. typedef struct
  67. {
  68. int type;
  69. union
  70. {
  71. FILE *file;
  72. struct
  73. {
  74. char *buf;
  75. int pos;
  76. }
  77. string;
  78. struct
  79. {
  80. void *data;
  81. int (*next_char) (void *data);
  82. void (*unget_char) (char c, void *data);
  83. }
  84. any;
  85. } v;
  86. }
  87. lisp_stream_t;
  88. typedef struct _lisp_object_t lisp_object_t;
  89. struct _lisp_object_t
  90. {
  91. int type;
  92. union
  93. {
  94. struct
  95. {
  96. struct _lisp_object_t *car;
  97. struct _lisp_object_t *cdr;
  98. }
  99. cons;
  100. char *string;
  101. int integer;
  102. float real;
  103. struct
  104. {
  105. int type;
  106. int index;
  107. struct _lisp_object_t *sub;
  108. }
  109. pattern;
  110. } v;
  111. };
  112. lisp_stream_t* lisp_stream_init_gzfile (lisp_stream_t *stream, gzFile file);
  113. lisp_stream_t* lisp_stream_init_file (lisp_stream_t *stream, FILE *file);
  114. lisp_stream_t* lisp_stream_init_string (lisp_stream_t *stream, char *buf);
  115. lisp_stream_t* lisp_stream_init_any (lisp_stream_t *stream, void *data,
  116. int (*next_char) (void *data),
  117. void (*unget_char) (char c, void *data));
  118. lisp_object_t* lisp_read (lisp_stream_t *in);
  119. lisp_object_t* lisp_read_from_file(const std::string& filename);
  120. void lisp_free (lisp_object_t *obj);
  121. lisp_object_t* lisp_read_from_string (const char *buf);
  122. int lisp_compile_pattern (lisp_object_t **obj, int *num_subs);
  123. int lisp_match_pattern (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars, int num_subs);
  124. int lisp_match_string (const char *pattern_string, lisp_object_t *obj, lisp_object_t **vars);
  125. int lisp_type (lisp_object_t *obj);
  126. int lisp_integer (lisp_object_t *obj);
  127. float lisp_real (lisp_object_t *obj);
  128. char* lisp_symbol (lisp_object_t *obj);
  129. char* lisp_string (lisp_object_t *obj);
  130. int lisp_boolean (lisp_object_t *obj);
  131. lisp_object_t* lisp_car (lisp_object_t *obj);
  132. lisp_object_t* lisp_cdr (lisp_object_t *obj);
  133. lisp_object_t* lisp_cxr (lisp_object_t *obj, const char *x);
  134. lisp_object_t* lisp_make_integer (int value);
  135. lisp_object_t* lisp_make_real (float value);
  136. lisp_object_t* lisp_make_symbol (const char *value);
  137. lisp_object_t* lisp_make_string (const char *value);
  138. lisp_object_t* lisp_make_cons (lisp_object_t *car, lisp_object_t *cdr);
  139. lisp_object_t* lisp_make_boolean (int value);
  140. int lisp_list_length (lisp_object_t *obj);
  141. lisp_object_t* lisp_list_nth_cdr (lisp_object_t *obj, int index);
  142. lisp_object_t* lisp_list_nth (lisp_object_t *obj, int index);
  143. void lisp_dump (lisp_object_t *obj, FILE *out);
  144. #define lisp_nil() ((lisp_object_t*)0)
  145. #define lisp_nil_p(obj) (obj == 0)
  146. #define lisp_integer_p(obj) (lisp_type((obj)) == LISP_TYPE_INTEGER)
  147. #define lisp_real_p(obj) (lisp_type((obj)) == LISP_TYPE_REAL)
  148. #define lisp_symbol_p(obj) (lisp_type((obj)) == LISP_TYPE_SYMBOL)
  149. #define lisp_string_p(obj) (lisp_type((obj)) == LISP_TYPE_STRING)
  150. #define lisp_cons_p(obj) (lisp_type((obj)) == LISP_TYPE_CONS)
  151. #define lisp_boolean_p(obj) (lisp_type((obj)) == LISP_TYPE_BOOLEAN)
  152. class LispReader
  153. {
  154. private:
  155. lisp_object_t* lst;
  156. lisp_object_t* search_for(const char* name);
  157. public:
  158. /** cur == ((pos 1 2 3) (id 12 3 4)...) */
  159. LispReader (lisp_object_t* l);
  160. bool read_int_vector (const char* name, std::vector<int>* vec);
  161. bool read_char_vector (const char* name, std::vector<char>* vec);
  162. bool read_string_vector (const char* name, std::vector<std::string>* vec);
  163. bool read_string (const char* name, std::string* str);
  164. bool read_int (const char* name, int* i);
  165. bool read_float (const char* name, float* f);
  166. bool read_bool (const char* name, bool* b);
  167. bool read_lisp (const char* name, lisp_object_t** b);
  168. };
  169. class LispWriter
  170. {
  171. private:
  172. std::vector<lisp_object_t*> lisp_objs;
  173. void append (lisp_object_t* obj);
  174. lisp_object_t* make_list3 (lisp_object_t*, lisp_object_t*, lisp_object_t*);
  175. lisp_object_t* make_list2 (lisp_object_t*, lisp_object_t*);
  176. public:
  177. LispWriter (const char* name);
  178. void write_float (const char* name, float f);
  179. void write_int (const char* name, int i);
  180. void write_boolean (const char* name, bool b);
  181. void write_string (const char* name, const char* str);
  182. void write_symbol (const char* name, const char* symname);
  183. void write_lisp_obj(const char* name, lisp_object_t* lst);
  184. /** caller is responible to free the returned lisp_object_t */
  185. lisp_object_t* create_lisp ();
  186. };
  187. #endif