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) ) | \
47 /* --- typedefs --- */
48 typedef struct _GScannerKey GScannerKey;
59 /* --- variables --- */
60 static GScannerConfig g_scanner_config_template =
64 ) /* cset_skip_characters */,
69 ) /* cset_identifier_first */,
76 ) /* cset_identifier_nth */,
77 ( "#\n" ) /* cpair_comment_single */,
79 FALSE /* case_sensitive */,
81 TRUE /* skip_comment_multi */,
82 TRUE /* skip_comment_single */,
83 TRUE /* scan_comment_multi */,
84 TRUE /* scan_identifier */,
85 FALSE /* scan_identifier_1char */,
86 FALSE /* scan_identifier_NULL */,
87 TRUE /* scan_symbols */,
88 FALSE /* scan_binary */,
89 TRUE /* scan_octal */,
90 TRUE /* scan_float */,
92 FALSE /* scan_hex_dollar */,
93 TRUE /* scan_string_sq */,
94 TRUE /* scan_string_dq */,
95 TRUE /* numbers_2_int */,
96 FALSE /* int_2_float */,
97 FALSE /* identifier_2_string */,
98 TRUE /* char_2_token */,
99 FALSE /* symbol_2_token */,
100 FALSE /* scope_0_fallback */,
104 /* --- prototypes --- */
106 gint g_scanner_char_2_num (guchar c,
109 GScannerKey* g_scanner_lookup_internal (GScanner *scanner,
111 const gchar *symbol);
112 static gint g_scanner_key_equal (gconstpointer v1,
114 static guint g_scanner_key_hash (gconstpointer v);
115 static void g_scanner_get_token_ll (GScanner *scanner,
120 static void g_scanner_get_token_i (GScanner *scanner,
125 static void g_scanner_free_value (GTokenType *token_p,
128 static guchar g_scanner_peek_next_char (GScanner *scanner);
129 static guchar g_scanner_get_char (GScanner *scanner,
132 static void g_scanner_msg_handler (GScanner *scanner,
137 /* --- functions --- */
139 g_scanner_char_2_num (guchar c,
142 if (c >= '0' && c <= '9')
144 else if (c >= 'A' && c <= 'Z')
146 else if (c >= 'a' && c <= 'z')
158 g_scanner_new (GScannerConfig *config_templ)
160 register GScanner *scanner;
163 config_templ = &g_scanner_config_template;
165 scanner = g_new0 (GScanner, 1);
167 scanner->user_data = NULL;
168 scanner->max_parse_errors = 0;
169 scanner->parse_errors = 0;
170 scanner->input_name = NULL;
171 scanner->derived_data = NULL;
173 scanner->config = g_new0 (GScannerConfig, 1);
175 scanner->config->case_sensitive = config_templ->case_sensitive;
176 scanner->config->cset_skip_characters = config_templ->cset_skip_characters;
177 scanner->config->cset_identifier_first= config_templ->cset_identifier_first;
178 scanner->config->cset_identifier_nth = config_templ->cset_identifier_nth;
179 scanner->config->cpair_comment_single = config_templ->cpair_comment_single;
180 scanner->config->skip_comment_multi = config_templ->skip_comment_multi;
181 scanner->config->skip_comment_single = config_templ->skip_comment_single;
182 scanner->config->scan_comment_multi = config_templ->scan_comment_multi;
183 scanner->config->scan_identifier = config_templ->scan_identifier;
184 scanner->config->scan_identifier_1char= config_templ->scan_identifier_1char;
185 scanner->config->scan_identifier_NULL = config_templ->scan_identifier_NULL;
186 scanner->config->scan_symbols = config_templ->scan_symbols;
187 scanner->config->scan_binary = config_templ->scan_binary;
188 scanner->config->scan_octal = config_templ->scan_octal;
189 scanner->config->scan_float = config_templ->scan_float;
190 scanner->config->scan_hex = config_templ->scan_hex;
191 scanner->config->scan_hex_dollar = config_templ->scan_hex_dollar;
192 scanner->config->scan_string_sq = config_templ->scan_string_sq;
193 scanner->config->scan_string_dq = config_templ->scan_string_dq;
194 scanner->config->numbers_2_int = config_templ->numbers_2_int;
195 scanner->config->int_2_float = config_templ->int_2_float;
196 scanner->config->identifier_2_string = config_templ->identifier_2_string;
197 scanner->config->char_2_token = config_templ->char_2_token;
198 scanner->config->symbol_2_token = config_templ->symbol_2_token;
199 scanner->config->scope_0_fallback = config_templ->scope_0_fallback;
201 scanner->token = G_TOKEN_NONE;
202 scanner->value.v_int = 0;
204 scanner->position = 0;
206 scanner->next_token = G_TOKEN_NONE;
207 scanner->next_value.v_int = 0;
208 scanner->next_line = 1;
209 scanner->next_position = 0;
211 scanner->symbol_table = g_hash_table_new (g_scanner_key_hash, g_scanner_key_equal);
212 scanner->text = NULL;
213 scanner->text_len = 0;
214 scanner->input_fd = -1;
215 scanner->peeked_char = -1;
216 scanner->scope_id = 0;
218 scanner->msg_handler = g_scanner_msg_handler;
224 g_scanner_destroy_symbol_table_entry (gpointer _key,
228 GScannerKey *key = _key;
230 g_free (key->symbol);
235 g_scanner_destroy (GScanner *scanner)
237 g_return_if_fail (scanner != NULL);
239 g_hash_table_foreach (scanner->symbol_table,
240 g_scanner_destroy_symbol_table_entry, NULL);
241 g_hash_table_destroy (scanner->symbol_table);
242 g_scanner_free_value (&scanner->token, &scanner->value);
243 g_scanner_free_value (&scanner->next_token, &scanner->next_value);
244 g_free (scanner->config);
249 g_scanner_msg_handler (GScanner *scanner,
253 g_return_if_fail (scanner != NULL);
255 fprintf (stdout, "%s:%d: ", scanner->input_name, scanner->line);
257 fprintf (stdout, "error: ");
258 fprintf (stdout, "%s\n", message);
262 g_scanner_error (GScanner *scanner,
266 g_return_if_fail (scanner != NULL);
267 g_return_if_fail (format != NULL);
269 scanner->parse_errors++;
271 if (scanner->msg_handler)
276 va_start (args, format);
277 string = g_strdup_vprintf (format, args);
280 scanner->msg_handler (scanner, string, TRUE);
287 g_scanner_warn (GScanner *scanner,
291 g_return_if_fail (scanner != NULL);
292 g_return_if_fail (format != NULL);
294 if (scanner->msg_handler)
299 va_start (args, format);
300 string = g_strdup_vprintf (format, args);
303 scanner->msg_handler (scanner, string, FALSE);
310 g_scanner_input_file (GScanner *scanner,
313 g_return_if_fail (input_fd >= 0);
315 scanner->token = G_TOKEN_NONE;
316 scanner->value.v_int = 0;
318 scanner->position = 0;
319 scanner->next_token = G_TOKEN_NONE;
321 scanner->text = NULL;
322 scanner->text_len = 0;
323 scanner->input_fd = input_fd;
324 scanner->peeked_char = -1;
328 g_scanner_input_text (GScanner *scanner,
332 g_return_if_fail (text != NULL);
334 scanner->token = G_TOKEN_NONE;
335 scanner->value.v_int = 0;
337 scanner->position = 0;
338 scanner->next_token = G_TOKEN_NONE;
340 scanner->text = text;
341 scanner->text_len = text_len;
342 scanner->input_fd = -1;
343 scanner->peeked_char = -1;
347 g_scanner_key_equal (gconstpointer v1,
350 register const GScannerKey *key1 = v1;
351 register 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 register const GScannerKey *key = v;
364 for (c = key->symbol; *c; c++)
380 static inline GScannerKey*
381 g_scanner_lookup_internal (GScanner *scanner,
385 register GScannerKey *key_p;
388 key.scope_id = scope_id;
390 if (!scanner->config->case_sensitive)
393 register const gchar *c;
395 key.symbol = g_new (gchar, strlen (symbol) + 1);
396 for (d = key.symbol, c = symbol; *c; c++, d++)
399 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
404 key.symbol = (gchar*) symbol;
405 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
412 g_scanner_scope_add_symbol (GScanner *scanner,
417 register GScannerKey *key;
419 g_return_if_fail (scanner != NULL);
420 g_return_if_fail (symbol != NULL);
422 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
426 key = g_new (GScannerKey, 1);
427 key->scope_id = scope_id;
428 key->symbol = g_strdup (symbol);
430 if (!scanner->config->case_sensitive)
441 g_hash_table_insert (scanner->symbol_table, key, key);
448 g_scanner_scope_remove_symbol (GScanner *scanner,
452 register GScannerKey *key;
454 g_return_if_fail (scanner != NULL);
455 g_return_if_fail (symbol != NULL);
457 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
461 g_hash_table_remove (scanner->symbol_table, key);
462 g_free (key->symbol);
468 g_scanner_lookup_symbol (GScanner *scanner,
471 register GScannerKey *key;
472 register guint scope_id;
474 g_return_val_if_fail (scanner != NULL, NULL);
479 scope_id = scanner->scope_id;
480 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
481 if (!key && scope_id && scanner->config->scope_0_fallback)
482 key = g_scanner_lookup_internal (scanner, 0, symbol);
491 g_scanner_scope_lookup_symbol (GScanner *scanner,
495 register GScannerKey *key;
497 g_return_val_if_fail (scanner != NULL, NULL);
502 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
511 g_scanner_set_scope (GScanner *scanner,
514 register guint old_scope_id;
516 g_return_val_if_fail (scanner != NULL, 0);
518 old_scope_id = scanner->scope_id;
519 scanner->scope_id = scope_id;
525 g_scanner_foreach_internal (gpointer _key,
529 register GScannerKey *key;
530 register gpointer *d;
531 register GHFunc func;
532 register gpointer func_data;
533 register guint *scope_id;
536 func = (GHFunc) d[0];
541 if (key->scope_id == *scope_id)
542 func (key->symbol, key->value, func_data);
546 g_scanner_scope_foreach_symbol (GScanner *scanner,
553 g_return_if_fail (scanner != NULL);
555 d[0] = (gpointer) func;
559 g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
563 g_scanner_freeze_symbol_table (GScanner *scanner)
565 g_return_if_fail (scanner != NULL);
567 g_hash_table_freeze (scanner->symbol_table);
571 g_scanner_thaw_symbol_table (GScanner *scanner)
573 g_return_if_fail (scanner != NULL);
575 g_hash_table_thaw (scanner->symbol_table);
579 g_scanner_peek_next_token (GScanner *scanner)
581 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
583 if (scanner->next_token == G_TOKEN_NONE)
585 scanner->next_line = scanner->line;
586 scanner->next_position = scanner->position;
587 g_scanner_get_token_i (scanner,
588 &scanner->next_token,
589 &scanner->next_value,
591 &scanner->next_position);
594 return scanner->next_token;
598 g_scanner_get_next_token (GScanner *scanner)
600 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
602 if (scanner->next_token != G_TOKEN_NONE)
604 g_scanner_free_value (&scanner->token, &scanner->value);
606 scanner->token = scanner->next_token;
607 scanner->value = scanner->next_value;
608 scanner->line = scanner->next_line;
609 scanner->position = scanner->next_position;
610 scanner->next_token = G_TOKEN_NONE;
613 g_scanner_get_token_i (scanner,
619 return scanner->token;
623 g_scanner_cur_token (GScanner *scanner)
625 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
627 return scanner->token;
631 g_scanner_cur_value (GScanner *scanner)
636 g_return_val_if_fail (scanner != NULL, v);
638 return scanner->value;
642 g_scanner_cur_line (GScanner *scanner)
644 g_return_val_if_fail (scanner != NULL, 0);
646 return scanner->line;
650 g_scanner_cur_position (GScanner *scanner)
652 g_return_val_if_fail (scanner != NULL, 0);
654 return scanner->position;
658 g_scanner_eof (GScanner *scanner)
660 g_return_val_if_fail (scanner != NULL, TRUE);
662 return scanner->token == G_TOKEN_EOF;
666 g_scanner_peek_next_char (GScanner *scanner)
670 if (scanner->text_len)
672 fchar = scanner->text[0];
674 else if (scanner->input_fd >= 0)
676 if (scanner->peeked_char < 0)
682 count = read (scanner->input_fd, &fchar, 1);
684 while (count == -1 &&
691 scanner->peeked_char = fchar;
694 fchar = scanner->peeked_char;
703 g_scanner_get_char (GScanner *scanner,
709 if (scanner->text_len)
711 fchar = *(scanner->text++);
714 else if (scanner->input_fd >= 0)
716 if (scanner->peeked_char < 0)
722 count = read (scanner->input_fd, &fchar, 1);
724 while (count == -1 &&
727 if (count != 1 || fchar == 0)
730 scanner->peeked_char = 0;
735 fchar = scanner->peeked_char;
737 scanner->peeked_char = -1;
757 g_scanner_unexp_token (GScanner *scanner,
758 GTokenType expected_token,
759 const gchar *identifier_spec,
760 const gchar *symbol_spec,
761 const gchar *symbol_name,
762 const gchar *message,
765 register gchar *token_string;
766 register guint token_string_len;
767 register gchar *expected_string;
768 register guint expected_string_len;
769 register gchar *message_prefix;
770 register gboolean print_unexp;
771 void (*msg_handler) (GScanner*, const gchar*, ...);
773 g_return_if_fail (scanner != NULL);
776 msg_handler = g_scanner_error;
778 msg_handler = g_scanner_warn;
780 if (!identifier_spec)
781 identifier_spec = "identifier";
783 symbol_spec = "symbol";
785 token_string_len = 56;
786 token_string = g_new (gchar, token_string_len + 1);
787 expected_string_len = 64;
788 expected_string = g_new (gchar, expected_string_len + 1);
791 switch (scanner->token)
794 g_snprintf (token_string, token_string_len, "end of file");
798 if (scanner->token >= 1 && scanner->token <= 255)
800 if ((scanner->token >= ' ' && scanner->token <= '~') ||
801 strchr (scanner->config->cset_identifier_first, scanner->token) ||
802 strchr (scanner->config->cset_identifier_nth, scanner->token))
803 g_snprintf (token_string, expected_string_len, "character `%c'", scanner->token);
805 g_snprintf (token_string, expected_string_len, "character `\\%o'", scanner->token);
808 else if (!scanner->config->symbol_2_token)
810 g_snprintf (token_string, token_string_len, "(unknown) token <%d>", scanner->token);
815 if (expected_token == G_TOKEN_SYMBOL ||
816 (scanner->config->symbol_2_token &&
817 expected_token > G_TOKEN_LAST))
820 g_snprintf (token_string,
823 print_unexp ? "" : "invalid ",
827 g_snprintf (token_string,
830 print_unexp ? "" : "invalid ",
836 expected_token = G_TOKEN_NONE;
837 switch (scanner->value.v_error)
839 case G_ERR_UNEXP_EOF:
840 g_snprintf (token_string, token_string_len, "scanner: unexpected end of file");
843 case G_ERR_UNEXP_EOF_IN_STRING:
844 g_snprintf (token_string, token_string_len, "scanner: unterminated string constant");
847 case G_ERR_UNEXP_EOF_IN_COMMENT:
848 g_snprintf (token_string, token_string_len, "scanner: unterminated comment");
851 case G_ERR_NON_DIGIT_IN_CONST:
852 g_snprintf (token_string, token_string_len, "scanner: non digit in constant");
855 case G_ERR_FLOAT_RADIX:
856 g_snprintf (token_string, token_string_len, "scanner: invalid radix for floating constant");
859 case G_ERR_FLOAT_MALFORMED:
860 g_snprintf (token_string, token_string_len, "scanner: malformed floating constant");
863 case G_ERR_DIGIT_RADIX:
864 g_snprintf (token_string, token_string_len, "scanner: digit is beyond radix");
869 g_snprintf (token_string, token_string_len, "scanner: unknown error");
875 g_snprintf (token_string, token_string_len, "character `%c'", scanner->value.v_char);
878 case G_TOKEN_IDENTIFIER:
879 if (expected_token == G_TOKEN_IDENTIFIER)
881 g_snprintf (token_string,
884 print_unexp ? "" : "invalid ",
886 scanner->value.v_string);
893 g_snprintf (token_string, token_string_len, "number `%ld'", scanner->value.v_int);
897 g_snprintf (token_string, token_string_len, "number `%.3f'", scanner->value.v_float);
901 if (expected_token == G_TOKEN_STRING)
903 g_snprintf (token_string,
905 "%s%sstring constant \"%s\"",
906 print_unexp ? "" : "invalid ",
907 scanner->value.v_string[0] == 0 ? "empty " : "",
908 scanner->value.v_string);
909 token_string[token_string_len - 2] = '"';
910 token_string[token_string_len - 1] = 0;
913 case G_TOKEN_COMMENT_SINGLE:
914 case G_TOKEN_COMMENT_MULTI:
915 g_snprintf (token_string, token_string_len, "comment");
919 /* somehow the user's parsing code is screwed, there isn't much
920 * we can do about it.
921 * Note, a common case to trigger this is
922 * g_scanner_peek_next_token(); g_scanner_unexp_token();
923 * without an intermediate g_scanner_get_next_token().
925 g_assert_not_reached ();
930 switch (expected_token)
935 if (expected_token >= 1 && expected_token <= 255)
937 if ((expected_token >= ' ' && expected_token <= '~') ||
938 strchr (scanner->config->cset_identifier_first, expected_token) ||
939 strchr (scanner->config->cset_identifier_nth, expected_token))
940 g_snprintf (expected_string, expected_string_len, "character `%c'", expected_token);
942 g_snprintf (expected_string, expected_string_len, "character `\\%o'", expected_token);
945 else if (!scanner->config->symbol_2_token)
947 g_snprintf (expected_string, expected_string_len, "(unknown) token <%d>", expected_token);
952 need_valid = (scanner->token == G_TOKEN_SYMBOL ||
953 (scanner->config->symbol_2_token &&
954 scanner->token > G_TOKEN_LAST));
955 g_snprintf (expected_string,
958 need_valid ? "valid " : "",
960 /* FIXME: should we attempt to lookup the symbol_name for symbol_2_token? */
964 g_snprintf (expected_string, expected_string_len, "number (integer)");
968 g_snprintf (expected_string, expected_string_len, "number (float)");
972 g_snprintf (expected_string,
975 scanner->token == G_TOKEN_STRING ? "valid " : "");
978 case G_TOKEN_IDENTIFIER:
979 g_snprintf (expected_string,
982 scanner->token == G_TOKEN_IDENTIFIER ? "valid " : "",
990 if (message && message[0] != 0)
991 message_prefix = " - ";
998 if (expected_token != G_TOKEN_NONE)
1001 msg_handler (scanner,
1002 "unexpected %s, expected %s%s%s",
1008 msg_handler (scanner,
1009 "%s, expected %s%s%s",
1018 msg_handler (scanner,
1019 "unexpected %s%s%s",
1024 msg_handler (scanner,
1031 g_free (token_string);
1032 g_free (expected_string);
1036 g_scanner_stat_mode (const gchar *filename)
1038 struct stat *stat_buf;
1041 stat_buf = g_new0 (struct stat, 1);
1043 lstat (filename, stat_buf);
1045 st_mode = stat_buf->st_mode;
1053 g_scanner_free_value (GTokenType *token_p,
1058 case G_TOKEN_STRING:
1059 case G_TOKEN_IDENTIFIER:
1060 case G_TOKEN_IDENTIFIER_NULL:
1061 case G_TOKEN_COMMENT_SINGLE:
1062 case G_TOKEN_COMMENT_MULTI:
1063 g_free (value_p->v_string);
1070 *token_p = G_TOKEN_NONE;
1074 g_scanner_get_token_i (GScanner *scanner,
1075 GTokenType *token_p,
1082 g_scanner_free_value (token_p, value_p);
1083 g_scanner_get_token_ll (scanner, token_p, value_p, line_p, position_p);
1085 while (((*token_p > 0 && *token_p < 256) &&
1086 strchr (scanner->config->cset_skip_characters, *token_p)) ||
1087 (*token_p == G_TOKEN_CHAR &&
1088 strchr (scanner->config->cset_skip_characters, value_p->v_char)) ||
1089 (*token_p == G_TOKEN_COMMENT_MULTI &&
1090 scanner->config->skip_comment_multi) ||
1091 (*token_p == G_TOKEN_COMMENT_SINGLE &&
1092 scanner->config->skip_comment_single));
1096 case G_TOKEN_IDENTIFIER:
1097 if (scanner->config->identifier_2_string)
1098 *token_p = G_TOKEN_STRING;
1101 case G_TOKEN_SYMBOL:
1102 if (scanner->config->symbol_2_token)
1103 *token_p = (GTokenType) value_p->v_symbol;
1106 case G_TOKEN_BINARY:
1109 if (scanner->config->numbers_2_int)
1110 *token_p = G_TOKEN_INT;
1117 if (*token_p == G_TOKEN_INT &&
1118 scanner->config->int_2_float)
1120 *token_p = G_TOKEN_FLOAT;
1121 value_p->v_float = value_p->v_int;
1128 g_scanner_get_token_ll (GScanner *scanner,
1129 GTokenType *token_p,
1134 register GScannerConfig *config;
1135 register gboolean in_comment_multi;
1136 register gboolean in_comment_single;
1137 register gboolean in_string_sq;
1138 register gboolean in_string_dq;
1140 register GTokenType token;
1141 register GString *gstring;
1144 config = scanner->config;
1145 (*value_p).v_int = 0;
1147 if (scanner->token == G_TOKEN_EOF ||
1148 (!scanner->text_len &&
1149 (scanner->input_fd < 0 ||
1150 scanner->peeked_char == 0)))
1152 *token_p = G_TOKEN_EOF;
1156 in_comment_multi = FALSE;
1157 in_comment_single = FALSE;
1158 in_string_sq = FALSE;
1159 in_string_dq = FALSE;
1164 register gboolean dotted_float = FALSE;
1166 ch = g_scanner_get_char (scanner, line_p, position_p);
1169 token = G_TOKEN_NONE;
1171 /* this is *evil*, but needed ;(
1172 * we first check for identifier first character, because it
1173 * might interfere with other key chars like slashes or numbers
1175 if (config->scan_identifier &&
1176 ch && strchr (config->cset_identifier_first, ch))
1177 goto identifier_precedence;
1181 register gboolean in_number;
1182 static gchar *endptr;
1185 token = G_TOKEN_EOF;
1191 if (!config->scan_comment_multi ||
1192 g_scanner_peek_next_char (scanner) != '*')
1194 g_scanner_get_char (scanner, line_p, position_p);
1195 token = G_TOKEN_COMMENT_MULTI;
1196 in_comment_multi = TRUE;
1197 gstring = g_string_new ("");
1198 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1200 if (ch == '*' && g_scanner_peek_next_char (scanner) == '/')
1202 g_scanner_get_char (scanner, line_p, position_p);
1203 in_comment_multi = FALSE;
1207 gstring = g_string_append_c (gstring, ch);
1213 if (!config->scan_string_sq)
1215 token = G_TOKEN_STRING;
1216 in_string_sq = TRUE;
1217 gstring = g_string_new ("");
1218 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1222 in_string_sq = FALSE;
1226 gstring = g_string_append_c (gstring, ch);
1232 if (!config->scan_string_dq)
1234 token = G_TOKEN_STRING;
1235 in_string_dq = TRUE;
1236 gstring = g_string_new ("");
1237 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1241 in_string_dq = FALSE;
1248 ch = g_scanner_get_char (scanner, line_p, position_p);
1252 register guint fchar;
1258 gstring = g_string_append_c (gstring, '\\');
1262 gstring = g_string_append_c (gstring, '\n');
1266 gstring = g_string_append_c (gstring, '\t');
1270 gstring = g_string_append_c (gstring, '\r');
1274 gstring = g_string_append_c (gstring, '\b');
1278 gstring = g_string_append_c (gstring, '\f');
1290 fchar = g_scanner_peek_next_char (scanner);
1291 if (fchar >= '0' && fchar <= '7')
1293 ch = g_scanner_get_char (scanner, line_p, position_p);
1294 i= i * 8 + ch - '0';
1295 fchar = g_scanner_peek_next_char (scanner);
1296 if (fchar >= '0' && fchar <= '7')
1298 ch = g_scanner_get_char (scanner, line_p, position_p);
1299 i = i * 8 + ch - '0';
1302 gstring = g_string_append_c (gstring, i);
1306 gstring = g_string_append_c (gstring, ch);
1311 gstring = g_string_append_c (gstring, ch);
1318 if (!config->scan_float)
1320 token = G_TOKEN_FLOAT;
1321 dotted_float = TRUE;
1322 ch = g_scanner_get_char (scanner, line_p, position_p);
1323 goto number_parsing;
1326 if (!config->scan_hex_dollar)
1328 token = G_TOKEN_HEX;
1329 ch = g_scanner_get_char (scanner, line_p, position_p);
1330 goto number_parsing;
1333 if (config->scan_octal)
1334 token = G_TOKEN_OCTAL;
1336 token = G_TOKEN_INT;
1337 ch = g_scanner_peek_next_char (scanner);
1338 if (config->scan_hex && (ch == 'x' || ch == 'X'))
1340 token = G_TOKEN_HEX;
1341 g_scanner_get_char (scanner, line_p, position_p);
1342 ch = g_scanner_get_char (scanner, line_p, position_p);
1345 token = G_TOKEN_ERROR;
1346 value.v_error = G_ERR_UNEXP_EOF;
1350 if (g_scanner_char_2_num (ch, 16) < 0)
1352 token = G_TOKEN_ERROR;
1353 value.v_error = G_ERR_DIGIT_RADIX;
1358 else if (config->scan_binary && (ch == 'b' || ch == 'B'))
1360 token = G_TOKEN_BINARY;
1361 g_scanner_get_char (scanner, line_p, position_p);
1362 ch = g_scanner_get_char (scanner, line_p, position_p);
1365 token = G_TOKEN_ERROR;
1366 value.v_error = G_ERR_UNEXP_EOF;
1370 if (g_scanner_char_2_num (ch, 10) < 0)
1372 token = G_TOKEN_ERROR;
1373 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1391 if (token == G_TOKEN_NONE)
1392 token = G_TOKEN_INT;
1394 gstring = g_string_new (dotted_float ? "0." : "");
1395 gstring = g_string_append_c (gstring, ch);
1399 register gboolean is_E;
1401 is_E = (ch == 'e' || ch == 'E') && token == G_TOKEN_FLOAT;
1402 ch = g_scanner_peek_next_char (scanner);
1404 if (g_scanner_char_2_num (ch, 36) >= 0 ||
1405 (config->scan_float && ch == '.') ||
1406 (is_E && ch == '+') ||
1407 (is_E && ch == '-') )
1408 ch = g_scanner_get_char (scanner, line_p, position_p);
1416 if (token != G_TOKEN_INT &&
1417 token != G_TOKEN_OCTAL)
1419 token = G_TOKEN_ERROR;
1420 if (token == G_TOKEN_FLOAT)
1421 value.v_error = G_ERR_FLOAT_MALFORMED;
1423 value.v_error = G_ERR_FLOAT_RADIX;
1428 token = G_TOKEN_FLOAT;
1429 gstring = g_string_append_c (gstring, ch);
1443 gstring = g_string_append_c (gstring, ch);
1448 if (token != G_TOKEN_FLOAT)
1450 token = G_TOKEN_ERROR;
1451 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1455 gstring = g_string_append_c (gstring, ch);
1460 if ((token != G_TOKEN_HEX && !config->scan_float) ||
1461 (token != G_TOKEN_HEX &&
1462 token != G_TOKEN_OCTAL &&
1463 token != G_TOKEN_FLOAT &&
1464 token != G_TOKEN_INT))
1466 token = G_TOKEN_ERROR;
1467 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1472 if (token != G_TOKEN_HEX)
1473 token = G_TOKEN_FLOAT;
1474 gstring = g_string_append_c (gstring, ch);
1479 if (token != G_TOKEN_HEX)
1481 token = G_TOKEN_ERROR;
1482 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1486 gstring = g_string_append_c (gstring, ch);
1493 case G_TOKEN_BINARY:
1494 value.v_binary = strtol (gstring->str, &endptr, 2);
1498 value.v_octal = strtol (gstring->str, &endptr, 8);
1502 value.v_int = strtol (gstring->str, &endptr, 10);
1506 value.v_float = g_strtod (gstring->str, &endptr);
1510 value.v_hex = strtol (gstring->str, &endptr, 16);
1516 if (endptr && *endptr)
1518 token = G_TOKEN_ERROR;
1519 if (*endptr == 'e' || *endptr == 'E')
1520 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1522 value.v_error = G_ERR_DIGIT_RADIX;
1524 g_string_free (gstring, TRUE);
1531 if (config->cpair_comment_single &&
1532 ch == config->cpair_comment_single[0])
1534 token = G_TOKEN_COMMENT_SINGLE;
1535 in_comment_single = TRUE;
1536 gstring = g_string_new ("");
1537 while ((ch = g_scanner_get_char (scanner,
1541 if (ch == config->cpair_comment_single[1])
1543 in_comment_single = FALSE;
1548 gstring = g_string_append_c (gstring, ch);
1552 else if (config->scan_identifier && ch &&
1553 strchr (config->cset_identifier_first, ch))
1555 identifier_precedence:
1557 if (config->cset_identifier_nth && ch &&
1558 strchr (config->cset_identifier_nth,
1559 g_scanner_peek_next_char (scanner)))
1561 token = G_TOKEN_IDENTIFIER;
1562 gstring = g_string_new ("");
1563 gstring = g_string_append_c (gstring, ch);
1566 ch = g_scanner_get_char (scanner, line_p, position_p);
1567 gstring = g_string_append_c (gstring, ch);
1568 ch = g_scanner_peek_next_char (scanner);
1570 while (ch && strchr (config->cset_identifier_nth, ch));
1573 else if (config->scan_identifier_1char)
1575 token = G_TOKEN_IDENTIFIER;
1576 value.v_identifier = g_new0 (gchar, 2);
1577 value.v_identifier[0] = ch;
1583 if (config->char_2_token)
1587 token = G_TOKEN_CHAR;
1594 g_assert (ch == 0 && token != G_TOKEN_NONE);
1598 if (in_comment_multi ||
1599 in_comment_single ||
1603 token = G_TOKEN_ERROR;
1606 g_string_free (gstring, TRUE);
1610 if (in_comment_multi || in_comment_single)
1611 value.v_error = G_ERR_UNEXP_EOF_IN_COMMENT;
1612 else if (in_string_sq || in_string_dq)
1613 value.v_error = G_ERR_UNEXP_EOF_IN_STRING;
1618 value.v_string = gstring->str;
1619 g_string_free (gstring, FALSE);
1623 if (token == G_TOKEN_IDENTIFIER &&
1624 config->scan_symbols)
1626 register GScannerKey *key;
1627 register guint scope_id;
1629 scope_id = scanner->scope_id;
1630 key = g_scanner_lookup_internal (scanner, scope_id, value.v_identifier);
1631 if (!key && scope_id && scanner->config->scope_0_fallback)
1632 key = g_scanner_lookup_internal (scanner, 0, value.v_identifier);
1636 g_free (value.v_identifier);
1637 token = G_TOKEN_SYMBOL;
1638 value.v_symbol = key->value;
1642 if (token == G_TOKEN_IDENTIFIER &&
1643 config->scan_identifier_NULL &&
1644 strlen (value.v_identifier) == 4)
1646 gchar *null_upper = "NULL";
1647 gchar *null_lower = "null";
1649 if (scanner->config->case_sensitive)
1651 if (value.v_identifier[0] == null_upper[0] &&
1652 value.v_identifier[1] == null_upper[1] &&
1653 value.v_identifier[2] == null_upper[2] &&
1654 value.v_identifier[3] == null_upper[3])
1655 token = G_TOKEN_IDENTIFIER_NULL;
1659 if ((value.v_identifier[0] == null_upper[0] ||
1660 value.v_identifier[0] == null_lower[0]) &&
1661 (value.v_identifier[1] == null_upper[1] ||
1662 value.v_identifier[1] == null_lower[1]) &&
1663 (value.v_identifier[2] == null_upper[2] ||
1664 value.v_identifier[2] == null_lower[2]) &&
1665 (value.v_identifier[3] == null_upper[3] ||
1666 value.v_identifier[3] == null_lower[3]))
1667 token = G_TOKEN_IDENTIFIER_NULL;