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 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.
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.
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.
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/.
46 #include "gprintfint.h"
50 #include <io.h> /* For _read() */
54 #define to_lower(c) ( \
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) ) | \
62 #define READ_BUFFER_SIZE (4000)
65 /* --- typedefs --- */
66 typedef struct _GScannerKey GScannerKey;
76 /* --- variables --- */
77 static const GScannerConfig g_scanner_config_template =
81 ) /* cset_skip_characters */,
86 ) /* cset_identifier_first */,
94 ) /* cset_identifier_nth */,
95 ( "#\n" ) /* cpair_comment_single */,
97 FALSE /* case_sensitive */,
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 */,
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 */,
123 /* --- prototypes --- */
125 GScannerKey* g_scanner_lookup_internal (GScanner *scanner,
127 const gchar *symbol);
128 static gboolean g_scanner_key_equal (gconstpointer v1,
130 static guint g_scanner_key_hash (gconstpointer v);
131 static void g_scanner_get_token_ll (GScanner *scanner,
133 GTokenValue *value_p,
136 static void g_scanner_get_token_i (GScanner *scanner,
138 GTokenValue *value_p,
142 static guchar g_scanner_peek_next_char (GScanner *scanner);
143 static guchar g_scanner_get_char (GScanner *scanner,
146 static void g_scanner_msg_handler (GScanner *scanner,
151 /* --- functions --- */
153 g_scanner_char_2_num (guchar c,
156 if (c >= '0' && c <= '9')
158 else if (c >= 'A' && c <= 'Z')
160 else if (c >= 'a' && c <= 'z')
172 g_scanner_new (const GScannerConfig *config_templ)
177 config_templ = &g_scanner_config_template;
179 scanner = g_new0 (GScanner, 1);
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);
187 scanner->config = g_new0 (GScannerConfig, 1);
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;
218 scanner->token = G_TOKEN_NONE;
219 scanner->value.v_int64 = 0;
221 scanner->position = 0;
223 scanner->next_token = G_TOKEN_NONE;
224 scanner->next_value.v_int64 = 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_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);
289 g_scanner_msg_handler (GScanner *scanner,
293 g_return_if_fail (scanner != NULL);
295 _g_fprintf (stderr, "%s:%d: ",
296 scanner->input_name ? scanner->input_name : "<memory>",
299 _g_fprintf (stderr, "error: ");
300 _g_fprintf (stderr, "%s\n", message);
304 g_scanner_error (GScanner *scanner,
308 g_return_if_fail (scanner != NULL);
309 g_return_if_fail (format != NULL);
311 scanner->parse_errors++;
313 if (scanner->msg_handler)
318 va_start (args, format);
319 string = g_strdup_vprintf (format, args);
322 scanner->msg_handler (scanner, string, TRUE);
329 g_scanner_warn (GScanner *scanner,
333 g_return_if_fail (scanner != NULL);
334 g_return_if_fail (format != NULL);
336 if (scanner->msg_handler)
341 va_start (args, format);
342 string = g_strdup_vprintf (format, args);
345 scanner->msg_handler (scanner, string, FALSE);
352 g_scanner_key_equal (gconstpointer v1,
355 const GScannerKey *key1 = v1;
356 const GScannerKey *key2 = v2;
358 return (key1->scope_id == key2->scope_id) && (strcmp (key1->symbol, key2->symbol) == 0);
362 g_scanner_key_hash (gconstpointer v)
364 const GScannerKey *key = v;
369 for (c = key->symbol; *c; c++)
370 h = (h << 5) - h + *c;
375 static inline GScannerKey*
376 g_scanner_lookup_internal (GScanner *scanner,
383 key.scope_id = scope_id;
385 if (!scanner->config->case_sensitive)
390 key.symbol = g_new (gchar, strlen (symbol) + 1);
391 for (d = key.symbol, c = symbol; *c; c++, d++)
394 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
399 key.symbol = (gchar*) symbol;
400 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
407 g_scanner_scope_add_symbol (GScanner *scanner,
414 g_return_if_fail (scanner != NULL);
415 g_return_if_fail (symbol != NULL);
417 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
421 key = g_new (GScannerKey, 1);
422 key->scope_id = scope_id;
423 key->symbol = g_strdup (symbol);
425 if (!scanner->config->case_sensitive)
436 g_hash_table_insert (scanner->symbol_table, key, key);
443 g_scanner_scope_remove_symbol (GScanner *scanner,
449 g_return_if_fail (scanner != NULL);
450 g_return_if_fail (symbol != NULL);
452 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
456 g_hash_table_remove (scanner->symbol_table, key);
457 g_free (key->symbol);
463 g_scanner_lookup_symbol (GScanner *scanner,
469 g_return_val_if_fail (scanner != NULL, NULL);
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);
486 g_scanner_scope_lookup_symbol (GScanner *scanner,
492 g_return_val_if_fail (scanner != NULL, NULL);
497 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
506 g_scanner_set_scope (GScanner *scanner,
511 g_return_val_if_fail (scanner != NULL, 0);
513 old_scope_id = scanner->scope_id;
514 scanner->scope_id = scope_id;
520 g_scanner_foreach_internal (gpointer _key,
531 func = (GHFunc) d[0];
536 if (key->scope_id == *scope_id)
537 func (key->symbol, key->value, user_data);
541 g_scanner_scope_foreach_symbol (GScanner *scanner,
548 g_return_if_fail (scanner != NULL);
550 d[0] = (gpointer) func;
554 g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
558 g_scanner_peek_next_token (GScanner *scanner)
560 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
562 if (scanner->next_token == G_TOKEN_NONE)
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,
570 &scanner->next_position);
573 return scanner->next_token;
577 g_scanner_get_next_token (GScanner *scanner)
579 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
581 if (scanner->next_token != G_TOKEN_NONE)
583 g_scanner_free_value (&scanner->token, &scanner->value);
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;
592 g_scanner_get_token_i (scanner,
598 return scanner->token;
602 g_scanner_cur_token (GScanner *scanner)
604 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
606 return scanner->token;
610 g_scanner_cur_value (GScanner *scanner)
616 g_return_val_if_fail (scanner != NULL, v);
618 /* MSC isn't capable of handling return scanner->value; ? */
626 g_scanner_cur_line (GScanner *scanner)
628 g_return_val_if_fail (scanner != NULL, 0);
630 return scanner->line;
634 g_scanner_cur_position (GScanner *scanner)
636 g_return_val_if_fail (scanner != NULL, 0);
638 return scanner->position;
642 g_scanner_eof (GScanner *scanner)
644 g_return_val_if_fail (scanner != NULL, TRUE);
646 return scanner->token == G_TOKEN_EOF || scanner->token == G_TOKEN_ERROR;
650 g_scanner_input_file (GScanner *scanner,
653 g_return_if_fail (scanner != NULL);
654 g_return_if_fail (input_fd >= 0);
656 if (scanner->input_fd >= 0)
657 g_scanner_sync_file_offset (scanner);
659 scanner->token = G_TOKEN_NONE;
660 scanner->value.v_int64 = 0;
662 scanner->position = 0;
663 scanner->next_token = G_TOKEN_NONE;
665 scanner->input_fd = input_fd;
666 scanner->text = NULL;
667 scanner->text_end = NULL;
669 if (!scanner->buffer)
670 scanner->buffer = g_new (gchar, READ_BUFFER_SIZE + 1);
674 g_scanner_input_text (GScanner *scanner,
678 g_return_if_fail (scanner != NULL);
680 g_return_if_fail (text != NULL);
684 if (scanner->input_fd >= 0)
685 g_scanner_sync_file_offset (scanner);
687 scanner->token = G_TOKEN_NONE;
688 scanner->value.v_int64 = 0;
690 scanner->position = 0;
691 scanner->next_token = G_TOKEN_NONE;
693 scanner->input_fd = -1;
694 scanner->text = text;
695 scanner->text_end = text + text_len;
699 g_free (scanner->buffer);
700 scanner->buffer = NULL;
705 g_scanner_peek_next_char (GScanner *scanner)
707 if (scanner->text < scanner->text_end)
709 return *scanner->text;
711 else if (scanner->input_fd >= 0)
716 buffer = scanner->buffer;
719 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
721 while (count == -1 && (errno == EINTR || errno == EAGAIN));
725 scanner->input_fd = -1;
731 scanner->text = buffer;
732 scanner->text_end = buffer + count;
742 g_scanner_sync_file_offset (GScanner *scanner)
744 g_return_if_fail (scanner != NULL);
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.
752 if (scanner->input_fd >= 0 && scanner->text_end > scanner->text)
756 buffered = scanner->text_end - scanner->text;
757 if (lseek (scanner->input_fd, - buffered, SEEK_CUR) >= 0)
759 /* we succeeded, blow our buffer's contents now */
760 scanner->text = NULL;
761 scanner->text_end = NULL;
769 g_scanner_get_char (GScanner *scanner,
775 if (scanner->text < scanner->text_end)
776 fchar = *(scanner->text++);
777 else if (scanner->input_fd >= 0)
782 buffer = scanner->buffer;
785 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
787 while (count == -1 && (errno == EINTR || errno == EAGAIN));
791 scanner->input_fd = -1;
796 scanner->text = buffer + 1;
797 scanner->text_end = buffer + count;
801 g_scanner_sync_file_offset (scanner);
802 scanner->text_end = scanner->text;
803 scanner->input_fd = -1;
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,
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*, ...);
840 g_return_if_fail (scanner != NULL);
843 msg_handler = g_scanner_error;
845 msg_handler = g_scanner_warn;
847 if (!identifier_spec)
848 identifier_spec = "identifier";
850 symbol_spec = "symbol";
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);
858 switch (scanner->token)
861 _g_snprintf (token_string, token_string_len, "end of file");
865 if (scanner->token >= 1 && scanner->token <= 255)
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);
872 _g_snprintf (token_string, token_string_len, "character `\\%o'", scanner->token);
875 else if (!scanner->config->symbol_2_token)
877 _g_snprintf (token_string, token_string_len, "(unknown) token <%d>", scanner->token);
882 if (expected_token == G_TOKEN_SYMBOL ||
883 (scanner->config->symbol_2_token &&
884 expected_token > G_TOKEN_LAST))
887 _g_snprintf (token_string,
890 print_unexp ? "" : "invalid ",
894 _g_snprintf (token_string,
897 print_unexp ? "" : "invalid ",
903 expected_token = G_TOKEN_NONE;
904 switch (scanner->value.v_error)
906 case G_ERR_UNEXP_EOF:
907 _g_snprintf (token_string, token_string_len, "scanner: unexpected end of file");
910 case G_ERR_UNEXP_EOF_IN_STRING:
911 _g_snprintf (token_string, token_string_len, "scanner: unterminated string constant");
914 case G_ERR_UNEXP_EOF_IN_COMMENT:
915 _g_snprintf (token_string, token_string_len, "scanner: unterminated comment");
918 case G_ERR_NON_DIGIT_IN_CONST:
919 _g_snprintf (token_string, token_string_len, "scanner: non digit in constant");
922 case G_ERR_FLOAT_RADIX:
923 _g_snprintf (token_string, token_string_len, "scanner: invalid radix for floating constant");
926 case G_ERR_FLOAT_MALFORMED:
927 _g_snprintf (token_string, token_string_len, "scanner: malformed floating constant");
930 case G_ERR_DIGIT_RADIX:
931 _g_snprintf (token_string, token_string_len, "scanner: digit is beyond radix");
936 _g_snprintf (token_string, token_string_len, "scanner: unknown error");
942 _g_snprintf (token_string, token_string_len, "character `%c'", scanner->value.v_char);
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)
950 _g_snprintf (token_string,
953 print_unexp ? "" : "invalid ",
955 scanner->token == G_TOKEN_IDENTIFIER ? scanner->value.v_string : "null");
962 if (scanner->config->store_int64)
963 _g_snprintf (token_string, token_string_len, "number `%" G_GUINT64_FORMAT "'", scanner->value.v_int64);
965 _g_snprintf (token_string, token_string_len, "number `%lu'", scanner->value.v_int);
969 _g_snprintf (token_string, token_string_len, "number `%.3f'", scanner->value.v_float);
973 if (expected_token == G_TOKEN_STRING)
975 _g_snprintf (token_string,
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;
985 case G_TOKEN_COMMENT_SINGLE:
986 case G_TOKEN_COMMENT_MULTI:
987 _g_snprintf (token_string, token_string_len, "comment");
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().
997 g_assert_not_reached ();
1002 switch (expected_token)
1004 gboolean need_valid;
1007 _g_snprintf (expected_string, expected_string_len, "end of file");
1010 if (expected_token >= 1 && expected_token <= 255)
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);
1017 _g_snprintf (expected_string, expected_string_len, "character `\\%o'", expected_token);
1020 else if (!scanner->config->symbol_2_token)
1022 _g_snprintf (expected_string, expected_string_len, "(unknown) token <%d>", expected_token);
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,
1033 need_valid ? "valid " : "",
1035 /* FIXME: should we attempt to lookup the symbol_name for symbol_2_token? */
1038 _g_snprintf (expected_string, expected_string_len, "%scharacter",
1039 scanner->token == G_TOKEN_CHAR ? "valid " : "");
1041 case G_TOKEN_BINARY:
1043 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1044 scanner->token == expected_token ? "valid " : "", tstring);
1048 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1049 scanner->token == expected_token ? "valid " : "", tstring);
1052 tstring = "integer";
1053 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1054 scanner->token == expected_token ? "valid " : "", tstring);
1057 tstring = "hexadecimal";
1058 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1059 scanner->token == expected_token ? "valid " : "", tstring);
1063 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1064 scanner->token == expected_token ? "valid " : "", tstring);
1066 case G_TOKEN_STRING:
1067 _g_snprintf (expected_string,
1068 expected_string_len,
1069 "%sstring constant",
1070 scanner->token == G_TOKEN_STRING ? "valid " : "");
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,
1079 need_valid ? "valid " : "",
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);
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);
1094 /* this is handled upon printout */
1098 if (message && message[0] != 0)
1099 message_prefix = " - ";
1102 message_prefix = "";
1105 if (expected_token == G_TOKEN_ERROR)
1107 msg_handler (scanner,
1108 "failure around %s%s%s",
1113 else if (expected_token == G_TOKEN_NONE)
1116 msg_handler (scanner,
1117 "unexpected %s%s%s",
1122 msg_handler (scanner,
1131 msg_handler (scanner,
1132 "unexpected %s, expected %s%s%s",
1138 msg_handler (scanner,
1139 "%s, expected %s%s%s",
1146 g_free (token_string);
1147 g_free (expected_string);
1151 g_scanner_get_token_i (GScanner *scanner,
1152 GTokenType *token_p,
1153 GTokenValue *value_p,
1159 g_scanner_free_value (token_p, value_p);
1160 g_scanner_get_token_ll (scanner, token_p, value_p, line_p, position_p);
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));
1173 case G_TOKEN_IDENTIFIER:
1174 if (scanner->config->identifier_2_string)
1175 *token_p = G_TOKEN_STRING;
1178 case G_TOKEN_SYMBOL:
1179 if (scanner->config->symbol_2_token)
1180 *token_p = (GTokenType) value_p->v_symbol;
1183 case G_TOKEN_BINARY:
1186 if (scanner->config->numbers_2_int)
1187 *token_p = G_TOKEN_INT;
1194 if (*token_p == G_TOKEN_INT &&
1195 scanner->config->int_2_float)
1197 *token_p = G_TOKEN_FLOAT;
1198 if (scanner->config->store_int64)
1201 /* work around error C2520, see gvaluetransform.c */
1202 value_p->v_float = (__int64)value_p->v_int64;
1204 value_p->v_float = value_p->v_int64;
1208 value_p->v_float = value_p->v_int;
1215 g_scanner_get_token_ll (GScanner *scanner,
1216 GTokenType *token_p,
1217 GTokenValue *value_p,
1221 GScannerConfig *config;
1223 gboolean in_comment_multi;
1224 gboolean in_comment_single;
1225 gboolean in_string_sq;
1226 gboolean in_string_dq;
1231 config = scanner->config;
1232 (*value_p).v_int64 = 0;
1234 if ((scanner->text >= scanner->text_end && scanner->input_fd < 0) ||
1235 scanner->token == G_TOKEN_EOF)
1237 *token_p = G_TOKEN_EOF;
1241 in_comment_multi = FALSE;
1242 in_comment_single = FALSE;
1243 in_string_sq = FALSE;
1244 in_string_dq = FALSE;
1247 do /* while (ch != 0) */
1249 gboolean dotted_float = FALSE;
1251 ch = g_scanner_get_char (scanner, line_p, position_p);
1254 token = G_TOKEN_NONE;
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
1260 if (config->scan_identifier &&
1261 ch && strchr (config->cset_identifier_first, ch))
1262 goto identifier_precedence;
1267 token = G_TOKEN_EOF;
1273 if (!config->scan_comment_multi ||
1274 g_scanner_peek_next_char (scanner) != '*')
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)
1282 if (ch == '*' && g_scanner_peek_next_char (scanner) == '/')
1284 g_scanner_get_char (scanner, line_p, position_p);
1285 in_comment_multi = FALSE;
1289 gstring = g_string_append_c (gstring, ch);
1295 if (!config->scan_string_sq)
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)
1304 in_string_sq = FALSE;
1308 gstring = g_string_append_c (gstring, ch);
1314 if (!config->scan_string_dq)
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)
1323 in_string_dq = FALSE;
1330 ch = g_scanner_get_char (scanner, line_p, position_p);
1340 gstring = g_string_append_c (gstring, '\\');
1344 gstring = g_string_append_c (gstring, '\n');
1348 gstring = g_string_append_c (gstring, '\t');
1352 gstring = g_string_append_c (gstring, '\r');
1356 gstring = g_string_append_c (gstring, '\b');
1360 gstring = g_string_append_c (gstring, '\f');
1372 fchar = g_scanner_peek_next_char (scanner);
1373 if (fchar >= '0' && fchar <= '7')
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')
1380 ch = g_scanner_get_char (scanner, line_p, position_p);
1381 i = i * 8 + ch - '0';
1384 gstring = g_string_append_c (gstring, i);
1388 gstring = g_string_append_c (gstring, ch);
1393 gstring = g_string_append_c (gstring, ch);
1400 if (!config->scan_float)
1402 token = G_TOKEN_FLOAT;
1403 dotted_float = TRUE;
1404 ch = g_scanner_get_char (scanner, line_p, position_p);
1405 goto number_parsing;
1408 if (!config->scan_hex_dollar)
1410 token = G_TOKEN_HEX;
1411 ch = g_scanner_get_char (scanner, line_p, position_p);
1412 goto number_parsing;
1415 if (config->scan_octal)
1416 token = G_TOKEN_OCTAL;
1418 token = G_TOKEN_INT;
1419 ch = g_scanner_peek_next_char (scanner);
1420 if (config->scan_hex && (ch == 'x' || ch == 'X'))
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);
1427 token = G_TOKEN_ERROR;
1428 value.v_error = G_ERR_UNEXP_EOF;
1432 if (g_scanner_char_2_num (ch, 16) < 0)
1434 token = G_TOKEN_ERROR;
1435 value.v_error = G_ERR_DIGIT_RADIX;
1440 else if (config->scan_binary && (ch == 'b' || ch == 'B'))
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);
1447 token = G_TOKEN_ERROR;
1448 value.v_error = G_ERR_UNEXP_EOF;
1452 if (g_scanner_char_2_num (ch, 10) < 0)
1454 token = G_TOKEN_ERROR;
1455 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1474 gboolean in_number = TRUE;
1477 if (token == G_TOKEN_NONE)
1478 token = G_TOKEN_INT;
1480 gstring = g_string_new (dotted_float ? "0." : "");
1481 gstring = g_string_append_c (gstring, ch);
1483 do /* while (in_number) */
1487 is_E = token == G_TOKEN_FLOAT && (ch == 'e' || ch == 'E');
1489 ch = g_scanner_peek_next_char (scanner);
1491 if (g_scanner_char_2_num (ch, 36) >= 0 ||
1492 (config->scan_float && ch == '.') ||
1493 (is_E && (ch == '+' || ch == '-')))
1495 ch = g_scanner_get_char (scanner, line_p, position_p);
1500 if (token != G_TOKEN_INT && token != G_TOKEN_OCTAL)
1502 value.v_error = token == G_TOKEN_FLOAT ? G_ERR_FLOAT_MALFORMED : G_ERR_FLOAT_RADIX;
1503 token = G_TOKEN_ERROR;
1508 token = G_TOKEN_FLOAT;
1509 gstring = g_string_append_c (gstring, ch);
1523 gstring = g_string_append_c (gstring, ch);
1528 if (token != G_TOKEN_FLOAT)
1530 token = G_TOKEN_ERROR;
1531 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1535 gstring = g_string_append_c (gstring, ch);
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))
1546 token = G_TOKEN_ERROR;
1547 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1552 if (token != G_TOKEN_HEX)
1553 token = G_TOKEN_FLOAT;
1554 gstring = g_string_append_c (gstring, ch);
1559 if (token != G_TOKEN_HEX)
1561 token = G_TOKEN_ERROR;
1562 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1566 gstring = g_string_append_c (gstring, ch);
1576 if (token == G_TOKEN_FLOAT)
1577 value.v_float = g_strtod (gstring->str, &endptr);
1583 case G_TOKEN_BINARY:
1584 ui64 = g_ascii_strtoull (gstring->str, &endptr, 2);
1587 ui64 = g_ascii_strtoull (gstring->str, &endptr, 8);
1590 ui64 = g_ascii_strtoull (gstring->str, &endptr, 10);
1593 ui64 = g_ascii_strtoull (gstring->str, &endptr, 16);
1597 if (scanner->config->store_int64)
1598 value.v_int64 = ui64;
1602 if (endptr && *endptr)
1604 token = G_TOKEN_ERROR;
1605 if (*endptr == 'e' || *endptr == 'E')
1606 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1608 value.v_error = G_ERR_DIGIT_RADIX;
1610 g_string_free (gstring, TRUE);
1613 } /* number_parsing:... */
1619 if (config->cpair_comment_single &&
1620 ch == config->cpair_comment_single[0])
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);
1628 if (ch == config->cpair_comment_single[1])
1630 in_comment_single = FALSE;
1635 gstring = g_string_append_c (gstring, ch);
1636 ch = g_scanner_get_char (scanner, line_p, position_p);
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;
1643 else if (config->scan_identifier && ch &&
1644 strchr (config->cset_identifier_first, ch))
1646 identifier_precedence:
1648 if (config->cset_identifier_nth && ch &&
1649 strchr (config->cset_identifier_nth,
1650 g_scanner_peek_next_char (scanner)))
1652 token = G_TOKEN_IDENTIFIER;
1653 gstring = g_string_new (NULL);
1654 gstring = g_string_append_c (gstring, ch);
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);
1661 while (ch && strchr (config->cset_identifier_nth, ch));
1664 else if (config->scan_identifier_1char)
1666 token = G_TOKEN_IDENTIFIER;
1667 value.v_identifier = g_new0 (gchar, 2);
1668 value.v_identifier[0] = ch;
1674 if (config->char_2_token)
1678 token = G_TOKEN_CHAR;
1683 } /* default_case:... */
1686 g_assert (ch == 0 && token != G_TOKEN_NONE); /* paranoid */
1690 if (in_comment_multi || in_comment_single ||
1691 in_string_sq || in_string_dq)
1693 token = G_TOKEN_ERROR;
1696 g_string_free (gstring, TRUE);
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;
1708 value.v_string = g_string_free (gstring, FALSE);
1712 if (token == G_TOKEN_IDENTIFIER)
1714 if (config->scan_symbols)
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);
1726 g_free (value.v_identifier);
1727 token = G_TOKEN_SYMBOL;
1728 value.v_symbol = key->value;
1732 if (token == G_TOKEN_IDENTIFIER &&
1733 config->scan_identifier_NULL &&
1734 strlen (value.v_identifier) == 4)
1736 gchar *null_upper = "NULL";
1737 gchar *null_lower = "null";
1739 if (scanner->config->case_sensitive)
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;
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;
1766 #define __G_SCANNER_C__
1767 #include "galiasdef.c"