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