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.1 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, see <http://www.gnu.org/licenses/>.
22 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GLib Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GLib at ftp://ftp.gtk.org/pub/gtk/.
42 #include "gprintfint.h"
43 #include "gstrfuncs.h"
45 #include "gtestutils.h"
57 * @title: Lexical Scanner
58 * @short_description: a general purpose lexical scanner
60 * The #GScanner and its associated functions provide a
61 * general purpose lexical scanner.
66 * @scanner: a #GScanner
67 * @message: the message
68 * @error: %TRUE if the message signals an error,
69 * %FALSE if it signals a warning.
71 * Specifies the type of the message handler function.
77 * The set of lowercase ASCII alphabet characters.
78 * Used for specifying valid identifier characters
85 * The set of uppercase ASCII alphabet characters.
86 * Used for specifying valid identifier characters
93 * The set of ASCII digits.
94 * Used for specifying valid identifier characters
101 * The set of uppercase ISO 8859-1 alphabet characters
102 * which are not ASCII characters.
103 * Used for specifying valid identifier characters
104 * in #GScannerConfig.
110 * The set of lowercase ISO 8859-1 alphabet characters
111 * which are not ASCII characters.
112 * Used for specifying valid identifier characters
113 * in #GScannerConfig.
118 * @G_TOKEN_EOF: the end of the file
119 * @G_TOKEN_LEFT_PAREN: a '(' character
120 * @G_TOKEN_LEFT_CURLY: a '{' character
121 * @G_TOKEN_LEFT_BRACE: a '[' character
122 * @G_TOKEN_RIGHT_CURLY: a '}' character
123 * @G_TOKEN_RIGHT_PAREN: a ')' character
124 * @G_TOKEN_RIGHT_BRACE: a ']' character
125 * @G_TOKEN_EQUAL_SIGN: a '=' character
126 * @G_TOKEN_COMMA: a ',' character
127 * @G_TOKEN_NONE: not a token
128 * @G_TOKEN_ERROR: an error occurred
129 * @G_TOKEN_CHAR: a character
130 * @G_TOKEN_BINARY: a binary integer
131 * @G_TOKEN_OCTAL: an octal integer
132 * @G_TOKEN_INT: an integer
133 * @G_TOKEN_HEX: a hex integer
134 * @G_TOKEN_FLOAT: a floating point number
135 * @G_TOKEN_STRING: a string
136 * @G_TOKEN_SYMBOL: a symbol
137 * @G_TOKEN_IDENTIFIER: an identifier
138 * @G_TOKEN_IDENTIFIER_NULL: a null identifier
139 * @G_TOKEN_COMMENT_SINGLE: one line comment
140 * @G_TOKEN_COMMENT_MULTI: multi line comment
142 * The possible types of token returned from each
143 * g_scanner_get_next_token() call.
148 * @v_symbol: token symbol value
149 * @v_identifier: token identifier value
150 * @v_binary: token binary integer value
151 * @v_octal: octal integer value
152 * @v_int: integer value
153 * @v_int64: 64-bit integer value
154 * @v_float: floating point value
155 * @v_hex: hex integer value
156 * @v_string: string value
157 * @v_comment: comment value
158 * @v_char: character value
159 * @v_error: error value
161 * A union holding the value of the token.
166 * @G_ERR_UNKNOWN: unknown error
167 * @G_ERR_UNEXP_EOF: unexpected end of file
168 * @G_ERR_UNEXP_EOF_IN_STRING: unterminated string constant
169 * @G_ERR_UNEXP_EOF_IN_COMMENT: unterminated comment
170 * @G_ERR_NON_DIGIT_IN_CONST: non-digit character in a number
171 * @G_ERR_DIGIT_RADIX: digit beyond radix in a number
172 * @G_ERR_FLOAT_RADIX: non-decimal floating point number
173 * @G_ERR_FLOAT_MALFORMED: malformed floating point number
175 * The possible errors, used in the @v_error field
176 * of #GTokenValue, when the token is a %G_TOKEN_ERROR.
182 * @max_parse_errors: unused
183 * @parse_errors: g_scanner_error() increments this field
184 * @input_name: name of input stream, featured by the default message handler
185 * @qdata: quarked data
186 * @config: link into the scanner configuration
187 * @token: token parsed by the last g_scanner_get_next_token()
188 * @value: value of the last token from g_scanner_get_next_token()
189 * @line: line number of the last token from g_scanner_get_next_token()
190 * @position: char number of the last token from g_scanner_get_next_token()
191 * @next_token: token parsed by the last g_scanner_peek_next_token()
192 * @next_value: value of the last token from g_scanner_peek_next_token()
193 * @next_line: line number of the last token from g_scanner_peek_next_token()
194 * @next_position: char number of the last token from g_scanner_peek_next_token()
195 * @msg_handler: handler function for _warn and _error
197 * The data structure representing a lexical scanner.
199 * You should set @input_name after creating the scanner, since
200 * it is used by the default message handler when displaying
201 * warnings and errors. If you are scanning a file, the filename
202 * would be a good choice.
204 * The @user_data and @max_parse_errors fields are not used.
205 * If you need to associate extra data with the scanner you
206 * can place them here.
208 * If you want to use your own message handler you can set the
209 * @msg_handler field. The type of the message handler function
210 * is declared by #GScannerMsgFunc.
215 * @cset_skip_characters: specifies which characters should be skipped
216 * by the scanner (the default is the whitespace characters: space,
217 * tab, carriage-return and line-feed).
218 * @cset_identifier_first: specifies the characters which can start
219 * identifiers (the default is #G_CSET_a_2_z, "_", and #G_CSET_A_2_Z).
220 * @cset_identifier_nth: specifies the characters which can be used
221 * in identifiers, after the first character (the default is
222 * #G_CSET_a_2_z, "_0123456789", #G_CSET_A_2_Z, #G_CSET_LATINS,
224 * @cpair_comment_single: specifies the characters at the start and
225 * end of single-line comments. The default is "#\n" which means
226 * that single-line comments start with a '#' and continue until
227 * a '\n' (end of line).
228 * @case_sensitive: specifies if symbols are case sensitive (the
229 * default is %FALSE).
230 * @skip_comment_multi: specifies if multi-line comments are skipped
231 * and not returned as tokens (the default is %TRUE).
232 * @skip_comment_single: specifies if single-line comments are skipped
233 * and not returned as tokens (the default is %TRUE).
234 * @scan_comment_multi: specifies if multi-line comments are recognized
235 * (the default is %TRUE).
236 * @scan_identifier: specifies if identifiers are recognized (the
238 * @scan_identifier_1char: specifies if single-character
239 * identifiers are recognized (the default is %FALSE).
240 * @scan_identifier_NULL: specifies if %NULL is reported as
241 * %G_TOKEN_IDENTIFIER_NULL (the default is %FALSE).
242 * @scan_symbols: specifies if symbols are recognized (the default
244 * @scan_binary: specifies if binary numbers are recognized (the
245 * default is %FALSE).
246 * @scan_octal: specifies if octal numbers are recognized (the
248 * @scan_float: specifies if floating point numbers are recognized
249 * (the default is %TRUE).
250 * @scan_hex: specifies if hexadecimal numbers are recognized (the
252 * @scan_hex_dollar: specifies if '$' is recognized as a prefix for
253 * hexadecimal numbers (the default is %FALSE).
254 * @scan_string_sq: specifies if strings can be enclosed in single
255 * quotes (the default is %TRUE).
256 * @scan_string_dq: specifies if strings can be enclosed in double
257 * quotes (the default is %TRUE).
258 * @numbers_2_int: specifies if binary, octal and hexadecimal numbers
259 * are reported as #G_TOKEN_INT (the default is %TRUE).
260 * @int_2_float: specifies if all numbers are reported as %G_TOKEN_FLOAT
261 * (the default is %FALSE).
262 * @identifier_2_string: specifies if identifiers are reported as strings
263 * (the default is %FALSE).
264 * @char_2_token: specifies if characters are reported by setting
265 * `token = ch` or as %G_TOKEN_CHAR (the default is %TRUE).
266 * @symbol_2_token: specifies if symbols are reported by setting
267 * `token = v_symbol` or as %G_TOKEN_SYMBOL (the default is %FALSE).
268 * @scope_0_fallback: specifies if a symbol is searched for in the
269 * default scope in addition to the current scope (the default is %FALSE).
270 * @store_int64: use value.v_int64 rather than v_int
272 * Specifies the #GScanner parser configuration. Most settings can
273 * be changed during the parsing phase and will affect the lexical
274 * parsing of the next unpeeked token.
277 /* --- defines --- */
278 #define to_lower(c) ( \
280 ( (((guchar)(c))>='A' && ((guchar)(c))<='Z') * ('a'-'A') ) | \
281 ( (((guchar)(c))>=192 && ((guchar)(c))<=214) * (224-192) ) | \
282 ( (((guchar)(c))>=216 && ((guchar)(c))<=222) * (248-216) ) | \
286 #define READ_BUFFER_SIZE (4000)
289 /* --- typedefs --- */
290 typedef struct _GScannerKey GScannerKey;
300 /* --- variables --- */
301 static const GScannerConfig g_scanner_config_template =
305 ) /* cset_skip_characters */,
310 ) /* cset_identifier_first */,
318 ) /* cset_identifier_nth */,
319 ( "#\n" ) /* cpair_comment_single */,
321 FALSE /* case_sensitive */,
323 TRUE /* skip_comment_multi */,
324 TRUE /* skip_comment_single */,
325 TRUE /* scan_comment_multi */,
326 TRUE /* scan_identifier */,
327 FALSE /* scan_identifier_1char */,
328 FALSE /* scan_identifier_NULL */,
329 TRUE /* scan_symbols */,
330 FALSE /* scan_binary */,
331 TRUE /* scan_octal */,
332 TRUE /* scan_float */,
334 FALSE /* scan_hex_dollar */,
335 TRUE /* scan_string_sq */,
336 TRUE /* scan_string_dq */,
337 TRUE /* numbers_2_int */,
338 FALSE /* int_2_float */,
339 FALSE /* identifier_2_string */,
340 TRUE /* char_2_token */,
341 FALSE /* symbol_2_token */,
342 FALSE /* scope_0_fallback */,
343 FALSE /* store_int64 */,
344 0 /* padding_dummy */
348 /* --- prototypes --- */
350 GScannerKey* g_scanner_lookup_internal (GScanner *scanner,
352 const gchar *symbol);
353 static gboolean g_scanner_key_equal (gconstpointer v1,
355 static guint g_scanner_key_hash (gconstpointer v);
356 static void g_scanner_get_token_ll (GScanner *scanner,
358 GTokenValue *value_p,
361 static void g_scanner_get_token_i (GScanner *scanner,
363 GTokenValue *value_p,
367 static guchar g_scanner_peek_next_char (GScanner *scanner);
368 static guchar g_scanner_get_char (GScanner *scanner,
371 static void g_scanner_msg_handler (GScanner *scanner,
376 /* --- functions --- */
378 g_scanner_char_2_num (guchar c,
381 if (c >= '0' && c <= '9')
383 else if (c >= 'A' && c <= 'Z')
385 else if (c >= 'a' && c <= 'z')
398 * @config_templ: the initial scanner settings
400 * Creates a new #GScanner.
402 * The @config_templ structure specifies the initial settings
403 * of the scanner, which are copied into the #GScanner
404 * @config field. If you pass %NULL then the default settings
407 * Returns: the new #GScanner
410 g_scanner_new (const GScannerConfig *config_templ)
415 config_templ = &g_scanner_config_template;
417 scanner = g_new0 (GScanner, 1);
419 scanner->user_data = NULL;
420 scanner->max_parse_errors = 1;
421 scanner->parse_errors = 0;
422 scanner->input_name = NULL;
423 g_datalist_init (&scanner->qdata);
425 scanner->config = g_new0 (GScannerConfig, 1);
427 scanner->config->case_sensitive = config_templ->case_sensitive;
428 scanner->config->cset_skip_characters = config_templ->cset_skip_characters;
429 if (!scanner->config->cset_skip_characters)
430 scanner->config->cset_skip_characters = "";
431 scanner->config->cset_identifier_first = config_templ->cset_identifier_first;
432 scanner->config->cset_identifier_nth = config_templ->cset_identifier_nth;
433 scanner->config->cpair_comment_single = config_templ->cpair_comment_single;
434 scanner->config->skip_comment_multi = config_templ->skip_comment_multi;
435 scanner->config->skip_comment_single = config_templ->skip_comment_single;
436 scanner->config->scan_comment_multi = config_templ->scan_comment_multi;
437 scanner->config->scan_identifier = config_templ->scan_identifier;
438 scanner->config->scan_identifier_1char = config_templ->scan_identifier_1char;
439 scanner->config->scan_identifier_NULL = config_templ->scan_identifier_NULL;
440 scanner->config->scan_symbols = config_templ->scan_symbols;
441 scanner->config->scan_binary = config_templ->scan_binary;
442 scanner->config->scan_octal = config_templ->scan_octal;
443 scanner->config->scan_float = config_templ->scan_float;
444 scanner->config->scan_hex = config_templ->scan_hex;
445 scanner->config->scan_hex_dollar = config_templ->scan_hex_dollar;
446 scanner->config->scan_string_sq = config_templ->scan_string_sq;
447 scanner->config->scan_string_dq = config_templ->scan_string_dq;
448 scanner->config->numbers_2_int = config_templ->numbers_2_int;
449 scanner->config->int_2_float = config_templ->int_2_float;
450 scanner->config->identifier_2_string = config_templ->identifier_2_string;
451 scanner->config->char_2_token = config_templ->char_2_token;
452 scanner->config->symbol_2_token = config_templ->symbol_2_token;
453 scanner->config->scope_0_fallback = config_templ->scope_0_fallback;
454 scanner->config->store_int64 = config_templ->store_int64;
456 scanner->token = G_TOKEN_NONE;
457 scanner->value.v_int64 = 0;
459 scanner->position = 0;
461 scanner->next_token = G_TOKEN_NONE;
462 scanner->next_value.v_int64 = 0;
463 scanner->next_line = 1;
464 scanner->next_position = 0;
466 scanner->symbol_table = g_hash_table_new (g_scanner_key_hash, g_scanner_key_equal);
467 scanner->input_fd = -1;
468 scanner->text = NULL;
469 scanner->text_end = NULL;
470 scanner->buffer = NULL;
471 scanner->scope_id = 0;
473 scanner->msg_handler = g_scanner_msg_handler;
479 g_scanner_free_value (GTokenType *token_p,
480 GTokenValue *value_p)
485 case G_TOKEN_IDENTIFIER:
486 case G_TOKEN_IDENTIFIER_NULL:
487 case G_TOKEN_COMMENT_SINGLE:
488 case G_TOKEN_COMMENT_MULTI:
489 g_free (value_p->v_string);
496 *token_p = G_TOKEN_NONE;
500 g_scanner_destroy_symbol_table_entry (gpointer _key,
504 GScannerKey *key = _key;
506 g_free (key->symbol);
512 * @scanner: a #GScanner
514 * Frees all memory used by the #GScanner.
517 g_scanner_destroy (GScanner *scanner)
519 g_return_if_fail (scanner != NULL);
521 g_datalist_clear (&scanner->qdata);
522 g_hash_table_foreach (scanner->symbol_table,
523 g_scanner_destroy_symbol_table_entry, NULL);
524 g_hash_table_destroy (scanner->symbol_table);
525 g_scanner_free_value (&scanner->token, &scanner->value);
526 g_scanner_free_value (&scanner->next_token, &scanner->next_value);
527 g_free (scanner->config);
528 g_free (scanner->buffer);
533 g_scanner_msg_handler (GScanner *scanner,
537 g_return_if_fail (scanner != NULL);
539 _g_fprintf (stderr, "%s:%d: ",
540 scanner->input_name ? scanner->input_name : "<memory>",
543 _g_fprintf (stderr, "error: ");
544 _g_fprintf (stderr, "%s\n", message);
549 * @scanner: a #GScanner
550 * @format: the message format. See the printf() documentation
551 * @...: the parameters to insert into the format string
553 * Outputs an error message, via the #GScanner message handler.
556 g_scanner_error (GScanner *scanner,
560 g_return_if_fail (scanner != NULL);
561 g_return_if_fail (format != NULL);
563 scanner->parse_errors++;
565 if (scanner->msg_handler)
570 va_start (args, format);
571 string = g_strdup_vprintf (format, args);
574 scanner->msg_handler (scanner, string, TRUE);
582 * @scanner: a #GScanner
583 * @format: the message format. See the printf() documentation
584 * @...: the parameters to insert into the format string
586 * Outputs a warning message, via the #GScanner message handler.
589 g_scanner_warn (GScanner *scanner,
593 g_return_if_fail (scanner != NULL);
594 g_return_if_fail (format != NULL);
596 if (scanner->msg_handler)
601 va_start (args, format);
602 string = g_strdup_vprintf (format, args);
605 scanner->msg_handler (scanner, string, FALSE);
612 g_scanner_key_equal (gconstpointer v1,
615 const GScannerKey *key1 = v1;
616 const GScannerKey *key2 = v2;
618 return (key1->scope_id == key2->scope_id) && (strcmp (key1->symbol, key2->symbol) == 0);
622 g_scanner_key_hash (gconstpointer v)
624 const GScannerKey *key = v;
629 for (c = key->symbol; *c; c++)
630 h = (h << 5) - h + *c;
635 static inline GScannerKey*
636 g_scanner_lookup_internal (GScanner *scanner,
643 key.scope_id = scope_id;
645 if (!scanner->config->case_sensitive)
650 key.symbol = g_new (gchar, strlen (symbol) + 1);
651 for (d = key.symbol, c = symbol; *c; c++, d++)
654 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
659 key.symbol = (gchar*) symbol;
660 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
667 * g_scanner_add_symbol:
668 * @scanner: a #GScanner
669 * @symbol: the symbol to add
670 * @value: the value of the symbol
672 * Adds a symbol to the default scope.
674 * Deprecated: 2.2: Use g_scanner_scope_add_symbol() instead.
678 * g_scanner_scope_add_symbol:
679 * @scanner: a #GScanner
680 * @scope_id: the scope id
681 * @symbol: the symbol to add
682 * @value: the value of the symbol
684 * Adds a symbol to the given scope.
687 g_scanner_scope_add_symbol (GScanner *scanner,
694 g_return_if_fail (scanner != NULL);
695 g_return_if_fail (symbol != NULL);
697 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
701 key = g_new (GScannerKey, 1);
702 key->scope_id = scope_id;
703 key->symbol = g_strdup (symbol);
705 if (!scanner->config->case_sensitive)
716 g_hash_table_add (scanner->symbol_table, key);
723 * g_scanner_remove_symbol:
724 * @scanner: a #GScanner
725 * @symbol: the symbol to remove
727 * Removes a symbol from the default scope.
729 * Deprecated: 2.2: Use g_scanner_scope_remove_symbol() instead.
733 * g_scanner_scope_remove_symbol:
734 * @scanner: a #GScanner
735 * @scope_id: the scope id
736 * @symbol: the symbol to remove
738 * Removes a symbol from a scope.
741 g_scanner_scope_remove_symbol (GScanner *scanner,
747 g_return_if_fail (scanner != NULL);
748 g_return_if_fail (symbol != NULL);
750 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
754 g_hash_table_remove (scanner->symbol_table, key);
755 g_free (key->symbol);
761 * g_scanner_freeze_symbol_table:
762 * @scanner: a #GScanner
764 * There is no reason to use this macro, since it does nothing.
766 * Deprecated: 2.2: This macro does nothing.
770 * g_scanner_thaw_symbol_table:
771 * @scanner: a #GScanner
773 * There is no reason to use this macro, since it does nothing.
775 * Deprecated: 2.2: This macro does nothing.
779 * g_scanner_lookup_symbol:
780 * @scanner: a #GScanner
781 * @symbol: the symbol to look up
783 * Looks up a symbol in the current scope and return its value.
784 * If the symbol is not bound in the current scope, %NULL is
787 * Returns: the value of @symbol in the current scope, or %NULL
788 * if @symbol is not bound in the current scope
791 g_scanner_lookup_symbol (GScanner *scanner,
797 g_return_val_if_fail (scanner != NULL, NULL);
802 scope_id = scanner->scope_id;
803 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
804 if (!key && scope_id && scanner->config->scope_0_fallback)
805 key = g_scanner_lookup_internal (scanner, 0, symbol);
814 * g_scanner_scope_lookup_symbol:
815 * @scanner: a #GScanner
816 * @scope_id: the scope id
817 * @symbol: the symbol to look up
819 * Looks up a symbol in a scope and return its value. If the
820 * symbol is not bound in the scope, %NULL is returned.
822 * Returns: the value of @symbol in the given scope, or %NULL
823 * if @symbol is not bound in the given scope.
827 g_scanner_scope_lookup_symbol (GScanner *scanner,
833 g_return_val_if_fail (scanner != NULL, NULL);
838 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
847 * g_scanner_set_scope:
848 * @scanner: a #GScanner
849 * @scope_id: the new scope id
851 * Sets the current scope.
853 * Returns: the old scope id
856 g_scanner_set_scope (GScanner *scanner,
861 g_return_val_if_fail (scanner != NULL, 0);
863 old_scope_id = scanner->scope_id;
864 scanner->scope_id = scope_id;
870 g_scanner_foreach_internal (gpointer _key,
881 func = (GHFunc) d[0];
886 if (key->scope_id == *scope_id)
887 func (key->symbol, key->value, user_data);
891 * g_scanner_foreach_symbol:
892 * @scanner: a #GScanner
893 * @func: the function to call with each symbol
894 * @data: data to pass to the function
896 * Calls a function for each symbol in the default scope.
898 * Deprecated: 2.2: Use g_scanner_scope_foreach_symbol() instead.
902 * g_scanner_scope_foreach_symbol:
903 * @scanner: a #GScanner
904 * @scope_id: the scope id
905 * @func: the function to call for each symbol/value pair
906 * @user_data: user data to pass to the function
908 * Calls the given function for each of the symbol/value pairs
909 * in the given scope of the #GScanner. The function is passed
910 * the symbol and value of each pair, and the given @user_data
914 g_scanner_scope_foreach_symbol (GScanner *scanner,
921 g_return_if_fail (scanner != NULL);
923 d[0] = (gpointer) func;
927 g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
931 * g_scanner_peek_next_token:
932 * @scanner: a #GScanner
934 * Parses the next token, without removing it from the input stream.
935 * The token data is placed in the @next_token, @next_value, @next_line,
936 * and @next_position fields of the #GScanner structure.
938 * Note that, while the token is not removed from the input stream
939 * (i.e. the next call to g_scanner_get_next_token() will return the
940 * same token), it will not be reevaluated. This can lead to surprising
941 * results when changing scope or the scanner configuration after peeking
942 * the next token. Getting the next token after switching the scope or
943 * configuration will return whatever was peeked before, regardless of
944 * any symbols that may have been added or removed in the new scope.
946 * Returns: the type of the token
949 g_scanner_peek_next_token (GScanner *scanner)
951 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
953 if (scanner->next_token == G_TOKEN_NONE)
955 scanner->next_line = scanner->line;
956 scanner->next_position = scanner->position;
957 g_scanner_get_token_i (scanner,
958 &scanner->next_token,
959 &scanner->next_value,
961 &scanner->next_position);
964 return scanner->next_token;
968 * g_scanner_get_next_token:
969 * @scanner: a #GScanner
971 * Parses the next token just like g_scanner_peek_next_token()
972 * and also removes it from the input stream. The token data is
973 * placed in the @token, @value, @line, and @position fields of
974 * the #GScanner structure.
976 * Returns: the type of the token
979 g_scanner_get_next_token (GScanner *scanner)
981 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
983 if (scanner->next_token != G_TOKEN_NONE)
985 g_scanner_free_value (&scanner->token, &scanner->value);
987 scanner->token = scanner->next_token;
988 scanner->value = scanner->next_value;
989 scanner->line = scanner->next_line;
990 scanner->position = scanner->next_position;
991 scanner->next_token = G_TOKEN_NONE;
994 g_scanner_get_token_i (scanner,
1000 return scanner->token;
1004 * g_scanner_cur_token:
1005 * @scanner: a #GScanner
1007 * Gets the current token type. This is simply the @token
1008 * field in the #GScanner structure.
1010 * Returns: the current token type
1013 g_scanner_cur_token (GScanner *scanner)
1015 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
1017 return scanner->token;
1021 * g_scanner_cur_value:
1022 * @scanner: a #GScanner
1024 * Gets the current token value. This is simply the @value
1025 * field in the #GScanner structure.
1027 * Returns: the current token value
1030 g_scanner_cur_value (GScanner *scanner)
1036 g_return_val_if_fail (scanner != NULL, v);
1038 /* MSC isn't capable of handling return scanner->value; ? */
1046 * g_scanner_cur_line:
1047 * @scanner: a #GScanner
1049 * Returns the current line in the input stream (counting
1050 * from 1). This is the line of the last token parsed via
1051 * g_scanner_get_next_token().
1053 * Returns: the current line
1056 g_scanner_cur_line (GScanner *scanner)
1058 g_return_val_if_fail (scanner != NULL, 0);
1060 return scanner->line;
1064 * g_scanner_cur_position:
1065 * @scanner: a #GScanner
1067 * Returns the current position in the current line (counting
1068 * from 0). This is the position of the last token parsed via
1069 * g_scanner_get_next_token().
1071 * Returns: the current position on the line
1074 g_scanner_cur_position (GScanner *scanner)
1076 g_return_val_if_fail (scanner != NULL, 0);
1078 return scanner->position;
1083 * @scanner: a #GScanner
1085 * Returns %TRUE if the scanner has reached the end of
1086 * the file or text buffer.
1088 * Returns: %TRUE if the scanner has reached the end of
1089 * the file or text buffer
1092 g_scanner_eof (GScanner *scanner)
1094 g_return_val_if_fail (scanner != NULL, TRUE);
1096 return scanner->token == G_TOKEN_EOF || scanner->token == G_TOKEN_ERROR;
1100 * g_scanner_input_file:
1101 * @scanner: a #GScanner
1102 * @input_fd: a file descriptor
1104 * Prepares to scan a file.
1107 g_scanner_input_file (GScanner *scanner,
1110 g_return_if_fail (scanner != NULL);
1111 g_return_if_fail (input_fd >= 0);
1113 if (scanner->input_fd >= 0)
1114 g_scanner_sync_file_offset (scanner);
1116 scanner->token = G_TOKEN_NONE;
1117 scanner->value.v_int64 = 0;
1119 scanner->position = 0;
1120 scanner->next_token = G_TOKEN_NONE;
1122 scanner->input_fd = input_fd;
1123 scanner->text = NULL;
1124 scanner->text_end = NULL;
1126 if (!scanner->buffer)
1127 scanner->buffer = g_new (gchar, READ_BUFFER_SIZE + 1);
1131 * g_scanner_input_text:
1132 * @scanner: a #GScanner
1133 * @text: the text buffer to scan
1134 * @text_len: the length of the text buffer
1136 * Prepares to scan a text buffer.
1139 g_scanner_input_text (GScanner *scanner,
1143 g_return_if_fail (scanner != NULL);
1145 g_return_if_fail (text != NULL);
1149 if (scanner->input_fd >= 0)
1150 g_scanner_sync_file_offset (scanner);
1152 scanner->token = G_TOKEN_NONE;
1153 scanner->value.v_int64 = 0;
1155 scanner->position = 0;
1156 scanner->next_token = G_TOKEN_NONE;
1158 scanner->input_fd = -1;
1159 scanner->text = text;
1160 scanner->text_end = text + text_len;
1162 if (scanner->buffer)
1164 g_free (scanner->buffer);
1165 scanner->buffer = NULL;
1170 g_scanner_peek_next_char (GScanner *scanner)
1172 if (scanner->text < scanner->text_end)
1174 return *scanner->text;
1176 else if (scanner->input_fd >= 0)
1181 buffer = scanner->buffer;
1184 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
1186 while (count == -1 && (errno == EINTR || errno == EAGAIN));
1190 scanner->input_fd = -1;
1196 scanner->text = buffer;
1197 scanner->text_end = buffer + count;
1207 * g_scanner_sync_file_offset:
1208 * @scanner: a #GScanner
1210 * Rewinds the filedescriptor to the current buffer position
1211 * and blows the file read ahead buffer. This is useful for
1212 * third party uses of the scanners filedescriptor, which hooks
1213 * onto the current scanning position.
1216 g_scanner_sync_file_offset (GScanner *scanner)
1218 g_return_if_fail (scanner != NULL);
1220 /* for file input, rewind the filedescriptor to the current
1221 * buffer position and blow the file read ahead buffer. useful
1222 * for third party uses of our file descriptor, which hooks
1223 * onto the current scanning position.
1226 if (scanner->input_fd >= 0 && scanner->text_end > scanner->text)
1230 buffered = scanner->text_end - scanner->text;
1231 if (lseek (scanner->input_fd, - buffered, SEEK_CUR) >= 0)
1233 /* we succeeded, blow our buffer's contents now */
1234 scanner->text = NULL;
1235 scanner->text_end = NULL;
1243 g_scanner_get_char (GScanner *scanner,
1249 if (scanner->text < scanner->text_end)
1250 fchar = *(scanner->text++);
1251 else if (scanner->input_fd >= 0)
1256 buffer = scanner->buffer;
1259 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
1261 while (count == -1 && (errno == EINTR || errno == EAGAIN));
1265 scanner->input_fd = -1;
1270 scanner->text = buffer + 1;
1271 scanner->text_end = buffer + count;
1275 g_scanner_sync_file_offset (scanner);
1276 scanner->text_end = scanner->text;
1277 scanner->input_fd = -1;
1298 * g_scanner_unexp_token:
1299 * @scanner: a #GScanner
1300 * @expected_token: the expected token
1301 * @identifier_spec: a string describing how the scanner's user
1302 * refers to identifiers (%NULL defaults to "identifier").
1303 * This is used if @expected_token is %G_TOKEN_IDENTIFIER or
1304 * %G_TOKEN_IDENTIFIER_NULL.
1305 * @symbol_spec: a string describing how the scanner's user refers
1306 * to symbols (%NULL defaults to "symbol"). This is used if
1307 * @expected_token is %G_TOKEN_SYMBOL or any token value greater
1308 * than %G_TOKEN_LAST.
1309 * @symbol_name: the name of the symbol, if the scanner's current
1310 * token is a symbol.
1311 * @message: a message string to output at the end of the
1312 * warning/error, or %NULL.
1313 * @is_error: if %TRUE it is output as an error. If %FALSE it is
1314 * output as a warning.
1316 * Outputs a message through the scanner's msg_handler,
1317 * resulting from an unexpected token in the input stream.
1318 * Note that you should not call g_scanner_peek_next_token()
1319 * followed by g_scanner_unexp_token() without an intermediate
1320 * call to g_scanner_get_next_token(), as g_scanner_unexp_token()
1321 * evaluates the scanner's current token (not the peeked token)
1322 * to construct part of the message.
1325 g_scanner_unexp_token (GScanner *scanner,
1326 GTokenType expected_token,
1327 const gchar *identifier_spec,
1328 const gchar *symbol_spec,
1329 const gchar *symbol_name,
1330 const gchar *message,
1333 gchar *token_string;
1334 guint token_string_len;
1335 gchar *expected_string;
1336 guint expected_string_len;
1337 gchar *message_prefix;
1338 gboolean print_unexp;
1339 void (*msg_handler) (GScanner*, const gchar*, ...);
1341 g_return_if_fail (scanner != NULL);
1344 msg_handler = g_scanner_error;
1346 msg_handler = g_scanner_warn;
1348 if (!identifier_spec)
1349 identifier_spec = "identifier";
1351 symbol_spec = "symbol";
1353 token_string_len = 56;
1354 token_string = g_new (gchar, token_string_len + 1);
1355 expected_string_len = 64;
1356 expected_string = g_new (gchar, expected_string_len + 1);
1359 switch (scanner->token)
1362 _g_snprintf (token_string, token_string_len, "end of file");
1366 if (scanner->token >= 1 && scanner->token <= 255)
1368 if ((scanner->token >= ' ' && scanner->token <= '~') ||
1369 strchr (scanner->config->cset_identifier_first, scanner->token) ||
1370 strchr (scanner->config->cset_identifier_nth, scanner->token))
1371 _g_snprintf (token_string, token_string_len, "character '%c'", scanner->token);
1373 _g_snprintf (token_string, token_string_len, "character '\\%o'", scanner->token);
1376 else if (!scanner->config->symbol_2_token)
1378 _g_snprintf (token_string, token_string_len, "(unknown) token <%d>", scanner->token);
1382 case G_TOKEN_SYMBOL:
1383 if (expected_token == G_TOKEN_SYMBOL ||
1384 (scanner->config->symbol_2_token &&
1385 expected_token > G_TOKEN_LAST))
1386 print_unexp = FALSE;
1388 _g_snprintf (token_string,
1391 print_unexp ? "" : "invalid ",
1395 _g_snprintf (token_string,
1398 print_unexp ? "" : "invalid ",
1403 print_unexp = FALSE;
1404 expected_token = G_TOKEN_NONE;
1405 switch (scanner->value.v_error)
1407 case G_ERR_UNEXP_EOF:
1408 _g_snprintf (token_string, token_string_len, "scanner: unexpected end of file");
1411 case G_ERR_UNEXP_EOF_IN_STRING:
1412 _g_snprintf (token_string, token_string_len, "scanner: unterminated string constant");
1415 case G_ERR_UNEXP_EOF_IN_COMMENT:
1416 _g_snprintf (token_string, token_string_len, "scanner: unterminated comment");
1419 case G_ERR_NON_DIGIT_IN_CONST:
1420 _g_snprintf (token_string, token_string_len, "scanner: non digit in constant");
1423 case G_ERR_FLOAT_RADIX:
1424 _g_snprintf (token_string, token_string_len, "scanner: invalid radix for floating constant");
1427 case G_ERR_FLOAT_MALFORMED:
1428 _g_snprintf (token_string, token_string_len, "scanner: malformed floating constant");
1431 case G_ERR_DIGIT_RADIX:
1432 _g_snprintf (token_string, token_string_len, "scanner: digit is beyond radix");
1437 _g_snprintf (token_string, token_string_len, "scanner: unknown error");
1443 _g_snprintf (token_string, token_string_len, "character '%c'", scanner->value.v_char);
1446 case G_TOKEN_IDENTIFIER:
1447 case G_TOKEN_IDENTIFIER_NULL:
1448 if (expected_token == G_TOKEN_IDENTIFIER ||
1449 expected_token == G_TOKEN_IDENTIFIER_NULL)
1450 print_unexp = FALSE;
1451 _g_snprintf (token_string,
1454 print_unexp ? "" : "invalid ",
1456 scanner->token == G_TOKEN_IDENTIFIER ? scanner->value.v_string : "null");
1459 case G_TOKEN_BINARY:
1463 if (scanner->config->store_int64)
1464 _g_snprintf (token_string, token_string_len, "number '%" G_GUINT64_FORMAT "'", scanner->value.v_int64);
1466 _g_snprintf (token_string, token_string_len, "number '%lu'", scanner->value.v_int);
1470 _g_snprintf (token_string, token_string_len, "number '%.3f'", scanner->value.v_float);
1473 case G_TOKEN_STRING:
1474 if (expected_token == G_TOKEN_STRING)
1475 print_unexp = FALSE;
1476 _g_snprintf (token_string,
1478 "%s%sstring constant \"%s\"",
1479 print_unexp ? "" : "invalid ",
1480 scanner->value.v_string[0] == 0 ? "empty " : "",
1481 scanner->value.v_string);
1482 token_string[token_string_len - 2] = '"';
1483 token_string[token_string_len - 1] = 0;
1486 case G_TOKEN_COMMENT_SINGLE:
1487 case G_TOKEN_COMMENT_MULTI:
1488 _g_snprintf (token_string, token_string_len, "comment");
1492 /* somehow the user's parsing code is screwed, there isn't much
1493 * we can do about it.
1494 * Note, a common case to trigger this is
1495 * g_scanner_peek_next_token(); g_scanner_unexp_token();
1496 * without an intermediate g_scanner_get_next_token().
1498 g_assert_not_reached ();
1503 switch (expected_token)
1505 gboolean need_valid;
1508 _g_snprintf (expected_string, expected_string_len, "end of file");
1511 if (expected_token >= 1 && expected_token <= 255)
1513 if ((expected_token >= ' ' && expected_token <= '~') ||
1514 strchr (scanner->config->cset_identifier_first, expected_token) ||
1515 strchr (scanner->config->cset_identifier_nth, expected_token))
1516 _g_snprintf (expected_string, expected_string_len, "character '%c'", expected_token);
1518 _g_snprintf (expected_string, expected_string_len, "character '\\%o'", expected_token);
1521 else if (!scanner->config->symbol_2_token)
1523 _g_snprintf (expected_string, expected_string_len, "(unknown) token <%d>", expected_token);
1527 case G_TOKEN_SYMBOL:
1528 need_valid = (scanner->token == G_TOKEN_SYMBOL ||
1529 (scanner->config->symbol_2_token &&
1530 scanner->token > G_TOKEN_LAST));
1531 _g_snprintf (expected_string,
1532 expected_string_len,
1534 need_valid ? "valid " : "",
1536 /* FIXME: should we attempt to look up the symbol_name for symbol_2_token? */
1539 _g_snprintf (expected_string, expected_string_len, "%scharacter",
1540 scanner->token == G_TOKEN_CHAR ? "valid " : "");
1542 case G_TOKEN_BINARY:
1544 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1545 scanner->token == expected_token ? "valid " : "", tstring);
1549 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1550 scanner->token == expected_token ? "valid " : "", tstring);
1553 tstring = "integer";
1554 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1555 scanner->token == expected_token ? "valid " : "", tstring);
1558 tstring = "hexadecimal";
1559 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1560 scanner->token == expected_token ? "valid " : "", tstring);
1564 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1565 scanner->token == expected_token ? "valid " : "", tstring);
1567 case G_TOKEN_STRING:
1568 _g_snprintf (expected_string,
1569 expected_string_len,
1570 "%sstring constant",
1571 scanner->token == G_TOKEN_STRING ? "valid " : "");
1573 case G_TOKEN_IDENTIFIER:
1574 case G_TOKEN_IDENTIFIER_NULL:
1575 need_valid = (scanner->token == G_TOKEN_IDENTIFIER_NULL ||
1576 scanner->token == G_TOKEN_IDENTIFIER);
1577 _g_snprintf (expected_string,
1578 expected_string_len,
1580 need_valid ? "valid " : "",
1583 case G_TOKEN_COMMENT_SINGLE:
1584 tstring = "single-line";
1585 _g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1586 scanner->token == expected_token ? "valid " : "", tstring);
1588 case G_TOKEN_COMMENT_MULTI:
1589 tstring = "multi-line";
1590 _g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1591 scanner->token == expected_token ? "valid " : "", tstring);
1595 /* this is handled upon printout */
1599 if (message && message[0] != 0)
1600 message_prefix = " - ";
1603 message_prefix = "";
1606 if (expected_token == G_TOKEN_ERROR)
1608 msg_handler (scanner,
1609 "failure around %s%s%s",
1614 else if (expected_token == G_TOKEN_NONE)
1617 msg_handler (scanner,
1618 "unexpected %s%s%s",
1623 msg_handler (scanner,
1632 msg_handler (scanner,
1633 "unexpected %s, expected %s%s%s",
1639 msg_handler (scanner,
1640 "%s, expected %s%s%s",
1647 g_free (token_string);
1648 g_free (expected_string);
1652 g_scanner_get_token_i (GScanner *scanner,
1653 GTokenType *token_p,
1654 GTokenValue *value_p,
1660 g_scanner_free_value (token_p, value_p);
1661 g_scanner_get_token_ll (scanner, token_p, value_p, line_p, position_p);
1663 while (((*token_p > 0 && *token_p < 256) &&
1664 strchr (scanner->config->cset_skip_characters, *token_p)) ||
1665 (*token_p == G_TOKEN_CHAR &&
1666 strchr (scanner->config->cset_skip_characters, value_p->v_char)) ||
1667 (*token_p == G_TOKEN_COMMENT_MULTI &&
1668 scanner->config->skip_comment_multi) ||
1669 (*token_p == G_TOKEN_COMMENT_SINGLE &&
1670 scanner->config->skip_comment_single));
1674 case G_TOKEN_IDENTIFIER:
1675 if (scanner->config->identifier_2_string)
1676 *token_p = G_TOKEN_STRING;
1679 case G_TOKEN_SYMBOL:
1680 if (scanner->config->symbol_2_token)
1681 *token_p = (GTokenType) value_p->v_symbol;
1684 case G_TOKEN_BINARY:
1687 if (scanner->config->numbers_2_int)
1688 *token_p = G_TOKEN_INT;
1695 if (*token_p == G_TOKEN_INT &&
1696 scanner->config->int_2_float)
1698 *token_p = G_TOKEN_FLOAT;
1699 if (scanner->config->store_int64)
1701 value_p->v_float = value_p->v_int64;
1704 value_p->v_float = value_p->v_int;
1711 g_scanner_get_token_ll (GScanner *scanner,
1712 GTokenType *token_p,
1713 GTokenValue *value_p,
1717 GScannerConfig *config;
1719 gboolean in_comment_multi;
1720 gboolean in_comment_single;
1721 gboolean in_string_sq;
1722 gboolean in_string_dq;
1727 config = scanner->config;
1728 (*value_p).v_int64 = 0;
1730 if ((scanner->text >= scanner->text_end && scanner->input_fd < 0) ||
1731 scanner->token == G_TOKEN_EOF)
1733 *token_p = G_TOKEN_EOF;
1737 in_comment_multi = FALSE;
1738 in_comment_single = FALSE;
1739 in_string_sq = FALSE;
1740 in_string_dq = FALSE;
1743 do /* while (ch != 0) */
1745 gboolean dotted_float = FALSE;
1747 ch = g_scanner_get_char (scanner, line_p, position_p);
1750 token = G_TOKEN_NONE;
1752 /* this is *evil*, but needed ;(
1753 * we first check for identifier first character, because it
1754 * might interfere with other key chars like slashes or numbers
1756 if (config->scan_identifier &&
1757 ch && strchr (config->cset_identifier_first, ch))
1758 goto identifier_precedence;
1763 token = G_TOKEN_EOF;
1769 if (!config->scan_comment_multi ||
1770 g_scanner_peek_next_char (scanner) != '*')
1772 g_scanner_get_char (scanner, line_p, position_p);
1773 token = G_TOKEN_COMMENT_MULTI;
1774 in_comment_multi = TRUE;
1775 gstring = g_string_new (NULL);
1776 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1778 if (ch == '*' && g_scanner_peek_next_char (scanner) == '/')
1780 g_scanner_get_char (scanner, line_p, position_p);
1781 in_comment_multi = FALSE;
1785 gstring = g_string_append_c (gstring, ch);
1791 if (!config->scan_string_sq)
1793 token = G_TOKEN_STRING;
1794 in_string_sq = TRUE;
1795 gstring = g_string_new (NULL);
1796 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1800 in_string_sq = FALSE;
1804 gstring = g_string_append_c (gstring, ch);
1810 if (!config->scan_string_dq)
1812 token = G_TOKEN_STRING;
1813 in_string_dq = TRUE;
1814 gstring = g_string_new (NULL);
1815 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1819 in_string_dq = FALSE;
1826 ch = g_scanner_get_char (scanner, line_p, position_p);
1836 gstring = g_string_append_c (gstring, '\\');
1840 gstring = g_string_append_c (gstring, '\n');
1844 gstring = g_string_append_c (gstring, '\t');
1848 gstring = g_string_append_c (gstring, '\r');
1852 gstring = g_string_append_c (gstring, '\b');
1856 gstring = g_string_append_c (gstring, '\f');
1868 fchar = g_scanner_peek_next_char (scanner);
1869 if (fchar >= '0' && fchar <= '7')
1871 ch = g_scanner_get_char (scanner, line_p, position_p);
1872 i = i * 8 + ch - '0';
1873 fchar = g_scanner_peek_next_char (scanner);
1874 if (fchar >= '0' && fchar <= '7')
1876 ch = g_scanner_get_char (scanner, line_p, position_p);
1877 i = i * 8 + ch - '0';
1880 gstring = g_string_append_c (gstring, i);
1884 gstring = g_string_append_c (gstring, ch);
1889 gstring = g_string_append_c (gstring, ch);
1896 if (!config->scan_float)
1898 token = G_TOKEN_FLOAT;
1899 dotted_float = TRUE;
1900 ch = g_scanner_get_char (scanner, line_p, position_p);
1901 goto number_parsing;
1904 if (!config->scan_hex_dollar)
1906 token = G_TOKEN_HEX;
1907 ch = g_scanner_get_char (scanner, line_p, position_p);
1908 goto number_parsing;
1911 if (config->scan_octal)
1912 token = G_TOKEN_OCTAL;
1914 token = G_TOKEN_INT;
1915 ch = g_scanner_peek_next_char (scanner);
1916 if (config->scan_hex && (ch == 'x' || ch == 'X'))
1918 token = G_TOKEN_HEX;
1919 g_scanner_get_char (scanner, line_p, position_p);
1920 ch = g_scanner_get_char (scanner, line_p, position_p);
1923 token = G_TOKEN_ERROR;
1924 value.v_error = G_ERR_UNEXP_EOF;
1928 if (g_scanner_char_2_num (ch, 16) < 0)
1930 token = G_TOKEN_ERROR;
1931 value.v_error = G_ERR_DIGIT_RADIX;
1936 else if (config->scan_binary && (ch == 'b' || ch == 'B'))
1938 token = G_TOKEN_BINARY;
1939 g_scanner_get_char (scanner, line_p, position_p);
1940 ch = g_scanner_get_char (scanner, line_p, position_p);
1943 token = G_TOKEN_ERROR;
1944 value.v_error = G_ERR_UNEXP_EOF;
1948 if (g_scanner_char_2_num (ch, 10) < 0)
1950 token = G_TOKEN_ERROR;
1951 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1970 gboolean in_number = TRUE;
1973 if (token == G_TOKEN_NONE)
1974 token = G_TOKEN_INT;
1976 gstring = g_string_new (dotted_float ? "0." : "");
1977 gstring = g_string_append_c (gstring, ch);
1979 do /* while (in_number) */
1983 is_E = token == G_TOKEN_FLOAT && (ch == 'e' || ch == 'E');
1985 ch = g_scanner_peek_next_char (scanner);
1987 if (g_scanner_char_2_num (ch, 36) >= 0 ||
1988 (config->scan_float && ch == '.') ||
1989 (is_E && (ch == '+' || ch == '-')))
1991 ch = g_scanner_get_char (scanner, line_p, position_p);
1996 if (token != G_TOKEN_INT && token != G_TOKEN_OCTAL)
1998 value.v_error = token == G_TOKEN_FLOAT ? G_ERR_FLOAT_MALFORMED : G_ERR_FLOAT_RADIX;
1999 token = G_TOKEN_ERROR;
2004 token = G_TOKEN_FLOAT;
2005 gstring = g_string_append_c (gstring, ch);
2019 gstring = g_string_append_c (gstring, ch);
2024 if (token != G_TOKEN_FLOAT)
2026 token = G_TOKEN_ERROR;
2027 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2031 gstring = g_string_append_c (gstring, ch);
2036 if ((token != G_TOKEN_HEX && !config->scan_float) ||
2037 (token != G_TOKEN_HEX &&
2038 token != G_TOKEN_OCTAL &&
2039 token != G_TOKEN_FLOAT &&
2040 token != G_TOKEN_INT))
2042 token = G_TOKEN_ERROR;
2043 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2048 if (token != G_TOKEN_HEX)
2049 token = G_TOKEN_FLOAT;
2050 gstring = g_string_append_c (gstring, ch);
2055 if (token != G_TOKEN_HEX)
2057 token = G_TOKEN_ERROR;
2058 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2062 gstring = g_string_append_c (gstring, ch);
2072 if (token == G_TOKEN_FLOAT)
2073 value.v_float = g_strtod (gstring->str, &endptr);
2079 case G_TOKEN_BINARY:
2080 ui64 = g_ascii_strtoull (gstring->str, &endptr, 2);
2083 ui64 = g_ascii_strtoull (gstring->str, &endptr, 8);
2086 ui64 = g_ascii_strtoull (gstring->str, &endptr, 10);
2089 ui64 = g_ascii_strtoull (gstring->str, &endptr, 16);
2093 if (scanner->config->store_int64)
2094 value.v_int64 = ui64;
2098 if (endptr && *endptr)
2100 token = G_TOKEN_ERROR;
2101 if (*endptr == 'e' || *endptr == 'E')
2102 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2104 value.v_error = G_ERR_DIGIT_RADIX;
2106 g_string_free (gstring, TRUE);
2109 } /* number_parsing:... */
2115 if (config->cpair_comment_single &&
2116 ch == config->cpair_comment_single[0])
2118 token = G_TOKEN_COMMENT_SINGLE;
2119 in_comment_single = TRUE;
2120 gstring = g_string_new (NULL);
2121 ch = g_scanner_get_char (scanner, line_p, position_p);
2124 if (ch == config->cpair_comment_single[1])
2126 in_comment_single = FALSE;
2131 gstring = g_string_append_c (gstring, ch);
2132 ch = g_scanner_get_char (scanner, line_p, position_p);
2134 /* ignore a missing newline at EOF for single line comments */
2135 if (in_comment_single &&
2136 config->cpair_comment_single[1] == '\n')
2137 in_comment_single = FALSE;
2139 else if (config->scan_identifier && ch &&
2140 strchr (config->cset_identifier_first, ch))
2142 identifier_precedence:
2144 if (config->cset_identifier_nth && ch &&
2145 strchr (config->cset_identifier_nth,
2146 g_scanner_peek_next_char (scanner)))
2148 token = G_TOKEN_IDENTIFIER;
2149 gstring = g_string_new (NULL);
2150 gstring = g_string_append_c (gstring, ch);
2153 ch = g_scanner_get_char (scanner, line_p, position_p);
2154 gstring = g_string_append_c (gstring, ch);
2155 ch = g_scanner_peek_next_char (scanner);
2157 while (ch && strchr (config->cset_identifier_nth, ch));
2160 else if (config->scan_identifier_1char)
2162 token = G_TOKEN_IDENTIFIER;
2163 value.v_identifier = g_new0 (gchar, 2);
2164 value.v_identifier[0] = ch;
2170 if (config->char_2_token)
2174 token = G_TOKEN_CHAR;
2179 } /* default_case:... */
2182 g_assert (ch == 0 && token != G_TOKEN_NONE); /* paranoid */
2186 if (in_comment_multi || in_comment_single ||
2187 in_string_sq || in_string_dq)
2189 token = G_TOKEN_ERROR;
2192 g_string_free (gstring, TRUE);
2196 if (in_comment_multi || in_comment_single)
2197 value.v_error = G_ERR_UNEXP_EOF_IN_COMMENT;
2198 else /* (in_string_sq || in_string_dq) */
2199 value.v_error = G_ERR_UNEXP_EOF_IN_STRING;
2204 value.v_string = g_string_free (gstring, FALSE);
2208 if (token == G_TOKEN_IDENTIFIER)
2210 if (config->scan_symbols)
2215 scope_id = scanner->scope_id;
2216 key = g_scanner_lookup_internal (scanner, scope_id, value.v_identifier);
2217 if (!key && scope_id && scanner->config->scope_0_fallback)
2218 key = g_scanner_lookup_internal (scanner, 0, value.v_identifier);
2222 g_free (value.v_identifier);
2223 token = G_TOKEN_SYMBOL;
2224 value.v_symbol = key->value;
2228 if (token == G_TOKEN_IDENTIFIER &&
2229 config->scan_identifier_NULL &&
2230 strlen (value.v_identifier) == 4)
2232 gchar *null_upper = "NULL";
2233 gchar *null_lower = "null";
2235 if (scanner->config->case_sensitive)
2237 if (value.v_identifier[0] == null_upper[0] &&
2238 value.v_identifier[1] == null_upper[1] &&
2239 value.v_identifier[2] == null_upper[2] &&
2240 value.v_identifier[3] == null_upper[3])
2241 token = G_TOKEN_IDENTIFIER_NULL;
2245 if ((value.v_identifier[0] == null_upper[0] ||
2246 value.v_identifier[0] == null_lower[0]) &&
2247 (value.v_identifier[1] == null_upper[1] ||
2248 value.v_identifier[1] == null_lower[1]) &&
2249 (value.v_identifier[2] == null_upper[2] ||
2250 value.v_identifier[2] == null_lower[2]) &&
2251 (value.v_identifier[3] == null_upper[3] ||
2252 value.v_identifier[3] == null_lower[3]))
2253 token = G_TOKEN_IDENTIFIER_NULL;