Merge remote-tracking branch 'gvdb/master'
[platform/upstream/glib.git] / glib / gscanner.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * GScanner: Flexible lexical scanner for general purpose.
5  * Copyright (C) 1997, 1998 Tim Janik
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /*
24  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
25  * file for a list of people on the GLib Team.  See the ChangeLog
26  * files for a list of changes.  These files are distributed with
27  * GLib at ftp://ftp.gtk.org/pub/gtk/.
28  */
29
30 /*
31  * MT safe
32  */
33
34 #include "config.h"
35
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <stdarg.h>
39 #include <string.h>
40 #include <stdio.h>
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44
45 #include "gscanner.h"
46
47 #include "gprintfint.h"
48 #include "gstrfuncs.h"
49 #include "gstring.h"
50 #include "gtestutils.h"
51
52 #ifdef G_OS_WIN32
53 #include <io.h> /* For _read() */
54 #endif
55
56
57 /**
58  * SECTION:scanner
59  * @title: Lexical Scanner
60  * @short_description: a general purpose lexical scanner
61  *
62  * The #GScanner and its associated functions provide a
63  * general purpose lexical scanner.
64  */
65
66 /**
67  * GScannerMsgFunc:
68  * @scanner: a #GScanner
69  * @message: the message
70  * @error: %TRUE if the message signals an error,
71  *     %FALSE if it signals a warning.
72  *
73  * Specifies the type of the message handler function.
74  */
75
76 /**
77  * G_CSET_a_2_z:
78  *
79  * The set of lowercase ASCII alphabet characters.
80  * Used for specifying valid identifier characters
81  * in #GScannerConfig.
82  */
83
84 /**
85  * G_CSET_A_2_Z:
86  *
87  * The set of uppercase ASCII alphabet characters.
88  * Used for specifying valid identifier characters
89  * in #GScannerConfig.
90  */
91
92 /**
93  * G_CSET_LATINC:
94  *
95  * The set of uppercase ISO 8859-1 alphabet characters
96  * which are not ASCII characters.
97  * Used for specifying valid identifier characters
98  * in #GScannerConfig.
99  */
100
101 /**
102  * G_CSET_LATINS:
103  *
104  * The set of lowercase ISO 8859-1 alphabet characters
105  * which are not ASCII characters.
106  * Used for specifying valid identifier characters
107  * in #GScannerConfig.
108  */
109
110 /**
111  * GTokenType:
112  * @G_TOKEN_EOF: the end of the file
113  * @G_TOKEN_LEFT_PAREN: a '(' character
114  * @G_TOKEN_LEFT_CURLY: a '{' character
115  * @G_TOKEN_RIGHT_CURLY: a '}' character
116  * @G_TOKEN_RIGHT_PAREN: a ')' character
117  * @G_TOKEN_ERROR: an error occurred
118  * @G_TOKEN_CHAR: a character
119  * @G_TOKEN_BINARY: a binary integer
120  * @G_TOKEN_OCTAL: an octal integer
121  * @G_TOKEN_INT: an integer
122  * @G_TOKEN_HEX: a hex integer
123  * @G_TOKEN_FLOAT: a floating point number
124  * @G_TOKEN_STRING: a string
125  * @G_TOKEN_SYMBOL: a symbol
126  * @G_TOKEN_IDENTIFIER: an identifier
127  * @G_TOKEN_IDENTIFIER_NULL: a null identifier
128  * @G_TOKEN_COMMENT_SINGLE: one line comment
129  * @G_TOKEN_COMMENT_MULTI: multi line comment
130  *
131  * The possible types of token returned from each
132  * g_scanner_get_next_token() call.
133  */
134
135 /**
136  * GTokenValue:
137  * @v_symbol: token symbol value
138  * @v_identifier: token identifier value
139  * @v_binary: token binary integer value
140  * @v_octal: octal integer value
141  * @v_int: integer value
142  * @v_int64: 64-bit integer value
143  * @v_float: floating point value
144  * @v_hex: hex integer value
145  * @v_string: string value
146  * @v_comment: comment value
147  * @v_char: character value
148  * @v_error: error value
149  *
150  * A union holding the value of the token.
151  */
152
153 /**
154  * GErrorType:
155  * @G_ERR_UNKNOWN: unknown error
156  * @G_ERR_UNEXP_EOF: unexpected end of file
157  * @G_ERR_UNEXP_EOF_IN_STRING: unterminated string constant
158  * @G_ERR_UNEXP_EOF_IN_COMMENT: unterminated comment
159  * @G_ERR_NON_DIGIT_IN_CONST: non-digit character in a number
160  * @G_ERR_DIGIT_RADIX: digit beyond radix in a number
161  * @G_ERR_FLOAT_RADIX: non-decimal floating point number
162  * @G_ERR_FLOAT_MALFORMED: malformed floating point number
163  *
164  * The possible errors, used in the @v_error field
165  * of #GTokenValue, when the token is a %G_TOKEN_ERROR.
166  */
167
168 /**
169  * GScanner:
170  * @user_data: unused
171  * @max_parse_errors: unused
172  * @parse_errors: g_scanner_error() increments this field
173  * @input_name: name of input stream, featured by the default message handler
174  * @qdata: quarked data
175  * @config: link into the scanner configuration
176  * @token: token parsed by the last g_scanner_get_next_token()
177  * @value: value of the last token from g_scanner_get_next_token()
178  * @line: line number of the last token from g_scanner_get_next_token()
179  * @position: char number of the last token from g_scanner_get_next_token()
180  * @next_token: token parsed by the last g_scanner_peek_next_token()
181  * @next_value: value of the last token from g_scanner_peek_next_token()
182  * @next_line: line number of the last token from g_scanner_peek_next_token()
183  * @next_position: char number of the last token from g_scanner_peek_next_token()
184  * @msg_handler: handler function for _warn and _error
185  *
186  * The data structure representing a lexical scanner.
187  *
188  * You should set @input_name after creating the scanner, since
189  * it is used by the default message handler when displaying
190  * warnings and errors. If you are scanning a file, the filename
191  * would be a good choice.
192  *
193  * The @user_data and @max_parse_errors fields are not used.
194  * If you need to associate extra data with the scanner you
195  * can place them here.
196  *
197  * If you want to use your own message handler you can set the
198  * @msg_handler field. The type of the message handler function
199  * is declared by #GScannerMsgFunc.
200  */
201
202 /**
203  * GScannerConfig:
204  * @cset_skip_characters: specifies which characters should be skipped
205  *     by the scanner (the default is the whitespace characters: space,
206  *     tab, carriage-return and line-feed).
207  * @cset_identifier_first: specifies the characters which can start
208  *     identifiers (the default is #G_CSET_a_2_z, "_", and #G_CSET_A_2_Z).
209  * @cset_identifier_nth: specifies the characters which can be used
210  *     in identifiers, after the first character (the default is
211  *     #G_CSET_a_2_z, "_0123456789", #G_CSET_A_2_Z, #G_CSET_LATINS,
212  *     #G_CSET_LATINC).
213  * @cpair_comment_single: specifies the characters at the start and
214  *     end of single-line comments. The default is "#\n" which means
215  *     that single-line comments start with a '#' and continue until
216  *     a '\n' (end of line).
217  * @case_sensitive: specifies if symbols are case sensitive (the
218  *     default is %FALSE).
219  * @skip_comment_multi: specifies if multi-line comments are skipped
220  *     and not returned as tokens (the default is %TRUE).
221  * @skip_comment_single: specifies if single-line comments are skipped
222  *     and not returned as tokens (the default is %TRUE).
223  * @scan_comment_multi: specifies if multi-line comments are recognized
224  *     (the default is %TRUE).
225  * @scan_identifier: specifies if identifiers are recognized (the
226  *     default is %TRUE).
227  * @scan_identifier_1char: specifies if single-character
228  *     identifiers are recognized (the default is %FALSE).
229  * @scan_identifier_NULL: specifies if %NULL is reported as
230  *     %G_TOKEN_IDENTIFIER_NULL (the default is %FALSE).
231  * @scan_symbols: specifies if symbols are recognized (the default
232  *     is %TRUE).
233  * @scan_binary: specifies if binary numbers are recognized (the
234  *     default is %FALSE).
235  * @scan_octal: specifies if octal numbers are recognized (the
236  *     default is %TRUE).
237  * @scan_float: specifies if floating point numbers are recognized
238  *     (the default is %TRUE).
239  * @scan_hex: specifies if hexadecimal numbers are recognized (the
240  *     default is %TRUE).
241  * @scan_hex_dollar: specifies if '$' is recognized as a prefix for
242  *     hexadecimal numbers (the default is %FALSE).
243  * @scan_string_sq: specifies if strings can be enclosed in single
244  *     quotes (the default is %TRUE).
245  * @scan_string_dq: specifies if strings can be enclosed in double
246  *     quotes (the default is %TRUE).
247  * @numbers_2_int: specifies if binary, octal and hexadecimal numbers
248  *     are reported as #G_TOKEN_INT (the default is %TRUE).
249  * @int_2_float: specifies if all numbers are reported as %G_TOKEN_FLOAT
250  *     (the default is %FALSE).
251  * @identifier_2_string: specifies if identifiers are reported as strings
252  *     (the default is %FALSE).
253  * @char_2_token: specifies if characters are reported by setting
254  *     <literal>token = ch</literal> or as %G_TOKEN_CHAR (the default
255  *     is %TRUE).
256  * @symbol_2_token: specifies if symbols are reported by setting
257  *     <literal>token = v_symbol</literal> or as %G_TOKEN_SYMBOL (the
258  *     default is %FALSE).
259  * @scope_0_fallback: specifies if a symbol is searched for in the
260  *     default scope in addition to the current scope (the default is %FALSE).
261  * @store_int64: use value.v_int64 rather than v_int
262  *
263  * Specifies the #GScanner parser configuration. Most settings can
264  * be changed during the parsing phase and will affect the lexical
265  * parsing of the next unpeeked token.
266  */
267
268 /* --- defines --- */
269 #define to_lower(c)                             ( \
270         (guchar) (                                                      \
271           ( (((guchar)(c))>='A' && ((guchar)(c))<='Z') * ('a'-'A') ) |  \
272           ( (((guchar)(c))>=192 && ((guchar)(c))<=214) * (224-192) ) |  \
273           ( (((guchar)(c))>=216 && ((guchar)(c))<=222) * (248-216) ) |  \
274           ((guchar)(c))                                                 \
275         )                                                               \
276 )
277 #define READ_BUFFER_SIZE        (4000)
278
279
280 /* --- typedefs --- */
281 typedef struct  _GScannerKey    GScannerKey;
282
283 struct  _GScannerKey
284 {
285   guint          scope_id;
286   gchar         *symbol;
287   gpointer       value;
288 };
289
290
291 /* --- variables --- */
292 static const GScannerConfig g_scanner_config_template =
293 {
294   (
295    " \t\r\n"
296    )                    /* cset_skip_characters */,
297   (
298    G_CSET_a_2_z
299    "_"
300    G_CSET_A_2_Z
301    )                    /* cset_identifier_first */,
302   (
303    G_CSET_a_2_z
304    "_"
305    G_CSET_A_2_Z
306    G_CSET_DIGITS
307    G_CSET_LATINS
308    G_CSET_LATINC
309    )                    /* cset_identifier_nth */,
310   ( "#\n" )             /* cpair_comment_single */,
311   
312   FALSE                 /* case_sensitive */,
313   
314   TRUE                  /* skip_comment_multi */,
315   TRUE                  /* skip_comment_single */,
316   TRUE                  /* scan_comment_multi */,
317   TRUE                  /* scan_identifier */,
318   FALSE                 /* scan_identifier_1char */,
319   FALSE                 /* scan_identifier_NULL */,
320   TRUE                  /* scan_symbols */,
321   FALSE                 /* scan_binary */,
322   TRUE                  /* scan_octal */,
323   TRUE                  /* scan_float */,
324   TRUE                  /* scan_hex */,
325   FALSE                 /* scan_hex_dollar */,
326   TRUE                  /* scan_string_sq */,
327   TRUE                  /* scan_string_dq */,
328   TRUE                  /* numbers_2_int */,
329   FALSE                 /* int_2_float */,
330   FALSE                 /* identifier_2_string */,
331   TRUE                  /* char_2_token */,
332   FALSE                 /* symbol_2_token */,
333   FALSE                 /* scope_0_fallback */,
334   FALSE                 /* store_int64 */,
335 };
336
337
338 /* --- prototypes --- */
339 static inline
340 GScannerKey*    g_scanner_lookup_internal (GScanner     *scanner,
341                                            guint         scope_id,
342                                            const gchar  *symbol);
343 static gboolean g_scanner_key_equal       (gconstpointer v1,
344                                            gconstpointer v2);
345 static guint    g_scanner_key_hash        (gconstpointer v);
346 static void     g_scanner_get_token_ll    (GScanner     *scanner,
347                                            GTokenType   *token_p,
348                                            GTokenValue  *value_p,
349                                            guint        *line_p,
350                                            guint        *position_p);
351 static void     g_scanner_get_token_i     (GScanner     *scanner,
352                                            GTokenType   *token_p,
353                                            GTokenValue  *value_p,
354                                            guint        *line_p,
355                                            guint        *position_p);
356
357 static guchar   g_scanner_peek_next_char  (GScanner     *scanner);
358 static guchar   g_scanner_get_char        (GScanner     *scanner,
359                                            guint        *line_p,
360                                            guint        *position_p);
361 static void     g_scanner_msg_handler     (GScanner     *scanner,
362                                            gchar        *message,
363                                            gboolean      is_error);
364
365
366 /* --- functions --- */
367 static inline gint
368 g_scanner_char_2_num (guchar    c,
369                       guchar    base)
370 {
371   if (c >= '0' && c <= '9')
372     c -= '0';
373   else if (c >= 'A' && c <= 'Z')
374     c -= 'A' - 10;
375   else if (c >= 'a' && c <= 'z')
376     c -= 'a' - 10;
377   else
378     return -1;
379   
380   if (c < base)
381     return c;
382   
383   return -1;
384 }
385
386 /**
387  * g_scanner_new:
388  * @config_templ: the initial scanner settings
389  *
390  * Creates a new #GScanner.
391  *
392  * The @config_templ structure specifies the initial settings
393  * of the scanner, which are copied into the #GScanner
394  * @config field. If you pass %NULL then the default settings
395  * are used.
396  *
397  * Returns: the new #GScanner
398  */
399 GScanner *
400 g_scanner_new (const GScannerConfig *config_templ)
401 {
402   GScanner *scanner;
403   
404   if (!config_templ)
405     config_templ = &g_scanner_config_template;
406   
407   scanner = g_new0 (GScanner, 1);
408   
409   scanner->user_data = NULL;
410   scanner->max_parse_errors = 1;
411   scanner->parse_errors = 0;
412   scanner->input_name = NULL;
413   g_datalist_init (&scanner->qdata);
414   
415   scanner->config = g_new0 (GScannerConfig, 1);
416   
417   scanner->config->case_sensitive        = config_templ->case_sensitive;
418   scanner->config->cset_skip_characters  = config_templ->cset_skip_characters;
419   if (!scanner->config->cset_skip_characters)
420     scanner->config->cset_skip_characters = "";
421   scanner->config->cset_identifier_first = config_templ->cset_identifier_first;
422   scanner->config->cset_identifier_nth   = config_templ->cset_identifier_nth;
423   scanner->config->cpair_comment_single  = config_templ->cpair_comment_single;
424   scanner->config->skip_comment_multi    = config_templ->skip_comment_multi;
425   scanner->config->skip_comment_single   = config_templ->skip_comment_single;
426   scanner->config->scan_comment_multi    = config_templ->scan_comment_multi;
427   scanner->config->scan_identifier       = config_templ->scan_identifier;
428   scanner->config->scan_identifier_1char = config_templ->scan_identifier_1char;
429   scanner->config->scan_identifier_NULL  = config_templ->scan_identifier_NULL;
430   scanner->config->scan_symbols          = config_templ->scan_symbols;
431   scanner->config->scan_binary           = config_templ->scan_binary;
432   scanner->config->scan_octal            = config_templ->scan_octal;
433   scanner->config->scan_float            = config_templ->scan_float;
434   scanner->config->scan_hex              = config_templ->scan_hex;
435   scanner->config->scan_hex_dollar       = config_templ->scan_hex_dollar;
436   scanner->config->scan_string_sq        = config_templ->scan_string_sq;
437   scanner->config->scan_string_dq        = config_templ->scan_string_dq;
438   scanner->config->numbers_2_int         = config_templ->numbers_2_int;
439   scanner->config->int_2_float           = config_templ->int_2_float;
440   scanner->config->identifier_2_string   = config_templ->identifier_2_string;
441   scanner->config->char_2_token          = config_templ->char_2_token;
442   scanner->config->symbol_2_token        = config_templ->symbol_2_token;
443   scanner->config->scope_0_fallback      = config_templ->scope_0_fallback;
444   scanner->config->store_int64           = config_templ->store_int64;
445   
446   scanner->token = G_TOKEN_NONE;
447   scanner->value.v_int64 = 0;
448   scanner->line = 1;
449   scanner->position = 0;
450   
451   scanner->next_token = G_TOKEN_NONE;
452   scanner->next_value.v_int64 = 0;
453   scanner->next_line = 1;
454   scanner->next_position = 0;
455   
456   scanner->symbol_table = g_hash_table_new (g_scanner_key_hash, g_scanner_key_equal);
457   scanner->input_fd = -1;
458   scanner->text = NULL;
459   scanner->text_end = NULL;
460   scanner->buffer = NULL;
461   scanner->scope_id = 0;
462   
463   scanner->msg_handler = g_scanner_msg_handler;
464   
465   return scanner;
466 }
467
468 static inline void
469 g_scanner_free_value (GTokenType     *token_p,
470                       GTokenValue     *value_p)
471 {
472   switch (*token_p)
473     {
474     case G_TOKEN_STRING:
475     case G_TOKEN_IDENTIFIER:
476     case G_TOKEN_IDENTIFIER_NULL:
477     case G_TOKEN_COMMENT_SINGLE:
478     case G_TOKEN_COMMENT_MULTI:
479       g_free (value_p->v_string);
480       break;
481       
482     default:
483       break;
484     }
485   
486   *token_p = G_TOKEN_NONE;
487 }
488
489 static void
490 g_scanner_destroy_symbol_table_entry (gpointer _key,
491                                       gpointer _value,
492                                       gpointer _data)
493 {
494   GScannerKey *key = _key;
495   
496   g_free (key->symbol);
497   g_free (key);
498 }
499
500 /**
501  * g_scanner_destroy:
502  * @scanner: a #GScanner
503  *
504  * Frees all memory used by the #GScanner.
505  */
506 void
507 g_scanner_destroy (GScanner *scanner)
508 {
509   g_return_if_fail (scanner != NULL);
510   
511   g_datalist_clear (&scanner->qdata);
512   g_hash_table_foreach (scanner->symbol_table, 
513                         g_scanner_destroy_symbol_table_entry, NULL);
514   g_hash_table_destroy (scanner->symbol_table);
515   g_scanner_free_value (&scanner->token, &scanner->value);
516   g_scanner_free_value (&scanner->next_token, &scanner->next_value);
517   g_free (scanner->config);
518   g_free (scanner->buffer);
519   g_free (scanner);
520 }
521
522 static void
523 g_scanner_msg_handler (GScanner         *scanner,
524                        gchar            *message,
525                        gboolean         is_error)
526 {
527   g_return_if_fail (scanner != NULL);
528   
529   _g_fprintf (stderr, "%s:%d: ",
530               scanner->input_name ? scanner->input_name : "<memory>",
531               scanner->line);
532   if (is_error)
533     _g_fprintf (stderr, "error: ");
534   _g_fprintf (stderr, "%s\n", message);
535 }
536
537 /**
538  * g_scanner_error:
539  * @scanner: a #GScanner
540  * @format: the message format. See the printf() documentation
541  * @...: the parameters to insert into the format string
542  *
543  * Outputs an error message, via the #GScanner message handler.
544  */
545 void
546 g_scanner_error (GScanner       *scanner,
547                  const gchar    *format,
548                  ...)
549 {
550   g_return_if_fail (scanner != NULL);
551   g_return_if_fail (format != NULL);
552   
553   scanner->parse_errors++;
554   
555   if (scanner->msg_handler)
556     {
557       va_list args;
558       gchar *string;
559       
560       va_start (args, format);
561       string = g_strdup_vprintf (format, args);
562       va_end (args);
563       
564       scanner->msg_handler (scanner, string, TRUE);
565       
566       g_free (string);
567     }
568 }
569
570 /**
571  * g_scanner_warn:
572  * @scanner: a #GScanner
573  * @format: the message format. See the printf() documentation
574  * @...: the parameters to insert into the format string
575  *
576  * Outputs a warning message, via the #GScanner message handler.
577  */
578 void
579 g_scanner_warn (GScanner       *scanner,
580                 const gchar    *format,
581                 ...)
582 {
583   g_return_if_fail (scanner != NULL);
584   g_return_if_fail (format != NULL);
585   
586   if (scanner->msg_handler)
587     {
588       va_list args;
589       gchar *string;
590       
591       va_start (args, format);
592       string = g_strdup_vprintf (format, args);
593       va_end (args);
594       
595       scanner->msg_handler (scanner, string, FALSE);
596       
597       g_free (string);
598     }
599 }
600
601 static gboolean
602 g_scanner_key_equal (gconstpointer v1,
603                      gconstpointer v2)
604 {
605   const GScannerKey *key1 = v1;
606   const GScannerKey *key2 = v2;
607   
608   return (key1->scope_id == key2->scope_id) && (strcmp (key1->symbol, key2->symbol) == 0);
609 }
610
611 static guint
612 g_scanner_key_hash (gconstpointer v)
613 {
614   const GScannerKey *key = v;
615   gchar *c;
616   guint h;
617   
618   h = key->scope_id;
619   for (c = key->symbol; *c; c++)
620     h = (h << 5) - h + *c;
621   
622   return h;
623 }
624
625 static inline GScannerKey*
626 g_scanner_lookup_internal (GScanner     *scanner,
627                            guint         scope_id,
628                            const gchar  *symbol)
629 {
630   GScannerKey   *key_p;
631   GScannerKey key;
632   
633   key.scope_id = scope_id;
634   
635   if (!scanner->config->case_sensitive)
636     {
637       gchar *d;
638       const gchar *c;
639       
640       key.symbol = g_new (gchar, strlen (symbol) + 1);
641       for (d = key.symbol, c = symbol; *c; c++, d++)
642         *d = to_lower (*c);
643       *d = 0;
644       key_p = g_hash_table_lookup (scanner->symbol_table, &key);
645       g_free (key.symbol);
646     }
647   else
648     {
649       key.symbol = (gchar*) symbol;
650       key_p = g_hash_table_lookup (scanner->symbol_table, &key);
651     }
652   
653   return key_p;
654 }
655
656 /**
657  * g_scanner_add_symbol
658  * @scanner: a #GScanner
659  * @symbol: the symbol to add
660  * @value: the value of the symbol
661  *
662  * Adds a symbol to the default scope.
663  *
664  * Deprecated: 2.2: Use g_scanner_scope_add_symbol() instead.
665  */
666
667 /**
668  * g_scanner_scope_add_symbol:
669  * @scanner: a #GScanner
670  * @scope_id: the scope id
671  * @symbol: the symbol to add
672  * @value: the value of the symbol
673  *
674  * Adds a symbol to the given scope.
675  */
676 void
677 g_scanner_scope_add_symbol (GScanner    *scanner,
678                             guint        scope_id,
679                             const gchar *symbol,
680                             gpointer     value)
681 {
682   GScannerKey   *key;
683   
684   g_return_if_fail (scanner != NULL);
685   g_return_if_fail (symbol != NULL);
686   
687   key = g_scanner_lookup_internal (scanner, scope_id, symbol);
688   
689   if (!key)
690     {
691       key = g_new (GScannerKey, 1);
692       key->scope_id = scope_id;
693       key->symbol = g_strdup (symbol);
694       key->value = value;
695       if (!scanner->config->case_sensitive)
696         {
697           gchar *c;
698           
699           c = key->symbol;
700           while (*c != 0)
701             {
702               *c = to_lower (*c);
703               c++;
704             }
705         }
706       g_hash_table_insert (scanner->symbol_table, key, key);
707     }
708   else
709     key->value = value;
710 }
711
712 /**
713  * g_scanner_remove_symbol:
714  * @scanner: a #GScanner
715  * @symbol: the symbol to remove
716  *
717  * Removes a symbol from the default scope.
718  *
719  * Deprecated: 2.2: Use g_scanner_scope_remove_symbol() instead.
720  */
721
722 /**
723  * g_scanner_scope_remove_symbol:
724  * @scanner: a #GScanner
725  * @scope_id: the scope id
726  * @symbol: the symbol to remove
727  *
728  * Removes a symbol from a scope.
729  */
730 void
731 g_scanner_scope_remove_symbol (GScanner    *scanner,
732                                guint        scope_id,
733                                const gchar *symbol)
734 {
735   GScannerKey   *key;
736   
737   g_return_if_fail (scanner != NULL);
738   g_return_if_fail (symbol != NULL);
739   
740   key = g_scanner_lookup_internal (scanner, scope_id, symbol);
741   
742   if (key)
743     {
744       g_hash_table_remove (scanner->symbol_table, key);
745       g_free (key->symbol);
746       g_free (key);
747     }
748 }
749
750 /**
751  * g_scanner_freeze_symbol_table:
752  * @scanner: a #GScanner
753  *
754  * There is no reason to use this macro, since it does nothing.
755  *
756  * Deprecated: 2.2: This macro does nothing.
757  */
758
759 /**
760  * g_scanner_thaw_symbol_table:
761  * @scanner: a #GScanner
762  *
763  * There is no reason to use this macro, since it does nothing.
764  *
765  * Deprecated: 2.2: This macro does nothing.
766  */
767
768 /**
769  * g_scanner_lookup_symbol:
770  * @scanner: a #GScanner
771  * @symbol: the symbol to look up
772  *
773  * Looks up a symbol in the current scope and return its value.
774  * If the symbol is not bound in the current scope, %NULL is
775  * returned.
776  *
777  * Returns: the value of @symbol in the current scope, or %NULL
778  *     if @symbol is not bound in the current scope
779  */
780 gpointer
781 g_scanner_lookup_symbol (GScanner       *scanner,
782                          const gchar    *symbol)
783 {
784   GScannerKey   *key;
785   guint scope_id;
786   
787   g_return_val_if_fail (scanner != NULL, NULL);
788   
789   if (!symbol)
790     return NULL;
791   
792   scope_id = scanner->scope_id;
793   key = g_scanner_lookup_internal (scanner, scope_id, symbol);
794   if (!key && scope_id && scanner->config->scope_0_fallback)
795     key = g_scanner_lookup_internal (scanner, 0, symbol);
796   
797   if (key)
798     return key->value;
799   else
800     return NULL;
801 }
802
803 /**
804  * g_scanner_scope_lookup_symbol:
805  * @scanner: a #GScanner
806  * @scope_id: the scope id
807  * @symbol: the symbol to look up
808  *
809  * Looks up a symbol in a scope and return its value. If the
810  * symbol is not bound in the scope, %NULL is returned.
811  *
812  * Returns: the value of @symbol in the given scope, or %NULL
813  *     if @symbol is not bound in the given scope.
814  *
815  */
816 gpointer
817 g_scanner_scope_lookup_symbol (GScanner       *scanner,
818                                guint           scope_id,
819                                const gchar    *symbol)
820 {
821   GScannerKey   *key;
822   
823   g_return_val_if_fail (scanner != NULL, NULL);
824   
825   if (!symbol)
826     return NULL;
827   
828   key = g_scanner_lookup_internal (scanner, scope_id, symbol);
829   
830   if (key)
831     return key->value;
832   else
833     return NULL;
834 }
835
836 /**
837  * g_scanner_set_scope:
838  * @scanner: a #GScanner
839  * @scope_id: the new scope id
840  *
841  * Sets the current scope.
842  *
843  * Returns: the old scope id
844  */
845 guint
846 g_scanner_set_scope (GScanner       *scanner,
847                      guint           scope_id)
848 {
849   guint old_scope_id;
850   
851   g_return_val_if_fail (scanner != NULL, 0);
852   
853   old_scope_id = scanner->scope_id;
854   scanner->scope_id = scope_id;
855   
856   return old_scope_id;
857 }
858
859 static void
860 g_scanner_foreach_internal (gpointer  _key,
861                             gpointer  _value,
862                             gpointer  _user_data)
863 {
864   GScannerKey *key;
865   gpointer *d;
866   GHFunc func;
867   gpointer user_data;
868   guint *scope_id;
869   
870   d = _user_data;
871   func = (GHFunc) d[0];
872   user_data = d[1];
873   scope_id = d[2];
874   key = _value;
875   
876   if (key->scope_id == *scope_id)
877     func (key->symbol, key->value, user_data);
878 }
879
880 /**
881  * g_scanner_foreach_symbol:
882  * @scanner: a #GScanner
883  * @func: the function to call with each symbol
884  * @data: data to pass to the function
885  *
886  * Calls a function for each symbol in the default scope.
887  *
888  * Deprecated: 2.2: Use g_scanner_scope_foreach_symbol() instead.
889  */
890
891 /**
892  * g_scanner_scope_foreach_symbol:
893  * @scanner: a #GScanner
894  * @scope_id: the scope id
895  * @func: the function to call for each symbol/value pair
896  * @user_data: user data to pass to the function
897  *
898  * Calls the given function for each of the symbol/value pairs
899  * in the given scope of the #GScanner. The function is passed
900  * the symbol and value of each pair, and the given @user_data
901  * parameter.
902  */
903 void
904 g_scanner_scope_foreach_symbol (GScanner       *scanner,
905                                 guint           scope_id,
906                                 GHFunc          func,
907                                 gpointer        user_data)
908 {
909   gpointer d[3];
910   
911   g_return_if_fail (scanner != NULL);
912   
913   d[0] = (gpointer) func;
914   d[1] = user_data;
915   d[2] = &scope_id;
916   
917   g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
918 }
919
920 /**
921  * g_scanner_peek_next_token:
922  * @scanner: a #GScanner
923  *
924  * Parses the next token, without removing it from the input stream.
925  * The token data is placed in the @next_token, @next_value, @next_line,
926  * and @next_position fields of the #GScanner structure.
927  *
928  * Note that, while the token is not removed from the input stream
929  * (i.e. the next call to g_scanner_get_next_token() will return the
930  * same token), it will not be reevaluated. This can lead to surprising
931  * results when changing scope or the scanner configuration after peeking
932  * the next token. Getting the next token after switching the scope or
933  * configuration will return whatever was peeked before, regardless of
934  * any symbols that may have been added or removed in the new scope.
935  *
936  * Returns: the type of the token
937  */
938 GTokenType
939 g_scanner_peek_next_token (GScanner     *scanner)
940 {
941   g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
942   
943   if (scanner->next_token == G_TOKEN_NONE)
944     {
945       scanner->next_line = scanner->line;
946       scanner->next_position = scanner->position;
947       g_scanner_get_token_i (scanner,
948                              &scanner->next_token,
949                              &scanner->next_value,
950                              &scanner->next_line,
951                              &scanner->next_position);
952     }
953   
954   return scanner->next_token;
955 }
956
957 /**
958  * g_scanner_get_next_token:
959  * @scanner: a #GScanner
960  *
961  * Parses the next token just like g_scanner_peek_next_token()
962  * and also removes it from the input stream. The token data is
963  * placed in the @token, @value, @line, and @position fields of
964  * the #GScanner structure.
965  *
966  * Returns: the type of the token
967  */
968 GTokenType
969 g_scanner_get_next_token (GScanner      *scanner)
970 {
971   g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
972   
973   if (scanner->next_token != G_TOKEN_NONE)
974     {
975       g_scanner_free_value (&scanner->token, &scanner->value);
976       
977       scanner->token = scanner->next_token;
978       scanner->value = scanner->next_value;
979       scanner->line = scanner->next_line;
980       scanner->position = scanner->next_position;
981       scanner->next_token = G_TOKEN_NONE;
982     }
983   else
984     g_scanner_get_token_i (scanner,
985                            &scanner->token,
986                            &scanner->value,
987                            &scanner->line,
988                            &scanner->position);
989   
990   return scanner->token;
991 }
992
993 /**
994  * g_scanner_cur_token:
995  * @scanner: a #GScanner
996  *
997  * Gets the current token type. This is simply the @token
998  * field in the #GScanner structure.
999  *
1000  * Returns: the current token type
1001  */
1002 GTokenType
1003 g_scanner_cur_token (GScanner *scanner)
1004 {
1005   g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
1006   
1007   return scanner->token;
1008 }
1009
1010 /**
1011  * g_scanner_cur_value:
1012  * @scanner: a #GScanner
1013  *
1014  * Gets the current token value. This is simply the @value
1015  * field in the #GScanner structure.
1016  *
1017  * Returns: the current token value
1018  */
1019 GTokenValue
1020 g_scanner_cur_value (GScanner *scanner)
1021 {
1022   GTokenValue v;
1023   
1024   v.v_int64 = 0;
1025   
1026   g_return_val_if_fail (scanner != NULL, v);
1027
1028   /* MSC isn't capable of handling return scanner->value; ? */
1029
1030   v = scanner->value;
1031
1032   return v;
1033 }
1034
1035 /**
1036  * g_scanner_cur_line:
1037  * @scanner: a #GScanner
1038  *
1039  * Returns the current line in the input stream (counting
1040  * from 1). This is the line of the last token parsed via
1041  * g_scanner_get_next_token().
1042  *
1043  * Returns: the current line
1044  */
1045 guint
1046 g_scanner_cur_line (GScanner *scanner)
1047 {
1048   g_return_val_if_fail (scanner != NULL, 0);
1049   
1050   return scanner->line;
1051 }
1052
1053 /**
1054  * g_scanner_cur_position:
1055  * @scanner: a #GScanner
1056  *
1057  * Returns the current position in the current line (counting
1058  * from 0). This is the position of the last token parsed via
1059  * g_scanner_get_next_token().
1060  *
1061  * Returns: the current position on the line
1062  */
1063 guint
1064 g_scanner_cur_position (GScanner *scanner)
1065 {
1066   g_return_val_if_fail (scanner != NULL, 0);
1067   
1068   return scanner->position;
1069 }
1070
1071 /**
1072  * g_scanner_eof:
1073  * @scanner: a #GScanner
1074  *
1075  * Returns %TRUE if the scanner has reached the end of
1076  * the file or text buffer.
1077  *
1078  * Returns: %TRUE if the scanner has reached the end of
1079  *     the file or text buffer
1080  */
1081 gboolean
1082 g_scanner_eof (GScanner *scanner)
1083 {
1084   g_return_val_if_fail (scanner != NULL, TRUE);
1085   
1086   return scanner->token == G_TOKEN_EOF || scanner->token == G_TOKEN_ERROR;
1087 }
1088
1089 /**
1090  * g_scanner_input_file:
1091  * @scanner: a #GScanner
1092  * @input_fd: a file descriptor
1093  *
1094  * Prepares to scan a file.
1095  */
1096 void
1097 g_scanner_input_file (GScanner *scanner,
1098                       gint      input_fd)
1099 {
1100   g_return_if_fail (scanner != NULL);
1101   g_return_if_fail (input_fd >= 0);
1102
1103   if (scanner->input_fd >= 0)
1104     g_scanner_sync_file_offset (scanner);
1105
1106   scanner->token = G_TOKEN_NONE;
1107   scanner->value.v_int64 = 0;
1108   scanner->line = 1;
1109   scanner->position = 0;
1110   scanner->next_token = G_TOKEN_NONE;
1111
1112   scanner->input_fd = input_fd;
1113   scanner->text = NULL;
1114   scanner->text_end = NULL;
1115
1116   if (!scanner->buffer)
1117     scanner->buffer = g_new (gchar, READ_BUFFER_SIZE + 1);
1118 }
1119
1120 /**
1121  * g_scanner_input_text:
1122  * @scanner: a #GScanner
1123  * @text: the text buffer to scan
1124  * @text_len: the length of the text buffer
1125  *
1126  * Prepares to scan a text buffer.
1127  */
1128 void
1129 g_scanner_input_text (GScanner    *scanner,
1130                       const gchar *text,
1131                       guint        text_len)
1132 {
1133   g_return_if_fail (scanner != NULL);
1134   if (text_len)
1135     g_return_if_fail (text != NULL);
1136   else
1137     text = NULL;
1138
1139   if (scanner->input_fd >= 0)
1140     g_scanner_sync_file_offset (scanner);
1141
1142   scanner->token = G_TOKEN_NONE;
1143   scanner->value.v_int64 = 0;
1144   scanner->line = 1;
1145   scanner->position = 0;
1146   scanner->next_token = G_TOKEN_NONE;
1147
1148   scanner->input_fd = -1;
1149   scanner->text = text;
1150   scanner->text_end = text + text_len;
1151
1152   if (scanner->buffer)
1153     {
1154       g_free (scanner->buffer);
1155       scanner->buffer = NULL;
1156     }
1157 }
1158
1159 static guchar
1160 g_scanner_peek_next_char (GScanner *scanner)
1161 {
1162   if (scanner->text < scanner->text_end)
1163     {
1164       return *scanner->text;
1165     }
1166   else if (scanner->input_fd >= 0)
1167     {
1168       gint count;
1169       gchar *buffer;
1170
1171       buffer = scanner->buffer;
1172       do
1173         {
1174           count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
1175         }
1176       while (count == -1 && (errno == EINTR || errno == EAGAIN));
1177
1178       if (count < 1)
1179         {
1180           scanner->input_fd = -1;
1181
1182           return 0;
1183         }
1184       else
1185         {
1186           scanner->text = buffer;
1187           scanner->text_end = buffer + count;
1188
1189           return *buffer;
1190         }
1191     }
1192   else
1193     return 0;
1194 }
1195
1196 /**
1197  * g_scanner_sync_file_offset:
1198  * @scanner: a #GScanner
1199  *
1200  * Rewinds the filedescriptor to the current buffer position
1201  * and blows the file read ahead buffer. This is useful for
1202  * third party uses of the scanners filedescriptor, which hooks
1203  * onto the current scanning position.
1204  */
1205 void
1206 g_scanner_sync_file_offset (GScanner *scanner)
1207 {
1208   g_return_if_fail (scanner != NULL);
1209
1210   /* for file input, rewind the filedescriptor to the current
1211    * buffer position and blow the file read ahead buffer. useful
1212    * for third party uses of our file descriptor, which hooks 
1213    * onto the current scanning position.
1214    */
1215
1216   if (scanner->input_fd >= 0 && scanner->text_end > scanner->text)
1217     {
1218       gint buffered;
1219
1220       buffered = scanner->text_end - scanner->text;
1221       if (lseek (scanner->input_fd, - buffered, SEEK_CUR) >= 0)
1222         {
1223           /* we succeeded, blow our buffer's contents now */
1224           scanner->text = NULL;
1225           scanner->text_end = NULL;
1226         }
1227       else
1228         errno = 0;
1229     }
1230 }
1231
1232 static guchar
1233 g_scanner_get_char (GScanner    *scanner,
1234                     guint       *line_p,
1235                     guint       *position_p)
1236 {
1237   guchar fchar;
1238
1239   if (scanner->text < scanner->text_end)
1240     fchar = *(scanner->text++);
1241   else if (scanner->input_fd >= 0)
1242     {
1243       gint count;
1244       gchar *buffer;
1245
1246       buffer = scanner->buffer;
1247       do
1248         {
1249           count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
1250         }
1251       while (count == -1 && (errno == EINTR || errno == EAGAIN));
1252
1253       if (count < 1)
1254         {
1255           scanner->input_fd = -1;
1256           fchar = 0;
1257         }
1258       else
1259         {
1260           scanner->text = buffer + 1;
1261           scanner->text_end = buffer + count;
1262           fchar = *buffer;
1263           if (!fchar)
1264             {
1265               g_scanner_sync_file_offset (scanner);
1266               scanner->text_end = scanner->text;
1267               scanner->input_fd = -1;
1268             }
1269         }
1270     }
1271   else
1272     fchar = 0;
1273   
1274   if (fchar == '\n')
1275     {
1276       (*position_p) = 0;
1277       (*line_p)++;
1278     }
1279   else if (fchar)
1280     {
1281       (*position_p)++;
1282     }
1283   
1284   return fchar;
1285 }
1286
1287 /**
1288  * g_scanner_unexp_token:
1289  * @scanner: a #GScanner
1290  * @expected_token: the expected token
1291  * @identifier_spec: a string describing how the scanner's user
1292  *     refers to identifiers (%NULL defaults to "identifier").
1293  *     This is used if @expected_token is %G_TOKEN_IDENTIFIER or
1294  *     %G_TOKEN_IDENTIFIER_NULL.
1295  * @symbol_spec: a string describing how the scanner's user refers
1296  *     to symbols (%NULL defaults to "symbol"). This is used if
1297  *     @expected_token is %G_TOKEN_SYMBOL or any token value greater
1298  *     than %G_TOKEN_LAST.
1299  * @symbol_name: the name of the symbol, if the scanner's current
1300  *     token is a symbol.
1301  * @message: a message string to output at the end of the
1302  *     warning/error, or %NULL.
1303  * @is_error: if %TRUE it is output as an error. If %FALSE it is
1304  *     output as a warning.
1305  *
1306  * Outputs a message through the scanner's msg_handler,
1307  * resulting from an unexpected token in the input stream.
1308  * Note that you should not call g_scanner_peek_next_token()
1309  * followed by g_scanner_unexp_token() without an intermediate
1310  * call to g_scanner_get_next_token(), as g_scanner_unexp_token()
1311  * evaluates the scanner's current token (not the peeked token)
1312  * to construct part of the message.
1313  */
1314 void
1315 g_scanner_unexp_token (GScanner         *scanner,
1316                        GTokenType        expected_token,
1317                        const gchar      *identifier_spec,
1318                        const gchar      *symbol_spec,
1319                        const gchar      *symbol_name,
1320                        const gchar      *message,
1321                        gint              is_error)
1322 {
1323   gchar *token_string;
1324   guint token_string_len;
1325   gchar *expected_string;
1326   guint expected_string_len;
1327   gchar *message_prefix;
1328   gboolean print_unexp;
1329   void (*msg_handler)   (GScanner*, const gchar*, ...);
1330   
1331   g_return_if_fail (scanner != NULL);
1332   
1333   if (is_error)
1334     msg_handler = g_scanner_error;
1335   else
1336     msg_handler = g_scanner_warn;
1337   
1338   if (!identifier_spec)
1339     identifier_spec = "identifier";
1340   if (!symbol_spec)
1341     symbol_spec = "symbol";
1342   
1343   token_string_len = 56;
1344   token_string = g_new (gchar, token_string_len + 1);
1345   expected_string_len = 64;
1346   expected_string = g_new (gchar, expected_string_len + 1);
1347   print_unexp = TRUE;
1348   
1349   switch (scanner->token)
1350     {
1351     case G_TOKEN_EOF:
1352       _g_snprintf (token_string, token_string_len, "end of file");
1353       break;
1354       
1355     default:
1356       if (scanner->token >= 1 && scanner->token <= 255)
1357         {
1358           if ((scanner->token >= ' ' && scanner->token <= '~') ||
1359               strchr (scanner->config->cset_identifier_first, scanner->token) ||
1360               strchr (scanner->config->cset_identifier_nth, scanner->token))
1361             _g_snprintf (token_string, token_string_len, "character `%c'", scanner->token);
1362           else
1363             _g_snprintf (token_string, token_string_len, "character `\\%o'", scanner->token);
1364           break;
1365         }
1366       else if (!scanner->config->symbol_2_token)
1367         {
1368           _g_snprintf (token_string, token_string_len, "(unknown) token <%d>", scanner->token);
1369           break;
1370         }
1371       /* fall through */
1372     case G_TOKEN_SYMBOL:
1373       if (expected_token == G_TOKEN_SYMBOL ||
1374           (scanner->config->symbol_2_token &&
1375            expected_token > G_TOKEN_LAST))
1376         print_unexp = FALSE;
1377       if (symbol_name)
1378         _g_snprintf (token_string,
1379                      token_string_len,
1380                      "%s%s `%s'",
1381                      print_unexp ? "" : "invalid ",
1382                      symbol_spec,
1383                      symbol_name);
1384       else
1385         _g_snprintf (token_string,
1386                      token_string_len,
1387                      "%s%s",
1388                      print_unexp ? "" : "invalid ",
1389                      symbol_spec);
1390       break;
1391       
1392     case G_TOKEN_ERROR:
1393       print_unexp = FALSE;
1394       expected_token = G_TOKEN_NONE;
1395       switch (scanner->value.v_error)
1396         {
1397         case G_ERR_UNEXP_EOF:
1398           _g_snprintf (token_string, token_string_len, "scanner: unexpected end of file");
1399           break;
1400           
1401         case G_ERR_UNEXP_EOF_IN_STRING:
1402           _g_snprintf (token_string, token_string_len, "scanner: unterminated string constant");
1403           break;
1404           
1405         case G_ERR_UNEXP_EOF_IN_COMMENT:
1406           _g_snprintf (token_string, token_string_len, "scanner: unterminated comment");
1407           break;
1408           
1409         case G_ERR_NON_DIGIT_IN_CONST:
1410           _g_snprintf (token_string, token_string_len, "scanner: non digit in constant");
1411           break;
1412           
1413         case G_ERR_FLOAT_RADIX:
1414           _g_snprintf (token_string, token_string_len, "scanner: invalid radix for floating constant");
1415           break;
1416           
1417         case G_ERR_FLOAT_MALFORMED:
1418           _g_snprintf (token_string, token_string_len, "scanner: malformed floating constant");
1419           break;
1420           
1421         case G_ERR_DIGIT_RADIX:
1422           _g_snprintf (token_string, token_string_len, "scanner: digit is beyond radix");
1423           break;
1424           
1425         case G_ERR_UNKNOWN:
1426         default:
1427           _g_snprintf (token_string, token_string_len, "scanner: unknown error");
1428           break;
1429         }
1430       break;
1431       
1432     case G_TOKEN_CHAR:
1433       _g_snprintf (token_string, token_string_len, "character `%c'", scanner->value.v_char);
1434       break;
1435       
1436     case G_TOKEN_IDENTIFIER:
1437     case G_TOKEN_IDENTIFIER_NULL:
1438       if (expected_token == G_TOKEN_IDENTIFIER ||
1439           expected_token == G_TOKEN_IDENTIFIER_NULL)
1440         print_unexp = FALSE;
1441       _g_snprintf (token_string,
1442                   token_string_len,
1443                   "%s%s `%s'",
1444                   print_unexp ? "" : "invalid ",
1445                   identifier_spec,
1446                   scanner->token == G_TOKEN_IDENTIFIER ? scanner->value.v_string : "null");
1447       break;
1448       
1449     case G_TOKEN_BINARY:
1450     case G_TOKEN_OCTAL:
1451     case G_TOKEN_INT:
1452     case G_TOKEN_HEX:
1453       if (scanner->config->store_int64)
1454         _g_snprintf (token_string, token_string_len, "number `%" G_GUINT64_FORMAT "'", scanner->value.v_int64);
1455       else
1456         _g_snprintf (token_string, token_string_len, "number `%lu'", scanner->value.v_int);
1457       break;
1458       
1459     case G_TOKEN_FLOAT:
1460       _g_snprintf (token_string, token_string_len, "number `%.3f'", scanner->value.v_float);
1461       break;
1462       
1463     case G_TOKEN_STRING:
1464       if (expected_token == G_TOKEN_STRING)
1465         print_unexp = FALSE;
1466       _g_snprintf (token_string,
1467                    token_string_len,
1468                    "%s%sstring constant \"%s\"",
1469                    print_unexp ? "" : "invalid ",
1470                    scanner->value.v_string[0] == 0 ? "empty " : "",
1471                    scanner->value.v_string);
1472       token_string[token_string_len - 2] = '"';
1473       token_string[token_string_len - 1] = 0;
1474       break;
1475       
1476     case G_TOKEN_COMMENT_SINGLE:
1477     case G_TOKEN_COMMENT_MULTI:
1478       _g_snprintf (token_string, token_string_len, "comment");
1479       break;
1480       
1481     case G_TOKEN_NONE:
1482       /* somehow the user's parsing code is screwed, there isn't much
1483        * we can do about it.
1484        * Note, a common case to trigger this is
1485        * g_scanner_peek_next_token(); g_scanner_unexp_token();
1486        * without an intermediate g_scanner_get_next_token().
1487        */
1488       g_assert_not_reached ();
1489       break;
1490     }
1491   
1492   
1493   switch (expected_token)
1494     {
1495       gboolean need_valid;
1496       gchar *tstring;
1497     case G_TOKEN_EOF:
1498       _g_snprintf (expected_string, expected_string_len, "end of file");
1499       break;
1500     default:
1501       if (expected_token >= 1 && expected_token <= 255)
1502         {
1503           if ((expected_token >= ' ' && expected_token <= '~') ||
1504               strchr (scanner->config->cset_identifier_first, expected_token) ||
1505               strchr (scanner->config->cset_identifier_nth, expected_token))
1506             _g_snprintf (expected_string, expected_string_len, "character `%c'", expected_token);
1507           else
1508             _g_snprintf (expected_string, expected_string_len, "character `\\%o'", expected_token);
1509           break;
1510         }
1511       else if (!scanner->config->symbol_2_token)
1512         {
1513           _g_snprintf (expected_string, expected_string_len, "(unknown) token <%d>", expected_token);
1514           break;
1515         }
1516       /* fall through */
1517     case G_TOKEN_SYMBOL:
1518       need_valid = (scanner->token == G_TOKEN_SYMBOL ||
1519                     (scanner->config->symbol_2_token &&
1520                      scanner->token > G_TOKEN_LAST));
1521       _g_snprintf (expected_string,
1522                    expected_string_len,
1523                    "%s%s",
1524                    need_valid ? "valid " : "",
1525                    symbol_spec);
1526       /* FIXME: should we attempt to lookup the symbol_name for symbol_2_token? */
1527       break;
1528     case G_TOKEN_CHAR:
1529       _g_snprintf (expected_string, expected_string_len, "%scharacter",
1530                    scanner->token == G_TOKEN_CHAR ? "valid " : "");
1531       break;
1532     case G_TOKEN_BINARY:
1533       tstring = "binary";
1534       _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1535                    scanner->token == expected_token ? "valid " : "", tstring);
1536       break;
1537     case G_TOKEN_OCTAL:
1538       tstring = "octal";
1539       _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1540                    scanner->token == expected_token ? "valid " : "", tstring);
1541       break;
1542     case G_TOKEN_INT:
1543       tstring = "integer";
1544       _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1545                    scanner->token == expected_token ? "valid " : "", tstring);
1546       break;
1547     case G_TOKEN_HEX:
1548       tstring = "hexadecimal";
1549       _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1550                    scanner->token == expected_token ? "valid " : "", tstring);
1551       break;
1552     case G_TOKEN_FLOAT:
1553       tstring = "float";
1554       _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1555                    scanner->token == expected_token ? "valid " : "", tstring);
1556       break;
1557     case G_TOKEN_STRING:
1558       _g_snprintf (expected_string,
1559                    expected_string_len,
1560                    "%sstring constant",
1561                    scanner->token == G_TOKEN_STRING ? "valid " : "");
1562       break;
1563     case G_TOKEN_IDENTIFIER:
1564     case G_TOKEN_IDENTIFIER_NULL:
1565       need_valid = (scanner->token == G_TOKEN_IDENTIFIER_NULL ||
1566                     scanner->token == G_TOKEN_IDENTIFIER);
1567       _g_snprintf (expected_string,
1568                    expected_string_len,
1569                    "%s%s",
1570                    need_valid ? "valid " : "",
1571                    identifier_spec);
1572       break;
1573     case G_TOKEN_COMMENT_SINGLE:
1574       tstring = "single-line";
1575       _g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1576                    scanner->token == expected_token ? "valid " : "", tstring);
1577       break;
1578     case G_TOKEN_COMMENT_MULTI:
1579       tstring = "multi-line";
1580       _g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1581                    scanner->token == expected_token ? "valid " : "", tstring);
1582       break;
1583     case G_TOKEN_NONE:
1584     case G_TOKEN_ERROR:
1585       /* this is handled upon printout */
1586       break;
1587     }
1588   
1589   if (message && message[0] != 0)
1590     message_prefix = " - ";
1591   else
1592     {
1593       message_prefix = "";
1594       message = "";
1595     }
1596   if (expected_token == G_TOKEN_ERROR)
1597     {
1598       msg_handler (scanner,
1599                    "failure around %s%s%s",
1600                    token_string,
1601                    message_prefix,
1602                    message);
1603     }
1604   else if (expected_token == G_TOKEN_NONE)
1605     {
1606       if (print_unexp)
1607         msg_handler (scanner,
1608                      "unexpected %s%s%s",
1609                      token_string,
1610                      message_prefix,
1611                      message);
1612       else
1613         msg_handler (scanner,
1614                      "%s%s%s",
1615                      token_string,
1616                      message_prefix,
1617                      message);
1618     }
1619   else
1620     {
1621       if (print_unexp)
1622         msg_handler (scanner,
1623                      "unexpected %s, expected %s%s%s",
1624                      token_string,
1625                      expected_string,
1626                      message_prefix,
1627                      message);
1628       else
1629         msg_handler (scanner,
1630                      "%s, expected %s%s%s",
1631                      token_string,
1632                      expected_string,
1633                      message_prefix,
1634                      message);
1635     }
1636   
1637   g_free (token_string);
1638   g_free (expected_string);
1639 }
1640
1641 static void
1642 g_scanner_get_token_i (GScanner *scanner,
1643                        GTokenType       *token_p,
1644                        GTokenValue      *value_p,
1645                        guint            *line_p,
1646                        guint            *position_p)
1647 {
1648   do
1649     {
1650       g_scanner_free_value (token_p, value_p);
1651       g_scanner_get_token_ll (scanner, token_p, value_p, line_p, position_p);
1652     }
1653   while (((*token_p > 0 && *token_p < 256) &&
1654           strchr (scanner->config->cset_skip_characters, *token_p)) ||
1655          (*token_p == G_TOKEN_CHAR &&
1656           strchr (scanner->config->cset_skip_characters, value_p->v_char)) ||
1657          (*token_p == G_TOKEN_COMMENT_MULTI &&
1658           scanner->config->skip_comment_multi) ||
1659          (*token_p == G_TOKEN_COMMENT_SINGLE &&
1660           scanner->config->skip_comment_single));
1661   
1662   switch (*token_p)
1663     {
1664     case G_TOKEN_IDENTIFIER:
1665       if (scanner->config->identifier_2_string)
1666         *token_p = G_TOKEN_STRING;
1667       break;
1668       
1669     case G_TOKEN_SYMBOL:
1670       if (scanner->config->symbol_2_token)
1671         *token_p = (GTokenType) value_p->v_symbol;
1672       break;
1673       
1674     case G_TOKEN_BINARY:
1675     case G_TOKEN_OCTAL:
1676     case G_TOKEN_HEX:
1677       if (scanner->config->numbers_2_int)
1678         *token_p = G_TOKEN_INT;
1679       break;
1680       
1681     default:
1682       break;
1683     }
1684   
1685   if (*token_p == G_TOKEN_INT &&
1686       scanner->config->int_2_float)
1687     {
1688       *token_p = G_TOKEN_FLOAT;
1689       if (scanner->config->store_int64)
1690         {
1691 #ifdef _MSC_VER
1692           /* work around error C2520, see gvaluetransform.c */
1693           value_p->v_float = (__int64)value_p->v_int64;
1694 #else
1695           value_p->v_float = value_p->v_int64;
1696 #endif
1697         }
1698       else
1699         value_p->v_float = value_p->v_int;
1700     }
1701   
1702   errno = 0;
1703 }
1704
1705 static void
1706 g_scanner_get_token_ll  (GScanner       *scanner,
1707                          GTokenType     *token_p,
1708                          GTokenValue    *value_p,
1709                          guint          *line_p,
1710                          guint          *position_p)
1711 {
1712   GScannerConfig *config;
1713   GTokenType       token;
1714   gboolean         in_comment_multi;
1715   gboolean         in_comment_single;
1716   gboolean         in_string_sq;
1717   gboolean         in_string_dq;
1718   GString         *gstring;
1719   GTokenValue      value;
1720   guchar           ch;
1721   
1722   config = scanner->config;
1723   (*value_p).v_int64 = 0;
1724   
1725   if ((scanner->text >= scanner->text_end && scanner->input_fd < 0) ||
1726       scanner->token == G_TOKEN_EOF)
1727     {
1728       *token_p = G_TOKEN_EOF;
1729       return;
1730     }
1731   
1732   in_comment_multi = FALSE;
1733   in_comment_single = FALSE;
1734   in_string_sq = FALSE;
1735   in_string_dq = FALSE;
1736   gstring = NULL;
1737   
1738   do /* while (ch != 0) */
1739     {
1740       gboolean dotted_float = FALSE;
1741       
1742       ch = g_scanner_get_char (scanner, line_p, position_p);
1743       
1744       value.v_int64 = 0;
1745       token = G_TOKEN_NONE;
1746       
1747       /* this is *evil*, but needed ;(
1748        * we first check for identifier first character, because  it
1749        * might interfere with other key chars like slashes or numbers
1750        */
1751       if (config->scan_identifier &&
1752           ch && strchr (config->cset_identifier_first, ch))
1753         goto identifier_precedence;
1754       
1755       switch (ch)
1756         {
1757         case 0:
1758           token = G_TOKEN_EOF;
1759           (*position_p)++;
1760           /* ch = 0; */
1761           break;
1762           
1763         case '/':
1764           if (!config->scan_comment_multi ||
1765               g_scanner_peek_next_char (scanner) != '*')
1766             goto default_case;
1767           g_scanner_get_char (scanner, line_p, position_p);
1768           token = G_TOKEN_COMMENT_MULTI;
1769           in_comment_multi = TRUE;
1770           gstring = g_string_new (NULL);
1771           while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1772             {
1773               if (ch == '*' && g_scanner_peek_next_char (scanner) == '/')
1774                 {
1775                   g_scanner_get_char (scanner, line_p, position_p);
1776                   in_comment_multi = FALSE;
1777                   break;
1778                 }
1779               else
1780                 gstring = g_string_append_c (gstring, ch);
1781             }
1782           ch = 0;
1783           break;
1784           
1785         case '\'':
1786           if (!config->scan_string_sq)
1787             goto default_case;
1788           token = G_TOKEN_STRING;
1789           in_string_sq = TRUE;
1790           gstring = g_string_new (NULL);
1791           while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1792             {
1793               if (ch == '\'')
1794                 {
1795                   in_string_sq = FALSE;
1796                   break;
1797                 }
1798               else
1799                 gstring = g_string_append_c (gstring, ch);
1800             }
1801           ch = 0;
1802           break;
1803           
1804         case '"':
1805           if (!config->scan_string_dq)
1806             goto default_case;
1807           token = G_TOKEN_STRING;
1808           in_string_dq = TRUE;
1809           gstring = g_string_new (NULL);
1810           while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1811             {
1812               if (ch == '"')
1813                 {
1814                   in_string_dq = FALSE;
1815                   break;
1816                 }
1817               else
1818                 {
1819                   if (ch == '\\')
1820                     {
1821                       ch = g_scanner_get_char (scanner, line_p, position_p);
1822                       switch (ch)
1823                         {
1824                           guint i;
1825                           guint fchar;
1826                           
1827                         case 0:
1828                           break;
1829                           
1830                         case '\\':
1831                           gstring = g_string_append_c (gstring, '\\');
1832                           break;
1833                           
1834                         case 'n':
1835                           gstring = g_string_append_c (gstring, '\n');
1836                           break;
1837                           
1838                         case 't':
1839                           gstring = g_string_append_c (gstring, '\t');
1840                           break;
1841                           
1842                         case 'r':
1843                           gstring = g_string_append_c (gstring, '\r');
1844                           break;
1845                           
1846                         case 'b':
1847                           gstring = g_string_append_c (gstring, '\b');
1848                           break;
1849                           
1850                         case 'f':
1851                           gstring = g_string_append_c (gstring, '\f');
1852                           break;
1853                           
1854                         case '0':
1855                         case '1':
1856                         case '2':
1857                         case '3':
1858                         case '4':
1859                         case '5':
1860                         case '6':
1861                         case '7':
1862                           i = ch - '0';
1863                           fchar = g_scanner_peek_next_char (scanner);
1864                           if (fchar >= '0' && fchar <= '7')
1865                             {
1866                               ch = g_scanner_get_char (scanner, line_p, position_p);
1867                               i = i * 8 + ch - '0';
1868                               fchar = g_scanner_peek_next_char (scanner);
1869                               if (fchar >= '0' && fchar <= '7')
1870                                 {
1871                                   ch = g_scanner_get_char (scanner, line_p, position_p);
1872                                   i = i * 8 + ch - '0';
1873                                 }
1874                             }
1875                           gstring = g_string_append_c (gstring, i);
1876                           break;
1877                           
1878                         default:
1879                           gstring = g_string_append_c (gstring, ch);
1880                           break;
1881                         }
1882                     }
1883                   else
1884                     gstring = g_string_append_c (gstring, ch);
1885                 }
1886             }
1887           ch = 0;
1888           break;
1889           
1890         case '.':
1891           if (!config->scan_float)
1892             goto default_case;
1893           token = G_TOKEN_FLOAT;
1894           dotted_float = TRUE;
1895           ch = g_scanner_get_char (scanner, line_p, position_p);
1896           goto number_parsing;
1897           
1898         case '$':
1899           if (!config->scan_hex_dollar)
1900             goto default_case;
1901           token = G_TOKEN_HEX;
1902           ch = g_scanner_get_char (scanner, line_p, position_p);
1903           goto number_parsing;
1904           
1905         case '0':
1906           if (config->scan_octal)
1907             token = G_TOKEN_OCTAL;
1908           else
1909             token = G_TOKEN_INT;
1910           ch = g_scanner_peek_next_char (scanner);
1911           if (config->scan_hex && (ch == 'x' || ch == 'X'))
1912             {
1913               token = G_TOKEN_HEX;
1914               g_scanner_get_char (scanner, line_p, position_p);
1915               ch = g_scanner_get_char (scanner, line_p, position_p);
1916               if (ch == 0)
1917                 {
1918                   token = G_TOKEN_ERROR;
1919                   value.v_error = G_ERR_UNEXP_EOF;
1920                   (*position_p)++;
1921                   break;
1922                 }
1923               if (g_scanner_char_2_num (ch, 16) < 0)
1924                 {
1925                   token = G_TOKEN_ERROR;
1926                   value.v_error = G_ERR_DIGIT_RADIX;
1927                   ch = 0;
1928                   break;
1929                 }
1930             }
1931           else if (config->scan_binary && (ch == 'b' || ch == 'B'))
1932             {
1933               token = G_TOKEN_BINARY;
1934               g_scanner_get_char (scanner, line_p, position_p);
1935               ch = g_scanner_get_char (scanner, line_p, position_p);
1936               if (ch == 0)
1937                 {
1938                   token = G_TOKEN_ERROR;
1939                   value.v_error = G_ERR_UNEXP_EOF;
1940                   (*position_p)++;
1941                   break;
1942                 }
1943               if (g_scanner_char_2_num (ch, 10) < 0)
1944                 {
1945                   token = G_TOKEN_ERROR;
1946                   value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1947                   ch = 0;
1948                   break;
1949                 }
1950             }
1951           else
1952             ch = '0';
1953           /* fall through */
1954         case '1':
1955         case '2':
1956         case '3':
1957         case '4':
1958         case '5':
1959         case '6':
1960         case '7':
1961         case '8':
1962         case '9':
1963         number_parsing:
1964         {
1965           gboolean in_number = TRUE;
1966           gchar *endptr;
1967           
1968           if (token == G_TOKEN_NONE)
1969             token = G_TOKEN_INT;
1970           
1971           gstring = g_string_new (dotted_float ? "0." : "");
1972           gstring = g_string_append_c (gstring, ch);
1973           
1974           do /* while (in_number) */
1975             {
1976               gboolean is_E;
1977               
1978               is_E = token == G_TOKEN_FLOAT && (ch == 'e' || ch == 'E');
1979               
1980               ch = g_scanner_peek_next_char (scanner);
1981               
1982               if (g_scanner_char_2_num (ch, 36) >= 0 ||
1983                   (config->scan_float && ch == '.') ||
1984                   (is_E && (ch == '+' || ch == '-')))
1985                 {
1986                   ch = g_scanner_get_char (scanner, line_p, position_p);
1987                   
1988                   switch (ch)
1989                     {
1990                     case '.':
1991                       if (token != G_TOKEN_INT && token != G_TOKEN_OCTAL)
1992                         {
1993                           value.v_error = token == G_TOKEN_FLOAT ? G_ERR_FLOAT_MALFORMED : G_ERR_FLOAT_RADIX;
1994                           token = G_TOKEN_ERROR;
1995                           in_number = FALSE;
1996                         }
1997                       else
1998                         {
1999                           token = G_TOKEN_FLOAT;
2000                           gstring = g_string_append_c (gstring, ch);
2001                         }
2002                       break;
2003                       
2004                     case '0':
2005                     case '1':
2006                     case '2':
2007                     case '3':
2008                     case '4':
2009                     case '5':
2010                     case '6':
2011                     case '7':
2012                     case '8':
2013                     case '9':
2014                       gstring = g_string_append_c (gstring, ch);
2015                       break;
2016                       
2017                     case '-':
2018                     case '+':
2019                       if (token != G_TOKEN_FLOAT)
2020                         {
2021                           token = G_TOKEN_ERROR;
2022                           value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2023                           in_number = FALSE;
2024                         }
2025                       else
2026                         gstring = g_string_append_c (gstring, ch);
2027                       break;
2028                       
2029                     case 'e':
2030                     case 'E':
2031                       if ((token != G_TOKEN_HEX && !config->scan_float) ||
2032                           (token != G_TOKEN_HEX &&
2033                            token != G_TOKEN_OCTAL &&
2034                            token != G_TOKEN_FLOAT &&
2035                            token != G_TOKEN_INT))
2036                         {
2037                           token = G_TOKEN_ERROR;
2038                           value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2039                           in_number = FALSE;
2040                         }
2041                       else
2042                         {
2043                           if (token != G_TOKEN_HEX)
2044                             token = G_TOKEN_FLOAT;
2045                           gstring = g_string_append_c (gstring, ch);
2046                         }
2047                       break;
2048                       
2049                     default:
2050                       if (token != G_TOKEN_HEX)
2051                         {
2052                           token = G_TOKEN_ERROR;
2053                           value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2054                           in_number = FALSE;
2055                         }
2056                       else
2057                         gstring = g_string_append_c (gstring, ch);
2058                       break;
2059                     }
2060                 }
2061               else
2062                 in_number = FALSE;
2063             }
2064           while (in_number);
2065           
2066           endptr = NULL;
2067           if (token == G_TOKEN_FLOAT)
2068             value.v_float = g_strtod (gstring->str, &endptr);
2069           else
2070             {
2071               guint64 ui64 = 0;
2072               switch (token)
2073                 {
2074                 case G_TOKEN_BINARY:
2075                   ui64 = g_ascii_strtoull (gstring->str, &endptr, 2);
2076                   break;
2077                 case G_TOKEN_OCTAL:
2078                   ui64 = g_ascii_strtoull (gstring->str, &endptr, 8);
2079                   break;
2080                 case G_TOKEN_INT:
2081                   ui64 = g_ascii_strtoull (gstring->str, &endptr, 10);
2082                   break;
2083                 case G_TOKEN_HEX:
2084                   ui64 = g_ascii_strtoull (gstring->str, &endptr, 16);
2085                   break;
2086                 default: ;
2087                 }
2088               if (scanner->config->store_int64)
2089                 value.v_int64 = ui64;
2090               else
2091                 value.v_int = ui64;
2092             }
2093           if (endptr && *endptr)
2094             {
2095               token = G_TOKEN_ERROR;
2096               if (*endptr == 'e' || *endptr == 'E')
2097                 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2098               else
2099                 value.v_error = G_ERR_DIGIT_RADIX;
2100             }
2101           g_string_free (gstring, TRUE);
2102           gstring = NULL;
2103           ch = 0;
2104         } /* number_parsing:... */
2105         break;
2106         
2107         default:
2108         default_case:
2109         {
2110           if (config->cpair_comment_single &&
2111               ch == config->cpair_comment_single[0])
2112             {
2113               token = G_TOKEN_COMMENT_SINGLE;
2114               in_comment_single = TRUE;
2115               gstring = g_string_new (NULL);
2116               ch = g_scanner_get_char (scanner, line_p, position_p);
2117               while (ch != 0)
2118                 {
2119                   if (ch == config->cpair_comment_single[1])
2120                     {
2121                       in_comment_single = FALSE;
2122                       ch = 0;
2123                       break;
2124                     }
2125                   
2126                   gstring = g_string_append_c (gstring, ch);
2127                   ch = g_scanner_get_char (scanner, line_p, position_p);
2128                 }
2129               /* ignore a missing newline at EOF for single line comments */
2130               if (in_comment_single &&
2131                   config->cpair_comment_single[1] == '\n')
2132                 in_comment_single = FALSE;
2133             }
2134           else if (config->scan_identifier && ch &&
2135                    strchr (config->cset_identifier_first, ch))
2136             {
2137             identifier_precedence:
2138               
2139               if (config->cset_identifier_nth && ch &&
2140                   strchr (config->cset_identifier_nth,
2141                           g_scanner_peek_next_char (scanner)))
2142                 {
2143                   token = G_TOKEN_IDENTIFIER;
2144                   gstring = g_string_new (NULL);
2145                   gstring = g_string_append_c (gstring, ch);
2146                   do
2147                     {
2148                       ch = g_scanner_get_char (scanner, line_p, position_p);
2149                       gstring = g_string_append_c (gstring, ch);
2150                       ch = g_scanner_peek_next_char (scanner);
2151                     }
2152                   while (ch && strchr (config->cset_identifier_nth, ch));
2153                   ch = 0;
2154                 }
2155               else if (config->scan_identifier_1char)
2156                 {
2157                   token = G_TOKEN_IDENTIFIER;
2158                   value.v_identifier = g_new0 (gchar, 2);
2159                   value.v_identifier[0] = ch;
2160                   ch = 0;
2161                 }
2162             }
2163           if (ch)
2164             {
2165               if (config->char_2_token)
2166                 token = ch;
2167               else
2168                 {
2169                   token = G_TOKEN_CHAR;
2170                   value.v_char = ch;
2171                 }
2172               ch = 0;
2173             }
2174         } /* default_case:... */
2175         break;
2176         }
2177       g_assert (ch == 0 && token != G_TOKEN_NONE); /* paranoid */
2178     }
2179   while (ch != 0);
2180   
2181   if (in_comment_multi || in_comment_single ||
2182       in_string_sq || in_string_dq)
2183     {
2184       token = G_TOKEN_ERROR;
2185       if (gstring)
2186         {
2187           g_string_free (gstring, TRUE);
2188           gstring = NULL;
2189         }
2190       (*position_p)++;
2191       if (in_comment_multi || in_comment_single)
2192         value.v_error = G_ERR_UNEXP_EOF_IN_COMMENT;
2193       else /* (in_string_sq || in_string_dq) */
2194         value.v_error = G_ERR_UNEXP_EOF_IN_STRING;
2195     }
2196   
2197   if (gstring)
2198     {
2199       value.v_string = g_string_free (gstring, FALSE);
2200       gstring = NULL;
2201     }
2202   
2203   if (token == G_TOKEN_IDENTIFIER)
2204     {
2205       if (config->scan_symbols)
2206         {
2207           GScannerKey *key;
2208           guint scope_id;
2209           
2210           scope_id = scanner->scope_id;
2211           key = g_scanner_lookup_internal (scanner, scope_id, value.v_identifier);
2212           if (!key && scope_id && scanner->config->scope_0_fallback)
2213             key = g_scanner_lookup_internal (scanner, 0, value.v_identifier);
2214           
2215           if (key)
2216             {
2217               g_free (value.v_identifier);
2218               token = G_TOKEN_SYMBOL;
2219               value.v_symbol = key->value;
2220             }
2221         }
2222       
2223       if (token == G_TOKEN_IDENTIFIER &&
2224           config->scan_identifier_NULL &&
2225           strlen (value.v_identifier) == 4)
2226         {
2227           gchar *null_upper = "NULL";
2228           gchar *null_lower = "null";
2229           
2230           if (scanner->config->case_sensitive)
2231             {
2232               if (value.v_identifier[0] == null_upper[0] &&
2233                   value.v_identifier[1] == null_upper[1] &&
2234                   value.v_identifier[2] == null_upper[2] &&
2235                   value.v_identifier[3] == null_upper[3])
2236                 token = G_TOKEN_IDENTIFIER_NULL;
2237             }
2238           else
2239             {
2240               if ((value.v_identifier[0] == null_upper[0] ||
2241                    value.v_identifier[0] == null_lower[0]) &&
2242                   (value.v_identifier[1] == null_upper[1] ||
2243                    value.v_identifier[1] == null_lower[1]) &&
2244                   (value.v_identifier[2] == null_upper[2] ||
2245                    value.v_identifier[2] == null_lower[2]) &&
2246                   (value.v_identifier[3] == null_upper[3] ||
2247                    value.v_identifier[3] == null_lower[3]))
2248                 token = G_TOKEN_IDENTIFIER_NULL;
2249             }
2250         }
2251     }
2252   
2253   *token_p = token;
2254   *value_p = value;
2255 }