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/.
34 #define __gscanner_c__
49 #include <sys/types.h> /* needed for sys/stat.h */
52 #include <io.h> /* For _read() */
56 #define to_lower(c) ( \
58 ( (((guchar)(c))>='A' && ((guchar)(c))<='Z') * ('a'-'A') ) | \
59 ( (((guchar)(c))>=192 && ((guchar)(c))<=214) * (224-192) ) | \
60 ( (((guchar)(c))>=216 && ((guchar)(c))<=222) * (248-216) ) | \
64 #define READ_BUFFER_SIZE (4000)
67 /* --- typedefs --- */
68 typedef struct _GScannerKey GScannerKey;
79 /* --- variables --- */
80 static GScannerConfig g_scanner_config_template =
84 ) /* cset_skip_characters */,
89 ) /* cset_identifier_first */,
97 ) /* cset_identifier_nth */,
98 ( "#\n" ) /* cpair_comment_single */,
100 FALSE /* case_sensitive */,
102 TRUE /* skip_comment_multi */,
103 TRUE /* skip_comment_single */,
104 TRUE /* scan_comment_multi */,
105 TRUE /* scan_identifier */,
106 FALSE /* scan_identifier_1char */,
107 FALSE /* scan_identifier_NULL */,
108 TRUE /* scan_symbols */,
109 FALSE /* scan_binary */,
110 TRUE /* scan_octal */,
111 TRUE /* scan_float */,
113 FALSE /* scan_hex_dollar */,
114 TRUE /* scan_string_sq */,
115 TRUE /* scan_string_dq */,
116 TRUE /* numbers_2_int */,
117 FALSE /* int_2_float */,
118 FALSE /* identifier_2_string */,
119 TRUE /* char_2_token */,
120 FALSE /* symbol_2_token */,
121 FALSE /* scope_0_fallback */,
125 /* --- prototypes --- */
127 GScannerKey* g_scanner_lookup_internal (GScanner *scanner,
129 const gchar *symbol);
130 static gint g_scanner_key_equal (gconstpointer v1,
132 static guint g_scanner_key_hash (gconstpointer v);
133 static void g_scanner_get_token_ll (GScanner *scanner,
135 GTokenValue *value_p,
138 static void g_scanner_get_token_i (GScanner *scanner,
140 GTokenValue *value_p,
144 static guchar g_scanner_peek_next_char (GScanner *scanner);
145 static guchar g_scanner_get_char (GScanner *scanner,
148 static void g_scanner_msg_handler (GScanner *scanner,
153 /* --- functions --- */
155 g_scanner_char_2_num (guchar c,
158 if (c >= '0' && c <= '9')
160 else if (c >= 'A' && c <= 'Z')
162 else if (c >= 'a' && c <= 'z')
174 g_scanner_new (GScannerConfig *config_templ)
179 config_templ = &g_scanner_config_template;
181 scanner = g_new0 (GScanner, 1);
183 scanner->user_data = NULL;
184 scanner->max_parse_errors = 0;
185 scanner->parse_errors = 0;
186 scanner->input_name = NULL;
187 scanner->derived_data = NULL;
189 scanner->config = g_new0 (GScannerConfig, 1);
191 scanner->config->case_sensitive = config_templ->case_sensitive;
192 scanner->config->cset_skip_characters = config_templ->cset_skip_characters;
193 if (!scanner->config->cset_skip_characters)
194 scanner->config->cset_skip_characters = "";
195 scanner->config->cset_identifier_first= config_templ->cset_identifier_first;
196 scanner->config->cset_identifier_nth = config_templ->cset_identifier_nth;
197 scanner->config->cpair_comment_single = config_templ->cpair_comment_single;
198 scanner->config->skip_comment_multi = config_templ->skip_comment_multi;
199 scanner->config->skip_comment_single = config_templ->skip_comment_single;
200 scanner->config->scan_comment_multi = config_templ->scan_comment_multi;
201 scanner->config->scan_identifier = config_templ->scan_identifier;
202 scanner->config->scan_identifier_1char= config_templ->scan_identifier_1char;
203 scanner->config->scan_identifier_NULL = config_templ->scan_identifier_NULL;
204 scanner->config->scan_symbols = config_templ->scan_symbols;
205 scanner->config->scan_binary = config_templ->scan_binary;
206 scanner->config->scan_octal = config_templ->scan_octal;
207 scanner->config->scan_float = config_templ->scan_float;
208 scanner->config->scan_hex = config_templ->scan_hex;
209 scanner->config->scan_hex_dollar = config_templ->scan_hex_dollar;
210 scanner->config->scan_string_sq = config_templ->scan_string_sq;
211 scanner->config->scan_string_dq = config_templ->scan_string_dq;
212 scanner->config->numbers_2_int = config_templ->numbers_2_int;
213 scanner->config->int_2_float = config_templ->int_2_float;
214 scanner->config->identifier_2_string = config_templ->identifier_2_string;
215 scanner->config->char_2_token = config_templ->char_2_token;
216 scanner->config->symbol_2_token = config_templ->symbol_2_token;
217 scanner->config->scope_0_fallback = config_templ->scope_0_fallback;
219 scanner->token = G_TOKEN_NONE;
220 scanner->value.v_int = 0;
222 scanner->position = 0;
224 scanner->next_token = G_TOKEN_NONE;
225 scanner->next_value.v_int = 0;
226 scanner->next_line = 1;
227 scanner->next_position = 0;
229 scanner->symbol_table = g_hash_table_new (g_scanner_key_hash, g_scanner_key_equal);
230 scanner->input_fd = -1;
231 scanner->text = NULL;
232 scanner->text_end = NULL;
233 scanner->buffer = NULL;
234 scanner->scope_id = 0;
236 scanner->msg_handler = g_scanner_msg_handler;
242 g_scanner_free_value (GTokenType *token_p,
243 GTokenValue *value_p)
248 case G_TOKEN_IDENTIFIER:
249 case G_TOKEN_IDENTIFIER_NULL:
250 case G_TOKEN_COMMENT_SINGLE:
251 case G_TOKEN_COMMENT_MULTI:
252 g_free (value_p->v_string);
259 *token_p = G_TOKEN_NONE;
263 g_scanner_destroy_symbol_table_entry (gpointer _key,
267 GScannerKey *key = _key;
269 g_free (key->symbol);
274 g_scanner_destroy (GScanner *scanner)
276 g_return_if_fail (scanner != NULL);
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 fprintf (stdout, "%s:%d: ", scanner->input_name, scanner->line);
297 fprintf (stdout, "error: ");
298 fprintf (stdout, "%s\n", message);
302 g_scanner_error (GScanner *scanner,
306 g_return_if_fail (scanner != NULL);
307 g_return_if_fail (format != NULL);
309 scanner->parse_errors++;
311 if (scanner->msg_handler)
316 va_start (args, format);
317 string = g_strdup_vprintf (format, args);
320 scanner->msg_handler (scanner, string, TRUE);
327 g_scanner_warn (GScanner *scanner,
331 g_return_if_fail (scanner != NULL);
332 g_return_if_fail (format != NULL);
334 if (scanner->msg_handler)
339 va_start (args, format);
340 string = g_strdup_vprintf (format, args);
343 scanner->msg_handler (scanner, string, FALSE);
350 g_scanner_key_equal (gconstpointer v1,
353 const GScannerKey *key1 = v1;
354 const GScannerKey *key2 = v2;
356 return (key1->scope_id == key2->scope_id) && (strcmp (key1->symbol, key2->symbol) == 0);
360 g_scanner_key_hash (gconstpointer v)
362 const GScannerKey *key = v;
367 for (c = key->symbol; *c; c++)
383 static inline GScannerKey*
384 g_scanner_lookup_internal (GScanner *scanner,
391 key.scope_id = scope_id;
393 if (!scanner->config->case_sensitive)
398 key.symbol = g_new (gchar, strlen (symbol) + 1);
399 for (d = key.symbol, c = symbol; *c; c++, d++)
402 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
407 key.symbol = (gchar*) symbol;
408 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
415 g_scanner_scope_add_symbol (GScanner *scanner,
422 g_return_if_fail (scanner != NULL);
423 g_return_if_fail (symbol != NULL);
425 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
429 key = g_new (GScannerKey, 1);
430 key->scope_id = scope_id;
431 key->symbol = g_strdup (symbol);
433 if (!scanner->config->case_sensitive)
444 g_hash_table_insert (scanner->symbol_table, key, key);
451 g_scanner_scope_remove_symbol (GScanner *scanner,
457 g_return_if_fail (scanner != NULL);
458 g_return_if_fail (symbol != NULL);
460 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
464 g_hash_table_remove (scanner->symbol_table, key);
465 g_free (key->symbol);
471 g_scanner_lookup_symbol (GScanner *scanner,
477 g_return_val_if_fail (scanner != NULL, NULL);
482 scope_id = scanner->scope_id;
483 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
484 if (!key && scope_id && scanner->config->scope_0_fallback)
485 key = g_scanner_lookup_internal (scanner, 0, symbol);
494 g_scanner_scope_lookup_symbol (GScanner *scanner,
500 g_return_val_if_fail (scanner != NULL, NULL);
505 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
514 g_scanner_set_scope (GScanner *scanner,
519 g_return_val_if_fail (scanner != NULL, 0);
521 old_scope_id = scanner->scope_id;
522 scanner->scope_id = scope_id;
528 g_scanner_foreach_internal (gpointer _key,
539 func = (GHFunc) d[0];
544 if (key->scope_id == *scope_id)
545 func (key->symbol, key->value, user_data);
549 g_scanner_scope_foreach_symbol (GScanner *scanner,
556 g_return_if_fail (scanner != NULL);
558 d[0] = (gpointer) func;
562 g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
566 g_scanner_freeze_symbol_table (GScanner *scanner)
568 #ifdef G_ENABLE_DEBUG
569 static gboolean first_call = TRUE;
573 g_warning("g_scanner_freeze_symbol_table and "
574 "g_scanner_thaw_symbol_table are deprecated.");
577 #endif /* G_ENABLE_DEBUG */
581 g_scanner_thaw_symbol_table (GScanner *scanner)
586 g_scanner_peek_next_token (GScanner *scanner)
588 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
590 if (scanner->next_token == G_TOKEN_NONE)
592 scanner->next_line = scanner->line;
593 scanner->next_position = scanner->position;
594 g_scanner_get_token_i (scanner,
595 &scanner->next_token,
596 &scanner->next_value,
598 &scanner->next_position);
601 return scanner->next_token;
605 g_scanner_get_next_token (GScanner *scanner)
607 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
609 if (scanner->next_token != G_TOKEN_NONE)
611 g_scanner_free_value (&scanner->token, &scanner->value);
613 scanner->token = scanner->next_token;
614 scanner->value = scanner->next_value;
615 scanner->line = scanner->next_line;
616 scanner->position = scanner->next_position;
617 scanner->next_token = G_TOKEN_NONE;
620 g_scanner_get_token_i (scanner,
626 return scanner->token;
630 g_scanner_cur_token (GScanner *scanner)
632 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
634 return scanner->token;
638 g_scanner_cur_value (GScanner *scanner)
644 g_return_val_if_fail (scanner != NULL, v);
646 /* MSC isn't capable of handling return scanner->value; ? */
654 g_scanner_cur_line (GScanner *scanner)
656 g_return_val_if_fail (scanner != NULL, 0);
658 return scanner->line;
662 g_scanner_cur_position (GScanner *scanner)
664 g_return_val_if_fail (scanner != NULL, 0);
666 return scanner->position;
670 g_scanner_eof (GScanner *scanner)
672 g_return_val_if_fail (scanner != NULL, TRUE);
674 return scanner->token == G_TOKEN_EOF;
678 g_scanner_input_file (GScanner *scanner,
681 g_return_if_fail (scanner != NULL);
682 g_return_if_fail (input_fd >= 0);
684 if (scanner->input_fd >= 0)
685 g_scanner_sync_file_offset (scanner);
687 scanner->token = G_TOKEN_NONE;
688 scanner->value.v_int = 0;
690 scanner->position = 0;
691 scanner->next_token = G_TOKEN_NONE;
693 scanner->input_fd = input_fd;
694 scanner->text = NULL;
695 scanner->text_end = NULL;
697 if (!scanner->buffer)
698 scanner->buffer = g_new (gchar, READ_BUFFER_SIZE + 1);
702 g_scanner_input_text (GScanner *scanner,
706 g_return_if_fail (scanner != NULL);
708 g_return_if_fail (text != NULL);
712 if (scanner->input_fd >= 0)
713 g_scanner_sync_file_offset (scanner);
715 scanner->token = G_TOKEN_NONE;
716 scanner->value.v_int = 0;
718 scanner->position = 0;
719 scanner->next_token = G_TOKEN_NONE;
721 scanner->input_fd = -1;
722 scanner->text = text;
723 scanner->text_end = text + text_len;
727 g_free (scanner->buffer);
728 scanner->buffer = NULL;
733 g_scanner_peek_next_char (GScanner *scanner)
735 if (scanner->text < scanner->text_end)
737 return *scanner->text;
739 else if (scanner->input_fd >= 0)
744 buffer = scanner->buffer;
747 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
749 while (count == -1 && (errno == EINTR || errno == EAGAIN));
753 scanner->input_fd = -1;
759 scanner->text = buffer;
760 scanner->text_end = buffer + count;
770 g_scanner_sync_file_offset (GScanner *scanner)
772 g_return_if_fail (scanner != NULL);
774 /* for file input, rewind the filedescriptor to the current
775 * buffer position and blow the file read ahead buffer. usefull for
776 * third party uses of our filedescriptor, which hooks onto the current
780 if (scanner->input_fd >= 0 && scanner->text_end > scanner->text)
784 buffered = scanner->text_end - scanner->text;
785 if (lseek (scanner->input_fd, - buffered, SEEK_CUR) >= 0)
787 /* we succeeded, blow our buffer's contents now */
788 scanner->text = NULL;
789 scanner->text_end = NULL;
797 g_scanner_get_char (GScanner *scanner,
803 if (scanner->text < scanner->text_end)
804 fchar = *(scanner->text++);
805 else if (scanner->input_fd >= 0)
810 buffer = scanner->buffer;
813 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
815 while (count == -1 && (errno == EINTR || errno == EAGAIN));
819 scanner->input_fd = -1;
824 scanner->text = buffer + 1;
825 scanner->text_end = buffer + count;
829 g_scanner_sync_file_offset (scanner);
830 scanner->text_end = scanner->text;
831 scanner->input_fd = -1;
852 g_scanner_unexp_token (GScanner *scanner,
853 GTokenType expected_token,
854 const gchar *identifier_spec,
855 const gchar *symbol_spec,
856 const gchar *symbol_name,
857 const gchar *message,
861 guint token_string_len;
862 gchar *expected_string;
863 guint expected_string_len;
864 gchar *message_prefix;
865 gboolean print_unexp;
866 void (*msg_handler) (GScanner*, const gchar*, ...);
868 g_return_if_fail (scanner != NULL);
871 msg_handler = g_scanner_error;
873 msg_handler = g_scanner_warn;
875 if (!identifier_spec)
876 identifier_spec = "identifier";
878 symbol_spec = "symbol";
880 token_string_len = 56;
881 token_string = g_new (gchar, token_string_len + 1);
882 expected_string_len = 64;
883 expected_string = g_new (gchar, expected_string_len + 1);
886 switch (scanner->token)
889 g_snprintf (token_string, token_string_len, "end of file");
893 if (scanner->token >= 1 && scanner->token <= 255)
895 if ((scanner->token >= ' ' && scanner->token <= '~') ||
896 strchr (scanner->config->cset_identifier_first, scanner->token) ||
897 strchr (scanner->config->cset_identifier_nth, scanner->token))
898 g_snprintf (token_string, expected_string_len, "character `%c'", scanner->token);
900 g_snprintf (token_string, expected_string_len, "character `\\%o'", scanner->token);
903 else if (!scanner->config->symbol_2_token)
905 g_snprintf (token_string, token_string_len, "(unknown) token <%d>", scanner->token);
910 if (expected_token == G_TOKEN_SYMBOL ||
911 (scanner->config->symbol_2_token &&
912 expected_token > G_TOKEN_LAST))
915 g_snprintf (token_string,
918 print_unexp ? "" : "invalid ",
922 g_snprintf (token_string,
925 print_unexp ? "" : "invalid ",
931 expected_token = G_TOKEN_NONE;
932 switch (scanner->value.v_error)
934 case G_ERR_UNEXP_EOF:
935 g_snprintf (token_string, token_string_len, "scanner: unexpected end of file");
938 case G_ERR_UNEXP_EOF_IN_STRING:
939 g_snprintf (token_string, token_string_len, "scanner: unterminated string constant");
942 case G_ERR_UNEXP_EOF_IN_COMMENT:
943 g_snprintf (token_string, token_string_len, "scanner: unterminated comment");
946 case G_ERR_NON_DIGIT_IN_CONST:
947 g_snprintf (token_string, token_string_len, "scanner: non digit in constant");
950 case G_ERR_FLOAT_RADIX:
951 g_snprintf (token_string, token_string_len, "scanner: invalid radix for floating constant");
954 case G_ERR_FLOAT_MALFORMED:
955 g_snprintf (token_string, token_string_len, "scanner: malformed floating constant");
958 case G_ERR_DIGIT_RADIX:
959 g_snprintf (token_string, token_string_len, "scanner: digit is beyond radix");
964 g_snprintf (token_string, token_string_len, "scanner: unknown error");
970 g_snprintf (token_string, token_string_len, "character `%c'", scanner->value.v_char);
973 case G_TOKEN_IDENTIFIER:
974 case G_TOKEN_IDENTIFIER_NULL:
975 if (expected_token == G_TOKEN_IDENTIFIER ||
976 expected_token == G_TOKEN_IDENTIFIER_NULL)
978 g_snprintf (token_string,
981 print_unexp ? "" : "invalid ",
983 scanner->token == G_TOKEN_IDENTIFIER ? scanner->value.v_string : "null");
990 g_snprintf (token_string, token_string_len, "number `%ld'", scanner->value.v_int);
994 g_snprintf (token_string, token_string_len, "number `%.3f'", scanner->value.v_float);
998 if (expected_token == G_TOKEN_STRING)
1000 g_snprintf (token_string,
1002 "%s%sstring constant \"%s\"",
1003 print_unexp ? "" : "invalid ",
1004 scanner->value.v_string[0] == 0 ? "empty " : "",
1005 scanner->value.v_string);
1006 token_string[token_string_len - 2] = '"';
1007 token_string[token_string_len - 1] = 0;
1010 case G_TOKEN_COMMENT_SINGLE:
1011 case G_TOKEN_COMMENT_MULTI:
1012 g_snprintf (token_string, token_string_len, "comment");
1016 /* somehow the user's parsing code is screwed, there isn't much
1017 * we can do about it.
1018 * Note, a common case to trigger this is
1019 * g_scanner_peek_next_token(); g_scanner_unexp_token();
1020 * without an intermediate g_scanner_get_next_token().
1022 g_assert_not_reached ();
1027 switch (expected_token)
1029 gboolean need_valid;
1032 if (expected_token >= 1 && expected_token <= 255)
1034 if ((expected_token >= ' ' && expected_token <= '~') ||
1035 strchr (scanner->config->cset_identifier_first, expected_token) ||
1036 strchr (scanner->config->cset_identifier_nth, expected_token))
1037 g_snprintf (expected_string, expected_string_len, "character `%c'", expected_token);
1039 g_snprintf (expected_string, expected_string_len, "character `\\%o'", expected_token);
1042 else if (!scanner->config->symbol_2_token)
1044 g_snprintf (expected_string, expected_string_len, "(unknown) token <%d>", expected_token);
1048 case G_TOKEN_SYMBOL:
1049 need_valid = (scanner->token == G_TOKEN_SYMBOL ||
1050 (scanner->config->symbol_2_token &&
1051 scanner->token > G_TOKEN_LAST));
1052 g_snprintf (expected_string,
1053 expected_string_len,
1055 need_valid ? "valid " : "",
1057 /* FIXME: should we attempt to lookup the symbol_name for symbol_2_token? */
1061 g_snprintf (expected_string, expected_string_len, "number (integer)");
1065 g_snprintf (expected_string, expected_string_len, "number (float)");
1068 case G_TOKEN_STRING:
1069 g_snprintf (expected_string,
1070 expected_string_len,
1071 "%sstring constant",
1072 scanner->token == G_TOKEN_STRING ? "valid " : "");
1075 case G_TOKEN_IDENTIFIER:
1076 case G_TOKEN_IDENTIFIER_NULL:
1077 g_snprintf (expected_string,
1078 expected_string_len,
1080 (scanner->token == G_TOKEN_IDENTIFIER_NULL ||
1081 scanner->token == G_TOKEN_IDENTIFIER ? "valid " : ""),
1086 g_snprintf (expected_string, expected_string_len, "end of file");
1093 if (message && message[0] != 0)
1094 message_prefix = " - ";
1097 message_prefix = "";
1101 if (expected_token != G_TOKEN_NONE)
1104 msg_handler (scanner,
1105 "unexpected %s, expected %s%s%s",
1111 msg_handler (scanner,
1112 "%s, expected %s%s%s",
1121 msg_handler (scanner,
1122 "unexpected %s%s%s",
1127 msg_handler (scanner,
1134 g_free (token_string);
1135 g_free (expected_string);
1139 g_scanner_stat_mode (const gchar *filename)
1141 struct stat *stat_buf;
1144 stat_buf = g_new0 (struct stat, 1);
1146 lstat (filename, stat_buf);
1148 stat (filename, stat_buf);
1150 st_mode = stat_buf->st_mode;
1158 g_scanner_get_token_i (GScanner *scanner,
1159 GTokenType *token_p,
1160 GTokenValue *value_p,
1166 g_scanner_free_value (token_p, value_p);
1167 g_scanner_get_token_ll (scanner, token_p, value_p, line_p, position_p);
1169 while (((*token_p > 0 && *token_p < 256) &&
1170 strchr (scanner->config->cset_skip_characters, *token_p)) ||
1171 (*token_p == G_TOKEN_CHAR &&
1172 strchr (scanner->config->cset_skip_characters, value_p->v_char)) ||
1173 (*token_p == G_TOKEN_COMMENT_MULTI &&
1174 scanner->config->skip_comment_multi) ||
1175 (*token_p == G_TOKEN_COMMENT_SINGLE &&
1176 scanner->config->skip_comment_single));
1180 case G_TOKEN_IDENTIFIER:
1181 if (scanner->config->identifier_2_string)
1182 *token_p = G_TOKEN_STRING;
1185 case G_TOKEN_SYMBOL:
1186 if (scanner->config->symbol_2_token)
1187 *token_p = (GTokenType) value_p->v_symbol;
1190 case G_TOKEN_BINARY:
1193 if (scanner->config->numbers_2_int)
1194 *token_p = G_TOKEN_INT;
1201 if (*token_p == G_TOKEN_INT &&
1202 scanner->config->int_2_float)
1204 *token_p = G_TOKEN_FLOAT;
1205 value_p->v_float = value_p->v_int;
1212 g_scanner_get_token_ll (GScanner *scanner,
1213 GTokenType *token_p,
1214 GTokenValue *value_p,
1218 GScannerConfig *config;
1220 gboolean in_comment_multi;
1221 gboolean in_comment_single;
1222 gboolean in_string_sq;
1223 gboolean in_string_dq;
1228 config = scanner->config;
1229 (*value_p).v_int = 0;
1231 if ((scanner->text >= scanner->text_end && scanner->input_fd < 0) ||
1232 scanner->token == G_TOKEN_EOF)
1234 *token_p = G_TOKEN_EOF;
1238 in_comment_multi = FALSE;
1239 in_comment_single = FALSE;
1240 in_string_sq = FALSE;
1241 in_string_dq = FALSE;
1244 do /* while (ch != 0) */
1246 gboolean dotted_float = FALSE;
1248 ch = g_scanner_get_char (scanner, line_p, position_p);
1251 token = G_TOKEN_NONE;
1253 /* this is *evil*, but needed ;(
1254 * we first check for identifier first character, because it
1255 * might interfere with other key chars like slashes or numbers
1257 if (config->scan_identifier &&
1258 ch && strchr (config->cset_identifier_first, ch))
1259 goto identifier_precedence;
1264 token = G_TOKEN_EOF;
1270 if (!config->scan_comment_multi ||
1271 g_scanner_peek_next_char (scanner) != '*')
1273 g_scanner_get_char (scanner, line_p, position_p);
1274 token = G_TOKEN_COMMENT_MULTI;
1275 in_comment_multi = TRUE;
1276 gstring = g_string_new ("");
1277 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1279 if (ch == '*' && g_scanner_peek_next_char (scanner) == '/')
1281 g_scanner_get_char (scanner, line_p, position_p);
1282 in_comment_multi = FALSE;
1286 gstring = g_string_append_c (gstring, ch);
1292 if (!config->scan_string_sq)
1294 token = G_TOKEN_STRING;
1295 in_string_sq = TRUE;
1296 gstring = g_string_new ("");
1297 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1301 in_string_sq = FALSE;
1305 gstring = g_string_append_c (gstring, ch);
1311 if (!config->scan_string_dq)
1313 token = G_TOKEN_STRING;
1314 in_string_dq = TRUE;
1315 gstring = g_string_new ("");
1316 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1320 in_string_dq = FALSE;
1327 ch = g_scanner_get_char (scanner, line_p, position_p);
1337 gstring = g_string_append_c (gstring, '\\');
1341 gstring = g_string_append_c (gstring, '\n');
1345 gstring = g_string_append_c (gstring, '\t');
1349 gstring = g_string_append_c (gstring, '\r');
1353 gstring = g_string_append_c (gstring, '\b');
1357 gstring = g_string_append_c (gstring, '\f');
1369 fchar = g_scanner_peek_next_char (scanner);
1370 if (fchar >= '0' && fchar <= '7')
1372 ch = g_scanner_get_char (scanner, line_p, position_p);
1373 i = i * 8 + ch - '0';
1374 fchar = g_scanner_peek_next_char (scanner);
1375 if (fchar >= '0' && fchar <= '7')
1377 ch = g_scanner_get_char (scanner, line_p, position_p);
1378 i = i * 8 + ch - '0';
1381 gstring = g_string_append_c (gstring, i);
1385 gstring = g_string_append_c (gstring, ch);
1390 gstring = g_string_append_c (gstring, ch);
1397 if (!config->scan_float)
1399 token = G_TOKEN_FLOAT;
1400 dotted_float = TRUE;
1401 ch = g_scanner_get_char (scanner, line_p, position_p);
1402 goto number_parsing;
1405 if (!config->scan_hex_dollar)
1407 token = G_TOKEN_HEX;
1408 ch = g_scanner_get_char (scanner, line_p, position_p);
1409 goto number_parsing;
1412 if (config->scan_octal)
1413 token = G_TOKEN_OCTAL;
1415 token = G_TOKEN_INT;
1416 ch = g_scanner_peek_next_char (scanner);
1417 if (config->scan_hex && (ch == 'x' || ch == 'X'))
1419 token = G_TOKEN_HEX;
1420 g_scanner_get_char (scanner, line_p, position_p);
1421 ch = g_scanner_get_char (scanner, line_p, position_p);
1424 token = G_TOKEN_ERROR;
1425 value.v_error = G_ERR_UNEXP_EOF;
1429 if (g_scanner_char_2_num (ch, 16) < 0)
1431 token = G_TOKEN_ERROR;
1432 value.v_error = G_ERR_DIGIT_RADIX;
1437 else if (config->scan_binary && (ch == 'b' || ch == 'B'))
1439 token = G_TOKEN_BINARY;
1440 g_scanner_get_char (scanner, line_p, position_p);
1441 ch = g_scanner_get_char (scanner, line_p, position_p);
1444 token = G_TOKEN_ERROR;
1445 value.v_error = G_ERR_UNEXP_EOF;
1449 if (g_scanner_char_2_num (ch, 10) < 0)
1451 token = G_TOKEN_ERROR;
1452 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1471 gboolean in_number = TRUE;
1474 if (token == G_TOKEN_NONE)
1475 token = G_TOKEN_INT;
1477 gstring = g_string_new (dotted_float ? "0." : "");
1478 gstring = g_string_append_c (gstring, ch);
1480 do /* while (in_number) */
1484 is_E = token == G_TOKEN_FLOAT && (ch == 'e' || ch == 'E');
1486 ch = g_scanner_peek_next_char (scanner);
1488 if (g_scanner_char_2_num (ch, 36) >= 0 ||
1489 (config->scan_float && ch == '.') ||
1490 (is_E && (ch == '+' || ch == '-')))
1492 ch = g_scanner_get_char (scanner, line_p, position_p);
1497 if (token != G_TOKEN_INT && token != G_TOKEN_OCTAL)
1499 value.v_error = token == G_TOKEN_FLOAT ? G_ERR_FLOAT_MALFORMED : G_ERR_FLOAT_RADIX;
1500 token = G_TOKEN_ERROR;
1505 token = G_TOKEN_FLOAT;
1506 gstring = g_string_append_c (gstring, ch);
1520 gstring = g_string_append_c (gstring, ch);
1525 if (token != G_TOKEN_FLOAT)
1527 token = G_TOKEN_ERROR;
1528 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1532 gstring = g_string_append_c (gstring, ch);
1537 if ((token != G_TOKEN_HEX && !config->scan_float) ||
1538 (token != G_TOKEN_HEX &&
1539 token != G_TOKEN_OCTAL &&
1540 token != G_TOKEN_FLOAT &&
1541 token != G_TOKEN_INT))
1543 token = G_TOKEN_ERROR;
1544 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1549 if (token != G_TOKEN_HEX)
1550 token = G_TOKEN_FLOAT;
1551 gstring = g_string_append_c (gstring, ch);
1556 if (token != G_TOKEN_HEX)
1558 token = G_TOKEN_ERROR;
1559 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1563 gstring = g_string_append_c (gstring, ch);
1575 case G_TOKEN_BINARY:
1576 value.v_binary = strtol (gstring->str, &endptr, 2);
1580 value.v_octal = strtol (gstring->str, &endptr, 8);
1584 value.v_int = strtol (gstring->str, &endptr, 10);
1588 value.v_float = g_strtod (gstring->str, &endptr);
1592 value.v_hex = strtol (gstring->str, &endptr, 16);
1598 if (endptr && *endptr)
1600 token = G_TOKEN_ERROR;
1601 if (*endptr == 'e' || *endptr == 'E')
1602 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1604 value.v_error = G_ERR_DIGIT_RADIX;
1606 g_string_free (gstring, TRUE);
1609 } /* number_parsing:... */
1615 if (config->cpair_comment_single &&
1616 ch == config->cpair_comment_single[0])
1618 token = G_TOKEN_COMMENT_SINGLE;
1619 in_comment_single = TRUE;
1620 gstring = g_string_new ("");
1621 ch = g_scanner_get_char (scanner, line_p, position_p);
1624 if (ch == config->cpair_comment_single[1])
1626 in_comment_single = FALSE;
1631 gstring = g_string_append_c (gstring, ch);
1632 ch = g_scanner_get_char (scanner, line_p, position_p);
1635 else if (config->scan_identifier && ch &&
1636 strchr (config->cset_identifier_first, ch))
1638 identifier_precedence:
1640 if (config->cset_identifier_nth && ch &&
1641 strchr (config->cset_identifier_nth,
1642 g_scanner_peek_next_char (scanner)))
1644 token = G_TOKEN_IDENTIFIER;
1645 gstring = g_string_new ("");
1646 gstring = g_string_append_c (gstring, ch);
1649 ch = g_scanner_get_char (scanner, line_p, position_p);
1650 gstring = g_string_append_c (gstring, ch);
1651 ch = g_scanner_peek_next_char (scanner);
1653 while (ch && strchr (config->cset_identifier_nth, ch));
1656 else if (config->scan_identifier_1char)
1658 token = G_TOKEN_IDENTIFIER;
1659 value.v_identifier = g_new0 (gchar, 2);
1660 value.v_identifier[0] = ch;
1666 if (config->char_2_token)
1670 token = G_TOKEN_CHAR;
1675 } /* default_case:... */
1678 g_assert (ch == 0 && token != G_TOKEN_NONE); /* paranoid */
1682 if (in_comment_multi || in_comment_single ||
1683 in_string_sq || in_string_dq)
1685 token = G_TOKEN_ERROR;
1688 g_string_free (gstring, TRUE);
1692 if (in_comment_multi || in_comment_single)
1693 value.v_error = G_ERR_UNEXP_EOF_IN_COMMENT;
1694 else /* (in_string_sq || in_string_dq) */
1695 value.v_error = G_ERR_UNEXP_EOF_IN_STRING;
1700 value.v_string = gstring->str;
1701 g_string_free (gstring, FALSE);
1705 if (token == G_TOKEN_IDENTIFIER)
1707 if (config->scan_symbols)
1712 scope_id = scanner->scope_id;
1713 key = g_scanner_lookup_internal (scanner, scope_id, value.v_identifier);
1714 if (!key && scope_id && scanner->config->scope_0_fallback)
1715 key = g_scanner_lookup_internal (scanner, 0, value.v_identifier);
1719 g_free (value.v_identifier);
1720 token = G_TOKEN_SYMBOL;
1721 value.v_symbol = key->value;
1725 if (token == G_TOKEN_IDENTIFIER &&
1726 config->scan_identifier_NULL &&
1727 strlen (value.v_identifier) == 4)
1729 gchar *null_upper = "NULL";
1730 gchar *null_lower = "null";
1732 if (scanner->config->case_sensitive)
1734 if (value.v_identifier[0] == null_upper[0] &&
1735 value.v_identifier[1] == null_upper[1] &&
1736 value.v_identifier[2] == null_upper[2] &&
1737 value.v_identifier[3] == null_upper[3])
1738 token = G_TOKEN_IDENTIFIER_NULL;
1742 if ((value.v_identifier[0] == null_upper[0] ||
1743 value.v_identifier[0] == null_lower[0]) &&
1744 (value.v_identifier[1] == null_upper[1] ||
1745 value.v_identifier[1] == null_lower[1]) &&
1746 (value.v_identifier[2] == null_upper[2] ||
1747 value.v_identifier[2] == null_lower[2]) &&
1748 (value.v_identifier[3] == null_upper[3] ||
1749 value.v_identifier[3] == null_lower[3]))
1750 token = G_TOKEN_IDENTIFIER_NULL;