1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GScanner: Flexible lexical scanner for general purpose.
5 * Copyright (C) 1997, 1998 Tim Janik
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library 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.
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 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library 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.
24 * Modified by the GLib Team and others 1997-1999. 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/.
34 #define __gscanner_c__
49 #include <sys/types.h> /* needed for sys/stat.h */
52 #include <io.h> /* For _read() */
56 #define to_lower(c) ( \
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) ) | \
64 #define READ_BUFFER_SIZE (4000)
67 /* --- typedefs --- */
68 typedef struct _GScannerKey GScannerKey
;
79 /* --- variables --- */
80 static GScannerConfig g_scanner_config_template
=
84 ) /* cset_skip_characters */,
89 ) /* cset_identifier_first */,
96 ) /* cset_identifier_nth */,
97 ( "#\n" ) /* cpair_comment_single */,
99 FALSE
/* case_sensitive */,
101 TRUE
/* skip_comment_multi */,
102 TRUE
/* skip_comment_single */,
103 TRUE
/* scan_comment_multi */,
104 TRUE
/* scan_identifier */,
105 FALSE
/* scan_identifier_1char */,
106 FALSE
/* scan_identifier_NULL */,
107 TRUE
/* scan_symbols */,
108 FALSE
/* scan_binary */,
109 TRUE
/* scan_octal */,
110 TRUE
/* scan_float */,
112 FALSE
/* scan_hex_dollar */,
113 TRUE
/* scan_string_sq */,
114 TRUE
/* scan_string_dq */,
115 TRUE
/* numbers_2_int */,
116 FALSE
/* int_2_float */,
117 FALSE
/* identifier_2_string */,
118 TRUE
/* char_2_token */,
119 FALSE
/* symbol_2_token */,
120 FALSE
/* scope_0_fallback */,
124 /* --- prototypes --- */
126 GScannerKey
* g_scanner_lookup_internal (GScanner
*scanner
,
128 const gchar
*symbol
);
129 static gint
g_scanner_key_equal (gconstpointer v1
,
131 static guint
g_scanner_key_hash (gconstpointer v
);
132 static void g_scanner_get_token_ll (GScanner
*scanner
,
134 GTokenValue
*value_p
,
137 static void g_scanner_get_token_i (GScanner
*scanner
,
139 GTokenValue
*value_p
,
143 static guchar
g_scanner_peek_next_char (GScanner
*scanner
);
144 static guchar
g_scanner_get_char (GScanner
*scanner
,
147 static void g_scanner_msg_handler (GScanner
*scanner
,
152 /* --- functions --- */
154 g_scanner_char_2_num (guchar c
,
157 if (c
>= '0' && c
<= '9')
159 else if (c
>= 'A' && c
<= 'Z')
161 else if (c
>= 'a' && c
<= 'z')
173 g_scanner_new (GScannerConfig
*config_templ
)
178 config_templ
= &g_scanner_config_template
;
180 scanner
= g_new0 (GScanner
, 1);
182 scanner
->user_data
= NULL
;
183 scanner
->max_parse_errors
= 0;
184 scanner
->parse_errors
= 0;
185 scanner
->input_name
= NULL
;
186 scanner
->derived_data
= NULL
;
188 scanner
->config
= g_new0 (GScannerConfig
, 1);
190 scanner
->config
->case_sensitive
= config_templ
->case_sensitive
;
191 scanner
->config
->cset_skip_characters
= config_templ
->cset_skip_characters
;
192 if (!scanner
->config
->cset_skip_characters
)
193 scanner
->config
->cset_skip_characters
= "";
194 scanner
->config
->cset_identifier_first
= config_templ
->cset_identifier_first
;
195 scanner
->config
->cset_identifier_nth
= config_templ
->cset_identifier_nth
;
196 scanner
->config
->cpair_comment_single
= config_templ
->cpair_comment_single
;
197 scanner
->config
->skip_comment_multi
= config_templ
->skip_comment_multi
;
198 scanner
->config
->skip_comment_single
= config_templ
->skip_comment_single
;
199 scanner
->config
->scan_comment_multi
= config_templ
->scan_comment_multi
;
200 scanner
->config
->scan_identifier
= config_templ
->scan_identifier
;
201 scanner
->config
->scan_identifier_1char
= config_templ
->scan_identifier_1char
;
202 scanner
->config
->scan_identifier_NULL
= config_templ
->scan_identifier_NULL
;
203 scanner
->config
->scan_symbols
= config_templ
->scan_symbols
;
204 scanner
->config
->scan_binary
= config_templ
->scan_binary
;
205 scanner
->config
->scan_octal
= config_templ
->scan_octal
;
206 scanner
->config
->scan_float
= config_templ
->scan_float
;
207 scanner
->config
->scan_hex
= config_templ
->scan_hex
;
208 scanner
->config
->scan_hex_dollar
= config_templ
->scan_hex_dollar
;
209 scanner
->config
->scan_string_sq
= config_templ
->scan_string_sq
;
210 scanner
->config
->scan_string_dq
= config_templ
->scan_string_dq
;
211 scanner
->config
->numbers_2_int
= config_templ
->numbers_2_int
;
212 scanner
->config
->int_2_float
= config_templ
->int_2_float
;
213 scanner
->config
->identifier_2_string
= config_templ
->identifier_2_string
;
214 scanner
->config
->char_2_token
= config_templ
->char_2_token
;
215 scanner
->config
->symbol_2_token
= config_templ
->symbol_2_token
;
216 scanner
->config
->scope_0_fallback
= config_templ
->scope_0_fallback
;
218 scanner
->token
= G_TOKEN_NONE
;
219 scanner
->value
.v_int
= 0;
221 scanner
->position
= 0;
223 scanner
->next_token
= G_TOKEN_NONE
;
224 scanner
->next_value
.v_int
= 0;
225 scanner
->next_line
= 1;
226 scanner
->next_position
= 0;
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;
235 scanner
->msg_handler
= g_scanner_msg_handler
;
241 g_scanner_free_value (GTokenType
*token_p
,
242 GTokenValue
*value_p
)
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
);
258 *token_p
= G_TOKEN_NONE
;
262 g_scanner_destroy_symbol_table_entry (gpointer _key
,
266 GScannerKey
*key
= _key
;
268 g_free (key
->symbol
);
273 g_scanner_destroy (GScanner
*scanner
)
275 g_return_if_fail (scanner
!= NULL
);
277 g_hash_table_foreach (scanner
->symbol_table
,
278 g_scanner_destroy_symbol_table_entry
, NULL
);
279 g_hash_table_destroy (scanner
->symbol_table
);
280 g_scanner_free_value (&scanner
->token
, &scanner
->value
);
281 g_scanner_free_value (&scanner
->next_token
, &scanner
->next_value
);
282 g_free (scanner
->config
);
283 g_free (scanner
->buffer
);
288 g_scanner_msg_handler (GScanner
*scanner
,
292 g_return_if_fail (scanner
!= NULL
);
294 fprintf (stdout
, "%s:%d: ", scanner
->input_name
, scanner
->line
);
296 fprintf (stdout
, "error: ");
297 fprintf (stdout
, "%s\n", message
);
301 g_scanner_error (GScanner
*scanner
,
305 g_return_if_fail (scanner
!= NULL
);
306 g_return_if_fail (format
!= NULL
);
308 scanner
->parse_errors
++;
310 if (scanner
->msg_handler
)
315 va_start (args
, format
);
316 string
= g_strdup_vprintf (format
, args
);
319 scanner
->msg_handler (scanner
, string
, TRUE
);
326 g_scanner_warn (GScanner
*scanner
,
330 g_return_if_fail (scanner
!= NULL
);
331 g_return_if_fail (format
!= NULL
);
333 if (scanner
->msg_handler
)
338 va_start (args
, format
);
339 string
= g_strdup_vprintf (format
, args
);
342 scanner
->msg_handler (scanner
, string
, FALSE
);
349 g_scanner_key_equal (gconstpointer v1
,
352 const GScannerKey
*key1
= v1
;
353 const GScannerKey
*key2
= v2
;
355 return (key1
->scope_id
== key2
->scope_id
) && (strcmp (key1
->symbol
, key2
->symbol
) == 0);
359 g_scanner_key_hash (gconstpointer v
)
361 const GScannerKey
*key
= v
;
366 for (c
= key
->symbol
; *c
; c
++)
382 static inline GScannerKey
*
383 g_scanner_lookup_internal (GScanner
*scanner
,
390 key
.scope_id
= scope_id
;
392 if (!scanner
->config
->case_sensitive
)
397 key
.symbol
= g_new (gchar
, strlen (symbol
) + 1);
398 for (d
= key
.symbol
, c
= symbol
; *c
; c
++, d
++)
401 key_p
= g_hash_table_lookup (scanner
->symbol_table
, &key
);
406 key
.symbol
= (gchar
*) symbol
;
407 key_p
= g_hash_table_lookup (scanner
->symbol_table
, &key
);
414 g_scanner_scope_add_symbol (GScanner
*scanner
,
421 g_return_if_fail (scanner
!= NULL
);
422 g_return_if_fail (symbol
!= NULL
);
424 key
= g_scanner_lookup_internal (scanner
, scope_id
, symbol
);
428 key
= g_new (GScannerKey
, 1);
429 key
->scope_id
= scope_id
;
430 key
->symbol
= g_strdup (symbol
);
432 if (!scanner
->config
->case_sensitive
)
443 g_hash_table_insert (scanner
->symbol_table
, key
, key
);
450 g_scanner_scope_remove_symbol (GScanner
*scanner
,
456 g_return_if_fail (scanner
!= NULL
);
457 g_return_if_fail (symbol
!= NULL
);
459 key
= g_scanner_lookup_internal (scanner
, scope_id
, symbol
);
463 g_hash_table_remove (scanner
->symbol_table
, key
);
464 g_free (key
->symbol
);
470 g_scanner_lookup_symbol (GScanner
*scanner
,
476 g_return_val_if_fail (scanner
!= NULL
, NULL
);
481 scope_id
= scanner
->scope_id
;
482 key
= g_scanner_lookup_internal (scanner
, scope_id
, symbol
);
483 if (!key
&& scope_id
&& scanner
->config
->scope_0_fallback
)
484 key
= g_scanner_lookup_internal (scanner
, 0, symbol
);
493 g_scanner_scope_lookup_symbol (GScanner
*scanner
,
499 g_return_val_if_fail (scanner
!= NULL
, NULL
);
504 key
= g_scanner_lookup_internal (scanner
, scope_id
, symbol
);
513 g_scanner_set_scope (GScanner
*scanner
,
518 g_return_val_if_fail (scanner
!= NULL
, 0);
520 old_scope_id
= scanner
->scope_id
;
521 scanner
->scope_id
= scope_id
;
527 g_scanner_foreach_internal (gpointer _key
,
538 func
= (GHFunc
) d
[0];
543 if (key
->scope_id
== *scope_id
)
544 func (key
->symbol
, key
->value
, user_data
);
548 g_scanner_scope_foreach_symbol (GScanner
*scanner
,
555 g_return_if_fail (scanner
!= NULL
);
557 d
[0] = (gpointer
) func
;
561 g_hash_table_foreach (scanner
->symbol_table
, g_scanner_foreach_internal
, d
);
565 g_scanner_freeze_symbol_table (GScanner
*scanner
)
567 g_return_if_fail (scanner
!= NULL
);
569 g_hash_table_freeze (scanner
->symbol_table
);
573 g_scanner_thaw_symbol_table (GScanner
*scanner
)
575 g_return_if_fail (scanner
!= NULL
);
577 g_hash_table_thaw (scanner
->symbol_table
);
581 g_scanner_peek_next_token (GScanner
*scanner
)
583 g_return_val_if_fail (scanner
!= NULL
, G_TOKEN_EOF
);
585 if (scanner
->next_token
== G_TOKEN_NONE
)
587 scanner
->next_line
= scanner
->line
;
588 scanner
->next_position
= scanner
->position
;
589 g_scanner_get_token_i (scanner
,
590 &scanner
->next_token
,
591 &scanner
->next_value
,
593 &scanner
->next_position
);
596 return scanner
->next_token
;
600 g_scanner_get_next_token (GScanner
*scanner
)
602 g_return_val_if_fail (scanner
!= NULL
, G_TOKEN_EOF
);
604 if (scanner
->next_token
!= G_TOKEN_NONE
)
606 g_scanner_free_value (&scanner
->token
, &scanner
->value
);
608 scanner
->token
= scanner
->next_token
;
609 scanner
->value
= scanner
->next_value
;
610 scanner
->line
= scanner
->next_line
;
611 scanner
->position
= scanner
->next_position
;
612 scanner
->next_token
= G_TOKEN_NONE
;
615 g_scanner_get_token_i (scanner
,
621 return scanner
->token
;
625 g_scanner_cur_token (GScanner
*scanner
)
627 g_return_val_if_fail (scanner
!= NULL
, G_TOKEN_EOF
);
629 return scanner
->token
;
633 g_scanner_cur_value (GScanner
*scanner
)
639 g_return_val_if_fail (scanner
!= NULL
, v
);
641 /* MSC isn't capable of handling return scanner->value; ? */
649 g_scanner_cur_line (GScanner
*scanner
)
651 g_return_val_if_fail (scanner
!= NULL
, 0);
653 return scanner
->line
;
657 g_scanner_cur_position (GScanner
*scanner
)
659 g_return_val_if_fail (scanner
!= NULL
, 0);
661 return scanner
->position
;
665 g_scanner_eof (GScanner
*scanner
)
667 g_return_val_if_fail (scanner
!= NULL
, TRUE
);
669 return scanner
->token
== G_TOKEN_EOF
;
673 g_scanner_input_file (GScanner
*scanner
,
676 g_return_if_fail (scanner
!= NULL
);
677 g_return_if_fail (input_fd
>= 0);
679 if (scanner
->input_fd
>= 0)
680 g_scanner_sync_file_offset (scanner
);
682 scanner
->token
= G_TOKEN_NONE
;
683 scanner
->value
.v_int
= 0;
685 scanner
->position
= 0;
686 scanner
->next_token
= G_TOKEN_NONE
;
688 scanner
->input_fd
= input_fd
;
689 scanner
->text
= NULL
;
690 scanner
->text_end
= NULL
;
692 if (!scanner
->buffer
)
693 scanner
->buffer
= g_new (gchar
, READ_BUFFER_SIZE
+ 1);
697 g_scanner_input_text (GScanner
*scanner
,
701 g_return_if_fail (scanner
!= NULL
);
703 g_return_if_fail (text
!= NULL
);
707 if (scanner
->input_fd
>= 0)
708 g_scanner_sync_file_offset (scanner
);
710 scanner
->token
= G_TOKEN_NONE
;
711 scanner
->value
.v_int
= 0;
713 scanner
->position
= 0;
714 scanner
->next_token
= G_TOKEN_NONE
;
716 scanner
->input_fd
= -1;
717 scanner
->text
= text
;
718 scanner
->text_end
= text
+ text_len
;
722 g_free (scanner
->buffer
);
723 scanner
->buffer
= NULL
;
728 g_scanner_peek_next_char (GScanner
*scanner
)
730 if (scanner
->text
< scanner
->text_end
)
732 return *scanner
->text
;
734 else if (scanner
->input_fd
>= 0)
739 buffer
= scanner
->buffer
;
742 count
= read (scanner
->input_fd
, buffer
, READ_BUFFER_SIZE
);
744 while (count
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
748 scanner
->input_fd
= -1;
754 scanner
->text
= buffer
;
755 scanner
->text_end
= buffer
+ count
;
765 g_scanner_sync_file_offset (GScanner
*scanner
)
767 g_return_if_fail (scanner
!= NULL
);
769 /* for file input, rewind the filedescriptor to the current
770 * buffer position and blow the file read ahead buffer. usefull for
771 * third party uses of our filedescriptor, which hooks onto the current
775 if (scanner
->input_fd
>= 0 && scanner
->text_end
> scanner
->text
)
779 buffered
= scanner
->text_end
- scanner
->text
;
780 if (lseek (scanner
->input_fd
, - buffered
, SEEK_CUR
) >= 0)
782 /* we succeeded, blow our buffer's contents now */
783 scanner
->text
= NULL
;
784 scanner
->text_end
= NULL
;
792 g_scanner_get_char (GScanner
*scanner
,
798 if (scanner
->text
< scanner
->text_end
)
799 fchar
= *(scanner
->text
++);
800 else if (scanner
->input_fd
>= 0)
805 buffer
= scanner
->buffer
;
808 count
= read (scanner
->input_fd
, buffer
, READ_BUFFER_SIZE
);
810 while (count
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
814 scanner
->input_fd
= -1;
819 scanner
->text
= buffer
+ 1;
820 scanner
->text_end
= buffer
+ count
;
824 g_scanner_sync_file_offset (scanner
);
825 scanner
->text_end
= scanner
->text
;
826 scanner
->input_fd
= -1;
847 g_scanner_unexp_token (GScanner
*scanner
,
848 GTokenType expected_token
,
849 const gchar
*identifier_spec
,
850 const gchar
*symbol_spec
,
851 const gchar
*symbol_name
,
852 const gchar
*message
,
856 guint token_string_len
;
857 gchar
*expected_string
;
858 guint expected_string_len
;
859 gchar
*message_prefix
;
860 gboolean print_unexp
;
861 void (*msg_handler
) (GScanner
*, const gchar
*, ...);
863 g_return_if_fail (scanner
!= NULL
);
866 msg_handler
= g_scanner_error
;
868 msg_handler
= g_scanner_warn
;
870 if (!identifier_spec
)
871 identifier_spec
= "identifier";
873 symbol_spec
= "symbol";
875 token_string_len
= 56;
876 token_string
= g_new (gchar
, token_string_len
+ 1);
877 expected_string_len
= 64;
878 expected_string
= g_new (gchar
, expected_string_len
+ 1);
881 switch (scanner
->token
)
884 g_snprintf (token_string
, token_string_len
, "end of file");
888 if (scanner
->token
>= 1 && scanner
->token
<= 255)
890 if ((scanner
->token
>= ' ' && scanner
->token
<= '~') ||
891 strchr (scanner
->config
->cset_identifier_first
, scanner
->token
) ||
892 strchr (scanner
->config
->cset_identifier_nth
, scanner
->token
))
893 g_snprintf (token_string
, expected_string_len
, "character `%c'", scanner
->token
);
895 g_snprintf (token_string
, expected_string_len
, "character `\\%o'", scanner
->token
);
898 else if (!scanner
->config
->symbol_2_token
)
900 g_snprintf (token_string
, token_string_len
, "(unknown) token <%d>", scanner
->token
);
905 if (expected_token
== G_TOKEN_SYMBOL
||
906 (scanner
->config
->symbol_2_token
&&
907 expected_token
> G_TOKEN_LAST
))
910 g_snprintf (token_string
,
913 print_unexp
? "" : "invalid ",
917 g_snprintf (token_string
,
920 print_unexp
? "" : "invalid ",
926 expected_token
= G_TOKEN_NONE
;
927 switch (scanner
->value
.v_error
)
929 case G_ERR_UNEXP_EOF
:
930 g_snprintf (token_string
, token_string_len
, "scanner: unexpected end of file");
933 case G_ERR_UNEXP_EOF_IN_STRING
:
934 g_snprintf (token_string
, token_string_len
, "scanner: unterminated string constant");
937 case G_ERR_UNEXP_EOF_IN_COMMENT
:
938 g_snprintf (token_string
, token_string_len
, "scanner: unterminated comment");
941 case G_ERR_NON_DIGIT_IN_CONST
:
942 g_snprintf (token_string
, token_string_len
, "scanner: non digit in constant");
945 case G_ERR_FLOAT_RADIX
:
946 g_snprintf (token_string
, token_string_len
, "scanner: invalid radix for floating constant");
949 case G_ERR_FLOAT_MALFORMED
:
950 g_snprintf (token_string
, token_string_len
, "scanner: malformed floating constant");
953 case G_ERR_DIGIT_RADIX
:
954 g_snprintf (token_string
, token_string_len
, "scanner: digit is beyond radix");
959 g_snprintf (token_string
, token_string_len
, "scanner: unknown error");
965 g_snprintf (token_string
, token_string_len
, "character `%c'", scanner
->value
.v_char
);
968 case G_TOKEN_IDENTIFIER
:
969 case G_TOKEN_IDENTIFIER_NULL
:
970 if (expected_token
== G_TOKEN_IDENTIFIER
||
971 expected_token
== G_TOKEN_IDENTIFIER_NULL
)
973 g_snprintf (token_string
,
976 print_unexp
? "" : "invalid ",
978 scanner
->token
== G_TOKEN_IDENTIFIER
? scanner
->value
.v_string
: "null");
985 g_snprintf (token_string
, token_string_len
, "number `%ld'", scanner
->value
.v_int
);
989 g_snprintf (token_string
, token_string_len
, "number `%.3f'", scanner
->value
.v_float
);
993 if (expected_token
== G_TOKEN_STRING
)
995 g_snprintf (token_string
,
997 "%s%sstring constant \"%s\"",
998 print_unexp
? "" : "invalid ",
999 scanner
->value
.v_string
[0] == 0 ? "empty " : "",
1000 scanner
->value
.v_string
);
1001 token_string
[token_string_len
- 2] = '"';
1002 token_string
[token_string_len
- 1] = 0;
1005 case G_TOKEN_COMMENT_SINGLE
:
1006 case G_TOKEN_COMMENT_MULTI
:
1007 g_snprintf (token_string
, token_string_len
, "comment");
1011 /* somehow the user's parsing code is screwed, there isn't much
1012 * we can do about it.
1013 * Note, a common case to trigger this is
1014 * g_scanner_peek_next_token(); g_scanner_unexp_token();
1015 * without an intermediate g_scanner_get_next_token().
1017 g_assert_not_reached ();
1022 switch (expected_token
)
1024 gboolean need_valid
;
1027 if (expected_token
>= 1 && expected_token
<= 255)
1029 if ((expected_token
>= ' ' && expected_token
<= '~') ||
1030 strchr (scanner
->config
->cset_identifier_first
, expected_token
) ||
1031 strchr (scanner
->config
->cset_identifier_nth
, expected_token
))
1032 g_snprintf (expected_string
, expected_string_len
, "character `%c'", expected_token
);
1034 g_snprintf (expected_string
, expected_string_len
, "character `\\%o'", expected_token
);
1037 else if (!scanner
->config
->symbol_2_token
)
1039 g_snprintf (expected_string
, expected_string_len
, "(unknown) token <%d>", expected_token
);
1043 case G_TOKEN_SYMBOL
:
1044 need_valid
= (scanner
->token
== G_TOKEN_SYMBOL
||
1045 (scanner
->config
->symbol_2_token
&&
1046 scanner
->token
> G_TOKEN_LAST
));
1047 g_snprintf (expected_string
,
1048 expected_string_len
,
1050 need_valid
? "valid " : "",
1052 /* FIXME: should we attempt to lookup the symbol_name for symbol_2_token? */
1056 g_snprintf (expected_string
, expected_string_len
, "number (integer)");
1060 g_snprintf (expected_string
, expected_string_len
, "number (float)");
1063 case G_TOKEN_STRING
:
1064 g_snprintf (expected_string
,
1065 expected_string_len
,
1066 "%sstring constant",
1067 scanner
->token
== G_TOKEN_STRING
? "valid " : "");
1070 case G_TOKEN_IDENTIFIER
:
1071 case G_TOKEN_IDENTIFIER_NULL
:
1072 g_snprintf (expected_string
,
1073 expected_string_len
,
1075 (scanner
->token
== G_TOKEN_IDENTIFIER_NULL
||
1076 scanner
->token
== G_TOKEN_IDENTIFIER
? "valid " : ""),
1081 g_snprintf (expected_string
, expected_string_len
, "end of file");
1088 if (message
&& message
[0] != 0)
1089 message_prefix
= " - ";
1092 message_prefix
= "";
1096 if (expected_token
!= G_TOKEN_NONE
)
1099 msg_handler (scanner
,
1100 "unexpected %s, expected %s%s%s",
1106 msg_handler (scanner
,
1107 "%s, expected %s%s%s",
1116 msg_handler (scanner
,
1117 "unexpected %s%s%s",
1122 msg_handler (scanner
,
1129 g_free (token_string
);
1130 g_free (expected_string
);
1134 g_scanner_stat_mode (const gchar
*filename
)
1136 struct stat
*stat_buf
;
1139 stat_buf
= g_new0 (struct stat
, 1);
1141 lstat (filename
, stat_buf
);
1143 stat (filename
, stat_buf
);
1145 st_mode
= stat_buf
->st_mode
;
1153 g_scanner_get_token_i (GScanner
*scanner
,
1154 GTokenType
*token_p
,
1155 GTokenValue
*value_p
,
1161 g_scanner_free_value (token_p
, value_p
);
1162 g_scanner_get_token_ll (scanner
, token_p
, value_p
, line_p
, position_p
);
1164 while (((*token_p
> 0 && *token_p
< 256) &&
1165 strchr (scanner
->config
->cset_skip_characters
, *token_p
)) ||
1166 (*token_p
== G_TOKEN_CHAR
&&
1167 strchr (scanner
->config
->cset_skip_characters
, value_p
->v_char
)) ||
1168 (*token_p
== G_TOKEN_COMMENT_MULTI
&&
1169 scanner
->config
->skip_comment_multi
) ||
1170 (*token_p
== G_TOKEN_COMMENT_SINGLE
&&
1171 scanner
->config
->skip_comment_single
));
1175 case G_TOKEN_IDENTIFIER
:
1176 if (scanner
->config
->identifier_2_string
)
1177 *token_p
= G_TOKEN_STRING
;
1180 case G_TOKEN_SYMBOL
:
1181 if (scanner
->config
->symbol_2_token
)
1182 *token_p
= (GTokenType
) value_p
->v_symbol
;
1185 case G_TOKEN_BINARY
:
1188 if (scanner
->config
->numbers_2_int
)
1189 *token_p
= G_TOKEN_INT
;
1196 if (*token_p
== G_TOKEN_INT
&&
1197 scanner
->config
->int_2_float
)
1199 *token_p
= G_TOKEN_FLOAT
;
1200 value_p
->v_float
= value_p
->v_int
;
1207 g_scanner_get_token_ll (GScanner
*scanner
,
1208 GTokenType
*token_p
,
1209 GTokenValue
*value_p
,
1213 GScannerConfig
*config
;
1215 gboolean in_comment_multi
;
1216 gboolean in_comment_single
;
1217 gboolean in_string_sq
;
1218 gboolean in_string_dq
;
1223 config
= scanner
->config
;
1224 (*value_p
).v_int
= 0;
1226 if ((scanner
->text
>= scanner
->text_end
&& scanner
->input_fd
< 0) ||
1227 scanner
->token
== G_TOKEN_EOF
)
1229 *token_p
= G_TOKEN_EOF
;
1233 in_comment_multi
= FALSE
;
1234 in_comment_single
= FALSE
;
1235 in_string_sq
= FALSE
;
1236 in_string_dq
= FALSE
;
1239 do /* while (ch != 0) */
1241 gboolean dotted_float
= FALSE
;
1243 ch
= g_scanner_get_char (scanner
, line_p
, position_p
);
1246 token
= G_TOKEN_NONE
;
1248 /* this is *evil*, but needed ;(
1249 * we first check for identifier first character, because it
1250 * might interfere with other key chars like slashes or numbers
1252 if (config
->scan_identifier
&&
1253 ch
&& strchr (config
->cset_identifier_first
, ch
))
1254 goto identifier_precedence
;
1259 token
= G_TOKEN_EOF
;
1265 if (!config
->scan_comment_multi
||
1266 g_scanner_peek_next_char (scanner
) != '*')
1268 g_scanner_get_char (scanner
, line_p
, position_p
);
1269 token
= G_TOKEN_COMMENT_MULTI
;
1270 in_comment_multi
= TRUE
;
1271 gstring
= g_string_new ("");
1272 while ((ch
= g_scanner_get_char (scanner
, line_p
, position_p
)) != 0)
1274 if (ch
== '*' && g_scanner_peek_next_char (scanner
) == '/')
1276 g_scanner_get_char (scanner
, line_p
, position_p
);
1277 in_comment_multi
= FALSE
;
1281 gstring
= g_string_append_c (gstring
, ch
);
1287 if (!config
->scan_string_sq
)
1289 token
= G_TOKEN_STRING
;
1290 in_string_sq
= TRUE
;
1291 gstring
= g_string_new ("");
1292 while ((ch
= g_scanner_get_char (scanner
, line_p
, position_p
)) != 0)
1296 in_string_sq
= FALSE
;
1300 gstring
= g_string_append_c (gstring
, ch
);
1306 if (!config
->scan_string_dq
)
1308 token
= G_TOKEN_STRING
;
1309 in_string_dq
= TRUE
;
1310 gstring
= g_string_new ("");
1311 while ((ch
= g_scanner_get_char (scanner
, line_p
, position_p
)) != 0)
1315 in_string_dq
= FALSE
;
1322 ch
= g_scanner_get_char (scanner
, line_p
, position_p
);
1332 gstring
= g_string_append_c (gstring
, '\\');
1336 gstring
= g_string_append_c (gstring
, '\n');
1340 gstring
= g_string_append_c (gstring
, '\t');
1344 gstring
= g_string_append_c (gstring
, '\r');
1348 gstring
= g_string_append_c (gstring
, '\b');
1352 gstring
= g_string_append_c (gstring
, '\f');
1364 fchar
= g_scanner_peek_next_char (scanner
);
1365 if (fchar
>= '0' && fchar
<= '7')
1367 ch
= g_scanner_get_char (scanner
, line_p
, position_p
);
1368 i
= i
* 8 + ch
- '0';
1369 fchar
= g_scanner_peek_next_char (scanner
);
1370 if (fchar
>= '0' && fchar
<= '7')
1372 ch
= g_scanner_get_char (scanner
, line_p
, position_p
);
1373 i
= i
* 8 + ch
- '0';
1376 gstring
= g_string_append_c (gstring
, i
);
1380 gstring
= g_string_append_c (gstring
, ch
);
1385 gstring
= g_string_append_c (gstring
, ch
);
1392 if (!config
->scan_float
)
1394 token
= G_TOKEN_FLOAT
;
1395 dotted_float
= TRUE
;
1396 ch
= g_scanner_get_char (scanner
, line_p
, position_p
);
1397 goto number_parsing
;
1400 if (!config
->scan_hex_dollar
)
1402 token
= G_TOKEN_HEX
;
1403 ch
= g_scanner_get_char (scanner
, line_p
, position_p
);
1404 goto number_parsing
;
1407 if (config
->scan_octal
)
1408 token
= G_TOKEN_OCTAL
;
1410 token
= G_TOKEN_INT
;
1411 ch
= g_scanner_peek_next_char (scanner
);
1412 if (config
->scan_hex
&& (ch
== 'x' || ch
== 'X'))
1414 token
= G_TOKEN_HEX
;
1415 g_scanner_get_char (scanner
, line_p
, position_p
);
1416 ch
= g_scanner_get_char (scanner
, line_p
, position_p
);
1419 token
= G_TOKEN_ERROR
;
1420 value
.v_error
= G_ERR_UNEXP_EOF
;
1424 if (g_scanner_char_2_num (ch
, 16) < 0)
1426 token
= G_TOKEN_ERROR
;
1427 value
.v_error
= G_ERR_DIGIT_RADIX
;
1432 else if (config
->scan_binary
&& (ch
== 'b' || ch
== 'B'))
1434 token
= G_TOKEN_BINARY
;
1435 g_scanner_get_char (scanner
, line_p
, position_p
);
1436 ch
= g_scanner_get_char (scanner
, line_p
, position_p
);
1439 token
= G_TOKEN_ERROR
;
1440 value
.v_error
= G_ERR_UNEXP_EOF
;
1444 if (g_scanner_char_2_num (ch
, 10) < 0)
1446 token
= G_TOKEN_ERROR
;
1447 value
.v_error
= G_ERR_NON_DIGIT_IN_CONST
;
1466 gboolean in_number
= TRUE
;
1469 if (token
== G_TOKEN_NONE
)
1470 token
= G_TOKEN_INT
;
1472 gstring
= g_string_new (dotted_float
? "0." : "");
1473 gstring
= g_string_append_c (gstring
, ch
);
1475 do /* while (in_number) */
1479 is_E
= token
== G_TOKEN_FLOAT
&& (ch
== 'e' || ch
== 'E');
1481 ch
= g_scanner_peek_next_char (scanner
);
1483 if (g_scanner_char_2_num (ch
, 36) >= 0 ||
1484 (config
->scan_float
&& ch
== '.') ||
1485 (is_E
&& (ch
== '+' || ch
== '-')))
1487 ch
= g_scanner_get_char (scanner
, line_p
, position_p
);
1492 if (token
!= G_TOKEN_INT
&& token
!= G_TOKEN_OCTAL
)
1494 value
.v_error
= token
== G_TOKEN_FLOAT
? G_ERR_FLOAT_MALFORMED
: G_ERR_FLOAT_RADIX
;
1495 token
= G_TOKEN_ERROR
;
1500 token
= G_TOKEN_FLOAT
;
1501 gstring
= g_string_append_c (gstring
, ch
);
1515 gstring
= g_string_append_c (gstring
, ch
);
1520 if (token
!= G_TOKEN_FLOAT
)
1522 token
= G_TOKEN_ERROR
;
1523 value
.v_error
= G_ERR_NON_DIGIT_IN_CONST
;
1527 gstring
= g_string_append_c (gstring
, ch
);
1532 if ((token
!= G_TOKEN_HEX
&& !config
->scan_float
) ||
1533 (token
!= G_TOKEN_HEX
&&
1534 token
!= G_TOKEN_OCTAL
&&
1535 token
!= G_TOKEN_FLOAT
&&
1536 token
!= G_TOKEN_INT
))
1538 token
= G_TOKEN_ERROR
;
1539 value
.v_error
= G_ERR_NON_DIGIT_IN_CONST
;
1544 if (token
!= G_TOKEN_HEX
)
1545 token
= G_TOKEN_FLOAT
;
1546 gstring
= g_string_append_c (gstring
, ch
);
1551 if (token
!= G_TOKEN_HEX
)
1553 token
= G_TOKEN_ERROR
;
1554 value
.v_error
= G_ERR_NON_DIGIT_IN_CONST
;
1558 gstring
= g_string_append_c (gstring
, ch
);
1570 case G_TOKEN_BINARY
:
1571 value
.v_binary
= strtol (gstring
->str
, &endptr
, 2);
1575 value
.v_octal
= strtol (gstring
->str
, &endptr
, 8);
1579 value
.v_int
= strtol (gstring
->str
, &endptr
, 10);
1583 value
.v_float
= g_strtod (gstring
->str
, &endptr
);
1587 value
.v_hex
= strtol (gstring
->str
, &endptr
, 16);
1593 if (endptr
&& *endptr
)
1595 token
= G_TOKEN_ERROR
;
1596 if (*endptr
== 'e' || *endptr
== 'E')
1597 value
.v_error
= G_ERR_NON_DIGIT_IN_CONST
;
1599 value
.v_error
= G_ERR_DIGIT_RADIX
;
1601 g_string_free (gstring
, TRUE
);
1604 } /* number_parsing:... */
1610 if (config
->cpair_comment_single
&&
1611 ch
== config
->cpair_comment_single
[0])
1613 token
= G_TOKEN_COMMENT_SINGLE
;
1614 in_comment_single
= TRUE
;
1615 gstring
= g_string_new ("");
1616 ch
= g_scanner_get_char (scanner
, line_p
, position_p
);
1619 if (ch
== config
->cpair_comment_single
[1])
1621 in_comment_single
= FALSE
;
1626 gstring
= g_string_append_c (gstring
, ch
);
1627 ch
= g_scanner_get_char (scanner
, line_p
, position_p
);
1630 else if (config
->scan_identifier
&& ch
&&
1631 strchr (config
->cset_identifier_first
, ch
))
1633 identifier_precedence
:
1635 if (config
->cset_identifier_nth
&& ch
&&
1636 strchr (config
->cset_identifier_nth
,
1637 g_scanner_peek_next_char (scanner
)))
1639 token
= G_TOKEN_IDENTIFIER
;
1640 gstring
= g_string_new ("");
1641 gstring
= g_string_append_c (gstring
, ch
);
1644 ch
= g_scanner_get_char (scanner
, line_p
, position_p
);
1645 gstring
= g_string_append_c (gstring
, ch
);
1646 ch
= g_scanner_peek_next_char (scanner
);
1648 while (ch
&& strchr (config
->cset_identifier_nth
, ch
));
1651 else if (config
->scan_identifier_1char
)
1653 token
= G_TOKEN_IDENTIFIER
;
1654 value
.v_identifier
= g_new0 (gchar
, 2);
1655 value
.v_identifier
[0] = ch
;
1661 if (config
->char_2_token
)
1665 token
= G_TOKEN_CHAR
;
1670 } /* default_case:... */
1673 g_assert (ch
== 0 && token
!= G_TOKEN_NONE
); /* paranoid */
1677 if (in_comment_multi
|| in_comment_single
||
1678 in_string_sq
|| in_string_dq
)
1680 token
= G_TOKEN_ERROR
;
1683 g_string_free (gstring
, TRUE
);
1687 if (in_comment_multi
|| in_comment_single
)
1688 value
.v_error
= G_ERR_UNEXP_EOF_IN_COMMENT
;
1689 else /* (in_string_sq || in_string_dq) */
1690 value
.v_error
= G_ERR_UNEXP_EOF_IN_STRING
;
1695 value
.v_string
= gstring
->str
;
1696 g_string_free (gstring
, FALSE
);
1700 if (token
== G_TOKEN_IDENTIFIER
)
1702 if (config
->scan_symbols
)
1707 scope_id
= scanner
->scope_id
;
1708 key
= g_scanner_lookup_internal (scanner
, scope_id
, value
.v_identifier
);
1709 if (!key
&& scope_id
&& scanner
->config
->scope_0_fallback
)
1710 key
= g_scanner_lookup_internal (scanner
, 0, value
.v_identifier
);
1714 g_free (value
.v_identifier
);
1715 token
= G_TOKEN_SYMBOL
;
1716 value
.v_symbol
= key
->value
;
1720 if (token
== G_TOKEN_IDENTIFIER
&&
1721 config
->scan_identifier_NULL
&&
1722 strlen (value
.v_identifier
) == 4)
1724 gchar
*null_upper
= "NULL";
1725 gchar
*null_lower
= "null";
1727 if (scanner
->config
->case_sensitive
)
1729 if (value
.v_identifier
[0] == null_upper
[0] &&
1730 value
.v_identifier
[1] == null_upper
[1] &&
1731 value
.v_identifier
[2] == null_upper
[2] &&
1732 value
.v_identifier
[3] == null_upper
[3])
1733 token
= G_TOKEN_IDENTIFIER_NULL
;
1737 if ((value
.v_identifier
[0] == null_upper
[0] ||
1738 value
.v_identifier
[0] == null_lower
[0]) &&
1739 (value
.v_identifier
[1] == null_upper
[1] ||
1740 value
.v_identifier
[1] == null_lower
[1]) &&
1741 (value
.v_identifier
[2] == null_upper
[2] ||
1742 value
.v_identifier
[2] == null_lower
[2]) &&
1743 (value
.v_identifier
[3] == null_upper
[3] ||
1744 value
.v_identifier
[3] == null_lower
[3]))
1745 token
= G_TOKEN_IDENTIFIER_NULL
;