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