gscanner.h

Go to the documentation of this file.
00001 /* GLIB - Library of useful routines for C programming
00002  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019 
00020 /*
00021  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
00022  * file for a list of people on the GLib Team.  See the ChangeLog
00023  * files for a list of changes.  These files are distributed with
00024  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
00025  */
00026 
00027 #ifndef __G_SCANNER_H__
00028 #define __G_SCANNER_H__
00029 
00030 #include <ghash.h>
00031 
00032 G_BEGIN_DECLS
00033 
00034 typedef struct _GScanner        GScanner;
00035 typedef struct _GScannerConfig  GScannerConfig;
00036 typedef union  _GTokenValue     GTokenValue;
00037 
00038 typedef void            (*GScannerMsgFunc)      (GScanner      *scanner,
00039                                                  gchar         *message,
00040                                                  gint           error);
00041 
00042 /* GScanner: Flexible lexical scanner for general purpose.
00043  */
00044 
00045 /* Character sets */
00046 #define G_CSET_A_2_Z    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00047 #define G_CSET_a_2_z    "abcdefghijklmnopqrstuvwxyz"
00048 #define G_CSET_DIGITS   "0123456789"
00049 #define G_CSET_LATINC   "\300\301\302\303\304\305\306"\
00050                         "\307\310\311\312\313\314\315\316\317\320"\
00051                         "\321\322\323\324\325\326"\
00052                         "\330\331\332\333\334\335\336"
00053 #define G_CSET_LATINS   "\337\340\341\342\343\344\345\346"\
00054                         "\347\350\351\352\353\354\355\356\357\360"\
00055                         "\361\362\363\364\365\366"\
00056                         "\370\371\372\373\374\375\376\377"
00057 
00058 /* Error types */
00059 typedef enum
00060 {
00061   G_ERR_UNKNOWN,
00062   G_ERR_UNEXP_EOF,
00063   G_ERR_UNEXP_EOF_IN_STRING,
00064   G_ERR_UNEXP_EOF_IN_COMMENT,
00065   G_ERR_NON_DIGIT_IN_CONST,
00066   G_ERR_DIGIT_RADIX,
00067   G_ERR_FLOAT_RADIX,
00068   G_ERR_FLOAT_MALFORMED
00069 } GErrorType;
00070 
00071 /* Token types */
00072 typedef enum
00073 {
00074   G_TOKEN_EOF                   =   0,
00075   
00076   G_TOKEN_LEFT_PAREN            = '(',
00077   G_TOKEN_RIGHT_PAREN           = ')',
00078   G_TOKEN_LEFT_CURLY            = '{',
00079   G_TOKEN_RIGHT_CURLY           = '}',
00080   G_TOKEN_LEFT_BRACE            = '[',
00081   G_TOKEN_RIGHT_BRACE           = ']',
00082   G_TOKEN_EQUAL_SIGN            = '=',
00083   G_TOKEN_COMMA                 = ',',
00084   
00085   G_TOKEN_NONE                  = 256,
00086   
00087   G_TOKEN_ERROR,
00088   
00089   G_TOKEN_CHAR,
00090   G_TOKEN_BINARY,
00091   G_TOKEN_OCTAL,
00092   G_TOKEN_INT,
00093   G_TOKEN_HEX,
00094   G_TOKEN_FLOAT,
00095   G_TOKEN_STRING,
00096   
00097   G_TOKEN_SYMBOL,
00098   G_TOKEN_IDENTIFIER,
00099   G_TOKEN_IDENTIFIER_NULL,
00100   
00101   G_TOKEN_COMMENT_SINGLE,
00102   G_TOKEN_COMMENT_MULTI,
00103   G_TOKEN_LAST
00104 } GTokenType;
00105 
00106 union   _GTokenValue
00107 {
00108   gpointer      v_symbol;
00109   gchar         *v_identifier;
00110   gulong        v_binary;
00111   gulong        v_octal;
00112   gulong        v_int;
00113   gdouble       v_float;
00114   gulong        v_hex;
00115   gchar         *v_string;
00116   gchar         *v_comment;
00117   guchar        v_char;
00118   guint         v_error;
00119 };
00120 
00121 struct  _GScannerConfig
00122 {
00123   /* Character sets
00124    */
00125   gchar         *cset_skip_characters;          /* default: " \t\n" */
00126   gchar         *cset_identifier_first;
00127   gchar         *cset_identifier_nth;
00128   gchar         *cpair_comment_single;          /* default: "#\n" */
00129   
00130   /* Should symbol lookup work case sensitive?
00131    */
00132   guint         case_sensitive : 1;
00133   
00134   /* Boolean values to be adjusted "on the fly"
00135    * to configure scanning behaviour.
00136    */
00137   guint         skip_comment_multi : 1;         /* C like comment */
00138   guint         skip_comment_single : 1;        /* single line comment */
00139   guint         scan_comment_multi : 1;         /* scan multi line comments? */
00140   guint         scan_identifier : 1;
00141   guint         scan_identifier_1char : 1;
00142   guint         scan_identifier_NULL : 1;
00143   guint         scan_symbols : 1;
00144   guint         scan_binary : 1;
00145   guint         scan_octal : 1;
00146   guint         scan_float : 1;
00147   guint         scan_hex : 1;                   /* `0x0ff0' */
00148   guint         scan_hex_dollar : 1;            /* `$0ff0' */
00149   guint         scan_string_sq : 1;             /* string: 'anything' */
00150   guint         scan_string_dq : 1;             /* string: "\\-escapes!\n" */
00151   guint         numbers_2_int : 1;              /* bin, octal, hex => int */
00152   guint         int_2_float : 1;                /* int => G_TOKEN_FLOAT? */
00153   guint         identifier_2_string : 1;
00154   guint         char_2_token : 1;               /* return G_TOKEN_CHAR? */
00155   guint         symbol_2_token : 1;
00156   guint         scope_0_fallback : 1;           /* try scope 0 on lookups? */
00157 };
00158 
00159 struct  _GScanner
00160 {
00161   /* unused fields */
00162   gpointer              user_data;
00163   guint                 max_parse_errors;
00164   
00165   /* g_scanner_error() increments this field */
00166   guint                 parse_errors;
00167   
00168   /* name of input stream, featured by the default message handler */
00169   const gchar           *input_name;
00170   
00171   /* data pointer for derived structures */
00172   gpointer              derived_data;
00173   
00174   /* link into the scanner configuration */
00175   GScannerConfig        *config;
00176   
00177   /* fields filled in after g_scanner_get_next_token() */
00178   GTokenType            token;
00179   GTokenValue           value;
00180   guint                 line;
00181   guint                 position;
00182   
00183   /* fields filled in after g_scanner_peek_next_token() */
00184   GTokenType            next_token;
00185   GTokenValue           next_value;
00186   guint                 next_line;
00187   guint                 next_position;
00188   
00189   /* to be considered private */
00190   GHashTable            *symbol_table;
00191   gint                  input_fd;
00192   const gchar           *text;
00193   const gchar           *text_end;
00194   gchar                 *buffer;
00195   guint                 scope_id;
00196   
00197   /* handler function for _warn and _error */
00198   GScannerMsgFunc       msg_handler;
00199 };
00200 
00201 GScanner*       g_scanner_new                   (GScannerConfig *config_templ);
00202 void            g_scanner_destroy               (GScanner       *scanner);
00203 void            g_scanner_input_file            (GScanner       *scanner,
00204                                                  gint           input_fd);
00205 void            g_scanner_sync_file_offset      (GScanner       *scanner);
00206 void            g_scanner_input_text            (GScanner       *scanner,
00207                                                  const  gchar   *text,
00208                                                  guint          text_len);
00209 GTokenType      g_scanner_get_next_token        (GScanner       *scanner);
00210 GTokenType      g_scanner_peek_next_token       (GScanner       *scanner);
00211 GTokenType      g_scanner_cur_token             (GScanner       *scanner);
00212 GTokenValue     g_scanner_cur_value             (GScanner       *scanner);
00213 guint           g_scanner_cur_line              (GScanner       *scanner);
00214 guint           g_scanner_cur_position          (GScanner       *scanner);
00215 gboolean        g_scanner_eof                   (GScanner       *scanner);
00216 guint           g_scanner_set_scope             (GScanner       *scanner,
00217                                                  guint           scope_id);
00218 void            g_scanner_scope_add_symbol      (GScanner       *scanner,
00219                                                  guint           scope_id,
00220                                                  const gchar    *symbol,
00221                                                  gpointer       value);
00222 void            g_scanner_scope_remove_symbol   (GScanner       *scanner,
00223                                                  guint           scope_id,
00224                                                  const gchar    *symbol);
00225 gpointer        g_scanner_scope_lookup_symbol   (GScanner       *scanner,
00226                                                  guint           scope_id,
00227                                                  const gchar    *symbol);
00228 void            g_scanner_scope_foreach_symbol  (GScanner       *scanner,
00229                                                  guint           scope_id,
00230                                                  GHFunc          func,
00231                                                  gpointer        user_data);
00232 gpointer        g_scanner_lookup_symbol         (GScanner       *scanner,
00233                                                  const gchar    *symbol);
00234 void            g_scanner_unexp_token           (GScanner       *scanner,
00235                                                  GTokenType     expected_token,
00236                                                  const gchar    *identifier_spec,
00237                                                  const gchar    *symbol_spec,
00238                                                  const gchar    *symbol_name,
00239                                                  const gchar    *message,
00240                                                  gint            is_error);
00241 void            g_scanner_error                 (GScanner       *scanner,
00242                                                  const gchar    *format,
00243                                                  ...) G_GNUC_PRINTF (2,3);
00244 void            g_scanner_warn                  (GScanner       *scanner,
00245                                                  const gchar    *format,
00246                                                  ...) G_GNUC_PRINTF (2,3);
00247 gint            g_scanner_stat_mode             (const gchar    *filename);
00248 /* keep downward source compatibility */
00249 #define         g_scanner_add_symbol( scanner, symbol, value )  G_STMT_START { \
00250   g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
00251 } G_STMT_END
00252 #define         g_scanner_remove_symbol( scanner, symbol )      G_STMT_START { \
00253   g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
00254 } G_STMT_END
00255 #define         g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
00256   g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
00257 } G_STMT_END
00258 
00259 /* The following two functions are deprecated and will be removed in
00260  * the next major release. They do no good. */
00261 void            g_scanner_freeze_symbol_table   (GScanner       *scanner);
00262 void            g_scanner_thaw_symbol_table     (GScanner       *scanner);
00263 
00264 G_END_DECLS
00265 
00266 #endif /* __G_SCANNER_H__ */
00267 

Generated on Tue Jul 5 14:14:41 2011 for ROOT_528-00b_version by  doxygen 1.5.1