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  * Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the
00017  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  * Boston, MA 02111-1307, USA.
00019  */
00020 
00021 /*
00022  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
00023  * file for a list of people on the GLib Team.  See the ChangeLog
00024  * files for a list of changes.  These files are distributed with
00025  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
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 /* GScanner: Flexible lexical scanner for general purpose.
00046  */
00047 
00048 /* Character sets */
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 /* Error types */
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 /* Token types */
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   /* Character sets
00128    */
00129   gchar         *cset_skip_characters;          /* default: " \t\n" */
00130   gchar         *cset_identifier_first;
00131   gchar         *cset_identifier_nth;
00132   gchar         *cpair_comment_single;          /* default: "#\n" */
00133   
00134   /* Should symbol lookup work case sensitive?
00135    */
00136   guint         case_sensitive : 1;
00137   
00138   /* Boolean values to be adjusted "on the fly"
00139    * to configure scanning behaviour.
00140    */
00141   guint         skip_comment_multi : 1;         /* C like comment */
00142   guint         skip_comment_single : 1;        /* single line comment */
00143   guint         scan_comment_multi : 1;         /* scan multi line comments? */
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;                   /* `0x0ff0' */
00152   guint         scan_hex_dollar : 1;            /* `$0ff0' */
00153   guint         scan_string_sq : 1;             /* string: 'anything' */
00154   guint         scan_string_dq : 1;             /* string: "\\-escapes!\n" */
00155   guint         numbers_2_int : 1;              /* bin, octal, hex => int */
00156   guint         int_2_float : 1;                /* int => G_TOKEN_FLOAT? */
00157   guint         identifier_2_string : 1;
00158   guint         char_2_token : 1;               /* return G_TOKEN_CHAR? */
00159   guint         symbol_2_token : 1;
00160   guint         scope_0_fallback : 1;           /* try scope 0 on lookups? */
00161   guint         store_int64 : 1;                /* use value.v_int64 rather than v_int */
00162   guint         padding_dummy;
00163 };
00164 
00165 struct  _GScanner
00166 {
00167   /* unused fields */
00168   gpointer              user_data;
00169   guint                 max_parse_errors;
00170   
00171   /* g_scanner_error() increments this field */
00172   guint                 parse_errors;
00173   
00174   /* name of input stream, featured by the default message handler */
00175   const gchar           *input_name;
00176   
00177   /* quarked data */
00178   GData                 *qdata;
00179   
00180   /* link into the scanner configuration */
00181   GScannerConfig        *config;
00182   
00183   /* fields filled in after g_scanner_get_next_token() */
00184   GTokenType            token;
00185   GTokenValue           value;
00186   guint                 line;
00187   guint                 position;
00188   
00189   /* fields filled in after g_scanner_peek_next_token() */
00190   GTokenType            next_token;
00191   GTokenValue           next_value;
00192   guint                 next_line;
00193   guint                 next_position;
00194   
00195   /* to be considered private */
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   /* handler function for _warn and _error */
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 /* keep downward source compatibility */
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 /* The following two functions are deprecated and will be removed in
00268  * the next major release. They do no good. */
00269 #define g_scanner_freeze_symbol_table(scanner) ((void)0)
00270 #define g_scanner_thaw_symbol_table(scanner) ((void)0)
00271 
00272 #endif /* G_DISABLE_DEPRECATED */
00273 
00274 G_END_DECLS
00275 
00276 #endif /* __G_SCANNER_H__ */
00277 

Copyright © Nokia Corporation 2001-2008
Back to top