gscanner.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  3. *
  4. * SPDX-License-Identifier: LGPL-2.1-or-later
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  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. #ifndef __G_SCANNER_H__
  26. #define __G_SCANNER_H__
  27. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  28. #error "Only <glib.h> can be included directly."
  29. #endif
  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. /*< private >*/
  92. G_TOKEN_LAST
  93. } GTokenType;
  94. union _GTokenValue
  95. {
  96. gpointer v_symbol;
  97. gchar *v_identifier;
  98. gulong v_binary;
  99. gulong v_octal;
  100. gulong v_int;
  101. guint64 v_int64;
  102. gdouble v_float;
  103. gulong v_hex;
  104. gchar *v_string;
  105. gchar *v_comment;
  106. guchar v_char;
  107. guint v_error;
  108. };
  109. struct _GScannerConfig
  110. {
  111. /* Character sets
  112. */
  113. gchar *cset_skip_characters; /* default: " \t\n" */
  114. gchar *cset_identifier_first;
  115. gchar *cset_identifier_nth;
  116. gchar *cpair_comment_single; /* default: "#\n" */
  117. /* Should symbol lookup work case sensitive?
  118. */
  119. guint case_sensitive : 1;
  120. /* Boolean values to be adjusted "on the fly"
  121. * to configure scanning behaviour.
  122. */
  123. guint skip_comment_multi : 1; /* C like comment */
  124. guint skip_comment_single : 1; /* single line comment */
  125. guint scan_comment_multi : 1; /* scan multi line comments? */
  126. guint scan_identifier : 1;
  127. guint scan_identifier_1char : 1;
  128. guint scan_identifier_NULL : 1;
  129. guint scan_symbols : 1;
  130. guint scan_binary : 1;
  131. guint scan_octal : 1;
  132. guint scan_float : 1;
  133. guint scan_hex : 1; /* '0x0ff0' */
  134. guint scan_hex_dollar : 1; /* '$0ff0' */
  135. guint scan_string_sq : 1; /* string: 'anything' */
  136. guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
  137. guint numbers_2_int : 1; /* bin, octal, hex => int */
  138. guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
  139. guint identifier_2_string : 1;
  140. guint char_2_token : 1; /* return G_TOKEN_CHAR? */
  141. guint symbol_2_token : 1;
  142. guint scope_0_fallback : 1; /* try scope 0 on lookups? */
  143. guint store_int64 : 1; /* use value.v_int64 rather than v_int */
  144. /*< private >*/
  145. guint padding_dummy;
  146. };
  147. struct _GScanner
  148. {
  149. /* unused fields */
  150. gpointer user_data;
  151. guint max_parse_errors;
  152. /* g_scanner_error() increments this field */
  153. guint parse_errors;
  154. /* name of input stream, featured by the default message handler */
  155. const gchar *input_name;
  156. /* quarked data */
  157. GData *qdata;
  158. /* link into the scanner configuration */
  159. GScannerConfig *config;
  160. /* fields filled in after g_scanner_get_next_token() */
  161. GTokenType token;
  162. GTokenValue value;
  163. guint line;
  164. guint position;
  165. /* fields filled in after g_scanner_peek_next_token() */
  166. GTokenType next_token;
  167. GTokenValue next_value;
  168. guint next_line;
  169. guint next_position;
  170. /*< private >*/
  171. /* to be considered private */
  172. GHashTable *symbol_table;
  173. gint input_fd;
  174. const gchar *text;
  175. const gchar *text_end;
  176. gchar *buffer;
  177. guint scope_id;
  178. /*< public >*/
  179. /* handler function for _warn and _error */
  180. GScannerMsgFunc msg_handler;
  181. };
  182. GLIB_AVAILABLE_IN_ALL
  183. GScanner* g_scanner_new (const GScannerConfig *config_templ);
  184. GLIB_AVAILABLE_IN_ALL
  185. void g_scanner_destroy (GScanner *scanner);
  186. GLIB_AVAILABLE_IN_ALL
  187. void g_scanner_input_file (GScanner *scanner,
  188. gint input_fd);
  189. GLIB_AVAILABLE_IN_ALL
  190. void g_scanner_sync_file_offset (GScanner *scanner);
  191. GLIB_AVAILABLE_IN_ALL
  192. void g_scanner_input_text (GScanner *scanner,
  193. const gchar *text,
  194. guint text_len);
  195. GLIB_AVAILABLE_IN_ALL
  196. GTokenType g_scanner_get_next_token (GScanner *scanner);
  197. GLIB_AVAILABLE_IN_ALL
  198. GTokenType g_scanner_peek_next_token (GScanner *scanner);
  199. GLIB_AVAILABLE_IN_ALL
  200. GTokenType g_scanner_cur_token (GScanner *scanner);
  201. GLIB_AVAILABLE_IN_ALL
  202. GTokenValue g_scanner_cur_value (GScanner *scanner);
  203. GLIB_AVAILABLE_IN_ALL
  204. guint g_scanner_cur_line (GScanner *scanner);
  205. GLIB_AVAILABLE_IN_ALL
  206. guint g_scanner_cur_position (GScanner *scanner);
  207. GLIB_AVAILABLE_IN_ALL
  208. gboolean g_scanner_eof (GScanner *scanner);
  209. GLIB_AVAILABLE_IN_ALL
  210. guint g_scanner_set_scope (GScanner *scanner,
  211. guint scope_id);
  212. GLIB_AVAILABLE_IN_ALL
  213. void g_scanner_scope_add_symbol (GScanner *scanner,
  214. guint scope_id,
  215. const gchar *symbol,
  216. gpointer value);
  217. GLIB_AVAILABLE_IN_ALL
  218. void g_scanner_scope_remove_symbol (GScanner *scanner,
  219. guint scope_id,
  220. const gchar *symbol);
  221. GLIB_AVAILABLE_IN_ALL
  222. gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
  223. guint scope_id,
  224. const gchar *symbol);
  225. GLIB_AVAILABLE_IN_ALL
  226. void g_scanner_scope_foreach_symbol (GScanner *scanner,
  227. guint scope_id,
  228. GHFunc func,
  229. gpointer user_data);
  230. GLIB_AVAILABLE_IN_ALL
  231. gpointer g_scanner_lookup_symbol (GScanner *scanner,
  232. const gchar *symbol);
  233. GLIB_AVAILABLE_IN_ALL
  234. void g_scanner_unexp_token (GScanner *scanner,
  235. GTokenType expected_token,
  236. const gchar *identifier_spec,
  237. const gchar *symbol_spec,
  238. const gchar *symbol_name,
  239. const gchar *message,
  240. gint is_error);
  241. GLIB_AVAILABLE_IN_ALL
  242. void g_scanner_error (GScanner *scanner,
  243. const gchar *format,
  244. ...) G_GNUC_PRINTF (2,3);
  245. GLIB_AVAILABLE_IN_ALL
  246. void g_scanner_warn (GScanner *scanner,
  247. const gchar *format,
  248. ...) G_GNUC_PRINTF (2,3);
  249. /* keep downward source compatibility */
  250. #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
  251. g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
  252. } G_STMT_END GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_scanner_scope_add_symbol)
  253. #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
  254. g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
  255. } G_STMT_END GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_scanner_scope_remove_symbol)
  256. #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
  257. g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
  258. } G_STMT_END GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_scanner_scope_foreach_symbol)
  259. /* The following two functions are deprecated and will be removed in
  260. * the next major release. They do no good. */
  261. #define g_scanner_freeze_symbol_table(scanner) ((void)0) GLIB_DEPRECATED_MACRO_IN_2_26
  262. #define g_scanner_thaw_symbol_table(scanner) ((void)0) GLIB_DEPRECATED_MACRO_IN_2_26
  263. G_END_DECLS
  264. #endif /* __G_SCANNER_H__ */