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 = 0;
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: ", scanner->input_name, scanner->line);
294 fprintf (stderr, "error: ");
295 fprintf (stderr, "%s\n", message);
299 g_scanner_error (GScanner *scanner,
303 g_return_if_fail (scanner != NULL);
304 g_return_if_fail (format != NULL);
306 scanner->parse_errors++;
308 if (scanner->msg_handler)
313 va_start (args, format);
314 string = g_strdup_vprintf (format, args);
317 scanner->msg_handler (scanner, string, TRUE);
324 g_scanner_warn (GScanner *scanner,
328 g_return_if_fail (scanner != NULL);
329 g_return_if_fail (format != NULL);
331 if (scanner->msg_handler)
336 va_start (args, format);
337 string = g_strdup_vprintf (format, args);
340 scanner->msg_handler (scanner, string, FALSE);
347 g_scanner_key_equal (gconstpointer v1,
350 const GScannerKey *key1 = v1;
351 const GScannerKey *key2 = v2;
353 return (key1->scope_id == key2->scope_id) && (strcmp (key1->symbol, key2->symbol) == 0);
357 g_scanner_key_hash (gconstpointer v)
359 const GScannerKey *key = v;
364 for (c = key->symbol; *c; c++)
365 h = (h << 5) - h + *c;
370 static inline GScannerKey*
371 g_scanner_lookup_internal (GScanner *scanner,
378 key.scope_id = scope_id;
380 if (!scanner->config->case_sensitive)
385 key.symbol = g_new (gchar, strlen (symbol) + 1);
386 for (d = key.symbol, c = symbol; *c; c++, d++)
389 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
394 key.symbol = (gchar*) symbol;
395 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
402 g_scanner_scope_add_symbol (GScanner *scanner,
409 g_return_if_fail (scanner != NULL);
410 g_return_if_fail (symbol != NULL);
412 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
416 key = g_new (GScannerKey, 1);
417 key->scope_id = scope_id;
418 key->symbol = g_strdup (symbol);
420 if (!scanner->config->case_sensitive)
431 g_hash_table_insert (scanner->symbol_table, key, key);
438 g_scanner_scope_remove_symbol (GScanner *scanner,
444 g_return_if_fail (scanner != NULL);
445 g_return_if_fail (symbol != NULL);
447 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
451 g_hash_table_remove (scanner->symbol_table, key);
452 g_free (key->symbol);
458 g_scanner_lookup_symbol (GScanner *scanner,
464 g_return_val_if_fail (scanner != NULL, NULL);
469 scope_id = scanner->scope_id;
470 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
471 if (!key && scope_id && scanner->config->scope_0_fallback)
472 key = g_scanner_lookup_internal (scanner, 0, symbol);
481 g_scanner_scope_lookup_symbol (GScanner *scanner,
487 g_return_val_if_fail (scanner != NULL, NULL);
492 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
501 g_scanner_set_scope (GScanner *scanner,
506 g_return_val_if_fail (scanner != NULL, 0);
508 old_scope_id = scanner->scope_id;
509 scanner->scope_id = scope_id;
515 g_scanner_foreach_internal (gpointer _key,
526 func = (GHFunc) d[0];
531 if (key->scope_id == *scope_id)
532 func (key->symbol, key->value, user_data);
536 g_scanner_scope_foreach_symbol (GScanner *scanner,
543 g_return_if_fail (scanner != NULL);
545 d[0] = (gpointer) func;
549 g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
553 g_scanner_peek_next_token (GScanner *scanner)
555 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
557 if (scanner->next_token == G_TOKEN_NONE)
559 scanner->next_line = scanner->line;
560 scanner->next_position = scanner->position;
561 g_scanner_get_token_i (scanner,
562 &scanner->next_token,
563 &scanner->next_value,
565 &scanner->next_position);
568 return scanner->next_token;
572 g_scanner_get_next_token (GScanner *scanner)
574 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
576 if (scanner->next_token != G_TOKEN_NONE)
578 g_scanner_free_value (&scanner->token, &scanner->value);
580 scanner->token = scanner->next_token;
581 scanner->value = scanner->next_value;
582 scanner->line = scanner->next_line;
583 scanner->position = scanner->next_position;
584 scanner->next_token = G_TOKEN_NONE;
587 g_scanner_get_token_i (scanner,
593 return scanner->token;
597 g_scanner_cur_token (GScanner *scanner)
599 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
601 return scanner->token;
605 g_scanner_cur_value (GScanner *scanner)
611 g_return_val_if_fail (scanner != NULL, v);
613 /* MSC isn't capable of handling return scanner->value; ? */
621 g_scanner_cur_line (GScanner *scanner)
623 g_return_val_if_fail (scanner != NULL, 0);
625 return scanner->line;
629 g_scanner_cur_position (GScanner *scanner)
631 g_return_val_if_fail (scanner != NULL, 0);
633 return scanner->position;
637 g_scanner_eof (GScanner *scanner)
639 g_return_val_if_fail (scanner != NULL, TRUE);
641 return scanner->token == G_TOKEN_EOF || scanner->token == G_TOKEN_ERROR;
645 g_scanner_input_file (GScanner *scanner,
648 g_return_if_fail (scanner != NULL);
649 g_return_if_fail (input_fd >= 0);
651 if (scanner->input_fd >= 0)
652 g_scanner_sync_file_offset (scanner);
654 scanner->token = G_TOKEN_NONE;
655 scanner->value.v_int = 0;
657 scanner->position = 0;
658 scanner->next_token = G_TOKEN_NONE;
660 scanner->input_fd = input_fd;
661 scanner->text = NULL;
662 scanner->text_end = NULL;
664 if (!scanner->buffer)
665 scanner->buffer = g_new (gchar, READ_BUFFER_SIZE + 1);
669 g_scanner_input_text (GScanner *scanner,
673 g_return_if_fail (scanner != NULL);
675 g_return_if_fail (text != NULL);
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 = -1;
689 scanner->text = text;
690 scanner->text_end = text + text_len;
694 g_free (scanner->buffer);
695 scanner->buffer = NULL;
700 g_scanner_peek_next_char (GScanner *scanner)
702 if (scanner->text < scanner->text_end)
704 return *scanner->text;
706 else if (scanner->input_fd >= 0)
711 buffer = scanner->buffer;
714 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
716 while (count == -1 && (errno == EINTR || errno == EAGAIN));
720 scanner->input_fd = -1;
726 scanner->text = buffer;
727 scanner->text_end = buffer + count;
737 g_scanner_sync_file_offset (GScanner *scanner)
739 g_return_if_fail (scanner != NULL);
741 /* for file input, rewind the filedescriptor to the current
742 * buffer position and blow the file read ahead buffer. usefull for
743 * third party uses of our filedescriptor, which hooks onto the current
747 if (scanner->input_fd >= 0 && scanner->text_end > scanner->text)
751 buffered = scanner->text_end - scanner->text;
752 if (lseek (scanner->input_fd, - buffered, SEEK_CUR) >= 0)
754 /* we succeeded, blow our buffer's contents now */
755 scanner->text = NULL;
756 scanner->text_end = NULL;
764 g_scanner_get_char (GScanner *scanner,
770 if (scanner->text < scanner->text_end)
771 fchar = *(scanner->text++);
772 else if (scanner->input_fd >= 0)
777 buffer = scanner->buffer;
780 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
782 while (count == -1 && (errno == EINTR || errno == EAGAIN));
786 scanner->input_fd = -1;
791 scanner->text = buffer + 1;
792 scanner->text_end = buffer + count;
796 g_scanner_sync_file_offset (scanner);
797 scanner->text_end = scanner->text;
798 scanner->input_fd = -1;
819 g_scanner_unexp_token (GScanner *scanner,
820 GTokenType expected_token,
821 const gchar *identifier_spec,
822 const gchar *symbol_spec,
823 const gchar *symbol_name,
824 const gchar *message,
828 guint token_string_len;
829 gchar *expected_string;
830 guint expected_string_len;
831 gchar *message_prefix;
832 gboolean print_unexp;
833 void (*msg_handler) (GScanner*, const gchar*, ...);
835 g_return_if_fail (scanner != NULL);
838 msg_handler = g_scanner_error;
840 msg_handler = g_scanner_warn;
842 if (!identifier_spec)
843 identifier_spec = "identifier";
845 symbol_spec = "symbol";
847 token_string_len = 56;
848 token_string = g_new (gchar, token_string_len + 1);
849 expected_string_len = 64;
850 expected_string = g_new (gchar, expected_string_len + 1);
853 switch (scanner->token)
856 g_snprintf (token_string, token_string_len, "end of file");
860 if (scanner->token >= 1 && scanner->token <= 255)
862 if ((scanner->token >= ' ' && scanner->token <= '~') ||
863 strchr (scanner->config->cset_identifier_first, scanner->token) ||
864 strchr (scanner->config->cset_identifier_nth, scanner->token))
865 g_snprintf (token_string, token_string_len, "character `%c'", scanner->token);
867 g_snprintf (token_string, token_string_len, "character `\\%o'", scanner->token);
870 else if (!scanner->config->symbol_2_token)
872 g_snprintf (token_string, token_string_len, "(unknown) token <%d>", scanner->token);
877 if (expected_token == G_TOKEN_SYMBOL ||
878 (scanner->config->symbol_2_token &&
879 expected_token > G_TOKEN_LAST))
882 g_snprintf (token_string,
885 print_unexp ? "" : "invalid ",
889 g_snprintf (token_string,
892 print_unexp ? "" : "invalid ",
898 expected_token = G_TOKEN_NONE;
899 switch (scanner->value.v_error)
901 case G_ERR_UNEXP_EOF:
902 g_snprintf (token_string, token_string_len, "scanner: unexpected end of file");
905 case G_ERR_UNEXP_EOF_IN_STRING:
906 g_snprintf (token_string, token_string_len, "scanner: unterminated string constant");
909 case G_ERR_UNEXP_EOF_IN_COMMENT:
910 g_snprintf (token_string, token_string_len, "scanner: unterminated comment");
913 case G_ERR_NON_DIGIT_IN_CONST:
914 g_snprintf (token_string, token_string_len, "scanner: non digit in constant");
917 case G_ERR_FLOAT_RADIX:
918 g_snprintf (token_string, token_string_len, "scanner: invalid radix for floating constant");
921 case G_ERR_FLOAT_MALFORMED:
922 g_snprintf (token_string, token_string_len, "scanner: malformed floating constant");
925 case G_ERR_DIGIT_RADIX:
926 g_snprintf (token_string, token_string_len, "scanner: digit is beyond radix");
931 g_snprintf (token_string, token_string_len, "scanner: unknown error");
937 g_snprintf (token_string, token_string_len, "character `%c'", scanner->value.v_char);
940 case G_TOKEN_IDENTIFIER:
941 case G_TOKEN_IDENTIFIER_NULL:
942 if (expected_token == G_TOKEN_IDENTIFIER ||
943 expected_token == G_TOKEN_IDENTIFIER_NULL)
945 g_snprintf (token_string,
948 print_unexp ? "" : "invalid ",
950 scanner->token == G_TOKEN_IDENTIFIER ? scanner->value.v_string : "null");
957 g_snprintf (token_string, token_string_len, "number `%ld'", scanner->value.v_int);
961 g_snprintf (token_string, token_string_len, "number `%.3f'", scanner->value.v_float);
965 if (expected_token == G_TOKEN_STRING)
967 g_snprintf (token_string,
969 "%s%sstring constant \"%s\"",
970 print_unexp ? "" : "invalid ",
971 scanner->value.v_string[0] == 0 ? "empty " : "",
972 scanner->value.v_string);
973 token_string[token_string_len - 2] = '"';
974 token_string[token_string_len - 1] = 0;
977 case G_TOKEN_COMMENT_SINGLE:
978 case G_TOKEN_COMMENT_MULTI:
979 g_snprintf (token_string, token_string_len, "comment");
983 /* somehow the user's parsing code is screwed, there isn't much
984 * we can do about it.
985 * Note, a common case to trigger this is
986 * g_scanner_peek_next_token(); g_scanner_unexp_token();
987 * without an intermediate g_scanner_get_next_token().
989 g_assert_not_reached ();
994 switch (expected_token)
999 if (expected_token >= 1 && expected_token <= 255)
1001 if ((expected_token >= ' ' && expected_token <= '~') ||
1002 strchr (scanner->config->cset_identifier_first, expected_token) ||
1003 strchr (scanner->config->cset_identifier_nth, expected_token))
1004 g_snprintf (expected_string, expected_string_len, "character `%c'", expected_token);
1006 g_snprintf (expected_string, expected_string_len, "character `\\%o'", expected_token);
1009 else if (!scanner->config->symbol_2_token)
1011 g_snprintf (expected_string, expected_string_len, "(unknown) token <%d>", expected_token);
1015 case G_TOKEN_SYMBOL:
1016 need_valid = (scanner->token == G_TOKEN_SYMBOL ||
1017 (scanner->config->symbol_2_token &&
1018 scanner->token > G_TOKEN_LAST));
1019 g_snprintf (expected_string,
1020 expected_string_len,
1022 need_valid ? "valid " : "",
1024 /* FIXME: should we attempt to lookup the symbol_name for symbol_2_token? */
1028 g_snprintf (expected_string, expected_string_len, "%snumber (integer)",
1029 scanner->token == G_TOKEN_INT ? "valid " : "");
1033 g_snprintf (expected_string, expected_string_len, "%snumber (float)",
1034 scanner->token == G_TOKEN_FLOAT ? "valid " : "");
1037 case G_TOKEN_STRING:
1038 g_snprintf (expected_string,
1039 expected_string_len,
1040 "%sstring constant",
1041 scanner->token == G_TOKEN_STRING ? "valid " : "");
1044 case G_TOKEN_IDENTIFIER:
1045 case G_TOKEN_IDENTIFIER_NULL:
1046 g_snprintf (expected_string,
1047 expected_string_len,
1049 (scanner->token == G_TOKEN_IDENTIFIER_NULL ||
1050 scanner->token == G_TOKEN_IDENTIFIER ? "valid " : ""),
1055 g_snprintf (expected_string, expected_string_len, "end of file");
1062 if (message && message[0] != 0)
1063 message_prefix = " - ";
1066 message_prefix = "";
1070 if (expected_token != G_TOKEN_NONE)
1073 msg_handler (scanner,
1074 "unexpected %s, expected %s%s%s",
1080 msg_handler (scanner,
1081 "%s, expected %s%s%s",
1090 msg_handler (scanner,
1091 "unexpected %s%s%s",
1096 msg_handler (scanner,
1103 g_free (token_string);
1104 g_free (expected_string);
1108 g_scanner_get_token_i (GScanner *scanner,
1109 GTokenType *token_p,
1110 GTokenValue *value_p,
1116 g_scanner_free_value (token_p, value_p);
1117 g_scanner_get_token_ll (scanner, token_p, value_p, line_p, position_p);
1119 while (((*token_p > 0 && *token_p < 256) &&
1120 strchr (scanner->config->cset_skip_characters, *token_p)) ||
1121 (*token_p == G_TOKEN_CHAR &&
1122 strchr (scanner->config->cset_skip_characters, value_p->v_char)) ||
1123 (*token_p == G_TOKEN_COMMENT_MULTI &&
1124 scanner->config->skip_comment_multi) ||
1125 (*token_p == G_TOKEN_COMMENT_SINGLE &&
1126 scanner->config->skip_comment_single));
1130 case G_TOKEN_IDENTIFIER:
1131 if (scanner->config->identifier_2_string)
1132 *token_p = G_TOKEN_STRING;
1135 case G_TOKEN_SYMBOL:
1136 if (scanner->config->symbol_2_token)
1137 *token_p = (GTokenType) value_p->v_symbol;
1140 case G_TOKEN_BINARY:
1143 if (scanner->config->numbers_2_int)
1144 *token_p = G_TOKEN_INT;
1151 if (*token_p == G_TOKEN_INT &&
1152 scanner->config->int_2_float)
1154 *token_p = G_TOKEN_FLOAT;
1155 value_p->v_float = value_p->v_int;
1162 g_scanner_get_token_ll (GScanner *scanner,
1163 GTokenType *token_p,
1164 GTokenValue *value_p,
1168 GScannerConfig *config;
1170 gboolean in_comment_multi;
1171 gboolean in_comment_single;
1172 gboolean in_string_sq;
1173 gboolean in_string_dq;
1178 config = scanner->config;
1179 (*value_p).v_int = 0;
1181 if ((scanner->text >= scanner->text_end && scanner->input_fd < 0) ||
1182 scanner->token == G_TOKEN_EOF)
1184 *token_p = G_TOKEN_EOF;
1188 in_comment_multi = FALSE;
1189 in_comment_single = FALSE;
1190 in_string_sq = FALSE;
1191 in_string_dq = FALSE;
1194 do /* while (ch != 0) */
1196 gboolean dotted_float = FALSE;
1198 ch = g_scanner_get_char (scanner, line_p, position_p);
1201 token = G_TOKEN_NONE;
1203 /* this is *evil*, but needed ;(
1204 * we first check for identifier first character, because it
1205 * might interfere with other key chars like slashes or numbers
1207 if (config->scan_identifier &&
1208 ch && strchr (config->cset_identifier_first, ch))
1209 goto identifier_precedence;
1214 token = G_TOKEN_EOF;
1220 if (!config->scan_comment_multi ||
1221 g_scanner_peek_next_char (scanner) != '*')
1223 g_scanner_get_char (scanner, line_p, position_p);
1224 token = G_TOKEN_COMMENT_MULTI;
1225 in_comment_multi = TRUE;
1226 gstring = g_string_new ("");
1227 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1229 if (ch == '*' && g_scanner_peek_next_char (scanner) == '/')
1231 g_scanner_get_char (scanner, line_p, position_p);
1232 in_comment_multi = FALSE;
1236 gstring = g_string_append_c (gstring, ch);
1242 if (!config->scan_string_sq)
1244 token = G_TOKEN_STRING;
1245 in_string_sq = TRUE;
1246 gstring = g_string_new ("");
1247 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1251 in_string_sq = FALSE;
1255 gstring = g_string_append_c (gstring, ch);
1261 if (!config->scan_string_dq)
1263 token = G_TOKEN_STRING;
1264 in_string_dq = TRUE;
1265 gstring = g_string_new ("");
1266 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1270 in_string_dq = FALSE;
1277 ch = g_scanner_get_char (scanner, line_p, position_p);
1287 gstring = g_string_append_c (gstring, '\\');
1291 gstring = g_string_append_c (gstring, '\n');
1295 gstring = g_string_append_c (gstring, '\t');
1299 gstring = g_string_append_c (gstring, '\r');
1303 gstring = g_string_append_c (gstring, '\b');
1307 gstring = g_string_append_c (gstring, '\f');
1319 fchar = g_scanner_peek_next_char (scanner);
1320 if (fchar >= '0' && fchar <= '7')
1322 ch = g_scanner_get_char (scanner, line_p, position_p);
1323 i = i * 8 + ch - '0';
1324 fchar = g_scanner_peek_next_char (scanner);
1325 if (fchar >= '0' && fchar <= '7')
1327 ch = g_scanner_get_char (scanner, line_p, position_p);
1328 i = i * 8 + ch - '0';
1331 gstring = g_string_append_c (gstring, i);
1335 gstring = g_string_append_c (gstring, ch);
1340 gstring = g_string_append_c (gstring, ch);
1347 if (!config->scan_float)
1349 token = G_TOKEN_FLOAT;
1350 dotted_float = TRUE;
1351 ch = g_scanner_get_char (scanner, line_p, position_p);
1352 goto number_parsing;
1355 if (!config->scan_hex_dollar)
1357 token = G_TOKEN_HEX;
1358 ch = g_scanner_get_char (scanner, line_p, position_p);
1359 goto number_parsing;
1362 if (config->scan_octal)
1363 token = G_TOKEN_OCTAL;
1365 token = G_TOKEN_INT;
1366 ch = g_scanner_peek_next_char (scanner);
1367 if (config->scan_hex && (ch == 'x' || ch == 'X'))
1369 token = G_TOKEN_HEX;
1370 g_scanner_get_char (scanner, line_p, position_p);
1371 ch = g_scanner_get_char (scanner, line_p, position_p);
1374 token = G_TOKEN_ERROR;
1375 value.v_error = G_ERR_UNEXP_EOF;
1379 if (g_scanner_char_2_num (ch, 16) < 0)
1381 token = G_TOKEN_ERROR;
1382 value.v_error = G_ERR_DIGIT_RADIX;
1387 else if (config->scan_binary && (ch == 'b' || ch == 'B'))
1389 token = G_TOKEN_BINARY;
1390 g_scanner_get_char (scanner, line_p, position_p);
1391 ch = g_scanner_get_char (scanner, line_p, position_p);
1394 token = G_TOKEN_ERROR;
1395 value.v_error = G_ERR_UNEXP_EOF;
1399 if (g_scanner_char_2_num (ch, 10) < 0)
1401 token = G_TOKEN_ERROR;
1402 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1421 gboolean in_number = TRUE;
1424 if (token == G_TOKEN_NONE)
1425 token = G_TOKEN_INT;
1427 gstring = g_string_new (dotted_float ? "0." : "");
1428 gstring = g_string_append_c (gstring, ch);
1430 do /* while (in_number) */
1434 is_E = token == G_TOKEN_FLOAT && (ch == 'e' || ch == 'E');
1436 ch = g_scanner_peek_next_char (scanner);
1438 if (g_scanner_char_2_num (ch, 36) >= 0 ||
1439 (config->scan_float && ch == '.') ||
1440 (is_E && (ch == '+' || ch == '-')))
1442 ch = g_scanner_get_char (scanner, line_p, position_p);
1447 if (token != G_TOKEN_INT && token != G_TOKEN_OCTAL)
1449 value.v_error = token == G_TOKEN_FLOAT ? G_ERR_FLOAT_MALFORMED : G_ERR_FLOAT_RADIX;
1450 token = G_TOKEN_ERROR;
1455 token = G_TOKEN_FLOAT;
1456 gstring = g_string_append_c (gstring, ch);
1470 gstring = g_string_append_c (gstring, ch);
1475 if (token != G_TOKEN_FLOAT)
1477 token = G_TOKEN_ERROR;
1478 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1482 gstring = g_string_append_c (gstring, ch);
1487 if ((token != G_TOKEN_HEX && !config->scan_float) ||
1488 (token != G_TOKEN_HEX &&
1489 token != G_TOKEN_OCTAL &&
1490 token != G_TOKEN_FLOAT &&
1491 token != G_TOKEN_INT))
1493 token = G_TOKEN_ERROR;
1494 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1499 if (token != G_TOKEN_HEX)
1500 token = G_TOKEN_FLOAT;
1501 gstring = g_string_append_c (gstring, ch);
1506 if (token != G_TOKEN_HEX)
1508 token = G_TOKEN_ERROR;
1509 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1513 gstring = g_string_append_c (gstring, ch);
1525 case G_TOKEN_BINARY:
1526 value.v_binary = strtol (gstring->str, &endptr, 2);
1530 value.v_octal = strtol (gstring->str, &endptr, 8);
1534 value.v_int = strtol (gstring->str, &endptr, 10);
1538 value.v_float = g_strtod (gstring->str, &endptr);
1542 value.v_hex = strtol (gstring->str, &endptr, 16);
1548 if (endptr && *endptr)
1550 token = G_TOKEN_ERROR;
1551 if (*endptr == 'e' || *endptr == 'E')
1552 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1554 value.v_error = G_ERR_DIGIT_RADIX;
1556 g_string_free (gstring, TRUE);
1559 } /* number_parsing:... */
1565 if (config->cpair_comment_single &&
1566 ch == config->cpair_comment_single[0])
1568 token = G_TOKEN_COMMENT_SINGLE;
1569 in_comment_single = TRUE;
1570 gstring = g_string_new ("");
1571 ch = g_scanner_get_char (scanner, line_p, position_p);
1574 if (ch == config->cpair_comment_single[1])
1576 in_comment_single = FALSE;
1581 gstring = g_string_append_c (gstring, ch);
1582 ch = g_scanner_get_char (scanner, line_p, position_p);
1585 else if (config->scan_identifier && ch &&
1586 strchr (config->cset_identifier_first, ch))
1588 identifier_precedence:
1590 if (config->cset_identifier_nth && ch &&
1591 strchr (config->cset_identifier_nth,
1592 g_scanner_peek_next_char (scanner)))
1594 token = G_TOKEN_IDENTIFIER;
1595 gstring = g_string_new ("");
1596 gstring = g_string_append_c (gstring, ch);
1599 ch = g_scanner_get_char (scanner, line_p, position_p);
1600 gstring = g_string_append_c (gstring, ch);
1601 ch = g_scanner_peek_next_char (scanner);
1603 while (ch && strchr (config->cset_identifier_nth, ch));
1606 else if (config->scan_identifier_1char)
1608 token = G_TOKEN_IDENTIFIER;
1609 value.v_identifier = g_new0 (gchar, 2);
1610 value.v_identifier[0] = ch;
1616 if (config->char_2_token)
1620 token = G_TOKEN_CHAR;
1625 } /* default_case:... */
1628 g_assert (ch == 0 && token != G_TOKEN_NONE); /* paranoid */
1632 if (in_comment_multi || in_comment_single ||
1633 in_string_sq || in_string_dq)
1635 token = G_TOKEN_ERROR;
1638 g_string_free (gstring, TRUE);
1642 if (in_comment_multi || in_comment_single)
1643 value.v_error = G_ERR_UNEXP_EOF_IN_COMMENT;
1644 else /* (in_string_sq || in_string_dq) */
1645 value.v_error = G_ERR_UNEXP_EOF_IN_STRING;
1650 value.v_string = gstring->str;
1651 g_string_free (gstring, FALSE);
1655 if (token == G_TOKEN_IDENTIFIER)
1657 if (config->scan_symbols)
1662 scope_id = scanner->scope_id;
1663 key = g_scanner_lookup_internal (scanner, scope_id, value.v_identifier);
1664 if (!key && scope_id && scanner->config->scope_0_fallback)
1665 key = g_scanner_lookup_internal (scanner, 0, value.v_identifier);
1669 g_free (value.v_identifier);
1670 token = G_TOKEN_SYMBOL;
1671 value.v_symbol = key->value;
1675 if (token == G_TOKEN_IDENTIFIER &&
1676 config->scan_identifier_NULL &&
1677 strlen (value.v_identifier) == 4)
1679 gchar *null_upper = "NULL";
1680 gchar *null_lower = "null";
1682 if (scanner->config->case_sensitive)
1684 if (value.v_identifier[0] == null_upper[0] &&
1685 value.v_identifier[1] == null_upper[1] &&
1686 value.v_identifier[2] == null_upper[2] &&
1687 value.v_identifier[3] == null_upper[3])
1688 token = G_TOKEN_IDENTIFIER_NULL;
1692 if ((value.v_identifier[0] == null_upper[0] ||
1693 value.v_identifier[0] == null_lower[0]) &&
1694 (value.v_identifier[1] == null_upper[1] ||
1695 value.v_identifier[1] == null_lower[1]) &&
1696 (value.v_identifier[2] == null_upper[2] ||
1697 value.v_identifier[2] == null_lower[2]) &&
1698 (value.v_identifier[3] == null_upper[3] ||
1699 value.v_identifier[3] == null_lower[3]))
1700 token = G_TOKEN_IDENTIFIER_NULL;