1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GScanner: Flexible lexical scanner for general purpose.
5 * Copyright (C) 1997, 1998 Tim Janik
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
22 #define __gscanner_c__
37 #include <sys/types.h> /* needed for sys/stat.h */
40 #include <io.h> /* For _read() */
44 #define to_lower(c) ( \
46 ( (((guchar)(c))>='A' && ((guchar)(c))<='Z') * ('a'-'A') ) | \
47 ( (((guchar)(c))>=192 && ((guchar)(c))<=214) * (224-192) ) | \
48 ( (((guchar)(c))>=216 && ((guchar)(c))<=222) * (248-216) ) | \
52 #define READ_BUFFER_SIZE (4000)
55 /* --- typedefs --- */
56 typedef struct _GScannerKey GScannerKey;
67 /* --- variables --- */
68 static GScannerConfig g_scanner_config_template =
72 ) /* cset_skip_characters */,
77 ) /* cset_identifier_first */,
84 ) /* cset_identifier_nth */,
85 ( "#\n" ) /* cpair_comment_single */,
87 FALSE /* case_sensitive */,
89 TRUE /* skip_comment_multi */,
90 TRUE /* skip_comment_single */,
91 TRUE /* scan_comment_multi */,
92 TRUE /* scan_identifier */,
93 FALSE /* scan_identifier_1char */,
94 FALSE /* scan_identifier_NULL */,
95 TRUE /* scan_symbols */,
96 FALSE /* scan_binary */,
97 TRUE /* scan_octal */,
98 TRUE /* scan_float */,
100 FALSE /* scan_hex_dollar */,
101 TRUE /* scan_string_sq */,
102 TRUE /* scan_string_dq */,
103 TRUE /* numbers_2_int */,
104 FALSE /* int_2_float */,
105 FALSE /* identifier_2_string */,
106 TRUE /* char_2_token */,
107 FALSE /* symbol_2_token */,
108 FALSE /* scope_0_fallback */,
112 /* --- prototypes --- */
114 GScannerKey* g_scanner_lookup_internal (GScanner *scanner,
116 const gchar *symbol);
117 static gint g_scanner_key_equal (gconstpointer v1,
119 static guint g_scanner_key_hash (gconstpointer v);
120 static void g_scanner_get_token_ll (GScanner *scanner,
122 GTokenValue *value_p,
125 static void g_scanner_get_token_i (GScanner *scanner,
127 GTokenValue *value_p,
131 static guchar g_scanner_peek_next_char (GScanner *scanner);
132 static guchar g_scanner_get_char (GScanner *scanner,
135 static void g_scanner_msg_handler (GScanner *scanner,
140 /* --- functions --- */
142 g_scanner_char_2_num (guchar c,
145 if (c >= '0' && c <= '9')
147 else if (c >= 'A' && c <= 'Z')
149 else if (c >= 'a' && c <= 'z')
161 g_scanner_new (GScannerConfig *config_templ)
163 register GScanner *scanner;
166 config_templ = &g_scanner_config_template;
168 scanner = g_new0 (GScanner, 1);
170 scanner->user_data = NULL;
171 scanner->max_parse_errors = 0;
172 scanner->parse_errors = 0;
173 scanner->input_name = NULL;
174 scanner->derived_data = NULL;
176 scanner->config = g_new0 (GScannerConfig, 1);
178 scanner->config->case_sensitive = config_templ->case_sensitive;
179 scanner->config->cset_skip_characters = config_templ->cset_skip_characters;
180 scanner->config->cset_identifier_first= config_templ->cset_identifier_first;
181 scanner->config->cset_identifier_nth = config_templ->cset_identifier_nth;
182 scanner->config->cpair_comment_single = config_templ->cpair_comment_single;
183 scanner->config->skip_comment_multi = config_templ->skip_comment_multi;
184 scanner->config->skip_comment_single = config_templ->skip_comment_single;
185 scanner->config->scan_comment_multi = config_templ->scan_comment_multi;
186 scanner->config->scan_identifier = config_templ->scan_identifier;
187 scanner->config->scan_identifier_1char= config_templ->scan_identifier_1char;
188 scanner->config->scan_identifier_NULL = config_templ->scan_identifier_NULL;
189 scanner->config->scan_symbols = config_templ->scan_symbols;
190 scanner->config->scan_binary = config_templ->scan_binary;
191 scanner->config->scan_octal = config_templ->scan_octal;
192 scanner->config->scan_float = config_templ->scan_float;
193 scanner->config->scan_hex = config_templ->scan_hex;
194 scanner->config->scan_hex_dollar = config_templ->scan_hex_dollar;
195 scanner->config->scan_string_sq = config_templ->scan_string_sq;
196 scanner->config->scan_string_dq = config_templ->scan_string_dq;
197 scanner->config->numbers_2_int = config_templ->numbers_2_int;
198 scanner->config->int_2_float = config_templ->int_2_float;
199 scanner->config->identifier_2_string = config_templ->identifier_2_string;
200 scanner->config->char_2_token = config_templ->char_2_token;
201 scanner->config->symbol_2_token = config_templ->symbol_2_token;
202 scanner->config->scope_0_fallback = config_templ->scope_0_fallback;
204 scanner->token = G_TOKEN_NONE;
205 scanner->value.v_int = 0;
207 scanner->position = 0;
209 scanner->next_token = G_TOKEN_NONE;
210 scanner->next_value.v_int = 0;
211 scanner->next_line = 1;
212 scanner->next_position = 0;
214 scanner->symbol_table = g_hash_table_new (g_scanner_key_hash, g_scanner_key_equal);
215 scanner->input_fd = -1;
216 scanner->text = NULL;
217 scanner->text_end = NULL;
218 scanner->buffer = NULL;
219 scanner->scope_id = 0;
221 scanner->msg_handler = g_scanner_msg_handler;
227 g_scanner_free_value (GTokenType *token_p,
228 GTokenValue *value_p)
233 case G_TOKEN_IDENTIFIER:
234 case G_TOKEN_IDENTIFIER_NULL:
235 case G_TOKEN_COMMENT_SINGLE:
236 case G_TOKEN_COMMENT_MULTI:
237 g_free (value_p->v_string);
244 *token_p = G_TOKEN_NONE;
248 g_scanner_destroy_symbol_table_entry (gpointer _key,
252 GScannerKey *key = _key;
254 g_free (key->symbol);
259 g_scanner_destroy (GScanner *scanner)
261 g_return_if_fail (scanner != NULL);
263 g_hash_table_foreach (scanner->symbol_table,
264 g_scanner_destroy_symbol_table_entry, NULL);
265 g_hash_table_destroy (scanner->symbol_table);
266 g_scanner_free_value (&scanner->token, &scanner->value);
267 g_scanner_free_value (&scanner->next_token, &scanner->next_value);
268 g_free (scanner->config);
269 g_free (scanner->buffer);
274 g_scanner_msg_handler (GScanner *scanner,
278 g_return_if_fail (scanner != NULL);
280 fprintf (stdout, "%s:%d: ", scanner->input_name, scanner->line);
282 fprintf (stdout, "error: ");
283 fprintf (stdout, "%s\n", message);
287 g_scanner_error (GScanner *scanner,
291 g_return_if_fail (scanner != NULL);
292 g_return_if_fail (format != NULL);
294 scanner->parse_errors++;
296 if (scanner->msg_handler)
301 va_start (args, format);
302 string = g_strdup_vprintf (format, args);
305 scanner->msg_handler (scanner, string, TRUE);
312 g_scanner_warn (GScanner *scanner,
316 g_return_if_fail (scanner != NULL);
317 g_return_if_fail (format != NULL);
319 if (scanner->msg_handler)
324 va_start (args, format);
325 string = g_strdup_vprintf (format, args);
328 scanner->msg_handler (scanner, string, FALSE);
335 g_scanner_key_equal (gconstpointer v1,
338 register const GScannerKey *key1 = v1;
339 register const GScannerKey *key2 = v2;
341 return (key1->scope_id == key2->scope_id) && (strcmp (key1->symbol, key2->symbol) == 0);
345 g_scanner_key_hash (gconstpointer v)
347 register const GScannerKey *key = v;
352 for (c = key->symbol; *c; c++)
368 static inline GScannerKey*
369 g_scanner_lookup_internal (GScanner *scanner,
373 register GScannerKey *key_p;
376 key.scope_id = scope_id;
378 if (!scanner->config->case_sensitive)
381 register const gchar *c;
383 key.symbol = g_new (gchar, strlen (symbol) + 1);
384 for (d = key.symbol, c = symbol; *c; c++, d++)
387 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
392 key.symbol = (gchar*) symbol;
393 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
400 g_scanner_scope_add_symbol (GScanner *scanner,
405 register GScannerKey *key;
407 g_return_if_fail (scanner != NULL);
408 g_return_if_fail (symbol != NULL);
410 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
414 key = g_new (GScannerKey, 1);
415 key->scope_id = scope_id;
416 key->symbol = g_strdup (symbol);
418 if (!scanner->config->case_sensitive)
429 g_hash_table_insert (scanner->symbol_table, key, key);
436 g_scanner_scope_remove_symbol (GScanner *scanner,
440 register GScannerKey *key;
442 g_return_if_fail (scanner != NULL);
443 g_return_if_fail (symbol != NULL);
445 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
449 g_hash_table_remove (scanner->symbol_table, key);
450 g_free (key->symbol);
456 g_scanner_lookup_symbol (GScanner *scanner,
459 register GScannerKey *key;
460 register guint scope_id;
462 g_return_val_if_fail (scanner != NULL, NULL);
467 scope_id = scanner->scope_id;
468 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
469 if (!key && scope_id && scanner->config->scope_0_fallback)
470 key = g_scanner_lookup_internal (scanner, 0, symbol);
479 g_scanner_scope_lookup_symbol (GScanner *scanner,
483 register GScannerKey *key;
485 g_return_val_if_fail (scanner != NULL, NULL);
490 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
499 g_scanner_set_scope (GScanner *scanner,
502 register guint old_scope_id;
504 g_return_val_if_fail (scanner != NULL, 0);
506 old_scope_id = scanner->scope_id;
507 scanner->scope_id = scope_id;
513 g_scanner_foreach_internal (gpointer _key,
517 register GScannerKey *key;
518 register gpointer *d;
519 register GHFunc func;
520 register gpointer func_data;
521 register guint *scope_id;
524 func = (GHFunc) d[0];
529 if (key->scope_id == *scope_id)
530 func (key->symbol, key->value, func_data);
534 g_scanner_scope_foreach_symbol (GScanner *scanner,
541 g_return_if_fail (scanner != NULL);
543 d[0] = (gpointer) func;
547 g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
551 g_scanner_freeze_symbol_table (GScanner *scanner)
553 g_return_if_fail (scanner != NULL);
555 g_hash_table_freeze (scanner->symbol_table);
559 g_scanner_thaw_symbol_table (GScanner *scanner)
561 g_return_if_fail (scanner != NULL);
563 g_hash_table_thaw (scanner->symbol_table);
567 g_scanner_peek_next_token (GScanner *scanner)
569 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
571 if (scanner->next_token == G_TOKEN_NONE)
573 scanner->next_line = scanner->line;
574 scanner->next_position = scanner->position;
575 g_scanner_get_token_i (scanner,
576 &scanner->next_token,
577 &scanner->next_value,
579 &scanner->next_position);
582 return scanner->next_token;
586 g_scanner_get_next_token (GScanner *scanner)
588 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
590 if (scanner->next_token != G_TOKEN_NONE)
592 g_scanner_free_value (&scanner->token, &scanner->value);
594 scanner->token = scanner->next_token;
595 scanner->value = scanner->next_value;
596 scanner->line = scanner->next_line;
597 scanner->position = scanner->next_position;
598 scanner->next_token = G_TOKEN_NONE;
601 g_scanner_get_token_i (scanner,
607 return scanner->token;
611 g_scanner_cur_token (GScanner *scanner)
613 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
615 return scanner->token;
619 g_scanner_cur_value (GScanner *scanner)
623 g_return_val_if_fail (scanner != NULL, v);
625 /* MSC isn't capable of handling return scanner->value; ? */
635 g_scanner_cur_line (GScanner *scanner)
637 g_return_val_if_fail (scanner != NULL, 0);
639 return scanner->line;
643 g_scanner_cur_position (GScanner *scanner)
645 g_return_val_if_fail (scanner != NULL, 0);
647 return scanner->position;
651 g_scanner_eof (GScanner *scanner)
653 g_return_val_if_fail (scanner != NULL, TRUE);
655 return scanner->token == G_TOKEN_EOF;
659 g_scanner_input_file (GScanner *scanner,
662 g_return_if_fail (scanner != NULL);
663 g_return_if_fail (input_fd >= 0);
665 if (scanner->input_fd >= 0)
666 g_scanner_sync_file_offset (scanner);
668 scanner->token = G_TOKEN_NONE;
669 scanner->value.v_int = 0;
671 scanner->position = 0;
672 scanner->next_token = G_TOKEN_NONE;
674 scanner->input_fd = input_fd;
675 scanner->text = NULL;
676 scanner->text_end = NULL;
678 if (!scanner->buffer)
679 scanner->buffer = g_new (gchar, READ_BUFFER_SIZE + 1);
683 g_scanner_input_text (GScanner *scanner,
687 g_return_if_fail (scanner != NULL);
689 g_return_if_fail (text != NULL);
693 if (scanner->input_fd >= 0)
694 g_scanner_sync_file_offset (scanner);
696 scanner->token = G_TOKEN_NONE;
697 scanner->value.v_int = 0;
699 scanner->position = 0;
700 scanner->next_token = G_TOKEN_NONE;
702 scanner->input_fd = -1;
703 scanner->text = text;
704 scanner->text_end = text + text_len;
708 g_free (scanner->buffer);
709 scanner->buffer = NULL;
714 g_scanner_peek_next_char (GScanner *scanner)
716 if (scanner->text < scanner->text_end)
718 return *scanner->text;
720 else if (scanner->input_fd >= 0)
723 register gchar *buffer;
725 buffer = scanner->buffer;
728 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
730 while (count == -1 && (errno == EINTR || errno == EAGAIN));
734 scanner->input_fd = -1;
740 scanner->text = buffer;
741 scanner->text_end = buffer + count;
751 g_scanner_sync_file_offset (GScanner *scanner)
753 g_return_if_fail (scanner != NULL);
755 /* for file input, rewind the filedescriptor to the current
756 * buffer position and blow the file read ahead buffer. usefull for
757 * third party uses of our filedescriptor, which hooks onto the current
761 if (scanner->input_fd >= 0 && scanner->text_end > scanner->text)
765 buffered = scanner->text_end - scanner->text;
766 if (lseek (scanner->input_fd, - buffered, SEEK_CUR) >= 0)
768 /* we succeeded, blow our buffer's contents now */
769 scanner->text = NULL;
770 scanner->text_end = NULL;
778 g_scanner_get_char (GScanner *scanner,
782 register guchar fchar;
784 if (scanner->text < scanner->text_end)
785 fchar = *(scanner->text++);
786 else if (scanner->input_fd >= 0)
789 register gchar *buffer;
791 buffer = scanner->buffer;
794 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
796 while (count == -1 && (errno == EINTR || errno == EAGAIN));
800 scanner->input_fd = -1;
805 scanner->text = buffer + 1;
806 scanner->text_end = buffer + count;
810 g_scanner_sync_file_offset (scanner);
811 scanner->text_end = scanner->text;
812 scanner->input_fd = -1;
833 g_scanner_unexp_token (GScanner *scanner,
834 GTokenType expected_token,
835 const gchar *identifier_spec,
836 const gchar *symbol_spec,
837 const gchar *symbol_name,
838 const gchar *message,
841 register gchar *token_string;
842 register guint token_string_len;
843 register gchar *expected_string;
844 register guint expected_string_len;
845 register gchar *message_prefix;
846 register gboolean print_unexp;
847 void (*msg_handler) (GScanner*, const gchar*, ...);
849 g_return_if_fail (scanner != NULL);
852 msg_handler = g_scanner_error;
854 msg_handler = g_scanner_warn;
856 if (!identifier_spec)
857 identifier_spec = "identifier";
859 symbol_spec = "symbol";
861 token_string_len = 56;
862 token_string = g_new (gchar, token_string_len + 1);
863 expected_string_len = 64;
864 expected_string = g_new (gchar, expected_string_len + 1);
867 switch (scanner->token)
870 g_snprintf (token_string, token_string_len, "end of file");
874 if (scanner->token >= 1 && scanner->token <= 255)
876 if ((scanner->token >= ' ' && scanner->token <= '~') ||
877 strchr (scanner->config->cset_identifier_first, scanner->token) ||
878 strchr (scanner->config->cset_identifier_nth, scanner->token))
879 g_snprintf (token_string, expected_string_len, "character `%c'", scanner->token);
881 g_snprintf (token_string, expected_string_len, "character `\\%o'", scanner->token);
884 else if (!scanner->config->symbol_2_token)
886 g_snprintf (token_string, token_string_len, "(unknown) token <%d>", scanner->token);
891 if (expected_token == G_TOKEN_SYMBOL ||
892 (scanner->config->symbol_2_token &&
893 expected_token > G_TOKEN_LAST))
896 g_snprintf (token_string,
899 print_unexp ? "" : "invalid ",
903 g_snprintf (token_string,
906 print_unexp ? "" : "invalid ",
912 expected_token = G_TOKEN_NONE;
913 switch (scanner->value.v_error)
915 case G_ERR_UNEXP_EOF:
916 g_snprintf (token_string, token_string_len, "scanner: unexpected end of file");
919 case G_ERR_UNEXP_EOF_IN_STRING:
920 g_snprintf (token_string, token_string_len, "scanner: unterminated string constant");
923 case G_ERR_UNEXP_EOF_IN_COMMENT:
924 g_snprintf (token_string, token_string_len, "scanner: unterminated comment");
927 case G_ERR_NON_DIGIT_IN_CONST:
928 g_snprintf (token_string, token_string_len, "scanner: non digit in constant");
931 case G_ERR_FLOAT_RADIX:
932 g_snprintf (token_string, token_string_len, "scanner: invalid radix for floating constant");
935 case G_ERR_FLOAT_MALFORMED:
936 g_snprintf (token_string, token_string_len, "scanner: malformed floating constant");
939 case G_ERR_DIGIT_RADIX:
940 g_snprintf (token_string, token_string_len, "scanner: digit is beyond radix");
945 g_snprintf (token_string, token_string_len, "scanner: unknown error");
951 g_snprintf (token_string, token_string_len, "character `%c'", scanner->value.v_char);
954 case G_TOKEN_IDENTIFIER:
955 if (expected_token == G_TOKEN_IDENTIFIER)
957 g_snprintf (token_string,
960 print_unexp ? "" : "invalid ",
962 scanner->value.v_string);
969 g_snprintf (token_string, token_string_len, "number `%ld'", scanner->value.v_int);
973 g_snprintf (token_string, token_string_len, "number `%.3f'", scanner->value.v_float);
977 if (expected_token == G_TOKEN_STRING)
979 g_snprintf (token_string,
981 "%s%sstring constant \"%s\"",
982 print_unexp ? "" : "invalid ",
983 scanner->value.v_string[0] == 0 ? "empty " : "",
984 scanner->value.v_string);
985 token_string[token_string_len - 2] = '"';
986 token_string[token_string_len - 1] = 0;
989 case G_TOKEN_COMMENT_SINGLE:
990 case G_TOKEN_COMMENT_MULTI:
991 g_snprintf (token_string, token_string_len, "comment");
995 /* somehow the user's parsing code is screwed, there isn't much
996 * we can do about it.
997 * Note, a common case to trigger this is
998 * g_scanner_peek_next_token(); g_scanner_unexp_token();
999 * without an intermediate g_scanner_get_next_token().
1001 g_assert_not_reached ();
1006 switch (expected_token)
1008 gboolean need_valid;
1011 if (expected_token >= 1 && expected_token <= 255)
1013 if ((expected_token >= ' ' && expected_token <= '~') ||
1014 strchr (scanner->config->cset_identifier_first, expected_token) ||
1015 strchr (scanner->config->cset_identifier_nth, expected_token))
1016 g_snprintf (expected_string, expected_string_len, "character `%c'", expected_token);
1018 g_snprintf (expected_string, expected_string_len, "character `\\%o'", expected_token);
1021 else if (!scanner->config->symbol_2_token)
1023 g_snprintf (expected_string, expected_string_len, "(unknown) token <%d>", expected_token);
1027 case G_TOKEN_SYMBOL:
1028 need_valid = (scanner->token == G_TOKEN_SYMBOL ||
1029 (scanner->config->symbol_2_token &&
1030 scanner->token > G_TOKEN_LAST));
1031 g_snprintf (expected_string,
1032 expected_string_len,
1034 need_valid ? "valid " : "",
1036 /* FIXME: should we attempt to lookup the symbol_name for symbol_2_token? */
1040 g_snprintf (expected_string, expected_string_len, "number (integer)");
1044 g_snprintf (expected_string, expected_string_len, "number (float)");
1047 case G_TOKEN_STRING:
1048 g_snprintf (expected_string,
1049 expected_string_len,
1050 "%sstring constant",
1051 scanner->token == G_TOKEN_STRING ? "valid " : "");
1054 case G_TOKEN_IDENTIFIER:
1055 g_snprintf (expected_string,
1056 expected_string_len,
1058 scanner->token == G_TOKEN_IDENTIFIER ? "valid " : "",
1066 if (message && message[0] != 0)
1067 message_prefix = " - ";
1070 message_prefix = "";
1074 if (expected_token != G_TOKEN_NONE)
1077 msg_handler (scanner,
1078 "unexpected %s, expected %s%s%s",
1084 msg_handler (scanner,
1085 "%s, expected %s%s%s",
1094 msg_handler (scanner,
1095 "unexpected %s%s%s",
1100 msg_handler (scanner,
1107 g_free (token_string);
1108 g_free (expected_string);
1112 g_scanner_stat_mode (const gchar *filename)
1114 struct stat *stat_buf;
1117 stat_buf = g_new0 (struct stat, 1);
1119 lstat (filename, stat_buf);
1121 stat (filename, stat_buf);
1123 st_mode = stat_buf->st_mode;
1131 g_scanner_get_token_i (GScanner *scanner,
1132 GTokenType *token_p,
1133 GTokenValue *value_p,
1139 g_scanner_free_value (token_p, value_p);
1140 g_scanner_get_token_ll (scanner, token_p, value_p, line_p, position_p);
1142 while (((*token_p > 0 && *token_p < 256) &&
1143 strchr (scanner->config->cset_skip_characters, *token_p)) ||
1144 (*token_p == G_TOKEN_CHAR &&
1145 strchr (scanner->config->cset_skip_characters, value_p->v_char)) ||
1146 (*token_p == G_TOKEN_COMMENT_MULTI &&
1147 scanner->config->skip_comment_multi) ||
1148 (*token_p == G_TOKEN_COMMENT_SINGLE &&
1149 scanner->config->skip_comment_single));
1153 case G_TOKEN_IDENTIFIER:
1154 if (scanner->config->identifier_2_string)
1155 *token_p = G_TOKEN_STRING;
1158 case G_TOKEN_SYMBOL:
1159 if (scanner->config->symbol_2_token)
1160 *token_p = (GTokenType) value_p->v_symbol;
1163 case G_TOKEN_BINARY:
1166 if (scanner->config->numbers_2_int)
1167 *token_p = G_TOKEN_INT;
1174 if (*token_p == G_TOKEN_INT &&
1175 scanner->config->int_2_float)
1177 *token_p = G_TOKEN_FLOAT;
1178 value_p->v_float = value_p->v_int;
1185 g_scanner_get_token_ll (GScanner *scanner,
1186 GTokenType *token_p,
1187 GTokenValue *value_p,
1191 register GScannerConfig *config;
1192 register GTokenType token;
1193 register gboolean in_comment_multi;
1194 register gboolean in_comment_single;
1195 register gboolean in_string_sq;
1196 register gboolean in_string_dq;
1197 register GString *gstring;
1201 config = scanner->config;
1202 (*value_p).v_int = 0;
1204 if ((scanner->text >= scanner->text_end && scanner->input_fd < 0) ||
1205 scanner->token == G_TOKEN_EOF)
1207 *token_p = G_TOKEN_EOF;
1211 in_comment_multi = FALSE;
1212 in_comment_single = FALSE;
1213 in_string_sq = FALSE;
1214 in_string_dq = FALSE;
1217 do /* while (ch != 0) */
1219 register gboolean dotted_float = FALSE;
1221 ch = g_scanner_get_char (scanner, line_p, position_p);
1224 token = G_TOKEN_NONE;
1226 /* this is *evil*, but needed ;(
1227 * we first check for identifier first character, because it
1228 * might interfere with other key chars like slashes or numbers
1230 if (config->scan_identifier &&
1231 ch && strchr (config->cset_identifier_first, ch))
1232 goto identifier_precedence;
1237 token = G_TOKEN_EOF;
1243 if (!config->scan_comment_multi ||
1244 g_scanner_peek_next_char (scanner) != '*')
1246 g_scanner_get_char (scanner, line_p, position_p);
1247 token = G_TOKEN_COMMENT_MULTI;
1248 in_comment_multi = TRUE;
1249 gstring = g_string_new ("");
1250 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1252 if (ch == '*' && g_scanner_peek_next_char (scanner) == '/')
1254 g_scanner_get_char (scanner, line_p, position_p);
1255 in_comment_multi = FALSE;
1259 gstring = g_string_append_c (gstring, ch);
1265 if (!config->scan_string_sq)
1267 token = G_TOKEN_STRING;
1268 in_string_sq = TRUE;
1269 gstring = g_string_new ("");
1270 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1274 in_string_sq = FALSE;
1278 gstring = g_string_append_c (gstring, ch);
1284 if (!config->scan_string_dq)
1286 token = G_TOKEN_STRING;
1287 in_string_dq = TRUE;
1288 gstring = g_string_new ("");
1289 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1293 in_string_dq = FALSE;
1300 ch = g_scanner_get_char (scanner, line_p, position_p);
1304 register guint fchar;
1310 gstring = g_string_append_c (gstring, '\\');
1314 gstring = g_string_append_c (gstring, '\n');
1318 gstring = g_string_append_c (gstring, '\t');
1322 gstring = g_string_append_c (gstring, '\r');
1326 gstring = g_string_append_c (gstring, '\b');
1330 gstring = g_string_append_c (gstring, '\f');
1342 fchar = g_scanner_peek_next_char (scanner);
1343 if (fchar >= '0' && fchar <= '7')
1345 ch = g_scanner_get_char (scanner, line_p, position_p);
1346 i = i * 8 + ch - '0';
1347 fchar = g_scanner_peek_next_char (scanner);
1348 if (fchar >= '0' && fchar <= '7')
1350 ch = g_scanner_get_char (scanner, line_p, position_p);
1351 i = i * 8 + ch - '0';
1354 gstring = g_string_append_c (gstring, i);
1358 gstring = g_string_append_c (gstring, ch);
1363 gstring = g_string_append_c (gstring, ch);
1370 if (!config->scan_float)
1372 token = G_TOKEN_FLOAT;
1373 dotted_float = TRUE;
1374 ch = g_scanner_get_char (scanner, line_p, position_p);
1375 goto number_parsing;
1378 if (!config->scan_hex_dollar)
1380 token = G_TOKEN_HEX;
1381 ch = g_scanner_get_char (scanner, line_p, position_p);
1382 goto number_parsing;
1385 if (config->scan_octal)
1386 token = G_TOKEN_OCTAL;
1388 token = G_TOKEN_INT;
1389 ch = g_scanner_peek_next_char (scanner);
1390 if (config->scan_hex && (ch == 'x' || ch == 'X'))
1392 token = G_TOKEN_HEX;
1393 g_scanner_get_char (scanner, line_p, position_p);
1394 ch = g_scanner_get_char (scanner, line_p, position_p);
1397 token = G_TOKEN_ERROR;
1398 value.v_error = G_ERR_UNEXP_EOF;
1402 if (g_scanner_char_2_num (ch, 16) < 0)
1404 token = G_TOKEN_ERROR;
1405 value.v_error = G_ERR_DIGIT_RADIX;
1410 else if (config->scan_binary && (ch == 'b' || ch == 'B'))
1412 token = G_TOKEN_BINARY;
1413 g_scanner_get_char (scanner, line_p, position_p);
1414 ch = g_scanner_get_char (scanner, line_p, position_p);
1417 token = G_TOKEN_ERROR;
1418 value.v_error = G_ERR_UNEXP_EOF;
1422 if (g_scanner_char_2_num (ch, 10) < 0)
1424 token = G_TOKEN_ERROR;
1425 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1444 register gboolean in_number = TRUE;
1447 if (token == G_TOKEN_NONE)
1448 token = G_TOKEN_INT;
1450 gstring = g_string_new (dotted_float ? "0." : "");
1451 gstring = g_string_append_c (gstring, ch);
1453 do /* while (in_number) */
1455 register gboolean is_E;
1457 is_E = token == G_TOKEN_FLOAT && (ch == 'e' || ch == 'E');
1459 ch = g_scanner_peek_next_char (scanner);
1461 if (g_scanner_char_2_num (ch, 36) >= 0 ||
1462 (config->scan_float && ch == '.') ||
1463 (is_E && (ch == '+' || ch == '-')))
1465 ch = g_scanner_get_char (scanner, line_p, position_p);
1470 if (token != G_TOKEN_INT && token != G_TOKEN_OCTAL)
1472 value.v_error = token == G_TOKEN_FLOAT ? G_ERR_FLOAT_MALFORMED : G_ERR_FLOAT_RADIX;
1473 token = G_TOKEN_ERROR;
1478 token = G_TOKEN_FLOAT;
1479 gstring = g_string_append_c (gstring, ch);
1493 gstring = g_string_append_c (gstring, ch);
1498 if (token != G_TOKEN_FLOAT)
1500 token = G_TOKEN_ERROR;
1501 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1505 gstring = g_string_append_c (gstring, ch);
1510 if ((token != G_TOKEN_HEX && !config->scan_float) ||
1511 (token != G_TOKEN_HEX &&
1512 token != G_TOKEN_OCTAL &&
1513 token != G_TOKEN_FLOAT &&
1514 token != G_TOKEN_INT))
1516 token = G_TOKEN_ERROR;
1517 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1522 if (token != G_TOKEN_HEX)
1523 token = G_TOKEN_FLOAT;
1524 gstring = g_string_append_c (gstring, ch);
1529 if (token != G_TOKEN_HEX)
1531 token = G_TOKEN_ERROR;
1532 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1536 gstring = g_string_append_c (gstring, ch);
1548 case G_TOKEN_BINARY:
1549 value.v_binary = strtol (gstring->str, &endptr, 2);
1553 value.v_octal = strtol (gstring->str, &endptr, 8);
1557 value.v_int = strtol (gstring->str, &endptr, 10);
1561 value.v_float = g_strtod (gstring->str, &endptr);
1565 value.v_hex = strtol (gstring->str, &endptr, 16);
1571 if (endptr && *endptr)
1573 token = G_TOKEN_ERROR;
1574 if (*endptr == 'e' || *endptr == 'E')
1575 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1577 value.v_error = G_ERR_DIGIT_RADIX;
1579 g_string_free (gstring, TRUE);
1582 } /* number_parsing:... */
1588 if (config->cpair_comment_single &&
1589 ch == config->cpair_comment_single[0])
1591 token = G_TOKEN_COMMENT_SINGLE;
1592 in_comment_single = TRUE;
1593 gstring = g_string_new ("");
1594 while ((ch = g_scanner_get_char (scanner,
1598 if (ch == config->cpair_comment_single[1])
1600 in_comment_single = FALSE;
1605 gstring = g_string_append_c (gstring, ch);
1609 else if (config->scan_identifier && ch &&
1610 strchr (config->cset_identifier_first, ch))
1612 identifier_precedence:
1614 if (config->cset_identifier_nth && ch &&
1615 strchr (config->cset_identifier_nth,
1616 g_scanner_peek_next_char (scanner)))
1618 token = G_TOKEN_IDENTIFIER;
1619 gstring = g_string_new ("");
1620 gstring = g_string_append_c (gstring, ch);
1623 ch = g_scanner_get_char (scanner, line_p, position_p);
1624 gstring = g_string_append_c (gstring, ch);
1625 ch = g_scanner_peek_next_char (scanner);
1627 while (ch && strchr (config->cset_identifier_nth, ch));
1630 else if (config->scan_identifier_1char)
1632 token = G_TOKEN_IDENTIFIER;
1633 value.v_identifier = g_new0 (gchar, 2);
1634 value.v_identifier[0] = ch;
1640 if (config->char_2_token)
1644 token = G_TOKEN_CHAR;
1649 } /* default_case:... */
1652 g_assert (ch == 0 && token != G_TOKEN_NONE); /* paranoid */
1656 if (in_comment_multi || in_comment_single ||
1657 in_string_sq || in_string_dq)
1659 token = G_TOKEN_ERROR;
1662 g_string_free (gstring, TRUE);
1666 if (in_comment_multi || in_comment_single)
1667 value.v_error = G_ERR_UNEXP_EOF_IN_COMMENT;
1668 else /* (in_string_sq || in_string_dq) */
1669 value.v_error = G_ERR_UNEXP_EOF_IN_STRING;
1674 value.v_string = gstring->str;
1675 g_string_free (gstring, FALSE);
1679 if (token == G_TOKEN_IDENTIFIER)
1681 if (config->scan_symbols)
1683 register GScannerKey *key;
1684 register guint scope_id;
1686 scope_id = scanner->scope_id;
1687 key = g_scanner_lookup_internal (scanner, scope_id, value.v_identifier);
1688 if (!key && scope_id && scanner->config->scope_0_fallback)
1689 key = g_scanner_lookup_internal (scanner, 0, value.v_identifier);
1693 g_free (value.v_identifier);
1694 token = G_TOKEN_SYMBOL;
1695 value.v_symbol = key->value;
1699 if (token == G_TOKEN_IDENTIFIER &&
1700 config->scan_identifier_NULL &&
1701 strlen (value.v_identifier) == 4)
1703 gchar *null_upper = "NULL";
1704 gchar *null_lower = "null";
1706 if (scanner->config->case_sensitive)
1708 if (value.v_identifier[0] == null_upper[0] &&
1709 value.v_identifier[1] == null_upper[1] &&
1710 value.v_identifier[2] == null_upper[2] &&
1711 value.v_identifier[3] == null_upper[3])
1712 token = G_TOKEN_IDENTIFIER_NULL;
1716 if ((value.v_identifier[0] == null_upper[0] ||
1717 value.v_identifier[0] == null_lower[0]) &&
1718 (value.v_identifier[1] == null_upper[1] ||
1719 value.v_identifier[1] == null_lower[1]) &&
1720 (value.v_identifier[2] == null_upper[2] ||
1721 value.v_identifier[2] == null_lower[2]) &&
1722 (value.v_identifier[3] == null_upper[3] ||
1723 value.v_identifier[3] == null_lower[3]))
1724 token = G_TOKEN_IDENTIFIER_NULL;