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