ghostutils: Fix a crash and add some tests
[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        "glib.h"
46 #include        "gprintfint.h"
47 #include        "galias.h"
48
49 #ifdef G_OS_WIN32
50 #include        <io.h>          /* For _read() */
51 #endif
52
53 /* --- defines --- */
54 #define to_lower(c)                             ( \
55         (guchar) (                                                      \
56           ( (((guchar)(c))>='A' && ((guchar)(c))<='Z') * ('a'-'A') ) |  \
57           ( (((guchar)(c))>=192 && ((guchar)(c))<=214) * (224-192) ) |  \
58           ( (((guchar)(c))>=216 && ((guchar)(c))<=222) * (248-216) ) |  \
59           ((guchar)(c))                                                 \
60         )                                                               \
61 )
62 #define READ_BUFFER_SIZE        (4000)
63
64
65 /* --- typedefs --- */
66 typedef struct  _GScannerKey    GScannerKey;
67
68 struct  _GScannerKey
69 {
70   guint          scope_id;
71   gchar         *symbol;
72   gpointer       value;
73 };
74
75
76 /* --- variables --- */
77 static const GScannerConfig g_scanner_config_template =
78 {
79   (
80    " \t\r\n"
81    )                    /* cset_skip_characters */,
82   (
83    G_CSET_a_2_z
84    "_"
85    G_CSET_A_2_Z
86    )                    /* cset_identifier_first */,
87   (
88    G_CSET_a_2_z
89    "_"
90    G_CSET_A_2_Z
91    G_CSET_DIGITS
92    G_CSET_LATINS
93    G_CSET_LATINC
94    )                    /* cset_identifier_nth */,
95   ( "#\n" )             /* cpair_comment_single */,
96   
97   FALSE                 /* case_sensitive */,
98   
99   TRUE                  /* skip_comment_multi */,
100   TRUE                  /* skip_comment_single */,
101   TRUE                  /* scan_comment_multi */,
102   TRUE                  /* scan_identifier */,
103   FALSE                 /* scan_identifier_1char */,
104   FALSE                 /* scan_identifier_NULL */,
105   TRUE                  /* scan_symbols */,
106   FALSE                 /* scan_binary */,
107   TRUE                  /* scan_octal */,
108   TRUE                  /* scan_float */,
109   TRUE                  /* scan_hex */,
110   FALSE                 /* scan_hex_dollar */,
111   TRUE                  /* scan_string_sq */,
112   TRUE                  /* scan_string_dq */,
113   TRUE                  /* numbers_2_int */,
114   FALSE                 /* int_2_float */,
115   FALSE                 /* identifier_2_string */,
116   TRUE                  /* char_2_token */,
117   FALSE                 /* symbol_2_token */,
118   FALSE                 /* scope_0_fallback */,
119   FALSE                 /* store_int64 */,
120 };
121
122
123 /* --- prototypes --- */
124 static inline
125 GScannerKey*    g_scanner_lookup_internal (GScanner     *scanner,
126                                            guint         scope_id,
127                                            const gchar  *symbol);
128 static gboolean g_scanner_key_equal       (gconstpointer v1,
129                                            gconstpointer v2);
130 static guint    g_scanner_key_hash        (gconstpointer v);
131 static void     g_scanner_get_token_ll    (GScanner     *scanner,
132                                            GTokenType   *token_p,
133                                            GTokenValue  *value_p,
134                                            guint        *line_p,
135                                            guint        *position_p);
136 static void     g_scanner_get_token_i     (GScanner     *scanner,
137                                            GTokenType   *token_p,
138                                            GTokenValue  *value_p,
139                                            guint        *line_p,
140                                            guint        *position_p);
141
142 static guchar   g_scanner_peek_next_char  (GScanner     *scanner);
143 static guchar   g_scanner_get_char        (GScanner     *scanner,
144                                            guint        *line_p,
145                                            guint        *position_p);
146 static void     g_scanner_msg_handler     (GScanner     *scanner,
147                                            gchar        *message,
148                                            gboolean      is_error);
149
150
151 /* --- functions --- */
152 static inline gint
153 g_scanner_char_2_num (guchar    c,
154                       guchar    base)
155 {
156   if (c >= '0' && c <= '9')
157     c -= '0';
158   else if (c >= 'A' && c <= 'Z')
159     c -= 'A' - 10;
160   else if (c >= 'a' && c <= 'z')
161     c -= 'a' - 10;
162   else
163     return -1;
164   
165   if (c < base)
166     return c;
167   
168   return -1;
169 }
170
171 GScanner*
172 g_scanner_new (const GScannerConfig *config_templ)
173 {
174   GScanner *scanner;
175   
176   if (!config_templ)
177     config_templ = &g_scanner_config_template;
178   
179   scanner = g_new0 (GScanner, 1);
180   
181   scanner->user_data = NULL;
182   scanner->max_parse_errors = 1;
183   scanner->parse_errors = 0;
184   scanner->input_name = NULL;
185   g_datalist_init (&scanner->qdata);
186   
187   scanner->config = g_new0 (GScannerConfig, 1);
188   
189   scanner->config->case_sensitive        = config_templ->case_sensitive;
190   scanner->config->cset_skip_characters  = config_templ->cset_skip_characters;
191   if (!scanner->config->cset_skip_characters)
192     scanner->config->cset_skip_characters = "";
193   scanner->config->cset_identifier_first = config_templ->cset_identifier_first;
194   scanner->config->cset_identifier_nth   = config_templ->cset_identifier_nth;
195   scanner->config->cpair_comment_single  = config_templ->cpair_comment_single;
196   scanner->config->skip_comment_multi    = config_templ->skip_comment_multi;
197   scanner->config->skip_comment_single   = config_templ->skip_comment_single;
198   scanner->config->scan_comment_multi    = config_templ->scan_comment_multi;
199   scanner->config->scan_identifier       = config_templ->scan_identifier;
200   scanner->config->scan_identifier_1char = config_templ->scan_identifier_1char;
201   scanner->config->scan_identifier_NULL  = config_templ->scan_identifier_NULL;
202   scanner->config->scan_symbols          = config_templ->scan_symbols;
203   scanner->config->scan_binary           = config_templ->scan_binary;
204   scanner->config->scan_octal            = config_templ->scan_octal;
205   scanner->config->scan_float            = config_templ->scan_float;
206   scanner->config->scan_hex              = config_templ->scan_hex;
207   scanner->config->scan_hex_dollar       = config_templ->scan_hex_dollar;
208   scanner->config->scan_string_sq        = config_templ->scan_string_sq;
209   scanner->config->scan_string_dq        = config_templ->scan_string_dq;
210   scanner->config->numbers_2_int         = config_templ->numbers_2_int;
211   scanner->config->int_2_float           = config_templ->int_2_float;
212   scanner->config->identifier_2_string   = config_templ->identifier_2_string;
213   scanner->config->char_2_token          = config_templ->char_2_token;
214   scanner->config->symbol_2_token        = config_templ->symbol_2_token;
215   scanner->config->scope_0_fallback      = config_templ->scope_0_fallback;
216   scanner->config->store_int64           = config_templ->store_int64;
217   
218   scanner->token = G_TOKEN_NONE;
219   scanner->value.v_int64 = 0;
220   scanner->line = 1;
221   scanner->position = 0;
222   
223   scanner->next_token = G_TOKEN_NONE;
224   scanner->next_value.v_int64 = 0;
225   scanner->next_line = 1;
226   scanner->next_position = 0;
227   
228   scanner->symbol_table = g_hash_table_new (g_scanner_key_hash, g_scanner_key_equal);
229   scanner->input_fd = -1;
230   scanner->text = NULL;
231   scanner->text_end = NULL;
232   scanner->buffer = NULL;
233   scanner->scope_id = 0;
234   
235   scanner->msg_handler = g_scanner_msg_handler;
236   
237   return scanner;
238 }
239
240 static inline void
241 g_scanner_free_value (GTokenType     *token_p,
242                       GTokenValue     *value_p)
243 {
244   switch (*token_p)
245     {
246     case G_TOKEN_STRING:
247     case G_TOKEN_IDENTIFIER:
248     case G_TOKEN_IDENTIFIER_NULL:
249     case G_TOKEN_COMMENT_SINGLE:
250     case G_TOKEN_COMMENT_MULTI:
251       g_free (value_p->v_string);
252       break;
253       
254     default:
255       break;
256     }
257   
258   *token_p = G_TOKEN_NONE;
259 }
260
261 static void
262 g_scanner_destroy_symbol_table_entry (gpointer _key,
263                                       gpointer _value,
264                                       gpointer _data)
265 {
266   GScannerKey *key = _key;
267   
268   g_free (key->symbol);
269   g_free (key);
270 }
271
272 void
273 g_scanner_destroy (GScanner     *scanner)
274 {
275   g_return_if_fail (scanner != NULL);
276   
277   g_datalist_clear (&scanner->qdata);
278   g_hash_table_foreach (scanner->symbol_table, 
279                         g_scanner_destroy_symbol_table_entry, NULL);
280   g_hash_table_destroy (scanner->symbol_table);
281   g_scanner_free_value (&scanner->token, &scanner->value);
282   g_scanner_free_value (&scanner->next_token, &scanner->next_value);
283   g_free (scanner->config);
284   g_free (scanner->buffer);
285   g_free (scanner);
286 }
287
288 static void
289 g_scanner_msg_handler (GScanner         *scanner,
290                        gchar            *message,
291                        gboolean         is_error)
292 {
293   g_return_if_fail (scanner != NULL);
294   
295   _g_fprintf (stderr, "%s:%d: ",
296               scanner->input_name ? scanner->input_name : "<memory>",
297               scanner->line);
298   if (is_error)
299     _g_fprintf (stderr, "error: ");
300   _g_fprintf (stderr, "%s\n", message);
301 }
302
303 void
304 g_scanner_error (GScanner       *scanner,
305                  const gchar    *format,
306                  ...)
307 {
308   g_return_if_fail (scanner != NULL);
309   g_return_if_fail (format != NULL);
310   
311   scanner->parse_errors++;
312   
313   if (scanner->msg_handler)
314     {
315       va_list args;
316       gchar *string;
317       
318       va_start (args, format);
319       string = g_strdup_vprintf (format, args);
320       va_end (args);
321       
322       scanner->msg_handler (scanner, string, TRUE);
323       
324       g_free (string);
325     }
326 }
327
328 void
329 g_scanner_warn (GScanner       *scanner,
330                 const gchar    *format,
331                 ...)
332 {
333   g_return_if_fail (scanner != NULL);
334   g_return_if_fail (format != NULL);
335   
336   if (scanner->msg_handler)
337     {
338       va_list args;
339       gchar *string;
340       
341       va_start (args, format);
342       string = g_strdup_vprintf (format, args);
343       va_end (args);
344       
345       scanner->msg_handler (scanner, string, FALSE);
346       
347       g_free (string);
348     }
349 }
350
351 static gboolean
352 g_scanner_key_equal (gconstpointer v1,
353                      gconstpointer v2)
354 {
355   const GScannerKey *key1 = v1;
356   const GScannerKey *key2 = v2;
357   
358   return (key1->scope_id == key2->scope_id) && (strcmp (key1->symbol, key2->symbol) == 0);
359 }
360
361 static guint
362 g_scanner_key_hash (gconstpointer v)
363 {
364   const GScannerKey *key = v;
365   gchar *c;
366   guint h;
367   
368   h = key->scope_id;
369   for (c = key->symbol; *c; c++)
370     h = (h << 5) - h + *c;
371   
372   return h;
373 }
374
375 static inline GScannerKey*
376 g_scanner_lookup_internal (GScanner     *scanner,
377                            guint         scope_id,
378                            const gchar  *symbol)
379 {
380   GScannerKey   *key_p;
381   GScannerKey key;
382   
383   key.scope_id = scope_id;
384   
385   if (!scanner->config->case_sensitive)
386     {
387       gchar *d;
388       const gchar *c;
389       
390       key.symbol = g_new (gchar, strlen (symbol) + 1);
391       for (d = key.symbol, c = symbol; *c; c++, d++)
392         *d = to_lower (*c);
393       *d = 0;
394       key_p = g_hash_table_lookup (scanner->symbol_table, &key);
395       g_free (key.symbol);
396     }
397   else
398     {
399       key.symbol = (gchar*) symbol;
400       key_p = g_hash_table_lookup (scanner->symbol_table, &key);
401     }
402   
403   return key_p;
404 }
405
406 void
407 g_scanner_scope_add_symbol (GScanner    *scanner,
408                             guint        scope_id,
409                             const gchar *symbol,
410                             gpointer     value)
411 {
412   GScannerKey   *key;
413   
414   g_return_if_fail (scanner != NULL);
415   g_return_if_fail (symbol != NULL);
416   
417   key = g_scanner_lookup_internal (scanner, scope_id, symbol);
418   
419   if (!key)
420     {
421       key = g_new (GScannerKey, 1);
422       key->scope_id = scope_id;
423       key->symbol = g_strdup (symbol);
424       key->value = value;
425       if (!scanner->config->case_sensitive)
426         {
427           gchar *c;
428           
429           c = key->symbol;
430           while (*c != 0)
431             {
432               *c = to_lower (*c);
433               c++;
434             }
435         }
436       g_hash_table_insert (scanner->symbol_table, key, key);
437     }
438   else
439     key->value = value;
440 }
441
442 void
443 g_scanner_scope_remove_symbol (GScanner    *scanner,
444                                guint        scope_id,
445                                const gchar *symbol)
446 {
447   GScannerKey   *key;
448   
449   g_return_if_fail (scanner != NULL);
450   g_return_if_fail (symbol != NULL);
451   
452   key = g_scanner_lookup_internal (scanner, scope_id, symbol);
453   
454   if (key)
455     {
456       g_hash_table_remove (scanner->symbol_table, key);
457       g_free (key->symbol);
458       g_free (key);
459     }
460 }
461
462 gpointer
463 g_scanner_lookup_symbol (GScanner       *scanner,
464                          const gchar    *symbol)
465 {
466   GScannerKey   *key;
467   guint scope_id;
468   
469   g_return_val_if_fail (scanner != NULL, NULL);
470   
471   if (!symbol)
472     return NULL;
473   
474   scope_id = scanner->scope_id;
475   key = g_scanner_lookup_internal (scanner, scope_id, symbol);
476   if (!key && scope_id && scanner->config->scope_0_fallback)
477     key = g_scanner_lookup_internal (scanner, 0, symbol);
478   
479   if (key)
480     return key->value;
481   else
482     return NULL;
483 }
484
485 gpointer
486 g_scanner_scope_lookup_symbol (GScanner       *scanner,
487                                guint           scope_id,
488                                const gchar    *symbol)
489 {
490   GScannerKey   *key;
491   
492   g_return_val_if_fail (scanner != NULL, NULL);
493   
494   if (!symbol)
495     return NULL;
496   
497   key = g_scanner_lookup_internal (scanner, scope_id, symbol);
498   
499   if (key)
500     return key->value;
501   else
502     return NULL;
503 }
504
505 guint
506 g_scanner_set_scope (GScanner       *scanner,
507                      guint           scope_id)
508 {
509   guint old_scope_id;
510   
511   g_return_val_if_fail (scanner != NULL, 0);
512   
513   old_scope_id = scanner->scope_id;
514   scanner->scope_id = scope_id;
515   
516   return old_scope_id;
517 }
518
519 static void
520 g_scanner_foreach_internal (gpointer  _key,
521                             gpointer  _value,
522                             gpointer  _user_data)
523 {
524   GScannerKey *key;
525   gpointer *d;
526   GHFunc func;
527   gpointer user_data;
528   guint *scope_id;
529   
530   d = _user_data;
531   func = (GHFunc) d[0];
532   user_data = d[1];
533   scope_id = d[2];
534   key = _value;
535   
536   if (key->scope_id == *scope_id)
537     func (key->symbol, key->value, user_data);
538 }
539
540 void
541 g_scanner_scope_foreach_symbol (GScanner       *scanner,
542                                 guint           scope_id,
543                                 GHFunc          func,
544                                 gpointer        user_data)
545 {
546   gpointer d[3];
547   
548   g_return_if_fail (scanner != NULL);
549   
550   d[0] = (gpointer) func;
551   d[1] = user_data;
552   d[2] = &scope_id;
553   
554   g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
555 }
556
557 GTokenType
558 g_scanner_peek_next_token (GScanner     *scanner)
559 {
560   g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
561   
562   if (scanner->next_token == G_TOKEN_NONE)
563     {
564       scanner->next_line = scanner->line;
565       scanner->next_position = scanner->position;
566       g_scanner_get_token_i (scanner,
567                              &scanner->next_token,
568                              &scanner->next_value,
569                              &scanner->next_line,
570                              &scanner->next_position);
571     }
572   
573   return scanner->next_token;
574 }
575
576 GTokenType
577 g_scanner_get_next_token (GScanner      *scanner)
578 {
579   g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
580   
581   if (scanner->next_token != G_TOKEN_NONE)
582     {
583       g_scanner_free_value (&scanner->token, &scanner->value);
584       
585       scanner->token = scanner->next_token;
586       scanner->value = scanner->next_value;
587       scanner->line = scanner->next_line;
588       scanner->position = scanner->next_position;
589       scanner->next_token = G_TOKEN_NONE;
590     }
591   else
592     g_scanner_get_token_i (scanner,
593                            &scanner->token,
594                            &scanner->value,
595                            &scanner->line,
596                            &scanner->position);
597   
598   return scanner->token;
599 }
600
601 GTokenType
602 g_scanner_cur_token (GScanner *scanner)
603 {
604   g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
605   
606   return scanner->token;
607 }
608
609 GTokenValue
610 g_scanner_cur_value (GScanner *scanner)
611 {
612   GTokenValue v;
613   
614   v.v_int64 = 0;
615   
616   g_return_val_if_fail (scanner != NULL, v);
617
618   /* MSC isn't capable of handling return scanner->value; ? */
619
620   v = scanner->value;
621
622   return v;
623 }
624
625 guint
626 g_scanner_cur_line (GScanner *scanner)
627 {
628   g_return_val_if_fail (scanner != NULL, 0);
629   
630   return scanner->line;
631 }
632
633 guint
634 g_scanner_cur_position (GScanner *scanner)
635 {
636   g_return_val_if_fail (scanner != NULL, 0);
637   
638   return scanner->position;
639 }
640
641 gboolean
642 g_scanner_eof (GScanner *scanner)
643 {
644   g_return_val_if_fail (scanner != NULL, TRUE);
645   
646   return scanner->token == G_TOKEN_EOF || scanner->token == G_TOKEN_ERROR;
647 }
648
649 void
650 g_scanner_input_file (GScanner *scanner,
651                       gint      input_fd)
652 {
653   g_return_if_fail (scanner != NULL);
654   g_return_if_fail (input_fd >= 0);
655
656   if (scanner->input_fd >= 0)
657     g_scanner_sync_file_offset (scanner);
658
659   scanner->token = G_TOKEN_NONE;
660   scanner->value.v_int64 = 0;
661   scanner->line = 1;
662   scanner->position = 0;
663   scanner->next_token = G_TOKEN_NONE;
664
665   scanner->input_fd = input_fd;
666   scanner->text = NULL;
667   scanner->text_end = NULL;
668
669   if (!scanner->buffer)
670     scanner->buffer = g_new (gchar, READ_BUFFER_SIZE + 1);
671 }
672
673 void
674 g_scanner_input_text (GScanner    *scanner,
675                       const gchar *text,
676                       guint        text_len)
677 {
678   g_return_if_fail (scanner != NULL);
679   if (text_len)
680     g_return_if_fail (text != NULL);
681   else
682     text = NULL;
683
684   if (scanner->input_fd >= 0)
685     g_scanner_sync_file_offset (scanner);
686
687   scanner->token = G_TOKEN_NONE;
688   scanner->value.v_int64 = 0;
689   scanner->line = 1;
690   scanner->position = 0;
691   scanner->next_token = G_TOKEN_NONE;
692
693   scanner->input_fd = -1;
694   scanner->text = text;
695   scanner->text_end = text + text_len;
696
697   if (scanner->buffer)
698     {
699       g_free (scanner->buffer);
700       scanner->buffer = NULL;
701     }
702 }
703
704 static guchar
705 g_scanner_peek_next_char (GScanner *scanner)
706 {
707   if (scanner->text < scanner->text_end)
708     {
709       return *scanner->text;
710     }
711   else if (scanner->input_fd >= 0)
712     {
713       gint count;
714       gchar *buffer;
715
716       buffer = scanner->buffer;
717       do
718         {
719           count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
720         }
721       while (count == -1 && (errno == EINTR || errno == EAGAIN));
722
723       if (count < 1)
724         {
725           scanner->input_fd = -1;
726
727           return 0;
728         }
729       else
730         {
731           scanner->text = buffer;
732           scanner->text_end = buffer + count;
733
734           return *buffer;
735         }
736     }
737   else
738     return 0;
739 }
740
741 void
742 g_scanner_sync_file_offset (GScanner *scanner)
743 {
744   g_return_if_fail (scanner != NULL);
745
746   /* for file input, rewind the filedescriptor to the current
747    * buffer position and blow the file read ahead buffer. useful
748    * for third party uses of our file descriptor, which hooks 
749    * onto the current scanning position.
750    */
751
752   if (scanner->input_fd >= 0 && scanner->text_end > scanner->text)
753     {
754       gint buffered;
755
756       buffered = scanner->text_end - scanner->text;
757       if (lseek (scanner->input_fd, - buffered, SEEK_CUR) >= 0)
758         {
759           /* we succeeded, blow our buffer's contents now */
760           scanner->text = NULL;
761           scanner->text_end = NULL;
762         }
763       else
764         errno = 0;
765     }
766 }
767
768 static guchar
769 g_scanner_get_char (GScanner    *scanner,
770                     guint       *line_p,
771                     guint       *position_p)
772 {
773   guchar fchar;
774
775   if (scanner->text < scanner->text_end)
776     fchar = *(scanner->text++);
777   else if (scanner->input_fd >= 0)
778     {
779       gint count;
780       gchar *buffer;
781
782       buffer = scanner->buffer;
783       do
784         {
785           count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
786         }
787       while (count == -1 && (errno == EINTR || errno == EAGAIN));
788
789       if (count < 1)
790         {
791           scanner->input_fd = -1;
792           fchar = 0;
793         }
794       else
795         {
796           scanner->text = buffer + 1;
797           scanner->text_end = buffer + count;
798           fchar = *buffer;
799           if (!fchar)
800             {
801               g_scanner_sync_file_offset (scanner);
802               scanner->text_end = scanner->text;
803               scanner->input_fd = -1;
804             }
805         }
806     }
807   else
808     fchar = 0;
809   
810   if (fchar == '\n')
811     {
812       (*position_p) = 0;
813       (*line_p)++;
814     }
815   else if (fchar)
816     {
817       (*position_p)++;
818     }
819   
820   return fchar;
821 }
822
823 void
824 g_scanner_unexp_token (GScanner         *scanner,
825                        GTokenType        expected_token,
826                        const gchar      *identifier_spec,
827                        const gchar      *symbol_spec,
828                        const gchar      *symbol_name,
829                        const gchar      *message,
830                        gint              is_error)
831 {
832   gchar *token_string;
833   guint token_string_len;
834   gchar *expected_string;
835   guint expected_string_len;
836   gchar *message_prefix;
837   gboolean print_unexp;
838   void (*msg_handler)   (GScanner*, const gchar*, ...);
839   
840   g_return_if_fail (scanner != NULL);
841   
842   if (is_error)
843     msg_handler = g_scanner_error;
844   else
845     msg_handler = g_scanner_warn;
846   
847   if (!identifier_spec)
848     identifier_spec = "identifier";
849   if (!symbol_spec)
850     symbol_spec = "symbol";
851   
852   token_string_len = 56;
853   token_string = g_new (gchar, token_string_len + 1);
854   expected_string_len = 64;
855   expected_string = g_new (gchar, expected_string_len + 1);
856   print_unexp = TRUE;
857   
858   switch (scanner->token)
859     {
860     case G_TOKEN_EOF:
861       _g_snprintf (token_string, token_string_len, "end of file");
862       break;
863       
864     default:
865       if (scanner->token >= 1 && scanner->token <= 255)
866         {
867           if ((scanner->token >= ' ' && scanner->token <= '~') ||
868               strchr (scanner->config->cset_identifier_first, scanner->token) ||
869               strchr (scanner->config->cset_identifier_nth, scanner->token))
870             _g_snprintf (token_string, token_string_len, "character `%c'", scanner->token);
871           else
872             _g_snprintf (token_string, token_string_len, "character `\\%o'", scanner->token);
873           break;
874         }
875       else if (!scanner->config->symbol_2_token)
876         {
877           _g_snprintf (token_string, token_string_len, "(unknown) token <%d>", scanner->token);
878           break;
879         }
880       /* fall through */
881     case G_TOKEN_SYMBOL:
882       if (expected_token == G_TOKEN_SYMBOL ||
883           (scanner->config->symbol_2_token &&
884            expected_token > G_TOKEN_LAST))
885         print_unexp = FALSE;
886       if (symbol_name)
887         _g_snprintf (token_string,
888                      token_string_len,
889                      "%s%s `%s'",
890                      print_unexp ? "" : "invalid ",
891                      symbol_spec,
892                      symbol_name);
893       else
894         _g_snprintf (token_string,
895                      token_string_len,
896                      "%s%s",
897                      print_unexp ? "" : "invalid ",
898                      symbol_spec);
899       break;
900       
901     case G_TOKEN_ERROR:
902       print_unexp = FALSE;
903       expected_token = G_TOKEN_NONE;
904       switch (scanner->value.v_error)
905         {
906         case G_ERR_UNEXP_EOF:
907           _g_snprintf (token_string, token_string_len, "scanner: unexpected end of file");
908           break;
909           
910         case G_ERR_UNEXP_EOF_IN_STRING:
911           _g_snprintf (token_string, token_string_len, "scanner: unterminated string constant");
912           break;
913           
914         case G_ERR_UNEXP_EOF_IN_COMMENT:
915           _g_snprintf (token_string, token_string_len, "scanner: unterminated comment");
916           break;
917           
918         case G_ERR_NON_DIGIT_IN_CONST:
919           _g_snprintf (token_string, token_string_len, "scanner: non digit in constant");
920           break;
921           
922         case G_ERR_FLOAT_RADIX:
923           _g_snprintf (token_string, token_string_len, "scanner: invalid radix for floating constant");
924           break;
925           
926         case G_ERR_FLOAT_MALFORMED:
927           _g_snprintf (token_string, token_string_len, "scanner: malformed floating constant");
928           break;
929           
930         case G_ERR_DIGIT_RADIX:
931           _g_snprintf (token_string, token_string_len, "scanner: digit is beyond radix");
932           break;
933           
934         case G_ERR_UNKNOWN:
935         default:
936           _g_snprintf (token_string, token_string_len, "scanner: unknown error");
937           break;
938         }
939       break;
940       
941     case G_TOKEN_CHAR:
942       _g_snprintf (token_string, token_string_len, "character `%c'", scanner->value.v_char);
943       break;
944       
945     case G_TOKEN_IDENTIFIER:
946     case G_TOKEN_IDENTIFIER_NULL:
947       if (expected_token == G_TOKEN_IDENTIFIER ||
948           expected_token == G_TOKEN_IDENTIFIER_NULL)
949         print_unexp = FALSE;
950       _g_snprintf (token_string,
951                   token_string_len,
952                   "%s%s `%s'",
953                   print_unexp ? "" : "invalid ",
954                   identifier_spec,
955                   scanner->token == G_TOKEN_IDENTIFIER ? scanner->value.v_string : "null");
956       break;
957       
958     case G_TOKEN_BINARY:
959     case G_TOKEN_OCTAL:
960     case G_TOKEN_INT:
961     case G_TOKEN_HEX:
962       if (scanner->config->store_int64)
963         _g_snprintf (token_string, token_string_len, "number `%" G_GUINT64_FORMAT "'", scanner->value.v_int64);
964       else
965         _g_snprintf (token_string, token_string_len, "number `%lu'", scanner->value.v_int);
966       break;
967       
968     case G_TOKEN_FLOAT:
969       _g_snprintf (token_string, token_string_len, "number `%.3f'", scanner->value.v_float);
970       break;
971       
972     case G_TOKEN_STRING:
973       if (expected_token == G_TOKEN_STRING)
974         print_unexp = FALSE;
975       _g_snprintf (token_string,
976                    token_string_len,
977                    "%s%sstring constant \"%s\"",
978                    print_unexp ? "" : "invalid ",
979                    scanner->value.v_string[0] == 0 ? "empty " : "",
980                    scanner->value.v_string);
981       token_string[token_string_len - 2] = '"';
982       token_string[token_string_len - 1] = 0;
983       break;
984       
985     case G_TOKEN_COMMENT_SINGLE:
986     case G_TOKEN_COMMENT_MULTI:
987       _g_snprintf (token_string, token_string_len, "comment");
988       break;
989       
990     case G_TOKEN_NONE:
991       /* somehow the user's parsing code is screwed, there isn't much
992        * we can do about it.
993        * Note, a common case to trigger this is
994        * g_scanner_peek_next_token(); g_scanner_unexp_token();
995        * without an intermediate g_scanner_get_next_token().
996        */
997       g_assert_not_reached ();
998       break;
999     }
1000   
1001   
1002   switch (expected_token)
1003     {
1004       gboolean need_valid;
1005       gchar *tstring;
1006     case G_TOKEN_EOF:
1007       _g_snprintf (expected_string, expected_string_len, "end of file");
1008       break;
1009     default:
1010       if (expected_token >= 1 && expected_token <= 255)
1011         {
1012           if ((expected_token >= ' ' && expected_token <= '~') ||
1013               strchr (scanner->config->cset_identifier_first, expected_token) ||
1014               strchr (scanner->config->cset_identifier_nth, expected_token))
1015             _g_snprintf (expected_string, expected_string_len, "character `%c'", expected_token);
1016           else
1017             _g_snprintf (expected_string, expected_string_len, "character `\\%o'", expected_token);
1018           break;
1019         }
1020       else if (!scanner->config->symbol_2_token)
1021         {
1022           _g_snprintf (expected_string, expected_string_len, "(unknown) token <%d>", expected_token);
1023           break;
1024         }
1025       /* fall through */
1026     case G_TOKEN_SYMBOL:
1027       need_valid = (scanner->token == G_TOKEN_SYMBOL ||
1028                     (scanner->config->symbol_2_token &&
1029                      scanner->token > G_TOKEN_LAST));
1030       _g_snprintf (expected_string,
1031                    expected_string_len,
1032                    "%s%s",
1033                    need_valid ? "valid " : "",
1034                    symbol_spec);
1035       /* FIXME: should we attempt to lookup the symbol_name for symbol_2_token? */
1036       break;
1037     case G_TOKEN_CHAR:
1038       _g_snprintf (expected_string, expected_string_len, "%scharacter",
1039                    scanner->token == G_TOKEN_CHAR ? "valid " : "");
1040       break;
1041     case G_TOKEN_BINARY:
1042       tstring = "binary";
1043       _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1044                    scanner->token == expected_token ? "valid " : "", tstring);
1045       break;
1046     case G_TOKEN_OCTAL:
1047       tstring = "octal";
1048       _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1049                    scanner->token == expected_token ? "valid " : "", tstring);
1050       break;
1051     case G_TOKEN_INT:
1052       tstring = "integer";
1053       _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1054                    scanner->token == expected_token ? "valid " : "", tstring);
1055       break;
1056     case G_TOKEN_HEX:
1057       tstring = "hexadecimal";
1058       _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1059                    scanner->token == expected_token ? "valid " : "", tstring);
1060       break;
1061     case G_TOKEN_FLOAT:
1062       tstring = "float";
1063       _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1064                    scanner->token == expected_token ? "valid " : "", tstring);
1065       break;
1066     case G_TOKEN_STRING:
1067       _g_snprintf (expected_string,
1068                    expected_string_len,
1069                    "%sstring constant",
1070                    scanner->token == G_TOKEN_STRING ? "valid " : "");
1071       break;
1072     case G_TOKEN_IDENTIFIER:
1073     case G_TOKEN_IDENTIFIER_NULL:
1074       need_valid = (scanner->token == G_TOKEN_IDENTIFIER_NULL ||
1075                     scanner->token == G_TOKEN_IDENTIFIER);
1076       _g_snprintf (expected_string,
1077                    expected_string_len,
1078                    "%s%s",
1079                    need_valid ? "valid " : "",
1080                    identifier_spec);
1081       break;
1082     case G_TOKEN_COMMENT_SINGLE:
1083       tstring = "single-line";
1084       _g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1085                    scanner->token == expected_token ? "valid " : "", tstring);
1086       break;
1087     case G_TOKEN_COMMENT_MULTI:
1088       tstring = "multi-line";
1089       _g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1090                    scanner->token == expected_token ? "valid " : "", tstring);
1091       break;
1092     case G_TOKEN_NONE:
1093     case G_TOKEN_ERROR:
1094       /* this is handled upon printout */
1095       break;
1096     }
1097   
1098   if (message && message[0] != 0)
1099     message_prefix = " - ";
1100   else
1101     {
1102       message_prefix = "";
1103       message = "";
1104     }
1105   if (expected_token == G_TOKEN_ERROR)
1106     {
1107       msg_handler (scanner,
1108                    "failure around %s%s%s",
1109                    token_string,
1110                    message_prefix,
1111                    message);
1112     }
1113   else if (expected_token == G_TOKEN_NONE)
1114     {
1115       if (print_unexp)
1116         msg_handler (scanner,
1117                      "unexpected %s%s%s",
1118                      token_string,
1119                      message_prefix,
1120                      message);
1121       else
1122         msg_handler (scanner,
1123                      "%s%s%s",
1124                      token_string,
1125                      message_prefix,
1126                      message);
1127     }
1128   else
1129     {
1130       if (print_unexp)
1131         msg_handler (scanner,
1132                      "unexpected %s, expected %s%s%s",
1133                      token_string,
1134                      expected_string,
1135                      message_prefix,
1136                      message);
1137       else
1138         msg_handler (scanner,
1139                      "%s, expected %s%s%s",
1140                      token_string,
1141                      expected_string,
1142                      message_prefix,
1143                      message);
1144     }
1145   
1146   g_free (token_string);
1147   g_free (expected_string);
1148 }
1149
1150 static void
1151 g_scanner_get_token_i (GScanner *scanner,
1152                        GTokenType       *token_p,
1153                        GTokenValue      *value_p,
1154                        guint            *line_p,
1155                        guint            *position_p)
1156 {
1157   do
1158     {
1159       g_scanner_free_value (token_p, value_p);
1160       g_scanner_get_token_ll (scanner, token_p, value_p, line_p, position_p);
1161     }
1162   while (((*token_p > 0 && *token_p < 256) &&
1163           strchr (scanner->config->cset_skip_characters, *token_p)) ||
1164          (*token_p == G_TOKEN_CHAR &&
1165           strchr (scanner->config->cset_skip_characters, value_p->v_char)) ||
1166          (*token_p == G_TOKEN_COMMENT_MULTI &&
1167           scanner->config->skip_comment_multi) ||
1168          (*token_p == G_TOKEN_COMMENT_SINGLE &&
1169           scanner->config->skip_comment_single));
1170   
1171   switch (*token_p)
1172     {
1173     case G_TOKEN_IDENTIFIER:
1174       if (scanner->config->identifier_2_string)
1175         *token_p = G_TOKEN_STRING;
1176       break;
1177       
1178     case G_TOKEN_SYMBOL:
1179       if (scanner->config->symbol_2_token)
1180         *token_p = (GTokenType) value_p->v_symbol;
1181       break;
1182       
1183     case G_TOKEN_BINARY:
1184     case G_TOKEN_OCTAL:
1185     case G_TOKEN_HEX:
1186       if (scanner->config->numbers_2_int)
1187         *token_p = G_TOKEN_INT;
1188       break;
1189       
1190     default:
1191       break;
1192     }
1193   
1194   if (*token_p == G_TOKEN_INT &&
1195       scanner->config->int_2_float)
1196     {
1197       *token_p = G_TOKEN_FLOAT;
1198       if (scanner->config->store_int64)
1199         {
1200 #ifdef _MSC_VER
1201           /* work around error C2520, see gvaluetransform.c */
1202           value_p->v_float = (__int64)value_p->v_int64;
1203 #else
1204           value_p->v_float = value_p->v_int64;
1205 #endif
1206         }
1207       else
1208         value_p->v_float = value_p->v_int;
1209     }
1210   
1211   errno = 0;
1212 }
1213
1214 static void
1215 g_scanner_get_token_ll  (GScanner       *scanner,
1216                          GTokenType     *token_p,
1217                          GTokenValue    *value_p,
1218                          guint          *line_p,
1219                          guint          *position_p)
1220 {
1221   GScannerConfig *config;
1222   GTokenType       token;
1223   gboolean         in_comment_multi;
1224   gboolean         in_comment_single;
1225   gboolean         in_string_sq;
1226   gboolean         in_string_dq;
1227   GString         *gstring;
1228   GTokenValue      value;
1229   guchar           ch;
1230   
1231   config = scanner->config;
1232   (*value_p).v_int64 = 0;
1233   
1234   if ((scanner->text >= scanner->text_end && scanner->input_fd < 0) ||
1235       scanner->token == G_TOKEN_EOF)
1236     {
1237       *token_p = G_TOKEN_EOF;
1238       return;
1239     }
1240   
1241   in_comment_multi = FALSE;
1242   in_comment_single = FALSE;
1243   in_string_sq = FALSE;
1244   in_string_dq = FALSE;
1245   gstring = NULL;
1246   
1247   do /* while (ch != 0) */
1248     {
1249       gboolean dotted_float = FALSE;
1250       
1251       ch = g_scanner_get_char (scanner, line_p, position_p);
1252       
1253       value.v_int64 = 0;
1254       token = G_TOKEN_NONE;
1255       
1256       /* this is *evil*, but needed ;(
1257        * we first check for identifier first character, because  it
1258        * might interfere with other key chars like slashes or numbers
1259        */
1260       if (config->scan_identifier &&
1261           ch && strchr (config->cset_identifier_first, ch))
1262         goto identifier_precedence;
1263       
1264       switch (ch)
1265         {
1266         case 0:
1267           token = G_TOKEN_EOF;
1268           (*position_p)++;
1269           /* ch = 0; */
1270           break;
1271           
1272         case '/':
1273           if (!config->scan_comment_multi ||
1274               g_scanner_peek_next_char (scanner) != '*')
1275             goto default_case;
1276           g_scanner_get_char (scanner, line_p, position_p);
1277           token = G_TOKEN_COMMENT_MULTI;
1278           in_comment_multi = TRUE;
1279           gstring = g_string_new (NULL);
1280           while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1281             {
1282               if (ch == '*' && g_scanner_peek_next_char (scanner) == '/')
1283                 {
1284                   g_scanner_get_char (scanner, line_p, position_p);
1285                   in_comment_multi = FALSE;
1286                   break;
1287                 }
1288               else
1289                 gstring = g_string_append_c (gstring, ch);
1290             }
1291           ch = 0;
1292           break;
1293           
1294         case '\'':
1295           if (!config->scan_string_sq)
1296             goto default_case;
1297           token = G_TOKEN_STRING;
1298           in_string_sq = TRUE;
1299           gstring = g_string_new (NULL);
1300           while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1301             {
1302               if (ch == '\'')
1303                 {
1304                   in_string_sq = FALSE;
1305                   break;
1306                 }
1307               else
1308                 gstring = g_string_append_c (gstring, ch);
1309             }
1310           ch = 0;
1311           break;
1312           
1313         case '"':
1314           if (!config->scan_string_dq)
1315             goto default_case;
1316           token = G_TOKEN_STRING;
1317           in_string_dq = TRUE;
1318           gstring = g_string_new (NULL);
1319           while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1320             {
1321               if (ch == '"')
1322                 {
1323                   in_string_dq = FALSE;
1324                   break;
1325                 }
1326               else
1327                 {
1328                   if (ch == '\\')
1329                     {
1330                       ch = g_scanner_get_char (scanner, line_p, position_p);
1331                       switch (ch)
1332                         {
1333                           guint i;
1334                           guint fchar;
1335                           
1336                         case 0:
1337                           break;
1338                           
1339                         case '\\':
1340                           gstring = g_string_append_c (gstring, '\\');
1341                           break;
1342                           
1343                         case 'n':
1344                           gstring = g_string_append_c (gstring, '\n');
1345                           break;
1346                           
1347                         case 't':
1348                           gstring = g_string_append_c (gstring, '\t');
1349                           break;
1350                           
1351                         case 'r':
1352                           gstring = g_string_append_c (gstring, '\r');
1353                           break;
1354                           
1355                         case 'b':
1356                           gstring = g_string_append_c (gstring, '\b');
1357                           break;
1358                           
1359                         case 'f':
1360                           gstring = g_string_append_c (gstring, '\f');
1361                           break;
1362                           
1363                         case '0':
1364                         case '1':
1365                         case '2':
1366                         case '3':
1367                         case '4':
1368                         case '5':
1369                         case '6':
1370                         case '7':
1371                           i = ch - '0';
1372                           fchar = g_scanner_peek_next_char (scanner);
1373                           if (fchar >= '0' && fchar <= '7')
1374                             {
1375                               ch = g_scanner_get_char (scanner, line_p, position_p);
1376                               i = i * 8 + ch - '0';
1377                               fchar = g_scanner_peek_next_char (scanner);
1378                               if (fchar >= '0' && fchar <= '7')
1379                                 {
1380                                   ch = g_scanner_get_char (scanner, line_p, position_p);
1381                                   i = i * 8 + ch - '0';
1382                                 }
1383                             }
1384                           gstring = g_string_append_c (gstring, i);
1385                           break;
1386                           
1387                         default:
1388                           gstring = g_string_append_c (gstring, ch);
1389                           break;
1390                         }
1391                     }
1392                   else
1393                     gstring = g_string_append_c (gstring, ch);
1394                 }
1395             }
1396           ch = 0;
1397           break;
1398           
1399         case '.':
1400           if (!config->scan_float)
1401             goto default_case;
1402           token = G_TOKEN_FLOAT;
1403           dotted_float = TRUE;
1404           ch = g_scanner_get_char (scanner, line_p, position_p);
1405           goto number_parsing;
1406           
1407         case '$':
1408           if (!config->scan_hex_dollar)
1409             goto default_case;
1410           token = G_TOKEN_HEX;
1411           ch = g_scanner_get_char (scanner, line_p, position_p);
1412           goto number_parsing;
1413           
1414         case '0':
1415           if (config->scan_octal)
1416             token = G_TOKEN_OCTAL;
1417           else
1418             token = G_TOKEN_INT;
1419           ch = g_scanner_peek_next_char (scanner);
1420           if (config->scan_hex && (ch == 'x' || ch == 'X'))
1421             {
1422               token = G_TOKEN_HEX;
1423               g_scanner_get_char (scanner, line_p, position_p);
1424               ch = g_scanner_get_char (scanner, line_p, position_p);
1425               if (ch == 0)
1426                 {
1427                   token = G_TOKEN_ERROR;
1428                   value.v_error = G_ERR_UNEXP_EOF;
1429                   (*position_p)++;
1430                   break;
1431                 }
1432               if (g_scanner_char_2_num (ch, 16) < 0)
1433                 {
1434                   token = G_TOKEN_ERROR;
1435                   value.v_error = G_ERR_DIGIT_RADIX;
1436                   ch = 0;
1437                   break;
1438                 }
1439             }
1440           else if (config->scan_binary && (ch == 'b' || ch == 'B'))
1441             {
1442               token = G_TOKEN_BINARY;
1443               g_scanner_get_char (scanner, line_p, position_p);
1444               ch = g_scanner_get_char (scanner, line_p, position_p);
1445               if (ch == 0)
1446                 {
1447                   token = G_TOKEN_ERROR;
1448                   value.v_error = G_ERR_UNEXP_EOF;
1449                   (*position_p)++;
1450                   break;
1451                 }
1452               if (g_scanner_char_2_num (ch, 10) < 0)
1453                 {
1454                   token = G_TOKEN_ERROR;
1455                   value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1456                   ch = 0;
1457                   break;
1458                 }
1459             }
1460           else
1461             ch = '0';
1462           /* fall through */
1463         case '1':
1464         case '2':
1465         case '3':
1466         case '4':
1467         case '5':
1468         case '6':
1469         case '7':
1470         case '8':
1471         case '9':
1472         number_parsing:
1473         {
1474           gboolean in_number = TRUE;
1475           gchar *endptr;
1476           
1477           if (token == G_TOKEN_NONE)
1478             token = G_TOKEN_INT;
1479           
1480           gstring = g_string_new (dotted_float ? "0." : "");
1481           gstring = g_string_append_c (gstring, ch);
1482           
1483           do /* while (in_number) */
1484             {
1485               gboolean is_E;
1486               
1487               is_E = token == G_TOKEN_FLOAT && (ch == 'e' || ch == 'E');
1488               
1489               ch = g_scanner_peek_next_char (scanner);
1490               
1491               if (g_scanner_char_2_num (ch, 36) >= 0 ||
1492                   (config->scan_float && ch == '.') ||
1493                   (is_E && (ch == '+' || ch == '-')))
1494                 {
1495                   ch = g_scanner_get_char (scanner, line_p, position_p);
1496                   
1497                   switch (ch)
1498                     {
1499                     case '.':
1500                       if (token != G_TOKEN_INT && token != G_TOKEN_OCTAL)
1501                         {
1502                           value.v_error = token == G_TOKEN_FLOAT ? G_ERR_FLOAT_MALFORMED : G_ERR_FLOAT_RADIX;
1503                           token = G_TOKEN_ERROR;
1504                           in_number = FALSE;
1505                         }
1506                       else
1507                         {
1508                           token = G_TOKEN_FLOAT;
1509                           gstring = g_string_append_c (gstring, ch);
1510                         }
1511                       break;
1512                       
1513                     case '0':
1514                     case '1':
1515                     case '2':
1516                     case '3':
1517                     case '4':
1518                     case '5':
1519                     case '6':
1520                     case '7':
1521                     case '8':
1522                     case '9':
1523                       gstring = g_string_append_c (gstring, ch);
1524                       break;
1525                       
1526                     case '-':
1527                     case '+':
1528                       if (token != G_TOKEN_FLOAT)
1529                         {
1530                           token = G_TOKEN_ERROR;
1531                           value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1532                           in_number = FALSE;
1533                         }
1534                       else
1535                         gstring = g_string_append_c (gstring, ch);
1536                       break;
1537                       
1538                     case 'e':
1539                     case 'E':
1540                       if ((token != G_TOKEN_HEX && !config->scan_float) ||
1541                           (token != G_TOKEN_HEX &&
1542                            token != G_TOKEN_OCTAL &&
1543                            token != G_TOKEN_FLOAT &&
1544                            token != G_TOKEN_INT))
1545                         {
1546                           token = G_TOKEN_ERROR;
1547                           value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1548                           in_number = FALSE;
1549                         }
1550                       else
1551                         {
1552                           if (token != G_TOKEN_HEX)
1553                             token = G_TOKEN_FLOAT;
1554                           gstring = g_string_append_c (gstring, ch);
1555                         }
1556                       break;
1557                       
1558                     default:
1559                       if (token != G_TOKEN_HEX)
1560                         {
1561                           token = G_TOKEN_ERROR;
1562                           value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1563                           in_number = FALSE;
1564                         }
1565                       else
1566                         gstring = g_string_append_c (gstring, ch);
1567                       break;
1568                     }
1569                 }
1570               else
1571                 in_number = FALSE;
1572             }
1573           while (in_number);
1574           
1575           endptr = NULL;
1576           if (token == G_TOKEN_FLOAT)
1577             value.v_float = g_strtod (gstring->str, &endptr);
1578           else
1579             {
1580               guint64 ui64 = 0;
1581               switch (token)
1582                 {
1583                 case G_TOKEN_BINARY:
1584                   ui64 = g_ascii_strtoull (gstring->str, &endptr, 2);
1585                   break;
1586                 case G_TOKEN_OCTAL:
1587                   ui64 = g_ascii_strtoull (gstring->str, &endptr, 8);
1588                   break;
1589                 case G_TOKEN_INT:
1590                   ui64 = g_ascii_strtoull (gstring->str, &endptr, 10);
1591                   break;
1592                 case G_TOKEN_HEX:
1593                   ui64 = g_ascii_strtoull (gstring->str, &endptr, 16);
1594                   break;
1595                 default: ;
1596                 }
1597               if (scanner->config->store_int64)
1598                 value.v_int64 = ui64;
1599               else
1600                 value.v_int = ui64;
1601             }
1602           if (endptr && *endptr)
1603             {
1604               token = G_TOKEN_ERROR;
1605               if (*endptr == 'e' || *endptr == 'E')
1606                 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1607               else
1608                 value.v_error = G_ERR_DIGIT_RADIX;
1609             }
1610           g_string_free (gstring, TRUE);
1611           gstring = NULL;
1612           ch = 0;
1613         } /* number_parsing:... */
1614         break;
1615         
1616         default:
1617         default_case:
1618         {
1619           if (config->cpair_comment_single &&
1620               ch == config->cpair_comment_single[0])
1621             {
1622               token = G_TOKEN_COMMENT_SINGLE;
1623               in_comment_single = TRUE;
1624               gstring = g_string_new (NULL);
1625               ch = g_scanner_get_char (scanner, line_p, position_p);
1626               while (ch != 0)
1627                 {
1628                   if (ch == config->cpair_comment_single[1])
1629                     {
1630                       in_comment_single = FALSE;
1631                       ch = 0;
1632                       break;
1633                     }
1634                   
1635                   gstring = g_string_append_c (gstring, ch);
1636                   ch = g_scanner_get_char (scanner, line_p, position_p);
1637                 }
1638               /* ignore a missing newline at EOF for single line comments */
1639               if (in_comment_single &&
1640                   config->cpair_comment_single[1] == '\n')
1641                 in_comment_single = FALSE;
1642             }
1643           else if (config->scan_identifier && ch &&
1644                    strchr (config->cset_identifier_first, ch))
1645             {
1646             identifier_precedence:
1647               
1648               if (config->cset_identifier_nth && ch &&
1649                   strchr (config->cset_identifier_nth,
1650                           g_scanner_peek_next_char (scanner)))
1651                 {
1652                   token = G_TOKEN_IDENTIFIER;
1653                   gstring = g_string_new (NULL);
1654                   gstring = g_string_append_c (gstring, ch);
1655                   do
1656                     {
1657                       ch = g_scanner_get_char (scanner, line_p, position_p);
1658                       gstring = g_string_append_c (gstring, ch);
1659                       ch = g_scanner_peek_next_char (scanner);
1660                     }
1661                   while (ch && strchr (config->cset_identifier_nth, ch));
1662                   ch = 0;
1663                 }
1664               else if (config->scan_identifier_1char)
1665                 {
1666                   token = G_TOKEN_IDENTIFIER;
1667                   value.v_identifier = g_new0 (gchar, 2);
1668                   value.v_identifier[0] = ch;
1669                   ch = 0;
1670                 }
1671             }
1672           if (ch)
1673             {
1674               if (config->char_2_token)
1675                 token = ch;
1676               else
1677                 {
1678                   token = G_TOKEN_CHAR;
1679                   value.v_char = ch;
1680                 }
1681               ch = 0;
1682             }
1683         } /* default_case:... */
1684         break;
1685         }
1686       g_assert (ch == 0 && token != G_TOKEN_NONE); /* paranoid */
1687     }
1688   while (ch != 0);
1689   
1690   if (in_comment_multi || in_comment_single ||
1691       in_string_sq || in_string_dq)
1692     {
1693       token = G_TOKEN_ERROR;
1694       if (gstring)
1695         {
1696           g_string_free (gstring, TRUE);
1697           gstring = NULL;
1698         }
1699       (*position_p)++;
1700       if (in_comment_multi || in_comment_single)
1701         value.v_error = G_ERR_UNEXP_EOF_IN_COMMENT;
1702       else /* (in_string_sq || in_string_dq) */
1703         value.v_error = G_ERR_UNEXP_EOF_IN_STRING;
1704     }
1705   
1706   if (gstring)
1707     {
1708       value.v_string = g_string_free (gstring, FALSE);
1709       gstring = NULL;
1710     }
1711   
1712   if (token == G_TOKEN_IDENTIFIER)
1713     {
1714       if (config->scan_symbols)
1715         {
1716           GScannerKey *key;
1717           guint scope_id;
1718           
1719           scope_id = scanner->scope_id;
1720           key = g_scanner_lookup_internal (scanner, scope_id, value.v_identifier);
1721           if (!key && scope_id && scanner->config->scope_0_fallback)
1722             key = g_scanner_lookup_internal (scanner, 0, value.v_identifier);
1723           
1724           if (key)
1725             {
1726               g_free (value.v_identifier);
1727               token = G_TOKEN_SYMBOL;
1728               value.v_symbol = key->value;
1729             }
1730         }
1731       
1732       if (token == G_TOKEN_IDENTIFIER &&
1733           config->scan_identifier_NULL &&
1734           strlen (value.v_identifier) == 4)
1735         {
1736           gchar *null_upper = "NULL";
1737           gchar *null_lower = "null";
1738           
1739           if (scanner->config->case_sensitive)
1740             {
1741               if (value.v_identifier[0] == null_upper[0] &&
1742                   value.v_identifier[1] == null_upper[1] &&
1743                   value.v_identifier[2] == null_upper[2] &&
1744                   value.v_identifier[3] == null_upper[3])
1745                 token = G_TOKEN_IDENTIFIER_NULL;
1746             }
1747           else
1748             {
1749               if ((value.v_identifier[0] == null_upper[0] ||
1750                    value.v_identifier[0] == null_lower[0]) &&
1751                   (value.v_identifier[1] == null_upper[1] ||
1752                    value.v_identifier[1] == null_lower[1]) &&
1753                   (value.v_identifier[2] == null_upper[2] ||
1754                    value.v_identifier[2] == null_lower[2]) &&
1755                   (value.v_identifier[3] == null_upper[3] ||
1756                    value.v_identifier[3] == null_lower[3]))
1757                 token = G_TOKEN_IDENTIFIER_NULL;
1758             }
1759         }
1760     }
1761   
1762   *token_p = token;
1763   *value_p = value;
1764 }
1765
1766 #define __G_SCANNER_C__
1767 #include "galiasdef.c"