1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GScanner: Flexible lexical scanner for general purpose.
5 * Copyright (C) 1997, 1998 Tim Janik
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, 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 uppercase ISO 8859-1 alphabet characters
94 * which are not ASCII characters.
95 * Used for specifying valid identifier characters
102 * The set of lowercase ISO 8859-1 alphabet characters
103 * which are not ASCII characters.
104 * Used for specifying valid identifier characters
105 * in #GScannerConfig.
110 * @G_TOKEN_EOF: the end of the file
111 * @G_TOKEN_LEFT_PAREN: a '(' character
112 * @G_TOKEN_LEFT_CURLY: a '{' character
113 * @G_TOKEN_LEFT_BRACE: a '[' character
114 * @G_TOKEN_RIGHT_CURLY: a '}' character
115 * @G_TOKEN_RIGHT_PAREN: a ')' character
116 * @G_TOKEN_RIGHT_BRACE: a ']' character
117 * @G_TOKEN_EQUAL_SIGN: a '=' character
118 * @G_TOKEN_COMMA: a ',' character
119 * @G_TOKEN_NONE: not a token
120 * @G_TOKEN_ERROR: an error occurred
121 * @G_TOKEN_CHAR: a character
122 * @G_TOKEN_BINARY: a binary integer
123 * @G_TOKEN_OCTAL: an octal integer
124 * @G_TOKEN_INT: an integer
125 * @G_TOKEN_HEX: a hex integer
126 * @G_TOKEN_FLOAT: a floating point number
127 * @G_TOKEN_STRING: a string
128 * @G_TOKEN_SYMBOL: a symbol
129 * @G_TOKEN_IDENTIFIER: an identifier
130 * @G_TOKEN_IDENTIFIER_NULL: a null identifier
131 * @G_TOKEN_COMMENT_SINGLE: one line comment
132 * @G_TOKEN_COMMENT_MULTI: multi line comment
134 * The possible types of token returned from each
135 * g_scanner_get_next_token() call.
140 * @v_symbol: token symbol value
141 * @v_identifier: token identifier value
142 * @v_binary: token binary integer value
143 * @v_octal: octal integer value
144 * @v_int: integer value
145 * @v_int64: 64-bit integer value
146 * @v_float: floating point value
147 * @v_hex: hex integer value
148 * @v_string: string value
149 * @v_comment: comment value
150 * @v_char: character value
151 * @v_error: error value
153 * A union holding the value of the token.
158 * @G_ERR_UNKNOWN: unknown error
159 * @G_ERR_UNEXP_EOF: unexpected end of file
160 * @G_ERR_UNEXP_EOF_IN_STRING: unterminated string constant
161 * @G_ERR_UNEXP_EOF_IN_COMMENT: unterminated comment
162 * @G_ERR_NON_DIGIT_IN_CONST: non-digit character in a number
163 * @G_ERR_DIGIT_RADIX: digit beyond radix in a number
164 * @G_ERR_FLOAT_RADIX: non-decimal floating point number
165 * @G_ERR_FLOAT_MALFORMED: malformed floating point number
167 * The possible errors, used in the @v_error field
168 * of #GTokenValue, when the token is a %G_TOKEN_ERROR.
174 * @max_parse_errors: unused
175 * @parse_errors: g_scanner_error() increments this field
176 * @input_name: name of input stream, featured by the default message handler
177 * @qdata: quarked data
178 * @config: link into the scanner configuration
179 * @token: token parsed by the last g_scanner_get_next_token()
180 * @value: value of the last token from g_scanner_get_next_token()
181 * @line: line number of the last token from g_scanner_get_next_token()
182 * @position: char number of the last token from g_scanner_get_next_token()
183 * @next_token: token parsed by the last g_scanner_peek_next_token()
184 * @next_value: value of the last token from g_scanner_peek_next_token()
185 * @next_line: line number of the last token from g_scanner_peek_next_token()
186 * @next_position: char number of the last token from g_scanner_peek_next_token()
187 * @msg_handler: handler function for _warn and _error
189 * The data structure representing a lexical scanner.
191 * You should set @input_name after creating the scanner, since
192 * it is used by the default message handler when displaying
193 * warnings and errors. If you are scanning a file, the filename
194 * would be a good choice.
196 * The @user_data and @max_parse_errors fields are not used.
197 * If you need to associate extra data with the scanner you
198 * can place them here.
200 * If you want to use your own message handler you can set the
201 * @msg_handler field. The type of the message handler function
202 * is declared by #GScannerMsgFunc.
207 * @cset_skip_characters: specifies which characters should be skipped
208 * by the scanner (the default is the whitespace characters: space,
209 * tab, carriage-return and line-feed).
210 * @cset_identifier_first: specifies the characters which can start
211 * identifiers (the default is #G_CSET_a_2_z, "_", and #G_CSET_A_2_Z).
212 * @cset_identifier_nth: specifies the characters which can be used
213 * in identifiers, after the first character (the default is
214 * #G_CSET_a_2_z, "_0123456789", #G_CSET_A_2_Z, #G_CSET_LATINS,
216 * @cpair_comment_single: specifies the characters at the start and
217 * end of single-line comments. The default is "#\n" which means
218 * that single-line comments start with a '#' and continue until
219 * a '\n' (end of line).
220 * @case_sensitive: specifies if symbols are case sensitive (the
221 * default is %FALSE).
222 * @skip_comment_multi: specifies if multi-line comments are skipped
223 * and not returned as tokens (the default is %TRUE).
224 * @skip_comment_single: specifies if single-line comments are skipped
225 * and not returned as tokens (the default is %TRUE).
226 * @scan_comment_multi: specifies if multi-line comments are recognized
227 * (the default is %TRUE).
228 * @scan_identifier: specifies if identifiers are recognized (the
230 * @scan_identifier_1char: specifies if single-character
231 * identifiers are recognized (the default is %FALSE).
232 * @scan_identifier_NULL: specifies if %NULL is reported as
233 * %G_TOKEN_IDENTIFIER_NULL (the default is %FALSE).
234 * @scan_symbols: specifies if symbols are recognized (the default
236 * @scan_binary: specifies if binary numbers are recognized (the
237 * default is %FALSE).
238 * @scan_octal: specifies if octal numbers are recognized (the
240 * @scan_float: specifies if floating point numbers are recognized
241 * (the default is %TRUE).
242 * @scan_hex: specifies if hexadecimal numbers are recognized (the
244 * @scan_hex_dollar: specifies if '$' is recognized as a prefix for
245 * hexadecimal numbers (the default is %FALSE).
246 * @scan_string_sq: specifies if strings can be enclosed in single
247 * quotes (the default is %TRUE).
248 * @scan_string_dq: specifies if strings can be enclosed in double
249 * quotes (the default is %TRUE).
250 * @numbers_2_int: specifies if binary, octal and hexadecimal numbers
251 * are reported as #G_TOKEN_INT (the default is %TRUE).
252 * @int_2_float: specifies if all numbers are reported as %G_TOKEN_FLOAT
253 * (the default is %FALSE).
254 * @identifier_2_string: specifies if identifiers are reported as strings
255 * (the default is %FALSE).
256 * @char_2_token: specifies if characters are reported by setting
257 * <literal>token = ch</literal> or as %G_TOKEN_CHAR (the default
259 * @symbol_2_token: specifies if symbols are reported by setting
260 * <literal>token = v_symbol</literal> or as %G_TOKEN_SYMBOL (the
261 * default is %FALSE).
262 * @scope_0_fallback: specifies if a symbol is searched for in the
263 * default scope in addition to the current scope (the default is %FALSE).
264 * @store_int64: use value.v_int64 rather than v_int
266 * Specifies the #GScanner parser configuration. Most settings can
267 * be changed during the parsing phase and will affect the lexical
268 * parsing of the next unpeeked token.
271 /* --- defines --- */
272 #define to_lower(c) ( \
274 ( (((guchar)(c))>='A' && ((guchar)(c))<='Z') * ('a'-'A') ) | \
275 ( (((guchar)(c))>=192 && ((guchar)(c))<=214) * (224-192) ) | \
276 ( (((guchar)(c))>=216 && ((guchar)(c))<=222) * (248-216) ) | \
280 #define READ_BUFFER_SIZE (4000)
283 /* --- typedefs --- */
284 typedef struct _GScannerKey GScannerKey;
294 /* --- variables --- */
295 static const GScannerConfig g_scanner_config_template =
299 ) /* cset_skip_characters */,
304 ) /* cset_identifier_first */,
312 ) /* cset_identifier_nth */,
313 ( "#\n" ) /* cpair_comment_single */,
315 FALSE /* case_sensitive */,
317 TRUE /* skip_comment_multi */,
318 TRUE /* skip_comment_single */,
319 TRUE /* scan_comment_multi */,
320 TRUE /* scan_identifier */,
321 FALSE /* scan_identifier_1char */,
322 FALSE /* scan_identifier_NULL */,
323 TRUE /* scan_symbols */,
324 FALSE /* scan_binary */,
325 TRUE /* scan_octal */,
326 TRUE /* scan_float */,
328 FALSE /* scan_hex_dollar */,
329 TRUE /* scan_string_sq */,
330 TRUE /* scan_string_dq */,
331 TRUE /* numbers_2_int */,
332 FALSE /* int_2_float */,
333 FALSE /* identifier_2_string */,
334 TRUE /* char_2_token */,
335 FALSE /* symbol_2_token */,
336 FALSE /* scope_0_fallback */,
337 FALSE /* store_int64 */,
341 /* --- prototypes --- */
343 GScannerKey* g_scanner_lookup_internal (GScanner *scanner,
345 const gchar *symbol);
346 static gboolean g_scanner_key_equal (gconstpointer v1,
348 static guint g_scanner_key_hash (gconstpointer v);
349 static void g_scanner_get_token_ll (GScanner *scanner,
351 GTokenValue *value_p,
354 static void g_scanner_get_token_i (GScanner *scanner,
356 GTokenValue *value_p,
360 static guchar g_scanner_peek_next_char (GScanner *scanner);
361 static guchar g_scanner_get_char (GScanner *scanner,
364 static void g_scanner_msg_handler (GScanner *scanner,
369 /* --- functions --- */
371 g_scanner_char_2_num (guchar c,
374 if (c >= '0' && c <= '9')
376 else if (c >= 'A' && c <= 'Z')
378 else if (c >= 'a' && c <= 'z')
391 * @config_templ: the initial scanner settings
393 * Creates a new #GScanner.
395 * The @config_templ structure specifies the initial settings
396 * of the scanner, which are copied into the #GScanner
397 * @config field. If you pass %NULL then the default settings
400 * Returns: the new #GScanner
403 g_scanner_new (const GScannerConfig *config_templ)
408 config_templ = &g_scanner_config_template;
410 scanner = g_new0 (GScanner, 1);
412 scanner->user_data = NULL;
413 scanner->max_parse_errors = 1;
414 scanner->parse_errors = 0;
415 scanner->input_name = NULL;
416 g_datalist_init (&scanner->qdata);
418 scanner->config = g_new0 (GScannerConfig, 1);
420 scanner->config->case_sensitive = config_templ->case_sensitive;
421 scanner->config->cset_skip_characters = config_templ->cset_skip_characters;
422 if (!scanner->config->cset_skip_characters)
423 scanner->config->cset_skip_characters = "";
424 scanner->config->cset_identifier_first = config_templ->cset_identifier_first;
425 scanner->config->cset_identifier_nth = config_templ->cset_identifier_nth;
426 scanner->config->cpair_comment_single = config_templ->cpair_comment_single;
427 scanner->config->skip_comment_multi = config_templ->skip_comment_multi;
428 scanner->config->skip_comment_single = config_templ->skip_comment_single;
429 scanner->config->scan_comment_multi = config_templ->scan_comment_multi;
430 scanner->config->scan_identifier = config_templ->scan_identifier;
431 scanner->config->scan_identifier_1char = config_templ->scan_identifier_1char;
432 scanner->config->scan_identifier_NULL = config_templ->scan_identifier_NULL;
433 scanner->config->scan_symbols = config_templ->scan_symbols;
434 scanner->config->scan_binary = config_templ->scan_binary;
435 scanner->config->scan_octal = config_templ->scan_octal;
436 scanner->config->scan_float = config_templ->scan_float;
437 scanner->config->scan_hex = config_templ->scan_hex;
438 scanner->config->scan_hex_dollar = config_templ->scan_hex_dollar;
439 scanner->config->scan_string_sq = config_templ->scan_string_sq;
440 scanner->config->scan_string_dq = config_templ->scan_string_dq;
441 scanner->config->numbers_2_int = config_templ->numbers_2_int;
442 scanner->config->int_2_float = config_templ->int_2_float;
443 scanner->config->identifier_2_string = config_templ->identifier_2_string;
444 scanner->config->char_2_token = config_templ->char_2_token;
445 scanner->config->symbol_2_token = config_templ->symbol_2_token;
446 scanner->config->scope_0_fallback = config_templ->scope_0_fallback;
447 scanner->config->store_int64 = config_templ->store_int64;
449 scanner->token = G_TOKEN_NONE;
450 scanner->value.v_int64 = 0;
452 scanner->position = 0;
454 scanner->next_token = G_TOKEN_NONE;
455 scanner->next_value.v_int64 = 0;
456 scanner->next_line = 1;
457 scanner->next_position = 0;
459 scanner->symbol_table = g_hash_table_new (g_scanner_key_hash, g_scanner_key_equal);
460 scanner->input_fd = -1;
461 scanner->text = NULL;
462 scanner->text_end = NULL;
463 scanner->buffer = NULL;
464 scanner->scope_id = 0;
466 scanner->msg_handler = g_scanner_msg_handler;
472 g_scanner_free_value (GTokenType *token_p,
473 GTokenValue *value_p)
478 case G_TOKEN_IDENTIFIER:
479 case G_TOKEN_IDENTIFIER_NULL:
480 case G_TOKEN_COMMENT_SINGLE:
481 case G_TOKEN_COMMENT_MULTI:
482 g_free (value_p->v_string);
489 *token_p = G_TOKEN_NONE;
493 g_scanner_destroy_symbol_table_entry (gpointer _key,
497 GScannerKey *key = _key;
499 g_free (key->symbol);
505 * @scanner: a #GScanner
507 * Frees all memory used by the #GScanner.
510 g_scanner_destroy (GScanner *scanner)
512 g_return_if_fail (scanner != NULL);
514 g_datalist_clear (&scanner->qdata);
515 g_hash_table_foreach (scanner->symbol_table,
516 g_scanner_destroy_symbol_table_entry, NULL);
517 g_hash_table_destroy (scanner->symbol_table);
518 g_scanner_free_value (&scanner->token, &scanner->value);
519 g_scanner_free_value (&scanner->next_token, &scanner->next_value);
520 g_free (scanner->config);
521 g_free (scanner->buffer);
526 g_scanner_msg_handler (GScanner *scanner,
530 g_return_if_fail (scanner != NULL);
532 _g_fprintf (stderr, "%s:%d: ",
533 scanner->input_name ? scanner->input_name : "<memory>",
536 _g_fprintf (stderr, "error: ");
537 _g_fprintf (stderr, "%s\n", message);
542 * @scanner: a #GScanner
543 * @format: the message format. See the printf() documentation
544 * @...: the parameters to insert into the format string
546 * Outputs an error message, via the #GScanner message handler.
549 g_scanner_error (GScanner *scanner,
553 g_return_if_fail (scanner != NULL);
554 g_return_if_fail (format != NULL);
556 scanner->parse_errors++;
558 if (scanner->msg_handler)
563 va_start (args, format);
564 string = g_strdup_vprintf (format, args);
567 scanner->msg_handler (scanner, string, TRUE);
575 * @scanner: a #GScanner
576 * @format: the message format. See the printf() documentation
577 * @...: the parameters to insert into the format string
579 * Outputs a warning message, via the #GScanner message handler.
582 g_scanner_warn (GScanner *scanner,
586 g_return_if_fail (scanner != NULL);
587 g_return_if_fail (format != NULL);
589 if (scanner->msg_handler)
594 va_start (args, format);
595 string = g_strdup_vprintf (format, args);
598 scanner->msg_handler (scanner, string, FALSE);
605 g_scanner_key_equal (gconstpointer v1,
608 const GScannerKey *key1 = v1;
609 const GScannerKey *key2 = v2;
611 return (key1->scope_id == key2->scope_id) && (strcmp (key1->symbol, key2->symbol) == 0);
615 g_scanner_key_hash (gconstpointer v)
617 const GScannerKey *key = v;
622 for (c = key->symbol; *c; c++)
623 h = (h << 5) - h + *c;
628 static inline GScannerKey*
629 g_scanner_lookup_internal (GScanner *scanner,
636 key.scope_id = scope_id;
638 if (!scanner->config->case_sensitive)
643 key.symbol = g_new (gchar, strlen (symbol) + 1);
644 for (d = key.symbol, c = symbol; *c; c++, d++)
647 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
652 key.symbol = (gchar*) symbol;
653 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
660 * g_scanner_add_symbol:
661 * @scanner: a #GScanner
662 * @symbol: the symbol to add
663 * @value: the value of the symbol
665 * Adds a symbol to the default scope.
667 * Deprecated: 2.2: Use g_scanner_scope_add_symbol() instead.
671 * g_scanner_scope_add_symbol:
672 * @scanner: a #GScanner
673 * @scope_id: the scope id
674 * @symbol: the symbol to add
675 * @value: the value of the symbol
677 * Adds a symbol to the given scope.
680 g_scanner_scope_add_symbol (GScanner *scanner,
687 g_return_if_fail (scanner != NULL);
688 g_return_if_fail (symbol != NULL);
690 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
694 key = g_new (GScannerKey, 1);
695 key->scope_id = scope_id;
696 key->symbol = g_strdup (symbol);
698 if (!scanner->config->case_sensitive)
709 g_hash_table_insert (scanner->symbol_table, key, key);
716 * g_scanner_remove_symbol:
717 * @scanner: a #GScanner
718 * @symbol: the symbol to remove
720 * Removes a symbol from the default scope.
722 * Deprecated: 2.2: Use g_scanner_scope_remove_symbol() instead.
726 * g_scanner_scope_remove_symbol:
727 * @scanner: a #GScanner
728 * @scope_id: the scope id
729 * @symbol: the symbol to remove
731 * Removes a symbol from a scope.
734 g_scanner_scope_remove_symbol (GScanner *scanner,
740 g_return_if_fail (scanner != NULL);
741 g_return_if_fail (symbol != NULL);
743 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
747 g_hash_table_remove (scanner->symbol_table, key);
748 g_free (key->symbol);
754 * g_scanner_freeze_symbol_table:
755 * @scanner: a #GScanner
757 * There is no reason to use this macro, since it does nothing.
759 * Deprecated: 2.2: This macro does nothing.
763 * g_scanner_thaw_symbol_table:
764 * @scanner: a #GScanner
766 * There is no reason to use this macro, since it does nothing.
768 * Deprecated: 2.2: This macro does nothing.
772 * g_scanner_lookup_symbol:
773 * @scanner: a #GScanner
774 * @symbol: the symbol to look up
776 * Looks up a symbol in the current scope and return its value.
777 * If the symbol is not bound in the current scope, %NULL is
780 * Returns: the value of @symbol in the current scope, or %NULL
781 * if @symbol is not bound in the current scope
784 g_scanner_lookup_symbol (GScanner *scanner,
790 g_return_val_if_fail (scanner != NULL, NULL);
795 scope_id = scanner->scope_id;
796 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
797 if (!key && scope_id && scanner->config->scope_0_fallback)
798 key = g_scanner_lookup_internal (scanner, 0, symbol);
807 * g_scanner_scope_lookup_symbol:
808 * @scanner: a #GScanner
809 * @scope_id: the scope id
810 * @symbol: the symbol to look up
812 * Looks up a symbol in a scope and return its value. If the
813 * symbol is not bound in the scope, %NULL is returned.
815 * Returns: the value of @symbol in the given scope, or %NULL
816 * if @symbol is not bound in the given scope.
820 g_scanner_scope_lookup_symbol (GScanner *scanner,
826 g_return_val_if_fail (scanner != NULL, NULL);
831 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
840 * g_scanner_set_scope:
841 * @scanner: a #GScanner
842 * @scope_id: the new scope id
844 * Sets the current scope.
846 * Returns: the old scope id
849 g_scanner_set_scope (GScanner *scanner,
854 g_return_val_if_fail (scanner != NULL, 0);
856 old_scope_id = scanner->scope_id;
857 scanner->scope_id = scope_id;
863 g_scanner_foreach_internal (gpointer _key,
874 func = (GHFunc) d[0];
879 if (key->scope_id == *scope_id)
880 func (key->symbol, key->value, user_data);
884 * g_scanner_foreach_symbol:
885 * @scanner: a #GScanner
886 * @func: the function to call with each symbol
887 * @data: data to pass to the function
889 * Calls a function for each symbol in the default scope.
891 * Deprecated: 2.2: Use g_scanner_scope_foreach_symbol() instead.
895 * g_scanner_scope_foreach_symbol:
896 * @scanner: a #GScanner
897 * @scope_id: the scope id
898 * @func: the function to call for each symbol/value pair
899 * @user_data: user data to pass to the function
901 * Calls the given function for each of the symbol/value pairs
902 * in the given scope of the #GScanner. The function is passed
903 * the symbol and value of each pair, and the given @user_data
907 g_scanner_scope_foreach_symbol (GScanner *scanner,
914 g_return_if_fail (scanner != NULL);
916 d[0] = (gpointer) func;
920 g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
924 * g_scanner_peek_next_token:
925 * @scanner: a #GScanner
927 * Parses the next token, without removing it from the input stream.
928 * The token data is placed in the @next_token, @next_value, @next_line,
929 * and @next_position fields of the #GScanner structure.
931 * Note that, while the token is not removed from the input stream
932 * (i.e. the next call to g_scanner_get_next_token() will return the
933 * same token), it will not be reevaluated. This can lead to surprising
934 * results when changing scope or the scanner configuration after peeking
935 * the next token. Getting the next token after switching the scope or
936 * configuration will return whatever was peeked before, regardless of
937 * any symbols that may have been added or removed in the new scope.
939 * Returns: the type of the token
942 g_scanner_peek_next_token (GScanner *scanner)
944 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
946 if (scanner->next_token == G_TOKEN_NONE)
948 scanner->next_line = scanner->line;
949 scanner->next_position = scanner->position;
950 g_scanner_get_token_i (scanner,
951 &scanner->next_token,
952 &scanner->next_value,
954 &scanner->next_position);
957 return scanner->next_token;
961 * g_scanner_get_next_token:
962 * @scanner: a #GScanner
964 * Parses the next token just like g_scanner_peek_next_token()
965 * and also removes it from the input stream. The token data is
966 * placed in the @token, @value, @line, and @position fields of
967 * the #GScanner structure.
969 * Returns: the type of the token
972 g_scanner_get_next_token (GScanner *scanner)
974 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
976 if (scanner->next_token != G_TOKEN_NONE)
978 g_scanner_free_value (&scanner->token, &scanner->value);
980 scanner->token = scanner->next_token;
981 scanner->value = scanner->next_value;
982 scanner->line = scanner->next_line;
983 scanner->position = scanner->next_position;
984 scanner->next_token = G_TOKEN_NONE;
987 g_scanner_get_token_i (scanner,
993 return scanner->token;
997 * g_scanner_cur_token:
998 * @scanner: a #GScanner
1000 * Gets the current token type. This is simply the @token
1001 * field in the #GScanner structure.
1003 * Returns: the current token type
1006 g_scanner_cur_token (GScanner *scanner)
1008 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
1010 return scanner->token;
1014 * g_scanner_cur_value:
1015 * @scanner: a #GScanner
1017 * Gets the current token value. This is simply the @value
1018 * field in the #GScanner structure.
1020 * Returns: the current token value
1023 g_scanner_cur_value (GScanner *scanner)
1029 g_return_val_if_fail (scanner != NULL, v);
1031 /* MSC isn't capable of handling return scanner->value; ? */
1039 * g_scanner_cur_line:
1040 * @scanner: a #GScanner
1042 * Returns the current line in the input stream (counting
1043 * from 1). This is the line of the last token parsed via
1044 * g_scanner_get_next_token().
1046 * Returns: the current line
1049 g_scanner_cur_line (GScanner *scanner)
1051 g_return_val_if_fail (scanner != NULL, 0);
1053 return scanner->line;
1057 * g_scanner_cur_position:
1058 * @scanner: a #GScanner
1060 * Returns the current position in the current line (counting
1061 * from 0). This is the position of the last token parsed via
1062 * g_scanner_get_next_token().
1064 * Returns: the current position on the line
1067 g_scanner_cur_position (GScanner *scanner)
1069 g_return_val_if_fail (scanner != NULL, 0);
1071 return scanner->position;
1076 * @scanner: a #GScanner
1078 * Returns %TRUE if the scanner has reached the end of
1079 * the file or text buffer.
1081 * Returns: %TRUE if the scanner has reached the end of
1082 * the file or text buffer
1085 g_scanner_eof (GScanner *scanner)
1087 g_return_val_if_fail (scanner != NULL, TRUE);
1089 return scanner->token == G_TOKEN_EOF || scanner->token == G_TOKEN_ERROR;
1093 * g_scanner_input_file:
1094 * @scanner: a #GScanner
1095 * @input_fd: a file descriptor
1097 * Prepares to scan a file.
1100 g_scanner_input_file (GScanner *scanner,
1103 g_return_if_fail (scanner != NULL);
1104 g_return_if_fail (input_fd >= 0);
1106 if (scanner->input_fd >= 0)
1107 g_scanner_sync_file_offset (scanner);
1109 scanner->token = G_TOKEN_NONE;
1110 scanner->value.v_int64 = 0;
1112 scanner->position = 0;
1113 scanner->next_token = G_TOKEN_NONE;
1115 scanner->input_fd = input_fd;
1116 scanner->text = NULL;
1117 scanner->text_end = NULL;
1119 if (!scanner->buffer)
1120 scanner->buffer = g_new (gchar, READ_BUFFER_SIZE + 1);
1124 * g_scanner_input_text:
1125 * @scanner: a #GScanner
1126 * @text: the text buffer to scan
1127 * @text_len: the length of the text buffer
1129 * Prepares to scan a text buffer.
1132 g_scanner_input_text (GScanner *scanner,
1136 g_return_if_fail (scanner != NULL);
1138 g_return_if_fail (text != NULL);
1142 if (scanner->input_fd >= 0)
1143 g_scanner_sync_file_offset (scanner);
1145 scanner->token = G_TOKEN_NONE;
1146 scanner->value.v_int64 = 0;
1148 scanner->position = 0;
1149 scanner->next_token = G_TOKEN_NONE;
1151 scanner->input_fd = -1;
1152 scanner->text = text;
1153 scanner->text_end = text + text_len;
1155 if (scanner->buffer)
1157 g_free (scanner->buffer);
1158 scanner->buffer = NULL;
1163 g_scanner_peek_next_char (GScanner *scanner)
1165 if (scanner->text < scanner->text_end)
1167 return *scanner->text;
1169 else if (scanner->input_fd >= 0)
1174 buffer = scanner->buffer;
1177 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
1179 while (count == -1 && (errno == EINTR || errno == EAGAIN));
1183 scanner->input_fd = -1;
1189 scanner->text = buffer;
1190 scanner->text_end = buffer + count;
1200 * g_scanner_sync_file_offset:
1201 * @scanner: a #GScanner
1203 * Rewinds the filedescriptor to the current buffer position
1204 * and blows the file read ahead buffer. This is useful for
1205 * third party uses of the scanners filedescriptor, which hooks
1206 * onto the current scanning position.
1209 g_scanner_sync_file_offset (GScanner *scanner)
1211 g_return_if_fail (scanner != NULL);
1213 /* for file input, rewind the filedescriptor to the current
1214 * buffer position and blow the file read ahead buffer. useful
1215 * for third party uses of our file descriptor, which hooks
1216 * onto the current scanning position.
1219 if (scanner->input_fd >= 0 && scanner->text_end > scanner->text)
1223 buffered = scanner->text_end - scanner->text;
1224 if (lseek (scanner->input_fd, - buffered, SEEK_CUR) >= 0)
1226 /* we succeeded, blow our buffer's contents now */
1227 scanner->text = NULL;
1228 scanner->text_end = NULL;
1236 g_scanner_get_char (GScanner *scanner,
1242 if (scanner->text < scanner->text_end)
1243 fchar = *(scanner->text++);
1244 else if (scanner->input_fd >= 0)
1249 buffer = scanner->buffer;
1252 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
1254 while (count == -1 && (errno == EINTR || errno == EAGAIN));
1258 scanner->input_fd = -1;
1263 scanner->text = buffer + 1;
1264 scanner->text_end = buffer + count;
1268 g_scanner_sync_file_offset (scanner);
1269 scanner->text_end = scanner->text;
1270 scanner->input_fd = -1;
1291 * g_scanner_unexp_token:
1292 * @scanner: a #GScanner
1293 * @expected_token: the expected token
1294 * @identifier_spec: a string describing how the scanner's user
1295 * refers to identifiers (%NULL defaults to "identifier").
1296 * This is used if @expected_token is %G_TOKEN_IDENTIFIER or
1297 * %G_TOKEN_IDENTIFIER_NULL.
1298 * @symbol_spec: a string describing how the scanner's user refers
1299 * to symbols (%NULL defaults to "symbol"). This is used if
1300 * @expected_token is %G_TOKEN_SYMBOL or any token value greater
1301 * than %G_TOKEN_LAST.
1302 * @symbol_name: the name of the symbol, if the scanner's current
1303 * token is a symbol.
1304 * @message: a message string to output at the end of the
1305 * warning/error, or %NULL.
1306 * @is_error: if %TRUE it is output as an error. If %FALSE it is
1307 * output as a warning.
1309 * Outputs a message through the scanner's msg_handler,
1310 * resulting from an unexpected token in the input stream.
1311 * Note that you should not call g_scanner_peek_next_token()
1312 * followed by g_scanner_unexp_token() without an intermediate
1313 * call to g_scanner_get_next_token(), as g_scanner_unexp_token()
1314 * evaluates the scanner's current token (not the peeked token)
1315 * to construct part of the message.
1318 g_scanner_unexp_token (GScanner *scanner,
1319 GTokenType expected_token,
1320 const gchar *identifier_spec,
1321 const gchar *symbol_spec,
1322 const gchar *symbol_name,
1323 const gchar *message,
1326 gchar *token_string;
1327 guint token_string_len;
1328 gchar *expected_string;
1329 guint expected_string_len;
1330 gchar *message_prefix;
1331 gboolean print_unexp;
1332 void (*msg_handler) (GScanner*, const gchar*, ...);
1334 g_return_if_fail (scanner != NULL);
1337 msg_handler = g_scanner_error;
1339 msg_handler = g_scanner_warn;
1341 if (!identifier_spec)
1342 identifier_spec = "identifier";
1344 symbol_spec = "symbol";
1346 token_string_len = 56;
1347 token_string = g_new (gchar, token_string_len + 1);
1348 expected_string_len = 64;
1349 expected_string = g_new (gchar, expected_string_len + 1);
1352 switch (scanner->token)
1355 _g_snprintf (token_string, token_string_len, "end of file");
1359 if (scanner->token >= 1 && scanner->token <= 255)
1361 if ((scanner->token >= ' ' && scanner->token <= '~') ||
1362 strchr (scanner->config->cset_identifier_first, scanner->token) ||
1363 strchr (scanner->config->cset_identifier_nth, scanner->token))
1364 _g_snprintf (token_string, token_string_len, "character '%c'", scanner->token);
1366 _g_snprintf (token_string, token_string_len, "character '\\%o'", scanner->token);
1369 else if (!scanner->config->symbol_2_token)
1371 _g_snprintf (token_string, token_string_len, "(unknown) token <%d>", scanner->token);
1375 case G_TOKEN_SYMBOL:
1376 if (expected_token == G_TOKEN_SYMBOL ||
1377 (scanner->config->symbol_2_token &&
1378 expected_token > G_TOKEN_LAST))
1379 print_unexp = FALSE;
1381 _g_snprintf (token_string,
1384 print_unexp ? "" : "invalid ",
1388 _g_snprintf (token_string,
1391 print_unexp ? "" : "invalid ",
1396 print_unexp = FALSE;
1397 expected_token = G_TOKEN_NONE;
1398 switch (scanner->value.v_error)
1400 case G_ERR_UNEXP_EOF:
1401 _g_snprintf (token_string, token_string_len, "scanner: unexpected end of file");
1404 case G_ERR_UNEXP_EOF_IN_STRING:
1405 _g_snprintf (token_string, token_string_len, "scanner: unterminated string constant");
1408 case G_ERR_UNEXP_EOF_IN_COMMENT:
1409 _g_snprintf (token_string, token_string_len, "scanner: unterminated comment");
1412 case G_ERR_NON_DIGIT_IN_CONST:
1413 _g_snprintf (token_string, token_string_len, "scanner: non digit in constant");
1416 case G_ERR_FLOAT_RADIX:
1417 _g_snprintf (token_string, token_string_len, "scanner: invalid radix for floating constant");
1420 case G_ERR_FLOAT_MALFORMED:
1421 _g_snprintf (token_string, token_string_len, "scanner: malformed floating constant");
1424 case G_ERR_DIGIT_RADIX:
1425 _g_snprintf (token_string, token_string_len, "scanner: digit is beyond radix");
1430 _g_snprintf (token_string, token_string_len, "scanner: unknown error");
1436 _g_snprintf (token_string, token_string_len, "character '%c'", scanner->value.v_char);
1439 case G_TOKEN_IDENTIFIER:
1440 case G_TOKEN_IDENTIFIER_NULL:
1441 if (expected_token == G_TOKEN_IDENTIFIER ||
1442 expected_token == G_TOKEN_IDENTIFIER_NULL)
1443 print_unexp = FALSE;
1444 _g_snprintf (token_string,
1447 print_unexp ? "" : "invalid ",
1449 scanner->token == G_TOKEN_IDENTIFIER ? scanner->value.v_string : "null");
1452 case G_TOKEN_BINARY:
1456 if (scanner->config->store_int64)
1457 _g_snprintf (token_string, token_string_len, "number '%" G_GUINT64_FORMAT "'", scanner->value.v_int64);
1459 _g_snprintf (token_string, token_string_len, "number '%lu'", scanner->value.v_int);
1463 _g_snprintf (token_string, token_string_len, "number '%.3f'", scanner->value.v_float);
1466 case G_TOKEN_STRING:
1467 if (expected_token == G_TOKEN_STRING)
1468 print_unexp = FALSE;
1469 _g_snprintf (token_string,
1471 "%s%sstring constant \"%s\"",
1472 print_unexp ? "" : "invalid ",
1473 scanner->value.v_string[0] == 0 ? "empty " : "",
1474 scanner->value.v_string);
1475 token_string[token_string_len - 2] = '"';
1476 token_string[token_string_len - 1] = 0;
1479 case G_TOKEN_COMMENT_SINGLE:
1480 case G_TOKEN_COMMENT_MULTI:
1481 _g_snprintf (token_string, token_string_len, "comment");
1485 /* somehow the user's parsing code is screwed, there isn't much
1486 * we can do about it.
1487 * Note, a common case to trigger this is
1488 * g_scanner_peek_next_token(); g_scanner_unexp_token();
1489 * without an intermediate g_scanner_get_next_token().
1491 g_assert_not_reached ();
1496 switch (expected_token)
1498 gboolean need_valid;
1501 _g_snprintf (expected_string, expected_string_len, "end of file");
1504 if (expected_token >= 1 && expected_token <= 255)
1506 if ((expected_token >= ' ' && expected_token <= '~') ||
1507 strchr (scanner->config->cset_identifier_first, expected_token) ||
1508 strchr (scanner->config->cset_identifier_nth, expected_token))
1509 _g_snprintf (expected_string, expected_string_len, "character '%c'", expected_token);
1511 _g_snprintf (expected_string, expected_string_len, "character '\\%o'", expected_token);
1514 else if (!scanner->config->symbol_2_token)
1516 _g_snprintf (expected_string, expected_string_len, "(unknown) token <%d>", expected_token);
1520 case G_TOKEN_SYMBOL:
1521 need_valid = (scanner->token == G_TOKEN_SYMBOL ||
1522 (scanner->config->symbol_2_token &&
1523 scanner->token > G_TOKEN_LAST));
1524 _g_snprintf (expected_string,
1525 expected_string_len,
1527 need_valid ? "valid " : "",
1529 /* FIXME: should we attempt to lookup the symbol_name for symbol_2_token? */
1532 _g_snprintf (expected_string, expected_string_len, "%scharacter",
1533 scanner->token == G_TOKEN_CHAR ? "valid " : "");
1535 case G_TOKEN_BINARY:
1537 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1538 scanner->token == expected_token ? "valid " : "", tstring);
1542 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1543 scanner->token == expected_token ? "valid " : "", tstring);
1546 tstring = "integer";
1547 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1548 scanner->token == expected_token ? "valid " : "", tstring);
1551 tstring = "hexadecimal";
1552 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1553 scanner->token == expected_token ? "valid " : "", tstring);
1557 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1558 scanner->token == expected_token ? "valid " : "", tstring);
1560 case G_TOKEN_STRING:
1561 _g_snprintf (expected_string,
1562 expected_string_len,
1563 "%sstring constant",
1564 scanner->token == G_TOKEN_STRING ? "valid " : "");
1566 case G_TOKEN_IDENTIFIER:
1567 case G_TOKEN_IDENTIFIER_NULL:
1568 need_valid = (scanner->token == G_TOKEN_IDENTIFIER_NULL ||
1569 scanner->token == G_TOKEN_IDENTIFIER);
1570 _g_snprintf (expected_string,
1571 expected_string_len,
1573 need_valid ? "valid " : "",
1576 case G_TOKEN_COMMENT_SINGLE:
1577 tstring = "single-line";
1578 _g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1579 scanner->token == expected_token ? "valid " : "", tstring);
1581 case G_TOKEN_COMMENT_MULTI:
1582 tstring = "multi-line";
1583 _g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1584 scanner->token == expected_token ? "valid " : "", tstring);
1588 /* this is handled upon printout */
1592 if (message && message[0] != 0)
1593 message_prefix = " - ";
1596 message_prefix = "";
1599 if (expected_token == G_TOKEN_ERROR)
1601 msg_handler (scanner,
1602 "failure around %s%s%s",
1607 else if (expected_token == G_TOKEN_NONE)
1610 msg_handler (scanner,
1611 "unexpected %s%s%s",
1616 msg_handler (scanner,
1625 msg_handler (scanner,
1626 "unexpected %s, expected %s%s%s",
1632 msg_handler (scanner,
1633 "%s, expected %s%s%s",
1640 g_free (token_string);
1641 g_free (expected_string);
1645 g_scanner_get_token_i (GScanner *scanner,
1646 GTokenType *token_p,
1647 GTokenValue *value_p,
1653 g_scanner_free_value (token_p, value_p);
1654 g_scanner_get_token_ll (scanner, token_p, value_p, line_p, position_p);
1656 while (((*token_p > 0 && *token_p < 256) &&
1657 strchr (scanner->config->cset_skip_characters, *token_p)) ||
1658 (*token_p == G_TOKEN_CHAR &&
1659 strchr (scanner->config->cset_skip_characters, value_p->v_char)) ||
1660 (*token_p == G_TOKEN_COMMENT_MULTI &&
1661 scanner->config->skip_comment_multi) ||
1662 (*token_p == G_TOKEN_COMMENT_SINGLE &&
1663 scanner->config->skip_comment_single));
1667 case G_TOKEN_IDENTIFIER:
1668 if (scanner->config->identifier_2_string)
1669 *token_p = G_TOKEN_STRING;
1672 case G_TOKEN_SYMBOL:
1673 if (scanner->config->symbol_2_token)
1674 *token_p = (GTokenType) value_p->v_symbol;
1677 case G_TOKEN_BINARY:
1680 if (scanner->config->numbers_2_int)
1681 *token_p = G_TOKEN_INT;
1688 if (*token_p == G_TOKEN_INT &&
1689 scanner->config->int_2_float)
1691 *token_p = G_TOKEN_FLOAT;
1692 if (scanner->config->store_int64)
1695 /* work around error C2520, see gvaluetransform.c */
1696 value_p->v_float = (__int64)value_p->v_int64;
1698 value_p->v_float = value_p->v_int64;
1702 value_p->v_float = value_p->v_int;
1709 g_scanner_get_token_ll (GScanner *scanner,
1710 GTokenType *token_p,
1711 GTokenValue *value_p,
1715 GScannerConfig *config;
1717 gboolean in_comment_multi;
1718 gboolean in_comment_single;
1719 gboolean in_string_sq;
1720 gboolean in_string_dq;
1725 config = scanner->config;
1726 (*value_p).v_int64 = 0;
1728 if ((scanner->text >= scanner->text_end && scanner->input_fd < 0) ||
1729 scanner->token == G_TOKEN_EOF)
1731 *token_p = G_TOKEN_EOF;
1735 in_comment_multi = FALSE;
1736 in_comment_single = FALSE;
1737 in_string_sq = FALSE;
1738 in_string_dq = FALSE;
1741 do /* while (ch != 0) */
1743 gboolean dotted_float = FALSE;
1745 ch = g_scanner_get_char (scanner, line_p, position_p);
1748 token = G_TOKEN_NONE;
1750 /* this is *evil*, but needed ;(
1751 * we first check for identifier first character, because it
1752 * might interfere with other key chars like slashes or numbers
1754 if (config->scan_identifier &&
1755 ch && strchr (config->cset_identifier_first, ch))
1756 goto identifier_precedence;
1761 token = G_TOKEN_EOF;
1767 if (!config->scan_comment_multi ||
1768 g_scanner_peek_next_char (scanner) != '*')
1770 g_scanner_get_char (scanner, line_p, position_p);
1771 token = G_TOKEN_COMMENT_MULTI;
1772 in_comment_multi = TRUE;
1773 gstring = g_string_new (NULL);
1774 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1776 if (ch == '*' && g_scanner_peek_next_char (scanner) == '/')
1778 g_scanner_get_char (scanner, line_p, position_p);
1779 in_comment_multi = FALSE;
1783 gstring = g_string_append_c (gstring, ch);
1789 if (!config->scan_string_sq)
1791 token = G_TOKEN_STRING;
1792 in_string_sq = TRUE;
1793 gstring = g_string_new (NULL);
1794 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1798 in_string_sq = FALSE;
1802 gstring = g_string_append_c (gstring, ch);
1808 if (!config->scan_string_dq)
1810 token = G_TOKEN_STRING;
1811 in_string_dq = TRUE;
1812 gstring = g_string_new (NULL);
1813 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1817 in_string_dq = FALSE;
1824 ch = g_scanner_get_char (scanner, line_p, position_p);
1834 gstring = g_string_append_c (gstring, '\\');
1838 gstring = g_string_append_c (gstring, '\n');
1842 gstring = g_string_append_c (gstring, '\t');
1846 gstring = g_string_append_c (gstring, '\r');
1850 gstring = g_string_append_c (gstring, '\b');
1854 gstring = g_string_append_c (gstring, '\f');
1866 fchar = g_scanner_peek_next_char (scanner);
1867 if (fchar >= '0' && fchar <= '7')
1869 ch = g_scanner_get_char (scanner, line_p, position_p);
1870 i = i * 8 + ch - '0';
1871 fchar = g_scanner_peek_next_char (scanner);
1872 if (fchar >= '0' && fchar <= '7')
1874 ch = g_scanner_get_char (scanner, line_p, position_p);
1875 i = i * 8 + ch - '0';
1878 gstring = g_string_append_c (gstring, i);
1882 gstring = g_string_append_c (gstring, ch);
1887 gstring = g_string_append_c (gstring, ch);
1894 if (!config->scan_float)
1896 token = G_TOKEN_FLOAT;
1897 dotted_float = TRUE;
1898 ch = g_scanner_get_char (scanner, line_p, position_p);
1899 goto number_parsing;
1902 if (!config->scan_hex_dollar)
1904 token = G_TOKEN_HEX;
1905 ch = g_scanner_get_char (scanner, line_p, position_p);
1906 goto number_parsing;
1909 if (config->scan_octal)
1910 token = G_TOKEN_OCTAL;
1912 token = G_TOKEN_INT;
1913 ch = g_scanner_peek_next_char (scanner);
1914 if (config->scan_hex && (ch == 'x' || ch == 'X'))
1916 token = G_TOKEN_HEX;
1917 g_scanner_get_char (scanner, line_p, position_p);
1918 ch = g_scanner_get_char (scanner, line_p, position_p);
1921 token = G_TOKEN_ERROR;
1922 value.v_error = G_ERR_UNEXP_EOF;
1926 if (g_scanner_char_2_num (ch, 16) < 0)
1928 token = G_TOKEN_ERROR;
1929 value.v_error = G_ERR_DIGIT_RADIX;
1934 else if (config->scan_binary && (ch == 'b' || ch == 'B'))
1936 token = G_TOKEN_BINARY;
1937 g_scanner_get_char (scanner, line_p, position_p);
1938 ch = g_scanner_get_char (scanner, line_p, position_p);
1941 token = G_TOKEN_ERROR;
1942 value.v_error = G_ERR_UNEXP_EOF;
1946 if (g_scanner_char_2_num (ch, 10) < 0)
1948 token = G_TOKEN_ERROR;
1949 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1968 gboolean in_number = TRUE;
1971 if (token == G_TOKEN_NONE)
1972 token = G_TOKEN_INT;
1974 gstring = g_string_new (dotted_float ? "0." : "");
1975 gstring = g_string_append_c (gstring, ch);
1977 do /* while (in_number) */
1981 is_E = token == G_TOKEN_FLOAT && (ch == 'e' || ch == 'E');
1983 ch = g_scanner_peek_next_char (scanner);
1985 if (g_scanner_char_2_num (ch, 36) >= 0 ||
1986 (config->scan_float && ch == '.') ||
1987 (is_E && (ch == '+' || ch == '-')))
1989 ch = g_scanner_get_char (scanner, line_p, position_p);
1994 if (token != G_TOKEN_INT && token != G_TOKEN_OCTAL)
1996 value.v_error = token == G_TOKEN_FLOAT ? G_ERR_FLOAT_MALFORMED : G_ERR_FLOAT_RADIX;
1997 token = G_TOKEN_ERROR;
2002 token = G_TOKEN_FLOAT;
2003 gstring = g_string_append_c (gstring, ch);
2017 gstring = g_string_append_c (gstring, ch);
2022 if (token != G_TOKEN_FLOAT)
2024 token = G_TOKEN_ERROR;
2025 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2029 gstring = g_string_append_c (gstring, ch);
2034 if ((token != G_TOKEN_HEX && !config->scan_float) ||
2035 (token != G_TOKEN_HEX &&
2036 token != G_TOKEN_OCTAL &&
2037 token != G_TOKEN_FLOAT &&
2038 token != G_TOKEN_INT))
2040 token = G_TOKEN_ERROR;
2041 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2046 if (token != G_TOKEN_HEX)
2047 token = G_TOKEN_FLOAT;
2048 gstring = g_string_append_c (gstring, ch);
2053 if (token != G_TOKEN_HEX)
2055 token = G_TOKEN_ERROR;
2056 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2060 gstring = g_string_append_c (gstring, ch);
2070 if (token == G_TOKEN_FLOAT)
2071 value.v_float = g_strtod (gstring->str, &endptr);
2077 case G_TOKEN_BINARY:
2078 ui64 = g_ascii_strtoull (gstring->str, &endptr, 2);
2081 ui64 = g_ascii_strtoull (gstring->str, &endptr, 8);
2084 ui64 = g_ascii_strtoull (gstring->str, &endptr, 10);
2087 ui64 = g_ascii_strtoull (gstring->str, &endptr, 16);
2091 if (scanner->config->store_int64)
2092 value.v_int64 = ui64;
2096 if (endptr && *endptr)
2098 token = G_TOKEN_ERROR;
2099 if (*endptr == 'e' || *endptr == 'E')
2100 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2102 value.v_error = G_ERR_DIGIT_RADIX;
2104 g_string_free (gstring, TRUE);
2107 } /* number_parsing:... */
2113 if (config->cpair_comment_single &&
2114 ch == config->cpair_comment_single[0])
2116 token = G_TOKEN_COMMENT_SINGLE;
2117 in_comment_single = TRUE;
2118 gstring = g_string_new (NULL);
2119 ch = g_scanner_get_char (scanner, line_p, position_p);
2122 if (ch == config->cpair_comment_single[1])
2124 in_comment_single = FALSE;
2129 gstring = g_string_append_c (gstring, ch);
2130 ch = g_scanner_get_char (scanner, line_p, position_p);
2132 /* ignore a missing newline at EOF for single line comments */
2133 if (in_comment_single &&
2134 config->cpair_comment_single[1] == '\n')
2135 in_comment_single = FALSE;
2137 else if (config->scan_identifier && ch &&
2138 strchr (config->cset_identifier_first, ch))
2140 identifier_precedence:
2142 if (config->cset_identifier_nth && ch &&
2143 strchr (config->cset_identifier_nth,
2144 g_scanner_peek_next_char (scanner)))
2146 token = G_TOKEN_IDENTIFIER;
2147 gstring = g_string_new (NULL);
2148 gstring = g_string_append_c (gstring, ch);
2151 ch = g_scanner_get_char (scanner, line_p, position_p);
2152 gstring = g_string_append_c (gstring, ch);
2153 ch = g_scanner_peek_next_char (scanner);
2155 while (ch && strchr (config->cset_identifier_nth, ch));
2158 else if (config->scan_identifier_1char)
2160 token = G_TOKEN_IDENTIFIER;
2161 value.v_identifier = g_new0 (gchar, 2);
2162 value.v_identifier[0] = ch;
2168 if (config->char_2_token)
2172 token = G_TOKEN_CHAR;
2177 } /* default_case:... */
2180 g_assert (ch == 0 && token != G_TOKEN_NONE); /* paranoid */
2184 if (in_comment_multi || in_comment_single ||
2185 in_string_sq || in_string_dq)
2187 token = G_TOKEN_ERROR;
2190 g_string_free (gstring, TRUE);
2194 if (in_comment_multi || in_comment_single)
2195 value.v_error = G_ERR_UNEXP_EOF_IN_COMMENT;
2196 else /* (in_string_sq || in_string_dq) */
2197 value.v_error = G_ERR_UNEXP_EOF_IN_STRING;
2202 value.v_string = g_string_free (gstring, FALSE);
2206 if (token == G_TOKEN_IDENTIFIER)
2208 if (config->scan_symbols)
2213 scope_id = scanner->scope_id;
2214 key = g_scanner_lookup_internal (scanner, scope_id, value.v_identifier);
2215 if (!key && scope_id && scanner->config->scope_0_fallback)
2216 key = g_scanner_lookup_internal (scanner, 0, value.v_identifier);
2220 g_free (value.v_identifier);
2221 token = G_TOKEN_SYMBOL;
2222 value.v_symbol = key->value;
2226 if (token == G_TOKEN_IDENTIFIER &&
2227 config->scan_identifier_NULL &&
2228 strlen (value.v_identifier) == 4)
2230 gchar *null_upper = "NULL";
2231 gchar *null_lower = "null";
2233 if (scanner->config->case_sensitive)
2235 if (value.v_identifier[0] == null_upper[0] &&
2236 value.v_identifier[1] == null_upper[1] &&
2237 value.v_identifier[2] == null_upper[2] &&
2238 value.v_identifier[3] == null_upper[3])
2239 token = G_TOKEN_IDENTIFIER_NULL;
2243 if ((value.v_identifier[0] == null_upper[0] ||
2244 value.v_identifier[0] == null_lower[0]) &&
2245 (value.v_identifier[1] == null_upper[1] ||
2246 value.v_identifier[1] == null_lower[1]) &&
2247 (value.v_identifier[2] == null_upper[2] ||
2248 value.v_identifier[2] == null_lower[2]) &&
2249 (value.v_identifier[3] == null_upper[3] ||
2250 value.v_identifier[3] == null_lower[3]))
2251 token = G_TOKEN_IDENTIFIER_NULL;