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