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/.
48 #include <io.h> /* For _read() */
52 #define to_lower(c) ( \
54 ( (((guchar)(c))>='A' && ((guchar)(c))<='Z') * ('a'-'A') ) | \
55 ( (((guchar)(c))>=192 && ((guchar)(c))<=214) * (224-192) ) | \
56 ( (((guchar)(c))>=216 && ((guchar)(c))<=222) * (248-216) ) | \
60 #define READ_BUFFER_SIZE (4000)
63 /* --- typedefs --- */
64 typedef struct _GScannerKey GScannerKey;
75 /* --- variables --- */
76 static GScannerConfig g_scanner_config_template =
80 ) /* cset_skip_characters */,
85 ) /* cset_identifier_first */,
93 ) /* cset_identifier_nth */,
94 ( "#\n" ) /* cpair_comment_single */,
96 FALSE /* case_sensitive */,
98 TRUE /* skip_comment_multi */,
99 TRUE /* skip_comment_single */,
100 TRUE /* scan_comment_multi */,
101 TRUE /* scan_identifier */,
102 FALSE /* scan_identifier_1char */,
103 FALSE /* scan_identifier_NULL */,
104 TRUE /* scan_symbols */,
105 FALSE /* scan_binary */,
106 TRUE /* scan_octal */,
107 TRUE /* scan_float */,
109 FALSE /* scan_hex_dollar */,
110 TRUE /* scan_string_sq */,
111 TRUE /* scan_string_dq */,
112 TRUE /* numbers_2_int */,
113 FALSE /* int_2_float */,
114 FALSE /* identifier_2_string */,
115 TRUE /* char_2_token */,
116 FALSE /* symbol_2_token */,
117 FALSE /* scope_0_fallback */,
121 /* --- prototypes --- */
123 GScannerKey* g_scanner_lookup_internal (GScanner *scanner,
125 const gchar *symbol);
126 static gboolean g_scanner_key_equal (gconstpointer v1,
128 static guint g_scanner_key_hash (gconstpointer v);
129 static void g_scanner_get_token_ll (GScanner *scanner,
131 GTokenValue *value_p,
134 static void g_scanner_get_token_i (GScanner *scanner,
136 GTokenValue *value_p,
140 static guchar g_scanner_peek_next_char (GScanner *scanner);
141 static guchar g_scanner_get_char (GScanner *scanner,
144 static void g_scanner_msg_handler (GScanner *scanner,
149 /* --- functions --- */
151 g_scanner_char_2_num (guchar c,
154 if (c >= '0' && c <= '9')
156 else if (c >= 'A' && c <= 'Z')
158 else if (c >= 'a' && c <= 'z')
170 g_scanner_new (const GScannerConfig *config_templ)
175 config_templ = &g_scanner_config_template;
177 scanner = g_new0 (GScanner, 1);
179 scanner->user_data = NULL;
180 scanner->max_parse_errors = 1;
181 scanner->parse_errors = 0;
182 scanner->input_name = NULL;
183 g_datalist_init (&scanner->qdata);
185 scanner->config = g_new0 (GScannerConfig, 1);
187 scanner->config->case_sensitive = config_templ->case_sensitive;
188 scanner->config->cset_skip_characters = config_templ->cset_skip_characters;
189 if (!scanner->config->cset_skip_characters)
190 scanner->config->cset_skip_characters = "";
191 scanner->config->cset_identifier_first = config_templ->cset_identifier_first;
192 scanner->config->cset_identifier_nth = config_templ->cset_identifier_nth;
193 scanner->config->cpair_comment_single = config_templ->cpair_comment_single;
194 scanner->config->skip_comment_multi = config_templ->skip_comment_multi;
195 scanner->config->skip_comment_single = config_templ->skip_comment_single;
196 scanner->config->scan_comment_multi = config_templ->scan_comment_multi;
197 scanner->config->scan_identifier = config_templ->scan_identifier;
198 scanner->config->scan_identifier_1char = config_templ->scan_identifier_1char;
199 scanner->config->scan_identifier_NULL = config_templ->scan_identifier_NULL;
200 scanner->config->scan_symbols = config_templ->scan_symbols;
201 scanner->config->scan_binary = config_templ->scan_binary;
202 scanner->config->scan_octal = config_templ->scan_octal;
203 scanner->config->scan_float = config_templ->scan_float;
204 scanner->config->scan_hex = config_templ->scan_hex;
205 scanner->config->scan_hex_dollar = config_templ->scan_hex_dollar;
206 scanner->config->scan_string_sq = config_templ->scan_string_sq;
207 scanner->config->scan_string_dq = config_templ->scan_string_dq;
208 scanner->config->numbers_2_int = config_templ->numbers_2_int;
209 scanner->config->int_2_float = config_templ->int_2_float;
210 scanner->config->identifier_2_string = config_templ->identifier_2_string;
211 scanner->config->char_2_token = config_templ->char_2_token;
212 scanner->config->symbol_2_token = config_templ->symbol_2_token;
213 scanner->config->scope_0_fallback = config_templ->scope_0_fallback;
215 scanner->token = G_TOKEN_NONE;
216 scanner->value.v_int = 0;
218 scanner->position = 0;
220 scanner->next_token = G_TOKEN_NONE;
221 scanner->next_value.v_int = 0;
222 scanner->next_line = 1;
223 scanner->next_position = 0;
225 scanner->symbol_table = g_hash_table_new (g_scanner_key_hash, g_scanner_key_equal);
226 scanner->input_fd = -1;
227 scanner->text = NULL;
228 scanner->text_end = NULL;
229 scanner->buffer = NULL;
230 scanner->scope_id = 0;
232 scanner->msg_handler = g_scanner_msg_handler;
238 g_scanner_free_value (GTokenType *token_p,
239 GTokenValue *value_p)
244 case G_TOKEN_IDENTIFIER:
245 case G_TOKEN_IDENTIFIER_NULL:
246 case G_TOKEN_COMMENT_SINGLE:
247 case G_TOKEN_COMMENT_MULTI:
248 g_free (value_p->v_string);
255 *token_p = G_TOKEN_NONE;
259 g_scanner_destroy_symbol_table_entry (gpointer _key,
263 GScannerKey *key = _key;
265 g_free (key->symbol);
270 g_scanner_destroy (GScanner *scanner)
272 g_return_if_fail (scanner != NULL);
274 g_datalist_clear (&scanner->qdata);
275 g_hash_table_foreach (scanner->symbol_table,
276 g_scanner_destroy_symbol_table_entry, NULL);
277 g_hash_table_destroy (scanner->symbol_table);
278 g_scanner_free_value (&scanner->token, &scanner->value);
279 g_scanner_free_value (&scanner->next_token, &scanner->next_value);
280 g_free (scanner->config);
281 g_free (scanner->buffer);
286 g_scanner_msg_handler (GScanner *scanner,
290 g_return_if_fail (scanner != NULL);
292 fprintf (stderr, "%s:%d: ",
293 scanner->input_name ? scanner->input_name : "<memory>",
296 fprintf (stderr, "error: ");
297 fprintf (stderr, "%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++)
367 h = (h << 5) - h + *c;
372 static inline GScannerKey*
373 g_scanner_lookup_internal (GScanner *scanner,
380 key.scope_id = scope_id;
382 if (!scanner->config->case_sensitive)
387 key.symbol = g_new (gchar, strlen (symbol) + 1);
388 for (d = key.symbol, c = symbol; *c; c++, d++)
391 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
396 key.symbol = (gchar*) symbol;
397 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
404 g_scanner_scope_add_symbol (GScanner *scanner,
411 g_return_if_fail (scanner != NULL);
412 g_return_if_fail (symbol != NULL);
414 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
418 key = g_new (GScannerKey, 1);
419 key->scope_id = scope_id;
420 key->symbol = g_strdup (symbol);
422 if (!scanner->config->case_sensitive)
433 g_hash_table_insert (scanner->symbol_table, key, key);
440 g_scanner_scope_remove_symbol (GScanner *scanner,
446 g_return_if_fail (scanner != NULL);
447 g_return_if_fail (symbol != NULL);
449 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
453 g_hash_table_remove (scanner->symbol_table, key);
454 g_free (key->symbol);
460 g_scanner_lookup_symbol (GScanner *scanner,
466 g_return_val_if_fail (scanner != NULL, NULL);
471 scope_id = scanner->scope_id;
472 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
473 if (!key && scope_id && scanner->config->scope_0_fallback)
474 key = g_scanner_lookup_internal (scanner, 0, symbol);
483 g_scanner_scope_lookup_symbol (GScanner *scanner,
489 g_return_val_if_fail (scanner != NULL, NULL);
494 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
503 g_scanner_set_scope (GScanner *scanner,
508 g_return_val_if_fail (scanner != NULL, 0);
510 old_scope_id = scanner->scope_id;
511 scanner->scope_id = scope_id;
517 g_scanner_foreach_internal (gpointer _key,
528 func = (GHFunc) d[0];
533 if (key->scope_id == *scope_id)
534 func (key->symbol, key->value, user_data);
538 g_scanner_scope_foreach_symbol (GScanner *scanner,
545 g_return_if_fail (scanner != NULL);
547 d[0] = (gpointer) func;
551 g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
555 g_scanner_peek_next_token (GScanner *scanner)
557 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
559 if (scanner->next_token == G_TOKEN_NONE)
561 scanner->next_line = scanner->line;
562 scanner->next_position = scanner->position;
563 g_scanner_get_token_i (scanner,
564 &scanner->next_token,
565 &scanner->next_value,
567 &scanner->next_position);
570 return scanner->next_token;
574 g_scanner_get_next_token (GScanner *scanner)
576 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
578 if (scanner->next_token != G_TOKEN_NONE)
580 g_scanner_free_value (&scanner->token, &scanner->value);
582 scanner->token = scanner->next_token;
583 scanner->value = scanner->next_value;
584 scanner->line = scanner->next_line;
585 scanner->position = scanner->next_position;
586 scanner->next_token = G_TOKEN_NONE;
589 g_scanner_get_token_i (scanner,
595 return scanner->token;
599 g_scanner_cur_token (GScanner *scanner)
601 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
603 return scanner->token;
607 g_scanner_cur_value (GScanner *scanner)
613 g_return_val_if_fail (scanner != NULL, v);
615 /* MSC isn't capable of handling return scanner->value; ? */
623 g_scanner_cur_line (GScanner *scanner)
625 g_return_val_if_fail (scanner != NULL, 0);
627 return scanner->line;
631 g_scanner_cur_position (GScanner *scanner)
633 g_return_val_if_fail (scanner != NULL, 0);
635 return scanner->position;
639 g_scanner_eof (GScanner *scanner)
641 g_return_val_if_fail (scanner != NULL, TRUE);
643 return scanner->token == G_TOKEN_EOF || scanner->token == G_TOKEN_ERROR;
647 g_scanner_input_file (GScanner *scanner,
650 g_return_if_fail (scanner != NULL);
651 g_return_if_fail (input_fd >= 0);
653 if (scanner->input_fd >= 0)
654 g_scanner_sync_file_offset (scanner);
656 scanner->token = G_TOKEN_NONE;
657 scanner->value.v_int = 0;
659 scanner->position = 0;
660 scanner->next_token = G_TOKEN_NONE;
662 scanner->input_fd = input_fd;
663 scanner->text = NULL;
664 scanner->text_end = NULL;
666 if (!scanner->buffer)
667 scanner->buffer = g_new (gchar, READ_BUFFER_SIZE + 1);
671 g_scanner_input_text (GScanner *scanner,
675 g_return_if_fail (scanner != NULL);
677 g_return_if_fail (text != NULL);
681 if (scanner->input_fd >= 0)
682 g_scanner_sync_file_offset (scanner);
684 scanner->token = G_TOKEN_NONE;
685 scanner->value.v_int = 0;
687 scanner->position = 0;
688 scanner->next_token = G_TOKEN_NONE;
690 scanner->input_fd = -1;
691 scanner->text = text;
692 scanner->text_end = text + text_len;
696 g_free (scanner->buffer);
697 scanner->buffer = NULL;
702 g_scanner_peek_next_char (GScanner *scanner)
704 if (scanner->text < scanner->text_end)
706 return *scanner->text;
708 else if (scanner->input_fd >= 0)
713 buffer = scanner->buffer;
716 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
718 while (count == -1 && (errno == EINTR || errno == EAGAIN));
722 scanner->input_fd = -1;
728 scanner->text = buffer;
729 scanner->text_end = buffer + count;
739 g_scanner_sync_file_offset (GScanner *scanner)
741 g_return_if_fail (scanner != NULL);
743 /* for file input, rewind the filedescriptor to the current
744 * buffer position and blow the file read ahead buffer. usefull for
745 * third party uses of our filedescriptor, which hooks onto the current
749 if (scanner->input_fd >= 0 && scanner->text_end > scanner->text)
753 buffered = scanner->text_end - scanner->text;
754 if (lseek (scanner->input_fd, - buffered, SEEK_CUR) >= 0)
756 /* we succeeded, blow our buffer's contents now */
757 scanner->text = NULL;
758 scanner->text_end = NULL;
766 g_scanner_get_char (GScanner *scanner,
772 if (scanner->text < scanner->text_end)
773 fchar = *(scanner->text++);
774 else if (scanner->input_fd >= 0)
779 buffer = scanner->buffer;
782 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
784 while (count == -1 && (errno == EINTR || errno == EAGAIN));
788 scanner->input_fd = -1;
793 scanner->text = buffer + 1;
794 scanner->text_end = buffer + count;
798 g_scanner_sync_file_offset (scanner);
799 scanner->text_end = scanner->text;
800 scanner->input_fd = -1;
821 g_scanner_unexp_token (GScanner *scanner,
822 GTokenType expected_token,
823 const gchar *identifier_spec,
824 const gchar *symbol_spec,
825 const gchar *symbol_name,
826 const gchar *message,
830 guint token_string_len;
831 gchar *expected_string;
832 guint expected_string_len;
833 gchar *message_prefix;
834 gboolean print_unexp;
835 void (*msg_handler) (GScanner*, const gchar*, ...);
837 g_return_if_fail (scanner != NULL);
840 msg_handler = g_scanner_error;
842 msg_handler = g_scanner_warn;
844 if (!identifier_spec)
845 identifier_spec = "identifier";
847 symbol_spec = "symbol";
849 token_string_len = 56;
850 token_string = g_new (gchar, token_string_len + 1);
851 expected_string_len = 64;
852 expected_string = g_new (gchar, expected_string_len + 1);
855 switch (scanner->token)
858 g_snprintf (token_string, token_string_len, "end of file");
862 if (scanner->token >= 1 && scanner->token <= 255)
864 if ((scanner->token >= ' ' && scanner->token <= '~') ||
865 strchr (scanner->config->cset_identifier_first, scanner->token) ||
866 strchr (scanner->config->cset_identifier_nth, scanner->token))
867 g_snprintf (token_string, token_string_len, "character `%c'", scanner->token);
869 g_snprintf (token_string, token_string_len, "character `\\%o'", scanner->token);
872 else if (!scanner->config->symbol_2_token)
874 g_snprintf (token_string, token_string_len, "(unknown) token <%d>", scanner->token);
879 if (expected_token == G_TOKEN_SYMBOL ||
880 (scanner->config->symbol_2_token &&
881 expected_token > G_TOKEN_LAST))
884 g_snprintf (token_string,
887 print_unexp ? "" : "invalid ",
891 g_snprintf (token_string,
894 print_unexp ? "" : "invalid ",
900 expected_token = G_TOKEN_NONE;
901 switch (scanner->value.v_error)
903 case G_ERR_UNEXP_EOF:
904 g_snprintf (token_string, token_string_len, "scanner: unexpected end of file");
907 case G_ERR_UNEXP_EOF_IN_STRING:
908 g_snprintf (token_string, token_string_len, "scanner: unterminated string constant");
911 case G_ERR_UNEXP_EOF_IN_COMMENT:
912 g_snprintf (token_string, token_string_len, "scanner: unterminated comment");
915 case G_ERR_NON_DIGIT_IN_CONST:
916 g_snprintf (token_string, token_string_len, "scanner: non digit in constant");
919 case G_ERR_FLOAT_RADIX:
920 g_snprintf (token_string, token_string_len, "scanner: invalid radix for floating constant");
923 case G_ERR_FLOAT_MALFORMED:
924 g_snprintf (token_string, token_string_len, "scanner: malformed floating constant");
927 case G_ERR_DIGIT_RADIX:
928 g_snprintf (token_string, token_string_len, "scanner: digit is beyond radix");
933 g_snprintf (token_string, token_string_len, "scanner: unknown error");
939 g_snprintf (token_string, token_string_len, "character `%c'", scanner->value.v_char);
942 case G_TOKEN_IDENTIFIER:
943 case G_TOKEN_IDENTIFIER_NULL:
944 if (expected_token == G_TOKEN_IDENTIFIER ||
945 expected_token == G_TOKEN_IDENTIFIER_NULL)
947 g_snprintf (token_string,
950 print_unexp ? "" : "invalid ",
952 scanner->token == G_TOKEN_IDENTIFIER ? scanner->value.v_string : "null");
959 g_snprintf (token_string, token_string_len, "number `%ld'", scanner->value.v_int);
963 g_snprintf (token_string, token_string_len, "number `%.3f'", scanner->value.v_float);
967 if (expected_token == G_TOKEN_STRING)
969 g_snprintf (token_string,
971 "%s%sstring constant \"%s\"",
972 print_unexp ? "" : "invalid ",
973 scanner->value.v_string[0] == 0 ? "empty " : "",
974 scanner->value.v_string);
975 token_string[token_string_len - 2] = '"';
976 token_string[token_string_len - 1] = 0;
979 case G_TOKEN_COMMENT_SINGLE:
980 case G_TOKEN_COMMENT_MULTI:
981 g_snprintf (token_string, token_string_len, "comment");
985 /* somehow the user's parsing code is screwed, there isn't much
986 * we can do about it.
987 * Note, a common case to trigger this is
988 * g_scanner_peek_next_token(); g_scanner_unexp_token();
989 * without an intermediate g_scanner_get_next_token().
991 g_assert_not_reached ();
996 switch (expected_token)
1001 g_snprintf (expected_string, expected_string_len, "end of file");
1004 if (expected_token >= 1 && expected_token <= 255)
1006 if ((expected_token >= ' ' && expected_token <= '~') ||
1007 strchr (scanner->config->cset_identifier_first, expected_token) ||
1008 strchr (scanner->config->cset_identifier_nth, expected_token))
1009 g_snprintf (expected_string, expected_string_len, "character `%c'", expected_token);
1011 g_snprintf (expected_string, expected_string_len, "character `\\%o'", expected_token);
1014 else if (!scanner->config->symbol_2_token)
1016 g_snprintf (expected_string, expected_string_len, "(unknown) token <%d>", expected_token);
1020 case G_TOKEN_SYMBOL:
1021 need_valid = (scanner->token == G_TOKEN_SYMBOL ||
1022 (scanner->config->symbol_2_token &&
1023 scanner->token > G_TOKEN_LAST));
1024 g_snprintf (expected_string,
1025 expected_string_len,
1027 need_valid ? "valid " : "",
1029 /* FIXME: should we attempt to lookup the symbol_name for symbol_2_token? */
1032 g_snprintf (expected_string, expected_string_len, "%scharacter",
1033 scanner->token == G_TOKEN_CHAR ? "valid " : "");
1035 case G_TOKEN_BINARY:
1037 g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1038 scanner->token == expected_token ? "valid " : "", tstring);
1042 g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1043 scanner->token == expected_token ? "valid " : "", tstring);
1046 tstring = "integer";
1047 g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1048 scanner->token == expected_token ? "valid " : "", tstring);
1051 tstring = "hexadecimal";
1052 g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1053 scanner->token == expected_token ? "valid " : "", tstring);
1057 g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1058 scanner->token == expected_token ? "valid " : "", tstring);
1060 case G_TOKEN_STRING:
1061 g_snprintf (expected_string,
1062 expected_string_len,
1063 "%sstring constant",
1064 scanner->token == G_TOKEN_STRING ? "valid " : "");
1066 case G_TOKEN_IDENTIFIER:
1067 case G_TOKEN_IDENTIFIER_NULL:
1068 need_valid = (scanner->token == G_TOKEN_IDENTIFIER_NULL ||
1069 scanner->token == G_TOKEN_IDENTIFIER);
1070 g_snprintf (expected_string,
1071 expected_string_len,
1073 need_valid ? "valid " : "",
1076 case G_TOKEN_COMMENT_SINGLE:
1077 tstring = "single-line";
1078 g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1079 scanner->token == expected_token ? "valid " : "", tstring);
1081 case G_TOKEN_COMMENT_MULTI:
1082 tstring = "multi-line";
1083 g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1084 scanner->token == expected_token ? "valid " : "", tstring);
1088 /* this is handled upon printout */
1092 if (message && message[0] != 0)
1093 message_prefix = " - ";
1096 message_prefix = "";
1099 if (expected_token == G_TOKEN_ERROR)
1101 msg_handler (scanner,
1102 "failure around %s%s%s",
1107 else if (expected_token == G_TOKEN_NONE)
1110 msg_handler (scanner,
1111 "unexpected %s%s%s",
1116 msg_handler (scanner,
1125 msg_handler (scanner,
1126 "unexpected %s, expected %s%s%s",
1132 msg_handler (scanner,
1133 "%s, expected %s%s%s",
1140 g_free (token_string);
1141 g_free (expected_string);
1145 g_scanner_get_token_i (GScanner *scanner,
1146 GTokenType *token_p,
1147 GTokenValue *value_p,
1153 g_scanner_free_value (token_p, value_p);
1154 g_scanner_get_token_ll (scanner, token_p, value_p, line_p, position_p);
1156 while (((*token_p > 0 && *token_p < 256) &&
1157 strchr (scanner->config->cset_skip_characters, *token_p)) ||
1158 (*token_p == G_TOKEN_CHAR &&
1159 strchr (scanner->config->cset_skip_characters, value_p->v_char)) ||
1160 (*token_p == G_TOKEN_COMMENT_MULTI &&
1161 scanner->config->skip_comment_multi) ||
1162 (*token_p == G_TOKEN_COMMENT_SINGLE &&
1163 scanner->config->skip_comment_single));
1167 case G_TOKEN_IDENTIFIER:
1168 if (scanner->config->identifier_2_string)
1169 *token_p = G_TOKEN_STRING;
1172 case G_TOKEN_SYMBOL:
1173 if (scanner->config->symbol_2_token)
1174 *token_p = (GTokenType) value_p->v_symbol;
1177 case G_TOKEN_BINARY:
1180 if (scanner->config->numbers_2_int)
1181 *token_p = G_TOKEN_INT;
1188 if (*token_p == G_TOKEN_INT &&
1189 scanner->config->int_2_float)
1191 *token_p = G_TOKEN_FLOAT;
1192 value_p->v_float = value_p->v_int;
1199 g_scanner_get_token_ll (GScanner *scanner,
1200 GTokenType *token_p,
1201 GTokenValue *value_p,
1205 GScannerConfig *config;
1207 gboolean in_comment_multi;
1208 gboolean in_comment_single;
1209 gboolean in_string_sq;
1210 gboolean in_string_dq;
1215 config = scanner->config;
1216 (*value_p).v_int = 0;
1218 if ((scanner->text >= scanner->text_end && scanner->input_fd < 0) ||
1219 scanner->token == G_TOKEN_EOF)
1221 *token_p = G_TOKEN_EOF;
1225 in_comment_multi = FALSE;
1226 in_comment_single = FALSE;
1227 in_string_sq = FALSE;
1228 in_string_dq = FALSE;
1231 do /* while (ch != 0) */
1233 gboolean dotted_float = FALSE;
1235 ch = g_scanner_get_char (scanner, line_p, position_p);
1238 token = G_TOKEN_NONE;
1240 /* this is *evil*, but needed ;(
1241 * we first check for identifier first character, because it
1242 * might interfere with other key chars like slashes or numbers
1244 if (config->scan_identifier &&
1245 ch && strchr (config->cset_identifier_first, ch))
1246 goto identifier_precedence;
1251 token = G_TOKEN_EOF;
1257 if (!config->scan_comment_multi ||
1258 g_scanner_peek_next_char (scanner) != '*')
1260 g_scanner_get_char (scanner, line_p, position_p);
1261 token = G_TOKEN_COMMENT_MULTI;
1262 in_comment_multi = TRUE;
1263 gstring = g_string_new ("");
1264 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1266 if (ch == '*' && g_scanner_peek_next_char (scanner) == '/')
1268 g_scanner_get_char (scanner, line_p, position_p);
1269 in_comment_multi = FALSE;
1273 gstring = g_string_append_c (gstring, ch);
1279 if (!config->scan_string_sq)
1281 token = G_TOKEN_STRING;
1282 in_string_sq = TRUE;
1283 gstring = g_string_new ("");
1284 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1288 in_string_sq = FALSE;
1292 gstring = g_string_append_c (gstring, ch);
1298 if (!config->scan_string_dq)
1300 token = G_TOKEN_STRING;
1301 in_string_dq = TRUE;
1302 gstring = g_string_new ("");
1303 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1307 in_string_dq = FALSE;
1314 ch = g_scanner_get_char (scanner, line_p, position_p);
1324 gstring = g_string_append_c (gstring, '\\');
1328 gstring = g_string_append_c (gstring, '\n');
1332 gstring = g_string_append_c (gstring, '\t');
1336 gstring = g_string_append_c (gstring, '\r');
1340 gstring = g_string_append_c (gstring, '\b');
1344 gstring = g_string_append_c (gstring, '\f');
1356 fchar = g_scanner_peek_next_char (scanner);
1357 if (fchar >= '0' && fchar <= '7')
1359 ch = g_scanner_get_char (scanner, line_p, position_p);
1360 i = i * 8 + ch - '0';
1361 fchar = g_scanner_peek_next_char (scanner);
1362 if (fchar >= '0' && fchar <= '7')
1364 ch = g_scanner_get_char (scanner, line_p, position_p);
1365 i = i * 8 + ch - '0';
1368 gstring = g_string_append_c (gstring, i);
1372 gstring = g_string_append_c (gstring, ch);
1377 gstring = g_string_append_c (gstring, ch);
1384 if (!config->scan_float)
1386 token = G_TOKEN_FLOAT;
1387 dotted_float = TRUE;
1388 ch = g_scanner_get_char (scanner, line_p, position_p);
1389 goto number_parsing;
1392 if (!config->scan_hex_dollar)
1394 token = G_TOKEN_HEX;
1395 ch = g_scanner_get_char (scanner, line_p, position_p);
1396 goto number_parsing;
1399 if (config->scan_octal)
1400 token = G_TOKEN_OCTAL;
1402 token = G_TOKEN_INT;
1403 ch = g_scanner_peek_next_char (scanner);
1404 if (config->scan_hex && (ch == 'x' || ch == 'X'))
1406 token = G_TOKEN_HEX;
1407 g_scanner_get_char (scanner, line_p, position_p);
1408 ch = g_scanner_get_char (scanner, line_p, position_p);
1411 token = G_TOKEN_ERROR;
1412 value.v_error = G_ERR_UNEXP_EOF;
1416 if (g_scanner_char_2_num (ch, 16) < 0)
1418 token = G_TOKEN_ERROR;
1419 value.v_error = G_ERR_DIGIT_RADIX;
1424 else if (config->scan_binary && (ch == 'b' || ch == 'B'))
1426 token = G_TOKEN_BINARY;
1427 g_scanner_get_char (scanner, line_p, position_p);
1428 ch = g_scanner_get_char (scanner, line_p, position_p);
1431 token = G_TOKEN_ERROR;
1432 value.v_error = G_ERR_UNEXP_EOF;
1436 if (g_scanner_char_2_num (ch, 10) < 0)
1438 token = G_TOKEN_ERROR;
1439 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1458 gboolean in_number = TRUE;
1461 if (token == G_TOKEN_NONE)
1462 token = G_TOKEN_INT;
1464 gstring = g_string_new (dotted_float ? "0." : "");
1465 gstring = g_string_append_c (gstring, ch);
1467 do /* while (in_number) */
1471 is_E = token == G_TOKEN_FLOAT && (ch == 'e' || ch == 'E');
1473 ch = g_scanner_peek_next_char (scanner);
1475 if (g_scanner_char_2_num (ch, 36) >= 0 ||
1476 (config->scan_float && ch == '.') ||
1477 (is_E && (ch == '+' || ch == '-')))
1479 ch = g_scanner_get_char (scanner, line_p, position_p);
1484 if (token != G_TOKEN_INT && token != G_TOKEN_OCTAL)
1486 value.v_error = token == G_TOKEN_FLOAT ? G_ERR_FLOAT_MALFORMED : G_ERR_FLOAT_RADIX;
1487 token = G_TOKEN_ERROR;
1492 token = G_TOKEN_FLOAT;
1493 gstring = g_string_append_c (gstring, ch);
1507 gstring = g_string_append_c (gstring, ch);
1512 if (token != G_TOKEN_FLOAT)
1514 token = G_TOKEN_ERROR;
1515 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1519 gstring = g_string_append_c (gstring, ch);
1524 if ((token != G_TOKEN_HEX && !config->scan_float) ||
1525 (token != G_TOKEN_HEX &&
1526 token != G_TOKEN_OCTAL &&
1527 token != G_TOKEN_FLOAT &&
1528 token != G_TOKEN_INT))
1530 token = G_TOKEN_ERROR;
1531 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1536 if (token != G_TOKEN_HEX)
1537 token = G_TOKEN_FLOAT;
1538 gstring = g_string_append_c (gstring, ch);
1543 if (token != G_TOKEN_HEX)
1545 token = G_TOKEN_ERROR;
1546 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1550 gstring = g_string_append_c (gstring, ch);
1562 case G_TOKEN_BINARY:
1563 value.v_binary = strtol (gstring->str, &endptr, 2);
1567 value.v_octal = strtol (gstring->str, &endptr, 8);
1571 value.v_int = strtol (gstring->str, &endptr, 10);
1575 value.v_float = g_strtod (gstring->str, &endptr);
1579 value.v_hex = strtol (gstring->str, &endptr, 16);
1585 if (endptr && *endptr)
1587 token = G_TOKEN_ERROR;
1588 if (*endptr == 'e' || *endptr == 'E')
1589 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1591 value.v_error = G_ERR_DIGIT_RADIX;
1593 g_string_free (gstring, TRUE);
1596 } /* number_parsing:... */
1602 if (config->cpair_comment_single &&
1603 ch == config->cpair_comment_single[0])
1605 token = G_TOKEN_COMMENT_SINGLE;
1606 in_comment_single = TRUE;
1607 gstring = g_string_new ("");
1608 ch = g_scanner_get_char (scanner, line_p, position_p);
1611 if (ch == config->cpair_comment_single[1])
1613 in_comment_single = FALSE;
1618 gstring = g_string_append_c (gstring, ch);
1619 ch = g_scanner_get_char (scanner, line_p, position_p);
1622 else if (config->scan_identifier && ch &&
1623 strchr (config->cset_identifier_first, ch))
1625 identifier_precedence:
1627 if (config->cset_identifier_nth && ch &&
1628 strchr (config->cset_identifier_nth,
1629 g_scanner_peek_next_char (scanner)))
1631 token = G_TOKEN_IDENTIFIER;
1632 gstring = g_string_new ("");
1633 gstring = g_string_append_c (gstring, ch);
1636 ch = g_scanner_get_char (scanner, line_p, position_p);
1637 gstring = g_string_append_c (gstring, ch);
1638 ch = g_scanner_peek_next_char (scanner);
1640 while (ch && strchr (config->cset_identifier_nth, ch));
1643 else if (config->scan_identifier_1char)
1645 token = G_TOKEN_IDENTIFIER;
1646 value.v_identifier = g_new0 (gchar, 2);
1647 value.v_identifier[0] = ch;
1653 if (config->char_2_token)
1657 token = G_TOKEN_CHAR;
1662 } /* default_case:... */
1665 g_assert (ch == 0 && token != G_TOKEN_NONE); /* paranoid */
1669 if (in_comment_multi || in_comment_single ||
1670 in_string_sq || in_string_dq)
1672 token = G_TOKEN_ERROR;
1675 g_string_free (gstring, TRUE);
1679 if (in_comment_multi || in_comment_single)
1680 value.v_error = G_ERR_UNEXP_EOF_IN_COMMENT;
1681 else /* (in_string_sq || in_string_dq) */
1682 value.v_error = G_ERR_UNEXP_EOF_IN_STRING;
1687 value.v_string = gstring->str;
1688 g_string_free (gstring, FALSE);
1692 if (token == G_TOKEN_IDENTIFIER)
1694 if (config->scan_symbols)
1699 scope_id = scanner->scope_id;
1700 key = g_scanner_lookup_internal (scanner, scope_id, value.v_identifier);
1701 if (!key && scope_id && scanner->config->scope_0_fallback)
1702 key = g_scanner_lookup_internal (scanner, 0, value.v_identifier);
1706 g_free (value.v_identifier);
1707 token = G_TOKEN_SYMBOL;
1708 value.v_symbol = key->value;
1712 if (token == G_TOKEN_IDENTIFIER &&
1713 config->scan_identifier_NULL &&
1714 strlen (value.v_identifier) == 4)
1716 gchar *null_upper = "NULL";
1717 gchar *null_lower = "null";
1719 if (scanner->config->case_sensitive)
1721 if (value.v_identifier[0] == null_upper[0] &&
1722 value.v_identifier[1] == null_upper[1] &&
1723 value.v_identifier[2] == null_upper[2] &&
1724 value.v_identifier[3] == null_upper[3])
1725 token = G_TOKEN_IDENTIFIER_NULL;
1729 if ((value.v_identifier[0] == null_upper[0] ||
1730 value.v_identifier[0] == null_lower[0]) &&
1731 (value.v_identifier[1] == null_upper[1] ||
1732 value.v_identifier[1] == null_lower[1]) &&
1733 (value.v_identifier[2] == null_upper[2] ||
1734 value.v_identifier[2] == null_lower[2]) &&
1735 (value.v_identifier[3] == null_upper[3] ||
1736 value.v_identifier[3] == null_lower[3]))
1737 token = G_TOKEN_IDENTIFIER_NULL;