gscanner.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  19. * file for a list of people on the GLib Team. See the ChangeLog
  20. * files for a list of changes. These files are distributed with
  21. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  22. */
  23. #ifndef __G_SCANNER_H__
  24. #define __G_SCANNER_H__
  25. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  26. #error "Only <glib.h> can be included directly."
  27. #endif
  28. #include <glib/gdataset.h>
  29. #include <glib/ghash.h>
  30. G_BEGIN_DECLS
  31. typedef struct _GScanner GScanner;
  32. typedef struct _GScannerConfig GScannerConfig;
  33. typedef union _GTokenValue GTokenValue;
  34. typedef void (*GScannerMsgFunc) (GScanner *scanner,
  35. gchar *message,
  36. gboolean error);
  37. /* GScanner: Flexible lexical scanner for general purpose.
  38. */
  39. /* Character sets */
  40. #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  41. #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
  42. #define G_CSET_DIGITS "0123456789"
  43. #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
  44. "\307\310\311\312\313\314\315\316\317\320"\
  45. "\321\322\323\324\325\326"\
  46. "\330\331\332\333\334\335\336"
  47. #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
  48. "\347\350\351\352\353\354\355\356\357\360"\
  49. "\361\362\363\364\365\366"\
  50. "\370\371\372\373\374\375\376\377"
  51. /* Error types */
  52. typedef enum
  53. {
  54. G_ERR_UNKNOWN,
  55. G_ERR_UNEXP_EOF,
  56. G_ERR_UNEXP_EOF_IN_STRING,
  57. G_ERR_UNEXP_EOF_IN_COMMENT,
  58. G_ERR_NON_DIGIT_IN_CONST,
  59. G_ERR_DIGIT_RADIX,
  60. G_ERR_FLOAT_RADIX,
  61. G_ERR_FLOAT_MALFORMED
  62. } GErrorType;
  63. /* Token types */
  64. typedef enum
  65. {
  66. G_TOKEN_EOF = 0,
  67. G_TOKEN_LEFT_PAREN = '(',
  68. G_TOKEN_RIGHT_PAREN = ')',
  69. G_TOKEN_LEFT_CURLY = '{',
  70. G_TOKEN_RIGHT_CURLY = '}',
  71. G_TOKEN_LEFT_BRACE = '[',
  72. G_TOKEN_RIGHT_BRACE = ']',
  73. G_TOKEN_EQUAL_SIGN = '=',
  74. G_TOKEN_COMMA = ',',
  75. G_TOKEN_NONE = 256,
  76. G_TOKEN_ERROR,
  77. G_TOKEN_CHAR,
  78. G_TOKEN_BINARY,
  79. G_TOKEN_OCTAL,
  80. G_TOKEN_INT,
  81. G_TOKEN_HEX,
  82. G_TOKEN_FLOAT,
  83. G_TOKEN_STRING,
  84. G_TOKEN_SYMBOL,
  85. G_TOKEN_IDENTIFIER,
  86. G_TOKEN_IDENTIFIER_NULL,
  87. G_TOKEN_COMMENT_SINGLE,
  88. G_TOKEN_COMMENT_MULTI,
  89. /*< private >*/
  90. G_TOKEN_LAST
  91. } GTokenType;
  92. union _GTokenValue
  93. {
  94. gpointer v_symbol;
  95. gchar *v_identifier;
  96. gulong v_binary;
  97. gulong v_octal;
  98. gulong v_int;
  99. guint64 v_int64;
  100. gdouble v_float;
  101. gulong v_hex;
  102. gchar *v_string;
  103. gchar *v_comment;
  104. guchar v_char;
  105. guint v_error;
  106. };
  107. struct _GScannerConfig
  108. {
  109. /* Character sets
  110. */
  111. gchar *cset_skip_characters; /* default: " \t\n" */
  112. gchar *cset_identifier_first;
  113. gchar *cset_identifier_nth;
  114. gchar *cpair_comment_single; /* default: "#\n" */
  115. /* Should symbol lookup work case sensitive?
  116. */
  117. guint case_sensitive : 1;
  118. /* Boolean values to be adjusted "on the fly"
  119. * to configure scanning behaviour.
  120. */
  121. guint skip_comment_multi : 1; /* C like comment */
  122. guint skip_comment_single : 1; /* single line comment */
  123. guint scan_comment_multi : 1; /* scan multi line comments? */
  124. guint scan_identifier : 1;
  125. guint scan_identifier_1char : 1;
  126. guint scan_identifier_NULL : 1;
  127. guint scan_symbols : 1;
  128. guint scan_binary : 1;
  129. guint scan_octal : 1;
  130. guint scan_float : 1;
  131. guint scan_hex : 1; /* '0x0ff0' */
  132. guint scan_hex_dollar : 1; /* '$0ff0' */
  133. guint scan_string_sq : 1; /* string: 'anything' */
  134. guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
  135. guint numbers_2_int : 1; /* bin, octal, hex => int */
  136. guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
  137. guint identifier_2_string : 1;
  138. guint char_2_token : 1; /* return G_TOKEN_CHAR? */
  139. guint symbol_2_token : 1;
  140. guint scope_0_fallback : 1; /* try scope 0 on lookups? */
  141. guint store_int64 : 1; /* use value.v_int64 rather than v_int */
  142. /*< private >*/
  143. guint padding_dummy;
  144. };
  145. struct _GScanner
  146. {
  147. /* unused fields */
  148. gpointer user_data;
  149. guint max_parse_errors;
  150. /* g_scanner_error() increments this field */
  151. guint parse_errors;
  152. /* name of input stream, featured by the default message handler */
  153. const gchar *input_name;
  154. /* quarked data */
  155. GData *qdata;
  156. /* link into the scanner configuration */
  157. GScannerConfig *config;
  158. /* fields filled in after g_scanner_get_next_token() */
  159. GTokenType token;
  160. GTokenValue value;
  161. guint line;
  162. guint position;
  163. /* fields filled in after g_scanner_peek_next_token() */
  164. GTokenType next_token;
  165. GTokenValue next_value;
  166. guint next_line;
  167. guint next_position;
  168. /*< private >*/
  169. /* to be considered private */
  170. GHashTable *symbol_table;
  171. gint input_fd;
  172. const gchar *text;
  173. const gchar *text_end;
  174. gchar *buffer;
  175. guint scope_id;
  176. /*< public >*/
  177. /* handler function for _warn and _error */
  178. GScannerMsgFunc msg_handler;
  179. };
  180. GLIB_AVAILABLE_IN_ALL
  181. GScanner* g_scanner_new (const GScannerConfig *config_templ);
  182. GLIB_AVAILABLE_IN_ALL
  183. void g_scanner_destroy (GScanner *scanner);
  184. GLIB_AVAILABLE_IN_ALL
  185. void g_scanner_input_file (GScanner *scanner,
  186. gint input_fd);
  187. GLIB_AVAILABLE_IN_ALL
  188. void g_scanner_sync_file_offset (GScanner *scanner);
  189. GLIB_AVAILABLE_IN_ALL
  190. void g_scanner_input_text (GScanner *scanner,
  191. const gchar *text,
  192. guint text_len);
  193. GLIB_AVAILABLE_IN_ALL
  194. GTokenType g_scanner_get_next_token (GScanner *scanner);
  195. GLIB_AVAILABLE_IN_ALL
  196. GTokenType g_scanner_peek_next_token (GScanner *scanner);
  197. GLIB_AVAILABLE_IN_ALL
  198. GTokenType g_scanner_cur_token (GScanner *scanner);
  199. GLIB_AVAILABLE_IN_ALL
  200. GTokenValue g_scanner_cur_value (GScanner *scanner);
  201. GLIB_AVAILABLE_IN_ALL
  202. guint g_scanner_cur_line (GScanner *scanner);
  203. GLIB_AVAILABLE_IN_ALL
  204. guint g_scanner_cur_position (GScanner *scanner);
  205. GLIB_AVAILABLE_IN_ALL
  206. gboolean g_scanner_eof (GScanner *scanner);
  207. GLIB_AVAILABLE_IN_ALL
  208. guint g_scanner_set_scope (GScanner *scanner,
  209. guint scope_id);
  210. GLIB_AVAILABLE_IN_ALL
  211. void g_scanner_scope_add_symbol (GScanner *scanner,
  212. guint scope_id,
  213. const gchar *symbol,
  214. gpointer value);
  215. GLIB_AVAILABLE_IN_ALL
  216. void g_scanner_scope_remove_symbol (GScanner *scanner,
  217. guint scope_id,
  218. const gchar *symbol);
  219. GLIB_AVAILABLE_IN_ALL
  220. gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
  221. guint scope_id,
  222. const gchar *symbol);
  223. GLIB_AVAILABLE_IN_ALL
  224. void g_scanner_scope_foreach_symbol (GScanner *scanner,
  225. guint scope_id,
  226. GHFunc func,
  227. gpointer user_data);
  228. GLIB_AVAILABLE_IN_ALL
  229. gpointer g_scanner_lookup_symbol (GScanner *scanner,
  230. const gchar *symbol);
  231. GLIB_AVAILABLE_IN_ALL
  232. void g_scanner_unexp_token (GScanner *scanner,
  233. GTokenType expected_token,
  234. const gchar *identifier_spec,
  235. const gchar *symbol_spec,
  236. const gchar *symbol_name,
  237. const gchar *message,
  238. gint is_error);
  239. GLIB_AVAILABLE_IN_ALL
  240. void g_scanner_error (GScanner *scanner,
  241. const gchar *format,
  242. ...) G_GNUC_PRINTF (2,3);
  243. GLIB_AVAILABLE_IN_ALL
  244. void g_scanner_warn (GScanner *scanner,
  245. const gchar *format,
  246. ...) G_GNUC_PRINTF (2,3);
  247. #ifndef G_DISABLE_DEPRECATED
  248. /* keep downward source compatibility */
  249. #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
  250. g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
  251. } G_STMT_END
  252. #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
  253. g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
  254. } G_STMT_END
  255. #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
  256. g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
  257. } G_STMT_END
  258. /* The following two functions are deprecated and will be removed in
  259. * the next major release. They do no good. */
  260. #define g_scanner_freeze_symbol_table(scanner) ((void)0)
  261. #define g_scanner_thaw_symbol_table(scanner) ((void)0)
  262. #endif /* G_DISABLE_DEPRECATED */
  263. G_END_DECLS
  264. #endif /* __G_SCANNER_H__ */