00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef __G_SCANNER_H__
00029 #define __G_SCANNER_H__
00030
00031 #include <_ansi.h>
00032 #include <glib/gdataset.h>
00033 #include <glib/ghash.h>
00034
00035 G_BEGIN_DECLS
00036
00037 typedef struct _GScanner GScanner;
00038 typedef struct _GScannerConfig GScannerConfig;
00039 typedef union _GTokenValue GTokenValue;
00040
00041 typedef void (*GScannerMsgFunc) (GScanner *scanner,
00042 gchar *message,
00043 gboolean error);
00044
00045
00046
00047
00048
00049 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00050 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
00051 #define G_CSET_DIGITS "0123456789"
00052 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
00053 "\307\310\311\312\313\314\315\316\317\320"\
00054 "\321\322\323\324\325\326"\
00055 "\330\331\332\333\334\335\336"
00056 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
00057 "\347\350\351\352\353\354\355\356\357\360"\
00058 "\361\362\363\364\365\366"\
00059 "\370\371\372\373\374\375\376\377"
00060
00061
00062 typedef enum
00063 {
00064 G_ERR_UNKNOWN,
00065 G_ERR_UNEXP_EOF,
00066 G_ERR_UNEXP_EOF_IN_STRING,
00067 G_ERR_UNEXP_EOF_IN_COMMENT,
00068 G_ERR_NON_DIGIT_IN_CONST,
00069 G_ERR_DIGIT_RADIX,
00070 G_ERR_FLOAT_RADIX,
00071 G_ERR_FLOAT_MALFORMED
00072 } GErrorType;
00073
00074
00075 typedef enum
00076 {
00077 G_TOKEN_EOF = 0,
00078
00079 G_TOKEN_LEFT_PAREN = '(',
00080 G_TOKEN_RIGHT_PAREN = ')',
00081 G_TOKEN_LEFT_CURLY = '{',
00082 G_TOKEN_RIGHT_CURLY = '}',
00083 G_TOKEN_LEFT_BRACE = '[',
00084 G_TOKEN_RIGHT_BRACE = ']',
00085 G_TOKEN_EQUAL_SIGN = '=',
00086 G_TOKEN_COMMA = ',',
00087
00088 G_TOKEN_NONE = 256,
00089
00090 G_TOKEN_ERROR,
00091
00092 G_TOKEN_CHAR,
00093 G_TOKEN_BINARY,
00094 G_TOKEN_OCTAL,
00095 G_TOKEN_INT,
00096 G_TOKEN_HEX,
00097 G_TOKEN_FLOAT,
00098 G_TOKEN_STRING,
00099
00100 G_TOKEN_SYMBOL,
00101 G_TOKEN_IDENTIFIER,
00102 G_TOKEN_IDENTIFIER_NULL,
00103
00104 G_TOKEN_COMMENT_SINGLE,
00105 G_TOKEN_COMMENT_MULTI,
00106 G_TOKEN_LAST
00107 } GTokenType;
00108
00109 union _GTokenValue
00110 {
00111 gpointer v_symbol;
00112 gchar *v_identifier;
00113 gulong v_binary;
00114 gulong v_octal;
00115 gulong v_int;
00116 guint64 v_int64;
00117 gdouble v_float;
00118 gulong v_hex;
00119 gchar *v_string;
00120 gchar *v_comment;
00121 guchar v_char;
00122 guint v_error;
00123 };
00124
00125 struct _GScannerConfig
00126 {
00127
00128
00129 gchar *cset_skip_characters;
00130 gchar *cset_identifier_first;
00131 gchar *cset_identifier_nth;
00132 gchar *cpair_comment_single;
00133
00134
00135
00136 guint case_sensitive : 1;
00137
00138
00139
00140
00141 guint skip_comment_multi : 1;
00142 guint skip_comment_single : 1;
00143 guint scan_comment_multi : 1;
00144 guint scan_identifier : 1;
00145 guint scan_identifier_1char : 1;
00146 guint scan_identifier_NULL : 1;
00147 guint scan_symbols : 1;
00148 guint scan_binary : 1;
00149 guint scan_octal : 1;
00150 guint scan_float : 1;
00151 guint scan_hex : 1;
00152 guint scan_hex_dollar : 1;
00153 guint scan_string_sq : 1;
00154 guint scan_string_dq : 1;
00155 guint numbers_2_int : 1;
00156 guint int_2_float : 1;
00157 guint identifier_2_string : 1;
00158 guint char_2_token : 1;
00159 guint symbol_2_token : 1;
00160 guint scope_0_fallback : 1;
00161 guint store_int64 : 1;
00162 guint padding_dummy;
00163 };
00164
00165 struct _GScanner
00166 {
00167
00168 gpointer user_data;
00169 guint max_parse_errors;
00170
00171
00172 guint parse_errors;
00173
00174
00175 const gchar *input_name;
00176
00177
00178 GData *qdata;
00179
00180
00181 GScannerConfig *config;
00182
00183
00184 GTokenType token;
00185 GTokenValue value;
00186 guint line;
00187 guint position;
00188
00189
00190 GTokenType next_token;
00191 GTokenValue next_value;
00192 guint next_line;
00193 guint next_position;
00194
00195
00196 GHashTable *symbol_table;
00197 gint input_fd;
00198 const gchar *text;
00199 const gchar *text_end;
00200 gchar *buffer;
00201 guint scope_id;
00202
00203
00204 GScannerMsgFunc msg_handler;
00205 };
00206
00207 IMPORT_C GScanner* g_scanner_new (const GScannerConfig *config_templ);
00208 IMPORT_C void g_scanner_destroy (GScanner *scanner);
00209 IMPORT_C void g_scanner_input_file (GScanner *scanner,
00210 gint input_fd);
00211 IMPORT_C void g_scanner_sync_file_offset (GScanner *scanner);
00212 IMPORT_C void g_scanner_input_text (GScanner *scanner,
00213 const gchar *text,
00214 guint text_len);
00215 IMPORT_C GTokenType g_scanner_get_next_token (GScanner *scanner);
00216 IMPORT_C GTokenType g_scanner_peek_next_token (GScanner *scanner);
00217 IMPORT_C GTokenType g_scanner_cur_token (GScanner *scanner);
00218 IMPORT_C GTokenValue g_scanner_cur_value (GScanner *scanner);
00219 IMPORT_C guint g_scanner_cur_line (GScanner *scanner);
00220 IMPORT_C guint g_scanner_cur_position (GScanner *scanner);
00221 IMPORT_C gboolean g_scanner_eof (GScanner *scanner);
00222 IMPORT_C guint g_scanner_set_scope (GScanner *scanner,
00223 guint scope_id);
00224 IMPORT_C void g_scanner_scope_add_symbol (GScanner *scanner,
00225 guint scope_id,
00226 const gchar *symbol,
00227 gpointer value);
00228 IMPORT_C void g_scanner_scope_remove_symbol (GScanner *scanner,
00229 guint scope_id,
00230 const gchar *symbol);
00231 IMPORT_C gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
00232 guint scope_id,
00233 const gchar *symbol);
00234 IMPORT_C void g_scanner_scope_foreach_symbol (GScanner *scanner,
00235 guint scope_id,
00236 GHFunc func,
00237 gpointer user_data);
00238 IMPORT_C gpointer g_scanner_lookup_symbol (GScanner *scanner,
00239 const gchar *symbol);
00240 IMPORT_C void g_scanner_unexp_token (GScanner *scanner,
00241 GTokenType expected_token,
00242 const gchar *identifier_spec,
00243 const gchar *symbol_spec,
00244 const gchar *symbol_name,
00245 const gchar *message,
00246 gint is_error);
00247 IMPORT_C void g_scanner_error (GScanner *scanner,
00248 const gchar *format,
00249 ...) G_GNUC_PRINTF (2,3);
00250 IMPORT_C void g_scanner_warn (GScanner *scanner,
00251 const gchar *format,
00252 ...) G_GNUC_PRINTF (2,3);
00253
00254 #ifndef G_DISABLE_DEPRECATED
00255
00256
00257 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
00258 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
00259 } G_STMT_END
00260 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
00261 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
00262 } G_STMT_END
00263 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
00264 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
00265 } G_STMT_END
00266
00267
00268
00269 #define g_scanner_freeze_symbol_table(scanner) ((void)0)
00270 #define g_scanner_thaw_symbol_table(scanner) ((void)0)
00271
00272 #endif
00273
00274 G_END_DECLS
00275
00276 #endif
00277