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__
30 #include <sys/types.h> /* needed for sys/stat.h */
37 #define to_lower(c) ( \
39 ( (((guchar)(c))>='A' && ((guchar)(c))<='Z') * ('a'-'A') ) | \
40 ( (((guchar)(c))>=192 && ((guchar)(c))<=214) * (224-192) ) | \
41 ( (((guchar)(c))>=216 && ((guchar)(c))<=222) * (248-216) ) | \
45 #define READ_BUFFER_SIZE (4000)
48 /* --- typedefs --- */
49 typedef struct _GScannerKey GScannerKey;
60 /* --- variables --- */
61 static GScannerConfig g_scanner_config_template =
65 ) /* cset_skip_characters */,
70 ) /* cset_identifier_first */,
77 ) /* cset_identifier_nth */,
78 ( "#\n" ) /* cpair_comment_single */,
80 FALSE /* case_sensitive */,
82 TRUE /* skip_comment_multi */,
83 TRUE /* skip_comment_single */,
84 TRUE /* scan_comment_multi */,
85 TRUE /* scan_identifier */,
86 FALSE /* scan_identifier_1char */,
87 FALSE /* scan_identifier_NULL */,
88 TRUE /* scan_symbols */,
89 FALSE /* scan_binary */,
90 TRUE /* scan_octal */,
91 TRUE /* scan_float */,
93 FALSE /* scan_hex_dollar */,
94 TRUE /* scan_string_sq */,
95 TRUE /* scan_string_dq */,
96 TRUE /* numbers_2_int */,
97 FALSE /* int_2_float */,
98 FALSE /* identifier_2_string */,
99 TRUE /* char_2_token */,
100 FALSE /* symbol_2_token */,
101 FALSE /* scope_0_fallback */,
105 /* --- prototypes --- */
107 GScannerKey* g_scanner_lookup_internal (GScanner *scanner,
109 const gchar *symbol);
110 static gint g_scanner_key_equal (gconstpointer v1,
112 static guint g_scanner_key_hash (gconstpointer v);
113 static void g_scanner_get_token_ll (GScanner *scanner,
115 GTokenValue *value_p,
118 static void g_scanner_get_token_i (GScanner *scanner,
120 GTokenValue *value_p,
124 static guchar g_scanner_peek_next_char (GScanner *scanner);
125 static guchar g_scanner_get_char (GScanner *scanner,
128 static void g_scanner_msg_handler (GScanner *scanner,
133 /* --- functions --- */
135 g_scanner_char_2_num (guchar c,
138 if (c >= '0' && c <= '9')
140 else if (c >= 'A' && c <= 'Z')
142 else if (c >= 'a' && c <= 'z')
154 g_scanner_new (GScannerConfig *config_templ)
156 register GScanner *scanner;
159 config_templ = &g_scanner_config_template;
161 scanner = g_new0 (GScanner, 1);
163 scanner->user_data = NULL;
164 scanner->max_parse_errors = 0;
165 scanner->parse_errors = 0;
166 scanner->input_name = NULL;
167 scanner->derived_data = NULL;
169 scanner->config = g_new0 (GScannerConfig, 1);
171 scanner->config->case_sensitive = config_templ->case_sensitive;
172 scanner->config->cset_skip_characters = config_templ->cset_skip_characters;
173 scanner->config->cset_identifier_first= config_templ->cset_identifier_first;
174 scanner->config->cset_identifier_nth = config_templ->cset_identifier_nth;
175 scanner->config->cpair_comment_single = config_templ->cpair_comment_single;
176 scanner->config->skip_comment_multi = config_templ->skip_comment_multi;
177 scanner->config->skip_comment_single = config_templ->skip_comment_single;
178 scanner->config->scan_comment_multi = config_templ->scan_comment_multi;
179 scanner->config->scan_identifier = config_templ->scan_identifier;
180 scanner->config->scan_identifier_1char= config_templ->scan_identifier_1char;
181 scanner->config->scan_identifier_NULL = config_templ->scan_identifier_NULL;
182 scanner->config->scan_symbols = config_templ->scan_symbols;
183 scanner->config->scan_binary = config_templ->scan_binary;
184 scanner->config->scan_octal = config_templ->scan_octal;
185 scanner->config->scan_float = config_templ->scan_float;
186 scanner->config->scan_hex = config_templ->scan_hex;
187 scanner->config->scan_hex_dollar = config_templ->scan_hex_dollar;
188 scanner->config->scan_string_sq = config_templ->scan_string_sq;
189 scanner->config->scan_string_dq = config_templ->scan_string_dq;
190 scanner->config->numbers_2_int = config_templ->numbers_2_int;
191 scanner->config->int_2_float = config_templ->int_2_float;
192 scanner->config->identifier_2_string = config_templ->identifier_2_string;
193 scanner->config->char_2_token = config_templ->char_2_token;
194 scanner->config->symbol_2_token = config_templ->symbol_2_token;
195 scanner->config->scope_0_fallback = config_templ->scope_0_fallback;
197 scanner->token = G_TOKEN_NONE;
198 scanner->value.v_int = 0;
200 scanner->position = 0;
202 scanner->next_token = G_TOKEN_NONE;
203 scanner->next_value.v_int = 0;
204 scanner->next_line = 1;
205 scanner->next_position = 0;
207 scanner->symbol_table = g_hash_table_new (g_scanner_key_hash, g_scanner_key_equal);
208 scanner->input_fd = -1;
209 scanner->text = NULL;
210 scanner->text_end = NULL;
211 scanner->buffer = NULL;
212 scanner->scope_id = 0;
214 scanner->msg_handler = g_scanner_msg_handler;
220 g_scanner_free_value (GTokenType *token_p,
221 GTokenValue *value_p)
226 case G_TOKEN_IDENTIFIER:
227 case G_TOKEN_IDENTIFIER_NULL:
228 case G_TOKEN_COMMENT_SINGLE:
229 case G_TOKEN_COMMENT_MULTI:
230 g_free (value_p->v_string);
237 *token_p = G_TOKEN_NONE;
241 g_scanner_destroy_symbol_table_entry (gpointer _key,
245 GScannerKey *key = _key;
247 g_free (key->symbol);
252 g_scanner_destroy (GScanner *scanner)
254 g_return_if_fail (scanner != NULL);
256 g_hash_table_foreach (scanner->symbol_table,
257 g_scanner_destroy_symbol_table_entry, NULL);
258 g_hash_table_destroy (scanner->symbol_table);
259 g_scanner_free_value (&scanner->token, &scanner->value);
260 g_scanner_free_value (&scanner->next_token, &scanner->next_value);
261 g_free (scanner->config);
262 g_free (scanner->buffer);
267 g_scanner_msg_handler (GScanner *scanner,
271 g_return_if_fail (scanner != NULL);
273 fprintf (stdout, "%s:%d: ", scanner->input_name, scanner->line);
275 fprintf (stdout, "error: ");
276 fprintf (stdout, "%s\n", message);
280 g_scanner_error (GScanner *scanner,
284 g_return_if_fail (scanner != NULL);
285 g_return_if_fail (format != NULL);
287 scanner->parse_errors++;
289 if (scanner->msg_handler)
294 va_start (args, format);
295 string = g_strdup_vprintf (format, args);
298 scanner->msg_handler (scanner, string, TRUE);
305 g_scanner_warn (GScanner *scanner,
309 g_return_if_fail (scanner != NULL);
310 g_return_if_fail (format != NULL);
312 if (scanner->msg_handler)
317 va_start (args, format);
318 string = g_strdup_vprintf (format, args);
321 scanner->msg_handler (scanner, string, FALSE);
328 g_scanner_key_equal (gconstpointer v1,
331 register const GScannerKey *key1 = v1;
332 register const GScannerKey *key2 = v2;
334 return (key1->scope_id == key2->scope_id) && (strcmp (key1->symbol, key2->symbol) == 0);
338 g_scanner_key_hash (gconstpointer v)
340 register const GScannerKey *key = v;
345 for (c = key->symbol; *c; c++)
361 static inline GScannerKey*
362 g_scanner_lookup_internal (GScanner *scanner,
366 register GScannerKey *key_p;
369 key.scope_id = scope_id;
371 if (!scanner->config->case_sensitive)
374 register const gchar *c;
376 key.symbol = g_new (gchar, strlen (symbol) + 1);
377 for (d = key.symbol, c = symbol; *c; c++, d++)
380 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
385 key.symbol = (gchar*) symbol;
386 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
393 g_scanner_scope_add_symbol (GScanner *scanner,
398 register GScannerKey *key;
400 g_return_if_fail (scanner != NULL);
401 g_return_if_fail (symbol != NULL);
403 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
407 key = g_new (GScannerKey, 1);
408 key->scope_id = scope_id;
409 key->symbol = g_strdup (symbol);
411 if (!scanner->config->case_sensitive)
422 g_hash_table_insert (scanner->symbol_table, key, key);
429 g_scanner_scope_remove_symbol (GScanner *scanner,
433 register GScannerKey *key;
435 g_return_if_fail (scanner != NULL);
436 g_return_if_fail (symbol != NULL);
438 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
442 g_hash_table_remove (scanner->symbol_table, key);
443 g_free (key->symbol);
449 g_scanner_lookup_symbol (GScanner *scanner,
452 register GScannerKey *key;
453 register guint scope_id;
455 g_return_val_if_fail (scanner != NULL, NULL);
460 scope_id = scanner->scope_id;
461 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
462 if (!key && scope_id && scanner->config->scope_0_fallback)
463 key = g_scanner_lookup_internal (scanner, 0, symbol);
472 g_scanner_scope_lookup_symbol (GScanner *scanner,
476 register GScannerKey *key;
478 g_return_val_if_fail (scanner != NULL, NULL);
483 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
492 g_scanner_set_scope (GScanner *scanner,
495 register guint old_scope_id;
497 g_return_val_if_fail (scanner != NULL, 0);
499 old_scope_id = scanner->scope_id;
500 scanner->scope_id = scope_id;
506 g_scanner_foreach_internal (gpointer _key,
510 register GScannerKey *key;
511 register gpointer *d;
512 register GHFunc func;
513 register gpointer func_data;
514 register guint *scope_id;
517 func = (GHFunc) d[0];
522 if (key->scope_id == *scope_id)
523 func (key->symbol, key->value, func_data);
527 g_scanner_scope_foreach_symbol (GScanner *scanner,
534 g_return_if_fail (scanner != NULL);
536 d[0] = (gpointer) func;
540 g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
544 g_scanner_freeze_symbol_table (GScanner *scanner)
546 g_return_if_fail (scanner != NULL);
548 g_hash_table_freeze (scanner->symbol_table);
552 g_scanner_thaw_symbol_table (GScanner *scanner)
554 g_return_if_fail (scanner != NULL);
556 g_hash_table_thaw (scanner->symbol_table);
560 g_scanner_peek_next_token (GScanner *scanner)
562 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
564 if (scanner->next_token == G_TOKEN_NONE)
566 scanner->next_line = scanner->line;
567 scanner->next_position = scanner->position;
568 g_scanner_get_token_i (scanner,
569 &scanner->next_token,
570 &scanner->next_value,
572 &scanner->next_position);
575 return scanner->next_token;
579 g_scanner_get_next_token (GScanner *scanner)
581 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
583 if (scanner->next_token != G_TOKEN_NONE)
585 g_scanner_free_value (&scanner->token, &scanner->value);
587 scanner->token = scanner->next_token;
588 scanner->value = scanner->next_value;
589 scanner->line = scanner->next_line;
590 scanner->position = scanner->next_position;
591 scanner->next_token = G_TOKEN_NONE;
594 g_scanner_get_token_i (scanner,
600 return scanner->token;
604 g_scanner_cur_token (GScanner *scanner)
606 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
608 return scanner->token;
612 g_scanner_cur_value (GScanner *scanner)
617 g_return_val_if_fail (scanner != NULL, v);
619 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;
647 g_scanner_input_file (GScanner *scanner,
650 g_return_if_fail (scanner != NULL);
651 g_return_if_fail (input_fd >= 0);
653 scanner->token = G_TOKEN_NONE;
654 scanner->value.v_int = 0;
656 scanner->position = 0;
657 scanner->next_token = G_TOKEN_NONE;
659 scanner->input_fd = input_fd;
660 scanner->text = NULL;
661 scanner->text_end = NULL;
663 if (!scanner->buffer)
664 scanner->buffer = g_new (gchar, READ_BUFFER_SIZE + 1);
668 g_scanner_input_text (GScanner *scanner,
672 g_return_if_fail (scanner != NULL);
674 g_return_if_fail (text != NULL);
678 scanner->token = G_TOKEN_NONE;
679 scanner->value.v_int = 0;
681 scanner->position = 0;
682 scanner->next_token = G_TOKEN_NONE;
684 scanner->input_fd = -1;
685 scanner->text = text;
686 scanner->text_end = text + text_len;
690 g_free (scanner->buffer);
691 scanner->buffer = NULL;
696 g_scanner_peek_next_char (GScanner *scanner)
698 if (scanner->text < scanner->text_end)
700 return *scanner->text;
702 else if (scanner->input_fd >= 0)
705 register gchar *buffer;
707 buffer = scanner->buffer;
710 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
712 while (count == -1 && (errno == EINTR || errno == EAGAIN));
716 scanner->input_fd = -1;
722 scanner->text = buffer;
723 scanner->text_end = buffer + count;
733 g_scanner_get_char (GScanner *scanner,
737 register guchar fchar;
739 if (scanner->text < scanner->text_end)
740 fchar = *(scanner->text++);
741 else if (scanner->input_fd >= 0)
744 register gchar *buffer;
746 buffer = scanner->buffer;
749 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
751 while (count == -1 && (errno == EINTR || errno == EAGAIN));
755 scanner->input_fd = -1;
760 scanner->text = buffer + 1;
761 scanner->text_end = buffer + count;
782 g_scanner_unexp_token (GScanner *scanner,
783 GTokenType expected_token,
784 const gchar *identifier_spec,
785 const gchar *symbol_spec,
786 const gchar *symbol_name,
787 const gchar *message,
790 register gchar *token_string;
791 register guint token_string_len;
792 register gchar *expected_string;
793 register guint expected_string_len;
794 register gchar *message_prefix;
795 register gboolean print_unexp;
796 void (*msg_handler) (GScanner*, const gchar*, ...);
798 g_return_if_fail (scanner != NULL);
801 msg_handler = g_scanner_error;
803 msg_handler = g_scanner_warn;
805 if (!identifier_spec)
806 identifier_spec = "identifier";
808 symbol_spec = "symbol";
810 token_string_len = 56;
811 token_string = g_new (gchar, token_string_len + 1);
812 expected_string_len = 64;
813 expected_string = g_new (gchar, expected_string_len + 1);
816 switch (scanner->token)
819 g_snprintf (token_string, token_string_len, "end of file");
823 if (scanner->token >= 1 && scanner->token <= 255)
825 if ((scanner->token >= ' ' && scanner->token <= '~') ||
826 strchr (scanner->config->cset_identifier_first, scanner->token) ||
827 strchr (scanner->config->cset_identifier_nth, scanner->token))
828 g_snprintf (token_string, expected_string_len, "character `%c'", scanner->token);
830 g_snprintf (token_string, expected_string_len, "character `\\%o'", scanner->token);
833 else if (!scanner->config->symbol_2_token)
835 g_snprintf (token_string, token_string_len, "(unknown) token <%d>", scanner->token);
840 if (expected_token == G_TOKEN_SYMBOL ||
841 (scanner->config->symbol_2_token &&
842 expected_token > G_TOKEN_LAST))
845 g_snprintf (token_string,
848 print_unexp ? "" : "invalid ",
852 g_snprintf (token_string,
855 print_unexp ? "" : "invalid ",
861 expected_token = G_TOKEN_NONE;
862 switch (scanner->value.v_error)
864 case G_ERR_UNEXP_EOF:
865 g_snprintf (token_string, token_string_len, "scanner: unexpected end of file");
868 case G_ERR_UNEXP_EOF_IN_STRING:
869 g_snprintf (token_string, token_string_len, "scanner: unterminated string constant");
872 case G_ERR_UNEXP_EOF_IN_COMMENT:
873 g_snprintf (token_string, token_string_len, "scanner: unterminated comment");
876 case G_ERR_NON_DIGIT_IN_CONST:
877 g_snprintf (token_string, token_string_len, "scanner: non digit in constant");
880 case G_ERR_FLOAT_RADIX:
881 g_snprintf (token_string, token_string_len, "scanner: invalid radix for floating constant");
884 case G_ERR_FLOAT_MALFORMED:
885 g_snprintf (token_string, token_string_len, "scanner: malformed floating constant");
888 case G_ERR_DIGIT_RADIX:
889 g_snprintf (token_string, token_string_len, "scanner: digit is beyond radix");
894 g_snprintf (token_string, token_string_len, "scanner: unknown error");
900 g_snprintf (token_string, token_string_len, "character `%c'", scanner->value.v_char);
903 case G_TOKEN_IDENTIFIER:
904 if (expected_token == G_TOKEN_IDENTIFIER)
906 g_snprintf (token_string,
909 print_unexp ? "" : "invalid ",
911 scanner->value.v_string);
918 g_snprintf (token_string, token_string_len, "number `%ld'", scanner->value.v_int);
922 g_snprintf (token_string, token_string_len, "number `%.3f'", scanner->value.v_float);
926 if (expected_token == G_TOKEN_STRING)
928 g_snprintf (token_string,
930 "%s%sstring constant \"%s\"",
931 print_unexp ? "" : "invalid ",
932 scanner->value.v_string[0] == 0 ? "empty " : "",
933 scanner->value.v_string);
934 token_string[token_string_len - 2] = '"';
935 token_string[token_string_len - 1] = 0;
938 case G_TOKEN_COMMENT_SINGLE:
939 case G_TOKEN_COMMENT_MULTI:
940 g_snprintf (token_string, token_string_len, "comment");
944 /* somehow the user's parsing code is screwed, there isn't much
945 * we can do about it.
946 * Note, a common case to trigger this is
947 * g_scanner_peek_next_token(); g_scanner_unexp_token();
948 * without an intermediate g_scanner_get_next_token().
950 g_assert_not_reached ();
955 switch (expected_token)
960 if (expected_token >= 1 && expected_token <= 255)
962 if ((expected_token >= ' ' && expected_token <= '~') ||
963 strchr (scanner->config->cset_identifier_first, expected_token) ||
964 strchr (scanner->config->cset_identifier_nth, expected_token))
965 g_snprintf (expected_string, expected_string_len, "character `%c'", expected_token);
967 g_snprintf (expected_string, expected_string_len, "character `\\%o'", expected_token);
970 else if (!scanner->config->symbol_2_token)
972 g_snprintf (expected_string, expected_string_len, "(unknown) token <%d>", expected_token);
977 need_valid = (scanner->token == G_TOKEN_SYMBOL ||
978 (scanner->config->symbol_2_token &&
979 scanner->token > G_TOKEN_LAST));
980 g_snprintf (expected_string,
983 need_valid ? "valid " : "",
985 /* FIXME: should we attempt to lookup the symbol_name for symbol_2_token? */
989 g_snprintf (expected_string, expected_string_len, "number (integer)");
993 g_snprintf (expected_string, expected_string_len, "number (float)");
997 g_snprintf (expected_string,
1000 scanner->token == G_TOKEN_STRING ? "valid " : "");
1003 case G_TOKEN_IDENTIFIER:
1004 g_snprintf (expected_string,
1005 expected_string_len,
1007 scanner->token == G_TOKEN_IDENTIFIER ? "valid " : "",
1015 if (message && message[0] != 0)
1016 message_prefix = " - ";
1019 message_prefix = "";
1023 if (expected_token != G_TOKEN_NONE)
1026 msg_handler (scanner,
1027 "unexpected %s, expected %s%s%s",
1033 msg_handler (scanner,
1034 "%s, expected %s%s%s",
1043 msg_handler (scanner,
1044 "unexpected %s%s%s",
1049 msg_handler (scanner,
1056 g_free (token_string);
1057 g_free (expected_string);
1061 g_scanner_stat_mode (const gchar *filename)
1063 struct stat *stat_buf;
1066 stat_buf = g_new0 (struct stat, 1);
1068 lstat (filename, stat_buf);
1070 st_mode = stat_buf->st_mode;
1078 g_scanner_get_token_i (GScanner *scanner,
1079 GTokenType *token_p,
1080 GTokenValue *value_p,
1086 g_scanner_free_value (token_p, value_p);
1087 g_scanner_get_token_ll (scanner, token_p, value_p, line_p, position_p);
1089 while (((*token_p > 0 && *token_p < 256) &&
1090 strchr (scanner->config->cset_skip_characters, *token_p)) ||
1091 (*token_p == G_TOKEN_CHAR &&
1092 strchr (scanner->config->cset_skip_characters, value_p->v_char)) ||
1093 (*token_p == G_TOKEN_COMMENT_MULTI &&
1094 scanner->config->skip_comment_multi) ||
1095 (*token_p == G_TOKEN_COMMENT_SINGLE &&
1096 scanner->config->skip_comment_single));
1100 case G_TOKEN_IDENTIFIER:
1101 if (scanner->config->identifier_2_string)
1102 *token_p = G_TOKEN_STRING;
1105 case G_TOKEN_SYMBOL:
1106 if (scanner->config->symbol_2_token)
1107 *token_p = (GTokenType) value_p->v_symbol;
1110 case G_TOKEN_BINARY:
1113 if (scanner->config->numbers_2_int)
1114 *token_p = G_TOKEN_INT;
1121 if (*token_p == G_TOKEN_INT &&
1122 scanner->config->int_2_float)
1124 *token_p = G_TOKEN_FLOAT;
1125 value_p->v_float = value_p->v_int;
1132 g_scanner_get_token_ll (GScanner *scanner,
1133 GTokenType *token_p,
1134 GTokenValue *value_p,
1138 register GScannerConfig *config;
1139 register GTokenType token;
1140 register gboolean in_comment_multi;
1141 register gboolean in_comment_single;
1142 register gboolean in_string_sq;
1143 register gboolean in_string_dq;
1144 register GString *gstring;
1148 config = scanner->config;
1149 (*value_p).v_int = 0;
1151 if ((scanner->text >= scanner->text_end && scanner->input_fd < 0) ||
1152 scanner->token == G_TOKEN_EOF)
1154 *token_p = G_TOKEN_EOF;
1158 in_comment_multi = FALSE;
1159 in_comment_single = FALSE;
1160 in_string_sq = FALSE;
1161 in_string_dq = FALSE;
1164 do /* while (ch != 0) */
1166 register gboolean dotted_float = FALSE;
1168 ch = g_scanner_get_char (scanner, line_p, position_p);
1171 token = G_TOKEN_NONE;
1173 /* this is *evil*, but needed ;(
1174 * we first check for identifier first character, because it
1175 * might interfere with other key chars like slashes or numbers
1177 if (config->scan_identifier &&
1178 ch && strchr (config->cset_identifier_first, ch))
1179 goto identifier_precedence;
1184 token = G_TOKEN_EOF;
1190 if (!config->scan_comment_multi ||
1191 g_scanner_peek_next_char (scanner) != '*')
1193 g_scanner_get_char (scanner, line_p, position_p);
1194 token = G_TOKEN_COMMENT_MULTI;
1195 in_comment_multi = TRUE;
1196 gstring = g_string_new ("");
1197 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1199 if (ch == '*' && g_scanner_peek_next_char (scanner) == '/')
1201 g_scanner_get_char (scanner, line_p, position_p);
1202 in_comment_multi = FALSE;
1206 gstring = g_string_append_c (gstring, ch);
1212 if (!config->scan_string_sq)
1214 token = G_TOKEN_STRING;
1215 in_string_sq = TRUE;
1216 gstring = g_string_new ("");
1217 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1221 in_string_sq = FALSE;
1225 gstring = g_string_append_c (gstring, ch);
1231 if (!config->scan_string_dq)
1233 token = G_TOKEN_STRING;
1234 in_string_dq = TRUE;
1235 gstring = g_string_new ("");
1236 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1240 in_string_dq = FALSE;
1247 ch = g_scanner_get_char (scanner, line_p, position_p);
1251 register guint fchar;
1257 gstring = g_string_append_c (gstring, '\\');
1261 gstring = g_string_append_c (gstring, '\n');
1265 gstring = g_string_append_c (gstring, '\t');
1269 gstring = g_string_append_c (gstring, '\r');
1273 gstring = g_string_append_c (gstring, '\b');
1277 gstring = g_string_append_c (gstring, '\f');
1289 fchar = g_scanner_peek_next_char (scanner);
1290 if (fchar >= '0' && fchar <= '7')
1292 ch = g_scanner_get_char (scanner, line_p, position_p);
1293 i = i * 8 + ch - '0';
1294 fchar = g_scanner_peek_next_char (scanner);
1295 if (fchar >= '0' && fchar <= '7')
1297 ch = g_scanner_get_char (scanner, line_p, position_p);
1298 i = i * 8 + ch - '0';
1301 gstring = g_string_append_c (gstring, i);
1305 gstring = g_string_append_c (gstring, ch);
1310 gstring = g_string_append_c (gstring, ch);
1317 if (!config->scan_float)
1319 token = G_TOKEN_FLOAT;
1320 dotted_float = TRUE;
1321 ch = g_scanner_get_char (scanner, line_p, position_p);
1322 goto number_parsing;
1325 if (!config->scan_hex_dollar)
1327 token = G_TOKEN_HEX;
1328 ch = g_scanner_get_char (scanner, line_p, position_p);
1329 goto number_parsing;
1332 if (config->scan_octal)
1333 token = G_TOKEN_OCTAL;
1335 token = G_TOKEN_INT;
1336 ch = g_scanner_peek_next_char (scanner);
1337 if (config->scan_hex && (ch == 'x' || ch == 'X'))
1339 token = G_TOKEN_HEX;
1340 g_scanner_get_char (scanner, line_p, position_p);
1341 ch = g_scanner_get_char (scanner, line_p, position_p);
1344 token = G_TOKEN_ERROR;
1345 value.v_error = G_ERR_UNEXP_EOF;
1349 if (g_scanner_char_2_num (ch, 16) < 0)
1351 token = G_TOKEN_ERROR;
1352 value.v_error = G_ERR_DIGIT_RADIX;
1357 else if (config->scan_binary && (ch == 'b' || ch == 'B'))
1359 token = G_TOKEN_BINARY;
1360 g_scanner_get_char (scanner, line_p, position_p);
1361 ch = g_scanner_get_char (scanner, line_p, position_p);
1364 token = G_TOKEN_ERROR;
1365 value.v_error = G_ERR_UNEXP_EOF;
1369 if (g_scanner_char_2_num (ch, 10) < 0)
1371 token = G_TOKEN_ERROR;
1372 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1391 register gboolean in_number = TRUE;
1394 if (token == G_TOKEN_NONE)
1395 token = G_TOKEN_INT;
1397 gstring = g_string_new (dotted_float ? "0." : "");
1398 gstring = g_string_append_c (gstring, ch);
1400 do /* while (in_number) */
1402 register gboolean is_E;
1404 is_E = token == G_TOKEN_FLOAT && (ch == 'e' || ch == 'E');
1406 ch = g_scanner_peek_next_char (scanner);
1408 if (g_scanner_char_2_num (ch, 36) >= 0 ||
1409 (config->scan_float && ch == '.') ||
1410 (is_E && (ch == '+' || ch == '-')))
1412 ch = g_scanner_get_char (scanner, line_p, position_p);
1417 if (token != G_TOKEN_INT && token != G_TOKEN_OCTAL)
1419 value.v_error = token == G_TOKEN_FLOAT ? G_ERR_FLOAT_MALFORMED : G_ERR_FLOAT_RADIX;
1420 token = G_TOKEN_ERROR;
1425 token = G_TOKEN_FLOAT;
1426 gstring = g_string_append_c (gstring, ch);
1440 gstring = g_string_append_c (gstring, ch);
1445 if (token != G_TOKEN_FLOAT)
1447 token = G_TOKEN_ERROR;
1448 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1452 gstring = g_string_append_c (gstring, ch);
1457 if ((token != G_TOKEN_HEX && !config->scan_float) ||
1458 (token != G_TOKEN_HEX &&
1459 token != G_TOKEN_OCTAL &&
1460 token != G_TOKEN_FLOAT &&
1461 token != G_TOKEN_INT))
1463 token = G_TOKEN_ERROR;
1464 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1469 if (token != G_TOKEN_HEX)
1470 token = G_TOKEN_FLOAT;
1471 gstring = g_string_append_c (gstring, ch);
1476 if (token != G_TOKEN_HEX)
1478 token = G_TOKEN_ERROR;
1479 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1483 gstring = g_string_append_c (gstring, ch);
1495 case G_TOKEN_BINARY:
1496 value.v_binary = strtol (gstring->str, &endptr, 2);
1500 value.v_octal = strtol (gstring->str, &endptr, 8);
1504 value.v_int = strtol (gstring->str, &endptr, 10);
1508 value.v_float = g_strtod (gstring->str, &endptr);
1512 value.v_hex = strtol (gstring->str, &endptr, 16);
1518 if (endptr && *endptr)
1520 token = G_TOKEN_ERROR;
1521 if (*endptr == 'e' || *endptr == 'E')
1522 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1524 value.v_error = G_ERR_DIGIT_RADIX;
1526 g_string_free (gstring, TRUE);
1529 } /* number_parsing:... */
1535 if (config->cpair_comment_single &&
1536 ch == config->cpair_comment_single[0])
1538 token = G_TOKEN_COMMENT_SINGLE;
1539 in_comment_single = TRUE;
1540 gstring = g_string_new ("");
1541 while ((ch = g_scanner_get_char (scanner,
1545 if (ch == config->cpair_comment_single[1])
1547 in_comment_single = FALSE;
1552 gstring = g_string_append_c (gstring, ch);
1556 else if (config->scan_identifier && ch &&
1557 strchr (config->cset_identifier_first, ch))
1559 identifier_precedence:
1561 if (config->cset_identifier_nth && ch &&
1562 strchr (config->cset_identifier_nth,
1563 g_scanner_peek_next_char (scanner)))
1565 token = G_TOKEN_IDENTIFIER;
1566 gstring = g_string_new ("");
1567 gstring = g_string_append_c (gstring, ch);
1570 ch = g_scanner_get_char (scanner, line_p, position_p);
1571 gstring = g_string_append_c (gstring, ch);
1572 ch = g_scanner_peek_next_char (scanner);
1574 while (ch && strchr (config->cset_identifier_nth, ch));
1577 else if (config->scan_identifier_1char)
1579 token = G_TOKEN_IDENTIFIER;
1580 value.v_identifier = g_new0 (gchar, 2);
1581 value.v_identifier[0] = ch;
1587 if (config->char_2_token)
1591 token = G_TOKEN_CHAR;
1596 } /* default_case:... */
1599 g_assert (ch == 0 && token != G_TOKEN_NONE); /* paranoid */
1603 if (in_comment_multi || in_comment_single ||
1604 in_string_sq || in_string_dq)
1606 token = G_TOKEN_ERROR;
1609 g_string_free (gstring, TRUE);
1613 if (in_comment_multi || in_comment_single)
1614 value.v_error = G_ERR_UNEXP_EOF_IN_COMMENT;
1615 else /* (in_string_sq || in_string_dq) */
1616 value.v_error = G_ERR_UNEXP_EOF_IN_STRING;
1621 value.v_string = gstring->str;
1622 g_string_free (gstring, FALSE);
1626 if (token == G_TOKEN_IDENTIFIER)
1628 if (config->scan_symbols)
1630 register GScannerKey *key;
1631 register guint scope_id;
1633 scope_id = scanner->scope_id;
1634 key = g_scanner_lookup_internal (scanner, scope_id, value.v_identifier);
1635 if (!key && scope_id && scanner->config->scope_0_fallback)
1636 key = g_scanner_lookup_internal (scanner, 0, value.v_identifier);
1640 g_free (value.v_identifier);
1641 token = G_TOKEN_SYMBOL;
1642 value.v_symbol = key->value;
1646 if (token == G_TOKEN_IDENTIFIER &&
1647 config->scan_identifier_NULL &&
1648 strlen (value.v_identifier) == 4)
1650 gchar *null_upper = "NULL";
1651 gchar *null_lower = "null";
1653 if (scanner->config->case_sensitive)
1655 if (value.v_identifier[0] == null_upper[0] &&
1656 value.v_identifier[1] == null_upper[1] &&
1657 value.v_identifier[2] == null_upper[2] &&
1658 value.v_identifier[3] == null_upper[3])
1659 token = G_TOKEN_IDENTIFIER_NULL;
1663 if ((value.v_identifier[0] == null_upper[0] ||
1664 value.v_identifier[0] == null_lower[0]) &&
1665 (value.v_identifier[1] == null_upper[1] ||
1666 value.v_identifier[1] == null_lower[1]) &&
1667 (value.v_identifier[2] == null_upper[2] ||
1668 value.v_identifier[2] == null_lower[2]) &&
1669 (value.v_identifier[3] == null_upper[3] ||
1670 value.v_identifier[3] == null_lower[3]))
1671 token = G_TOKEN_IDENTIFIER_NULL;