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