Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

279 строки
8.1 KiB

  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, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. */
  19. /*
  20. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  21. * file for a list of people on the GLib Team. See the ChangeLog
  22. * files for a list of changes. These files are distributed with
  23. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  24. */
  25. #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  26. #error "Only <glib.h> can be included directly."
  27. #endif
  28. #ifndef __G_SCANNER_H__
  29. #define __G_SCANNER_H__
  30. #include <glib/gdataset.h>
  31. #include <glib/ghash.h>
  32. G_BEGIN_DECLS
  33. typedef struct _GScanner GScanner;
  34. typedef struct _GScannerConfig GScannerConfig;
  35. typedef union _GTokenValue GTokenValue;
  36. typedef void (*GScannerMsgFunc) (GScanner *scanner,
  37. gchar *message,
  38. gboolean error);
  39. /* GScanner: Flexible lexical scanner for general purpose.
  40. */
  41. /* Character sets */
  42. #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  43. #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
  44. #define G_CSET_DIGITS "0123456789"
  45. #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
  46. "\307\310\311\312\313\314\315\316\317\320"\
  47. "\321\322\323\324\325\326"\
  48. "\330\331\332\333\334\335\336"
  49. #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
  50. "\347\350\351\352\353\354\355\356\357\360"\
  51. "\361\362\363\364\365\366"\
  52. "\370\371\372\373\374\375\376\377"
  53. /* Error types */
  54. typedef enum
  55. {
  56. G_ERR_UNKNOWN,
  57. G_ERR_UNEXP_EOF,
  58. G_ERR_UNEXP_EOF_IN_STRING,
  59. G_ERR_UNEXP_EOF_IN_COMMENT,
  60. G_ERR_NON_DIGIT_IN_CONST,
  61. G_ERR_DIGIT_RADIX,
  62. G_ERR_FLOAT_RADIX,
  63. G_ERR_FLOAT_MALFORMED
  64. } GErrorType;
  65. /* Token types */
  66. typedef enum
  67. {
  68. G_TOKEN_EOF = 0,
  69. G_TOKEN_LEFT_PAREN = '(',
  70. G_TOKEN_RIGHT_PAREN = ')',
  71. G_TOKEN_LEFT_CURLY = '{',
  72. G_TOKEN_RIGHT_CURLY = '}',
  73. G_TOKEN_LEFT_BRACE = '[',
  74. G_TOKEN_RIGHT_BRACE = ']',
  75. G_TOKEN_EQUAL_SIGN = '=',
  76. G_TOKEN_COMMA = ',',
  77. G_TOKEN_NONE = 256,
  78. G_TOKEN_ERROR,
  79. G_TOKEN_CHAR,
  80. G_TOKEN_BINARY,
  81. G_TOKEN_OCTAL,
  82. G_TOKEN_INT,
  83. G_TOKEN_HEX,
  84. G_TOKEN_FLOAT,
  85. G_TOKEN_STRING,
  86. G_TOKEN_SYMBOL,
  87. G_TOKEN_IDENTIFIER,
  88. G_TOKEN_IDENTIFIER_NULL,
  89. G_TOKEN_COMMENT_SINGLE,
  90. G_TOKEN_COMMENT_MULTI,
  91. G_TOKEN_LAST
  92. } GTokenType;
  93. union _GTokenValue
  94. {
  95. gpointer v_symbol;
  96. gchar *v_identifier;
  97. gulong v_binary;
  98. gulong v_octal;
  99. gulong v_int;
  100. guint64 v_int64;
  101. gdouble v_float;
  102. gulong v_hex;
  103. gchar *v_string;
  104. gchar *v_comment;
  105. guchar v_char;
  106. guint v_error;
  107. };
  108. struct _GScannerConfig
  109. {
  110. /* Character sets
  111. */
  112. gchar *cset_skip_characters; /* default: " \t\n" */
  113. gchar *cset_identifier_first;
  114. gchar *cset_identifier_nth;
  115. gchar *cpair_comment_single; /* default: "#\n" */
  116. /* Should symbol lookup work case sensitive?
  117. */
  118. guint case_sensitive : 1;
  119. /* Boolean values to be adjusted "on the fly"
  120. * to configure scanning behaviour.
  121. */
  122. guint skip_comment_multi : 1; /* C like comment */
  123. guint skip_comment_single : 1; /* single line comment */
  124. guint scan_comment_multi : 1; /* scan multi line comments? */
  125. guint scan_identifier : 1;
  126. guint scan_identifier_1char : 1;
  127. guint scan_identifier_NULL : 1;
  128. guint scan_symbols : 1;
  129. guint scan_binary : 1;
  130. guint scan_octal : 1;
  131. guint scan_float : 1;
  132. guint scan_hex : 1; /* `0x0ff0' */
  133. guint scan_hex_dollar : 1; /* `$0ff0' */
  134. guint scan_string_sq : 1; /* string: 'anything' */
  135. guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
  136. guint numbers_2_int : 1; /* bin, octal, hex => int */
  137. guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
  138. guint identifier_2_string : 1;
  139. guint char_2_token : 1; /* return G_TOKEN_CHAR? */
  140. guint symbol_2_token : 1;
  141. guint scope_0_fallback : 1; /* try scope 0 on lookups? */
  142. guint store_int64 : 1; /* use value.v_int64 rather than v_int */
  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. /* to be considered private */
  169. GHashTable *symbol_table;
  170. gint input_fd;
  171. const gchar *text;
  172. const gchar *text_end;
  173. gchar *buffer;
  174. guint scope_id;
  175. /* handler function for _warn and _error */
  176. GScannerMsgFunc msg_handler;
  177. };
  178. GScanner* g_scanner_new (const GScannerConfig *config_templ);
  179. void g_scanner_destroy (GScanner *scanner);
  180. void g_scanner_input_file (GScanner *scanner,
  181. gint input_fd);
  182. void g_scanner_sync_file_offset (GScanner *scanner);
  183. void g_scanner_input_text (GScanner *scanner,
  184. const gchar *text,
  185. guint text_len);
  186. GTokenType g_scanner_get_next_token (GScanner *scanner);
  187. GTokenType g_scanner_peek_next_token (GScanner *scanner);
  188. GTokenType g_scanner_cur_token (GScanner *scanner);
  189. GTokenValue g_scanner_cur_value (GScanner *scanner);
  190. guint g_scanner_cur_line (GScanner *scanner);
  191. guint g_scanner_cur_position (GScanner *scanner);
  192. gboolean g_scanner_eof (GScanner *scanner);
  193. guint g_scanner_set_scope (GScanner *scanner,
  194. guint scope_id);
  195. void g_scanner_scope_add_symbol (GScanner *scanner,
  196. guint scope_id,
  197. const gchar *symbol,
  198. gpointer value);
  199. void g_scanner_scope_remove_symbol (GScanner *scanner,
  200. guint scope_id,
  201. const gchar *symbol);
  202. gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
  203. guint scope_id,
  204. const gchar *symbol);
  205. void g_scanner_scope_foreach_symbol (GScanner *scanner,
  206. guint scope_id,
  207. GHFunc func,
  208. gpointer user_data);
  209. gpointer g_scanner_lookup_symbol (GScanner *scanner,
  210. const gchar *symbol);
  211. void g_scanner_unexp_token (GScanner *scanner,
  212. GTokenType expected_token,
  213. const gchar *identifier_spec,
  214. const gchar *symbol_spec,
  215. const gchar *symbol_name,
  216. const gchar *message,
  217. gint is_error);
  218. void g_scanner_error (GScanner *scanner,
  219. const gchar *format,
  220. ...) G_GNUC_PRINTF (2,3);
  221. void g_scanner_warn (GScanner *scanner,
  222. const gchar *format,
  223. ...) G_GNUC_PRINTF (2,3);
  224. #ifndef G_DISABLE_DEPRECATED
  225. /* keep downward source compatibility */
  226. #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
  227. g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
  228. } G_STMT_END
  229. #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
  230. g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
  231. } G_STMT_END
  232. #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
  233. g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
  234. } G_STMT_END
  235. /* The following two functions are deprecated and will be removed in
  236. * the next major release. They do no good. */
  237. #define g_scanner_freeze_symbol_table(scanner) ((void)0)
  238. #define g_scanner_thaw_symbol_table(scanner) ((void)0)
  239. #endif /* G_DISABLE_DEPRECATED */
  240. G_END_DECLS
  241. #endif /* __G_SCANNER_H__ */