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 * `token = ch` or as %G_TOKEN_CHAR (the default is %TRUE).
258 * @symbol_2_token: specifies if symbols are reported by setting
259 * `token = v_symbol` or as %G_TOKEN_SYMBOL (the default is %FALSE).
260 * @scope_0_fallback: specifies if a symbol is searched for in the
261 * default scope in addition to the current scope (the default is %FALSE).
262 * @store_int64: use value.v_int64 rather than v_int
264 * Specifies the #GScanner parser configuration. Most settings can
265 * be changed during the parsing phase and will affect the lexical
266 * parsing of the next unpeeked token.
269 /* --- defines --- */
270 #define to_lower(c) ( \
272 ( (((guchar)(c))>='A' && ((guchar)(c))<='Z') * ('a'-'A') ) | \
273 ( (((guchar)(c))>=192 && ((guchar)(c))<=214) * (224-192) ) | \
274 ( (((guchar)(c))>=216 && ((guchar)(c))<=222) * (248-216) ) | \
278 #define READ_BUFFER_SIZE (4000)
281 /* --- typedefs --- */
282 typedef struct _GScannerKey GScannerKey;
292 /* --- variables --- */
293 static const GScannerConfig g_scanner_config_template =
297 ) /* cset_skip_characters */,
302 ) /* cset_identifier_first */,
310 ) /* cset_identifier_nth */,
311 ( "#\n" ) /* cpair_comment_single */,
313 FALSE /* case_sensitive */,
315 TRUE /* skip_comment_multi */,
316 TRUE /* skip_comment_single */,
317 TRUE /* scan_comment_multi */,
318 TRUE /* scan_identifier */,
319 FALSE /* scan_identifier_1char */,
320 FALSE /* scan_identifier_NULL */,
321 TRUE /* scan_symbols */,
322 FALSE /* scan_binary */,
323 TRUE /* scan_octal */,
324 TRUE /* scan_float */,
326 FALSE /* scan_hex_dollar */,
327 TRUE /* scan_string_sq */,
328 TRUE /* scan_string_dq */,
329 TRUE /* numbers_2_int */,
330 FALSE /* int_2_float */,
331 FALSE /* identifier_2_string */,
332 TRUE /* char_2_token */,
333 FALSE /* symbol_2_token */,
334 FALSE /* scope_0_fallback */,
335 FALSE /* store_int64 */,
339 /* --- prototypes --- */
341 GScannerKey* g_scanner_lookup_internal (GScanner *scanner,
343 const gchar *symbol);
344 static gboolean g_scanner_key_equal (gconstpointer v1,
346 static guint g_scanner_key_hash (gconstpointer v);
347 static void g_scanner_get_token_ll (GScanner *scanner,
349 GTokenValue *value_p,
352 static void g_scanner_get_token_i (GScanner *scanner,
354 GTokenValue *value_p,
358 static guchar g_scanner_peek_next_char (GScanner *scanner);
359 static guchar g_scanner_get_char (GScanner *scanner,
362 static void g_scanner_msg_handler (GScanner *scanner,
367 /* --- functions --- */
369 g_scanner_char_2_num (guchar c,
372 if (c >= '0' && c <= '9')
374 else if (c >= 'A' && c <= 'Z')
376 else if (c >= 'a' && c <= 'z')
389 * @config_templ: the initial scanner settings
391 * Creates a new #GScanner.
393 * The @config_templ structure specifies the initial settings
394 * of the scanner, which are copied into the #GScanner
395 * @config field. If you pass %NULL then the default settings
398 * Returns: the new #GScanner
401 g_scanner_new (const GScannerConfig *config_templ)
406 config_templ = &g_scanner_config_template;
408 scanner = g_new0 (GScanner, 1);
410 scanner->user_data = NULL;
411 scanner->max_parse_errors = 1;
412 scanner->parse_errors = 0;
413 scanner->input_name = NULL;
414 g_datalist_init (&scanner->qdata);
416 scanner->config = g_new0 (GScannerConfig, 1);
418 scanner->config->case_sensitive = config_templ->case_sensitive;
419 scanner->config->cset_skip_characters = config_templ->cset_skip_characters;
420 if (!scanner->config->cset_skip_characters)
421 scanner->config->cset_skip_characters = "";
422 scanner->config->cset_identifier_first = config_templ->cset_identifier_first;
423 scanner->config->cset_identifier_nth = config_templ->cset_identifier_nth;
424 scanner->config->cpair_comment_single = config_templ->cpair_comment_single;
425 scanner->config->skip_comment_multi = config_templ->skip_comment_multi;
426 scanner->config->skip_comment_single = config_templ->skip_comment_single;
427 scanner->config->scan_comment_multi = config_templ->scan_comment_multi;
428 scanner->config->scan_identifier = config_templ->scan_identifier;
429 scanner->config->scan_identifier_1char = config_templ->scan_identifier_1char;
430 scanner->config->scan_identifier_NULL = config_templ->scan_identifier_NULL;
431 scanner->config->scan_symbols = config_templ->scan_symbols;
432 scanner->config->scan_binary = config_templ->scan_binary;
433 scanner->config->scan_octal = config_templ->scan_octal;
434 scanner->config->scan_float = config_templ->scan_float;
435 scanner->config->scan_hex = config_templ->scan_hex;
436 scanner->config->scan_hex_dollar = config_templ->scan_hex_dollar;
437 scanner->config->scan_string_sq = config_templ->scan_string_sq;
438 scanner->config->scan_string_dq = config_templ->scan_string_dq;
439 scanner->config->numbers_2_int = config_templ->numbers_2_int;
440 scanner->config->int_2_float = config_templ->int_2_float;
441 scanner->config->identifier_2_string = config_templ->identifier_2_string;
442 scanner->config->char_2_token = config_templ->char_2_token;
443 scanner->config->symbol_2_token = config_templ->symbol_2_token;
444 scanner->config->scope_0_fallback = config_templ->scope_0_fallback;
445 scanner->config->store_int64 = config_templ->store_int64;
447 scanner->token = G_TOKEN_NONE;
448 scanner->value.v_int64 = 0;
450 scanner->position = 0;
452 scanner->next_token = G_TOKEN_NONE;
453 scanner->next_value.v_int64 = 0;
454 scanner->next_line = 1;
455 scanner->next_position = 0;
457 scanner->symbol_table = g_hash_table_new (g_scanner_key_hash, g_scanner_key_equal);
458 scanner->input_fd = -1;
459 scanner->text = NULL;
460 scanner->text_end = NULL;
461 scanner->buffer = NULL;
462 scanner->scope_id = 0;
464 scanner->msg_handler = g_scanner_msg_handler;
470 g_scanner_free_value (GTokenType *token_p,
471 GTokenValue *value_p)
476 case G_TOKEN_IDENTIFIER:
477 case G_TOKEN_IDENTIFIER_NULL:
478 case G_TOKEN_COMMENT_SINGLE:
479 case G_TOKEN_COMMENT_MULTI:
480 g_free (value_p->v_string);
487 *token_p = G_TOKEN_NONE;
491 g_scanner_destroy_symbol_table_entry (gpointer _key,
495 GScannerKey *key = _key;
497 g_free (key->symbol);
503 * @scanner: a #GScanner
505 * Frees all memory used by the #GScanner.
508 g_scanner_destroy (GScanner *scanner)
510 g_return_if_fail (scanner != NULL);
512 g_datalist_clear (&scanner->qdata);
513 g_hash_table_foreach (scanner->symbol_table,
514 g_scanner_destroy_symbol_table_entry, NULL);
515 g_hash_table_destroy (scanner->symbol_table);
516 g_scanner_free_value (&scanner->token, &scanner->value);
517 g_scanner_free_value (&scanner->next_token, &scanner->next_value);
518 g_free (scanner->config);
519 g_free (scanner->buffer);
524 g_scanner_msg_handler (GScanner *scanner,
528 g_return_if_fail (scanner != NULL);
530 _g_fprintf (stderr, "%s:%d: ",
531 scanner->input_name ? scanner->input_name : "<memory>",
534 _g_fprintf (stderr, "error: ");
535 _g_fprintf (stderr, "%s\n", message);
540 * @scanner: a #GScanner
541 * @format: the message format. See the printf() documentation
542 * @...: the parameters to insert into the format string
544 * Outputs an error message, via the #GScanner message handler.
547 g_scanner_error (GScanner *scanner,
551 g_return_if_fail (scanner != NULL);
552 g_return_if_fail (format != NULL);
554 scanner->parse_errors++;
556 if (scanner->msg_handler)
561 va_start (args, format);
562 string = g_strdup_vprintf (format, args);
565 scanner->msg_handler (scanner, string, TRUE);
573 * @scanner: a #GScanner
574 * @format: the message format. See the printf() documentation
575 * @...: the parameters to insert into the format string
577 * Outputs a warning message, via the #GScanner message handler.
580 g_scanner_warn (GScanner *scanner,
584 g_return_if_fail (scanner != NULL);
585 g_return_if_fail (format != NULL);
587 if (scanner->msg_handler)
592 va_start (args, format);
593 string = g_strdup_vprintf (format, args);
596 scanner->msg_handler (scanner, string, FALSE);
603 g_scanner_key_equal (gconstpointer v1,
606 const GScannerKey *key1 = v1;
607 const GScannerKey *key2 = v2;
609 return (key1->scope_id == key2->scope_id) && (strcmp (key1->symbol, key2->symbol) == 0);
613 g_scanner_key_hash (gconstpointer v)
615 const GScannerKey *key = v;
620 for (c = key->symbol; *c; c++)
621 h = (h << 5) - h + *c;
626 static inline GScannerKey*
627 g_scanner_lookup_internal (GScanner *scanner,
634 key.scope_id = scope_id;
636 if (!scanner->config->case_sensitive)
641 key.symbol = g_new (gchar, strlen (symbol) + 1);
642 for (d = key.symbol, c = symbol; *c; c++, d++)
645 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
650 key.symbol = (gchar*) symbol;
651 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
658 * g_scanner_add_symbol:
659 * @scanner: a #GScanner
660 * @symbol: the symbol to add
661 * @value: the value of the symbol
663 * Adds a symbol to the default scope.
665 * Deprecated: 2.2: Use g_scanner_scope_add_symbol() instead.
669 * g_scanner_scope_add_symbol:
670 * @scanner: a #GScanner
671 * @scope_id: the scope id
672 * @symbol: the symbol to add
673 * @value: the value of the symbol
675 * Adds a symbol to the given scope.
678 g_scanner_scope_add_symbol (GScanner *scanner,
685 g_return_if_fail (scanner != NULL);
686 g_return_if_fail (symbol != NULL);
688 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
692 key = g_new (GScannerKey, 1);
693 key->scope_id = scope_id;
694 key->symbol = g_strdup (symbol);
696 if (!scanner->config->case_sensitive)
707 g_hash_table_insert (scanner->symbol_table, key, key);
714 * g_scanner_remove_symbol:
715 * @scanner: a #GScanner
716 * @symbol: the symbol to remove
718 * Removes a symbol from the default scope.
720 * Deprecated: 2.2: Use g_scanner_scope_remove_symbol() instead.
724 * g_scanner_scope_remove_symbol:
725 * @scanner: a #GScanner
726 * @scope_id: the scope id
727 * @symbol: the symbol to remove
729 * Removes a symbol from a scope.
732 g_scanner_scope_remove_symbol (GScanner *scanner,
738 g_return_if_fail (scanner != NULL);
739 g_return_if_fail (symbol != NULL);
741 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
745 g_hash_table_remove (scanner->symbol_table, key);
746 g_free (key->symbol);
752 * g_scanner_freeze_symbol_table:
753 * @scanner: a #GScanner
755 * There is no reason to use this macro, since it does nothing.
757 * Deprecated: 2.2: This macro does nothing.
761 * g_scanner_thaw_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_lookup_symbol:
771 * @scanner: a #GScanner
772 * @symbol: the symbol to look up
774 * Looks up a symbol in the current scope and return its value.
775 * If the symbol is not bound in the current scope, %NULL is
778 * Returns: the value of @symbol in the current scope, or %NULL
779 * if @symbol is not bound in the current scope
782 g_scanner_lookup_symbol (GScanner *scanner,
788 g_return_val_if_fail (scanner != NULL, NULL);
793 scope_id = scanner->scope_id;
794 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
795 if (!key && scope_id && scanner->config->scope_0_fallback)
796 key = g_scanner_lookup_internal (scanner, 0, symbol);
805 * g_scanner_scope_lookup_symbol:
806 * @scanner: a #GScanner
807 * @scope_id: the scope id
808 * @symbol: the symbol to look up
810 * Looks up a symbol in a scope and return its value. If the
811 * symbol is not bound in the scope, %NULL is returned.
813 * Returns: the value of @symbol in the given scope, or %NULL
814 * if @symbol is not bound in the given scope.
818 g_scanner_scope_lookup_symbol (GScanner *scanner,
824 g_return_val_if_fail (scanner != NULL, NULL);
829 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
838 * g_scanner_set_scope:
839 * @scanner: a #GScanner
840 * @scope_id: the new scope id
842 * Sets the current scope.
844 * Returns: the old scope id
847 g_scanner_set_scope (GScanner *scanner,
852 g_return_val_if_fail (scanner != NULL, 0);
854 old_scope_id = scanner->scope_id;
855 scanner->scope_id = scope_id;
861 g_scanner_foreach_internal (gpointer _key,
872 func = (GHFunc) d[0];
877 if (key->scope_id == *scope_id)
878 func (key->symbol, key->value, user_data);
882 * g_scanner_foreach_symbol:
883 * @scanner: a #GScanner
884 * @func: the function to call with each symbol
885 * @data: data to pass to the function
887 * Calls a function for each symbol in the default scope.
889 * Deprecated: 2.2: Use g_scanner_scope_foreach_symbol() instead.
893 * g_scanner_scope_foreach_symbol:
894 * @scanner: a #GScanner
895 * @scope_id: the scope id
896 * @func: the function to call for each symbol/value pair
897 * @user_data: user data to pass to the function
899 * Calls the given function for each of the symbol/value pairs
900 * in the given scope of the #GScanner. The function is passed
901 * the symbol and value of each pair, and the given @user_data
905 g_scanner_scope_foreach_symbol (GScanner *scanner,
912 g_return_if_fail (scanner != NULL);
914 d[0] = (gpointer) func;
918 g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
922 * g_scanner_peek_next_token:
923 * @scanner: a #GScanner
925 * Parses the next token, without removing it from the input stream.
926 * The token data is placed in the @next_token, @next_value, @next_line,
927 * and @next_position fields of the #GScanner structure.
929 * Note that, while the token is not removed from the input stream
930 * (i.e. the next call to g_scanner_get_next_token() will return the
931 * same token), it will not be reevaluated. This can lead to surprising
932 * results when changing scope or the scanner configuration after peeking
933 * the next token. Getting the next token after switching the scope or
934 * configuration will return whatever was peeked before, regardless of
935 * any symbols that may have been added or removed in the new scope.
937 * Returns: the type of the token
940 g_scanner_peek_next_token (GScanner *scanner)
942 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
944 if (scanner->next_token == G_TOKEN_NONE)
946 scanner->next_line = scanner->line;
947 scanner->next_position = scanner->position;
948 g_scanner_get_token_i (scanner,
949 &scanner->next_token,
950 &scanner->next_value,
952 &scanner->next_position);
955 return scanner->next_token;
959 * g_scanner_get_next_token:
960 * @scanner: a #GScanner
962 * Parses the next token just like g_scanner_peek_next_token()
963 * and also removes it from the input stream. The token data is
964 * placed in the @token, @value, @line, and @position fields of
965 * the #GScanner structure.
967 * Returns: the type of the token
970 g_scanner_get_next_token (GScanner *scanner)
972 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
974 if (scanner->next_token != G_TOKEN_NONE)
976 g_scanner_free_value (&scanner->token, &scanner->value);
978 scanner->token = scanner->next_token;
979 scanner->value = scanner->next_value;
980 scanner->line = scanner->next_line;
981 scanner->position = scanner->next_position;
982 scanner->next_token = G_TOKEN_NONE;
985 g_scanner_get_token_i (scanner,
991 return scanner->token;
995 * g_scanner_cur_token:
996 * @scanner: a #GScanner
998 * Gets the current token type. This is simply the @token
999 * field in the #GScanner structure.
1001 * Returns: the current token type
1004 g_scanner_cur_token (GScanner *scanner)
1006 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
1008 return scanner->token;
1012 * g_scanner_cur_value:
1013 * @scanner: a #GScanner
1015 * Gets the current token value. This is simply the @value
1016 * field in the #GScanner structure.
1018 * Returns: the current token value
1021 g_scanner_cur_value (GScanner *scanner)
1027 g_return_val_if_fail (scanner != NULL, v);
1029 /* MSC isn't capable of handling return scanner->value; ? */
1037 * g_scanner_cur_line:
1038 * @scanner: a #GScanner
1040 * Returns the current line in the input stream (counting
1041 * from 1). This is the line of the last token parsed via
1042 * g_scanner_get_next_token().
1044 * Returns: the current line
1047 g_scanner_cur_line (GScanner *scanner)
1049 g_return_val_if_fail (scanner != NULL, 0);
1051 return scanner->line;
1055 * g_scanner_cur_position:
1056 * @scanner: a #GScanner
1058 * Returns the current position in the current line (counting
1059 * from 0). This is the position of the last token parsed via
1060 * g_scanner_get_next_token().
1062 * Returns: the current position on the line
1065 g_scanner_cur_position (GScanner *scanner)
1067 g_return_val_if_fail (scanner != NULL, 0);
1069 return scanner->position;
1074 * @scanner: a #GScanner
1076 * Returns %TRUE if the scanner has reached the end of
1077 * the file or text buffer.
1079 * Returns: %TRUE if the scanner has reached the end of
1080 * the file or text buffer
1083 g_scanner_eof (GScanner *scanner)
1085 g_return_val_if_fail (scanner != NULL, TRUE);
1087 return scanner->token == G_TOKEN_EOF || scanner->token == G_TOKEN_ERROR;
1091 * g_scanner_input_file:
1092 * @scanner: a #GScanner
1093 * @input_fd: a file descriptor
1095 * Prepares to scan a file.
1098 g_scanner_input_file (GScanner *scanner,
1101 g_return_if_fail (scanner != NULL);
1102 g_return_if_fail (input_fd >= 0);
1104 if (scanner->input_fd >= 0)
1105 g_scanner_sync_file_offset (scanner);
1107 scanner->token = G_TOKEN_NONE;
1108 scanner->value.v_int64 = 0;
1110 scanner->position = 0;
1111 scanner->next_token = G_TOKEN_NONE;
1113 scanner->input_fd = input_fd;
1114 scanner->text = NULL;
1115 scanner->text_end = NULL;
1117 if (!scanner->buffer)
1118 scanner->buffer = g_new (gchar, READ_BUFFER_SIZE + 1);
1122 * g_scanner_input_text:
1123 * @scanner: a #GScanner
1124 * @text: the text buffer to scan
1125 * @text_len: the length of the text buffer
1127 * Prepares to scan a text buffer.
1130 g_scanner_input_text (GScanner *scanner,
1134 g_return_if_fail (scanner != NULL);
1136 g_return_if_fail (text != NULL);
1140 if (scanner->input_fd >= 0)
1141 g_scanner_sync_file_offset (scanner);
1143 scanner->token = G_TOKEN_NONE;
1144 scanner->value.v_int64 = 0;
1146 scanner->position = 0;
1147 scanner->next_token = G_TOKEN_NONE;
1149 scanner->input_fd = -1;
1150 scanner->text = text;
1151 scanner->text_end = text + text_len;
1153 if (scanner->buffer)
1155 g_free (scanner->buffer);
1156 scanner->buffer = NULL;
1161 g_scanner_peek_next_char (GScanner *scanner)
1163 if (scanner->text < scanner->text_end)
1165 return *scanner->text;
1167 else if (scanner->input_fd >= 0)
1172 buffer = scanner->buffer;
1175 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
1177 while (count == -1 && (errno == EINTR || errno == EAGAIN));
1181 scanner->input_fd = -1;
1187 scanner->text = buffer;
1188 scanner->text_end = buffer + count;
1198 * g_scanner_sync_file_offset:
1199 * @scanner: a #GScanner
1201 * Rewinds the filedescriptor to the current buffer position
1202 * and blows the file read ahead buffer. This is useful for
1203 * third party uses of the scanners filedescriptor, which hooks
1204 * onto the current scanning position.
1207 g_scanner_sync_file_offset (GScanner *scanner)
1209 g_return_if_fail (scanner != NULL);
1211 /* for file input, rewind the filedescriptor to the current
1212 * buffer position and blow the file read ahead buffer. useful
1213 * for third party uses of our file descriptor, which hooks
1214 * onto the current scanning position.
1217 if (scanner->input_fd >= 0 && scanner->text_end > scanner->text)
1221 buffered = scanner->text_end - scanner->text;
1222 if (lseek (scanner->input_fd, - buffered, SEEK_CUR) >= 0)
1224 /* we succeeded, blow our buffer's contents now */
1225 scanner->text = NULL;
1226 scanner->text_end = NULL;
1234 g_scanner_get_char (GScanner *scanner,
1240 if (scanner->text < scanner->text_end)
1241 fchar = *(scanner->text++);
1242 else if (scanner->input_fd >= 0)
1247 buffer = scanner->buffer;
1250 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
1252 while (count == -1 && (errno == EINTR || errno == EAGAIN));
1256 scanner->input_fd = -1;
1261 scanner->text = buffer + 1;
1262 scanner->text_end = buffer + count;
1266 g_scanner_sync_file_offset (scanner);
1267 scanner->text_end = scanner->text;
1268 scanner->input_fd = -1;
1289 * g_scanner_unexp_token:
1290 * @scanner: a #GScanner
1291 * @expected_token: the expected token
1292 * @identifier_spec: a string describing how the scanner's user
1293 * refers to identifiers (%NULL defaults to "identifier").
1294 * This is used if @expected_token is %G_TOKEN_IDENTIFIER or
1295 * %G_TOKEN_IDENTIFIER_NULL.
1296 * @symbol_spec: a string describing how the scanner's user refers
1297 * to symbols (%NULL defaults to "symbol"). This is used if
1298 * @expected_token is %G_TOKEN_SYMBOL or any token value greater
1299 * than %G_TOKEN_LAST.
1300 * @symbol_name: the name of the symbol, if the scanner's current
1301 * token is a symbol.
1302 * @message: a message string to output at the end of the
1303 * warning/error, or %NULL.
1304 * @is_error: if %TRUE it is output as an error. If %FALSE it is
1305 * output as a warning.
1307 * Outputs a message through the scanner's msg_handler,
1308 * resulting from an unexpected token in the input stream.
1309 * Note that you should not call g_scanner_peek_next_token()
1310 * followed by g_scanner_unexp_token() without an intermediate
1311 * call to g_scanner_get_next_token(), as g_scanner_unexp_token()
1312 * evaluates the scanner's current token (not the peeked token)
1313 * to construct part of the message.
1316 g_scanner_unexp_token (GScanner *scanner,
1317 GTokenType expected_token,
1318 const gchar *identifier_spec,
1319 const gchar *symbol_spec,
1320 const gchar *symbol_name,
1321 const gchar *message,
1324 gchar *token_string;
1325 guint token_string_len;
1326 gchar *expected_string;
1327 guint expected_string_len;
1328 gchar *message_prefix;
1329 gboolean print_unexp;
1330 void (*msg_handler) (GScanner*, const gchar*, ...);
1332 g_return_if_fail (scanner != NULL);
1335 msg_handler = g_scanner_error;
1337 msg_handler = g_scanner_warn;
1339 if (!identifier_spec)
1340 identifier_spec = "identifier";
1342 symbol_spec = "symbol";
1344 token_string_len = 56;
1345 token_string = g_new (gchar, token_string_len + 1);
1346 expected_string_len = 64;
1347 expected_string = g_new (gchar, expected_string_len + 1);
1350 switch (scanner->token)
1353 _g_snprintf (token_string, token_string_len, "end of file");
1357 if (scanner->token >= 1 && scanner->token <= 255)
1359 if ((scanner->token >= ' ' && scanner->token <= '~') ||
1360 strchr (scanner->config->cset_identifier_first, scanner->token) ||
1361 strchr (scanner->config->cset_identifier_nth, scanner->token))
1362 _g_snprintf (token_string, token_string_len, "character '%c'", scanner->token);
1364 _g_snprintf (token_string, token_string_len, "character '\\%o'", scanner->token);
1367 else if (!scanner->config->symbol_2_token)
1369 _g_snprintf (token_string, token_string_len, "(unknown) token <%d>", scanner->token);
1373 case G_TOKEN_SYMBOL:
1374 if (expected_token == G_TOKEN_SYMBOL ||
1375 (scanner->config->symbol_2_token &&
1376 expected_token > G_TOKEN_LAST))
1377 print_unexp = FALSE;
1379 _g_snprintf (token_string,
1382 print_unexp ? "" : "invalid ",
1386 _g_snprintf (token_string,
1389 print_unexp ? "" : "invalid ",
1394 print_unexp = FALSE;
1395 expected_token = G_TOKEN_NONE;
1396 switch (scanner->value.v_error)
1398 case G_ERR_UNEXP_EOF:
1399 _g_snprintf (token_string, token_string_len, "scanner: unexpected end of file");
1402 case G_ERR_UNEXP_EOF_IN_STRING:
1403 _g_snprintf (token_string, token_string_len, "scanner: unterminated string constant");
1406 case G_ERR_UNEXP_EOF_IN_COMMENT:
1407 _g_snprintf (token_string, token_string_len, "scanner: unterminated comment");
1410 case G_ERR_NON_DIGIT_IN_CONST:
1411 _g_snprintf (token_string, token_string_len, "scanner: non digit in constant");
1414 case G_ERR_FLOAT_RADIX:
1415 _g_snprintf (token_string, token_string_len, "scanner: invalid radix for floating constant");
1418 case G_ERR_FLOAT_MALFORMED:
1419 _g_snprintf (token_string, token_string_len, "scanner: malformed floating constant");
1422 case G_ERR_DIGIT_RADIX:
1423 _g_snprintf (token_string, token_string_len, "scanner: digit is beyond radix");
1428 _g_snprintf (token_string, token_string_len, "scanner: unknown error");
1434 _g_snprintf (token_string, token_string_len, "character '%c'", scanner->value.v_char);
1437 case G_TOKEN_IDENTIFIER:
1438 case G_TOKEN_IDENTIFIER_NULL:
1439 if (expected_token == G_TOKEN_IDENTIFIER ||
1440 expected_token == G_TOKEN_IDENTIFIER_NULL)
1441 print_unexp = FALSE;
1442 _g_snprintf (token_string,
1445 print_unexp ? "" : "invalid ",
1447 scanner->token == G_TOKEN_IDENTIFIER ? scanner->value.v_string : "null");
1450 case G_TOKEN_BINARY:
1454 if (scanner->config->store_int64)
1455 _g_snprintf (token_string, token_string_len, "number '%" G_GUINT64_FORMAT "'", scanner->value.v_int64);
1457 _g_snprintf (token_string, token_string_len, "number '%lu'", scanner->value.v_int);
1461 _g_snprintf (token_string, token_string_len, "number '%.3f'", scanner->value.v_float);
1464 case G_TOKEN_STRING:
1465 if (expected_token == G_TOKEN_STRING)
1466 print_unexp = FALSE;
1467 _g_snprintf (token_string,
1469 "%s%sstring constant \"%s\"",
1470 print_unexp ? "" : "invalid ",
1471 scanner->value.v_string[0] == 0 ? "empty " : "",
1472 scanner->value.v_string);
1473 token_string[token_string_len - 2] = '"';
1474 token_string[token_string_len - 1] = 0;
1477 case G_TOKEN_COMMENT_SINGLE:
1478 case G_TOKEN_COMMENT_MULTI:
1479 _g_snprintf (token_string, token_string_len, "comment");
1483 /* somehow the user's parsing code is screwed, there isn't much
1484 * we can do about it.
1485 * Note, a common case to trigger this is
1486 * g_scanner_peek_next_token(); g_scanner_unexp_token();
1487 * without an intermediate g_scanner_get_next_token().
1489 g_assert_not_reached ();
1494 switch (expected_token)
1496 gboolean need_valid;
1499 _g_snprintf (expected_string, expected_string_len, "end of file");
1502 if (expected_token >= 1 && expected_token <= 255)
1504 if ((expected_token >= ' ' && expected_token <= '~') ||
1505 strchr (scanner->config->cset_identifier_first, expected_token) ||
1506 strchr (scanner->config->cset_identifier_nth, expected_token))
1507 _g_snprintf (expected_string, expected_string_len, "character '%c'", expected_token);
1509 _g_snprintf (expected_string, expected_string_len, "character '\\%o'", expected_token);
1512 else if (!scanner->config->symbol_2_token)
1514 _g_snprintf (expected_string, expected_string_len, "(unknown) token <%d>", expected_token);
1518 case G_TOKEN_SYMBOL:
1519 need_valid = (scanner->token == G_TOKEN_SYMBOL ||
1520 (scanner->config->symbol_2_token &&
1521 scanner->token > G_TOKEN_LAST));
1522 _g_snprintf (expected_string,
1523 expected_string_len,
1525 need_valid ? "valid " : "",
1527 /* FIXME: should we attempt to lookup the symbol_name for symbol_2_token? */
1530 _g_snprintf (expected_string, expected_string_len, "%scharacter",
1531 scanner->token == G_TOKEN_CHAR ? "valid " : "");
1533 case G_TOKEN_BINARY:
1535 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1536 scanner->token == expected_token ? "valid " : "", tstring);
1540 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1541 scanner->token == expected_token ? "valid " : "", tstring);
1544 tstring = "integer";
1545 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1546 scanner->token == expected_token ? "valid " : "", tstring);
1549 tstring = "hexadecimal";
1550 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1551 scanner->token == expected_token ? "valid " : "", tstring);
1555 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1556 scanner->token == expected_token ? "valid " : "", tstring);
1558 case G_TOKEN_STRING:
1559 _g_snprintf (expected_string,
1560 expected_string_len,
1561 "%sstring constant",
1562 scanner->token == G_TOKEN_STRING ? "valid " : "");
1564 case G_TOKEN_IDENTIFIER:
1565 case G_TOKEN_IDENTIFIER_NULL:
1566 need_valid = (scanner->token == G_TOKEN_IDENTIFIER_NULL ||
1567 scanner->token == G_TOKEN_IDENTIFIER);
1568 _g_snprintf (expected_string,
1569 expected_string_len,
1571 need_valid ? "valid " : "",
1574 case G_TOKEN_COMMENT_SINGLE:
1575 tstring = "single-line";
1576 _g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1577 scanner->token == expected_token ? "valid " : "", tstring);
1579 case G_TOKEN_COMMENT_MULTI:
1580 tstring = "multi-line";
1581 _g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1582 scanner->token == expected_token ? "valid " : "", tstring);
1586 /* this is handled upon printout */
1590 if (message && message[0] != 0)
1591 message_prefix = " - ";
1594 message_prefix = "";
1597 if (expected_token == G_TOKEN_ERROR)
1599 msg_handler (scanner,
1600 "failure around %s%s%s",
1605 else if (expected_token == G_TOKEN_NONE)
1608 msg_handler (scanner,
1609 "unexpected %s%s%s",
1614 msg_handler (scanner,
1623 msg_handler (scanner,
1624 "unexpected %s, expected %s%s%s",
1630 msg_handler (scanner,
1631 "%s, expected %s%s%s",
1638 g_free (token_string);
1639 g_free (expected_string);
1643 g_scanner_get_token_i (GScanner *scanner,
1644 GTokenType *token_p,
1645 GTokenValue *value_p,
1651 g_scanner_free_value (token_p, value_p);
1652 g_scanner_get_token_ll (scanner, token_p, value_p, line_p, position_p);
1654 while (((*token_p > 0 && *token_p < 256) &&
1655 strchr (scanner->config->cset_skip_characters, *token_p)) ||
1656 (*token_p == G_TOKEN_CHAR &&
1657 strchr (scanner->config->cset_skip_characters, value_p->v_char)) ||
1658 (*token_p == G_TOKEN_COMMENT_MULTI &&
1659 scanner->config->skip_comment_multi) ||
1660 (*token_p == G_TOKEN_COMMENT_SINGLE &&
1661 scanner->config->skip_comment_single));
1665 case G_TOKEN_IDENTIFIER:
1666 if (scanner->config->identifier_2_string)
1667 *token_p = G_TOKEN_STRING;
1670 case G_TOKEN_SYMBOL:
1671 if (scanner->config->symbol_2_token)
1672 *token_p = (GTokenType) value_p->v_symbol;
1675 case G_TOKEN_BINARY:
1678 if (scanner->config->numbers_2_int)
1679 *token_p = G_TOKEN_INT;
1686 if (*token_p == G_TOKEN_INT &&
1687 scanner->config->int_2_float)
1689 *token_p = G_TOKEN_FLOAT;
1690 if (scanner->config->store_int64)
1693 /* work around error C2520, see gvaluetransform.c */
1694 value_p->v_float = (__int64)value_p->v_int64;
1696 value_p->v_float = value_p->v_int64;
1700 value_p->v_float = value_p->v_int;
1707 g_scanner_get_token_ll (GScanner *scanner,
1708 GTokenType *token_p,
1709 GTokenValue *value_p,
1713 GScannerConfig *config;
1715 gboolean in_comment_multi;
1716 gboolean in_comment_single;
1717 gboolean in_string_sq;
1718 gboolean in_string_dq;
1723 config = scanner->config;
1724 (*value_p).v_int64 = 0;
1726 if ((scanner->text >= scanner->text_end && scanner->input_fd < 0) ||
1727 scanner->token == G_TOKEN_EOF)
1729 *token_p = G_TOKEN_EOF;
1733 in_comment_multi = FALSE;
1734 in_comment_single = FALSE;
1735 in_string_sq = FALSE;
1736 in_string_dq = FALSE;
1739 do /* while (ch != 0) */
1741 gboolean dotted_float = FALSE;
1743 ch = g_scanner_get_char (scanner, line_p, position_p);
1746 token = G_TOKEN_NONE;
1748 /* this is *evil*, but needed ;(
1749 * we first check for identifier first character, because it
1750 * might interfere with other key chars like slashes or numbers
1752 if (config->scan_identifier &&
1753 ch && strchr (config->cset_identifier_first, ch))
1754 goto identifier_precedence;
1759 token = G_TOKEN_EOF;
1765 if (!config->scan_comment_multi ||
1766 g_scanner_peek_next_char (scanner) != '*')
1768 g_scanner_get_char (scanner, line_p, position_p);
1769 token = G_TOKEN_COMMENT_MULTI;
1770 in_comment_multi = TRUE;
1771 gstring = g_string_new (NULL);
1772 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1774 if (ch == '*' && g_scanner_peek_next_char (scanner) == '/')
1776 g_scanner_get_char (scanner, line_p, position_p);
1777 in_comment_multi = FALSE;
1781 gstring = g_string_append_c (gstring, ch);
1787 if (!config->scan_string_sq)
1789 token = G_TOKEN_STRING;
1790 in_string_sq = TRUE;
1791 gstring = g_string_new (NULL);
1792 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1796 in_string_sq = FALSE;
1800 gstring = g_string_append_c (gstring, ch);
1806 if (!config->scan_string_dq)
1808 token = G_TOKEN_STRING;
1809 in_string_dq = TRUE;
1810 gstring = g_string_new (NULL);
1811 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1815 in_string_dq = FALSE;
1822 ch = g_scanner_get_char (scanner, line_p, position_p);
1832 gstring = g_string_append_c (gstring, '\\');
1836 gstring = g_string_append_c (gstring, '\n');
1840 gstring = g_string_append_c (gstring, '\t');
1844 gstring = g_string_append_c (gstring, '\r');
1848 gstring = g_string_append_c (gstring, '\b');
1852 gstring = g_string_append_c (gstring, '\f');
1864 fchar = g_scanner_peek_next_char (scanner);
1865 if (fchar >= '0' && fchar <= '7')
1867 ch = g_scanner_get_char (scanner, line_p, position_p);
1868 i = i * 8 + ch - '0';
1869 fchar = g_scanner_peek_next_char (scanner);
1870 if (fchar >= '0' && fchar <= '7')
1872 ch = g_scanner_get_char (scanner, line_p, position_p);
1873 i = i * 8 + ch - '0';
1876 gstring = g_string_append_c (gstring, i);
1880 gstring = g_string_append_c (gstring, ch);
1885 gstring = g_string_append_c (gstring, ch);
1892 if (!config->scan_float)
1894 token = G_TOKEN_FLOAT;
1895 dotted_float = TRUE;
1896 ch = g_scanner_get_char (scanner, line_p, position_p);
1897 goto number_parsing;
1900 if (!config->scan_hex_dollar)
1902 token = G_TOKEN_HEX;
1903 ch = g_scanner_get_char (scanner, line_p, position_p);
1904 goto number_parsing;
1907 if (config->scan_octal)
1908 token = G_TOKEN_OCTAL;
1910 token = G_TOKEN_INT;
1911 ch = g_scanner_peek_next_char (scanner);
1912 if (config->scan_hex && (ch == 'x' || ch == 'X'))
1914 token = G_TOKEN_HEX;
1915 g_scanner_get_char (scanner, line_p, position_p);
1916 ch = g_scanner_get_char (scanner, line_p, position_p);
1919 token = G_TOKEN_ERROR;
1920 value.v_error = G_ERR_UNEXP_EOF;
1924 if (g_scanner_char_2_num (ch, 16) < 0)
1926 token = G_TOKEN_ERROR;
1927 value.v_error = G_ERR_DIGIT_RADIX;
1932 else if (config->scan_binary && (ch == 'b' || ch == 'B'))
1934 token = G_TOKEN_BINARY;
1935 g_scanner_get_char (scanner, line_p, position_p);
1936 ch = g_scanner_get_char (scanner, line_p, position_p);
1939 token = G_TOKEN_ERROR;
1940 value.v_error = G_ERR_UNEXP_EOF;
1944 if (g_scanner_char_2_num (ch, 10) < 0)
1946 token = G_TOKEN_ERROR;
1947 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1966 gboolean in_number = TRUE;
1969 if (token == G_TOKEN_NONE)
1970 token = G_TOKEN_INT;
1972 gstring = g_string_new (dotted_float ? "0." : "");
1973 gstring = g_string_append_c (gstring, ch);
1975 do /* while (in_number) */
1979 is_E = token == G_TOKEN_FLOAT && (ch == 'e' || ch == 'E');
1981 ch = g_scanner_peek_next_char (scanner);
1983 if (g_scanner_char_2_num (ch, 36) >= 0 ||
1984 (config->scan_float && ch == '.') ||
1985 (is_E && (ch == '+' || ch == '-')))
1987 ch = g_scanner_get_char (scanner, line_p, position_p);
1992 if (token != G_TOKEN_INT && token != G_TOKEN_OCTAL)
1994 value.v_error = token == G_TOKEN_FLOAT ? G_ERR_FLOAT_MALFORMED : G_ERR_FLOAT_RADIX;
1995 token = G_TOKEN_ERROR;
2000 token = G_TOKEN_FLOAT;
2001 gstring = g_string_append_c (gstring, ch);
2015 gstring = g_string_append_c (gstring, ch);
2020 if (token != G_TOKEN_FLOAT)
2022 token = G_TOKEN_ERROR;
2023 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2027 gstring = g_string_append_c (gstring, ch);
2032 if ((token != G_TOKEN_HEX && !config->scan_float) ||
2033 (token != G_TOKEN_HEX &&
2034 token != G_TOKEN_OCTAL &&
2035 token != G_TOKEN_FLOAT &&
2036 token != G_TOKEN_INT))
2038 token = G_TOKEN_ERROR;
2039 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2044 if (token != G_TOKEN_HEX)
2045 token = G_TOKEN_FLOAT;
2046 gstring = g_string_append_c (gstring, ch);
2051 if (token != G_TOKEN_HEX)
2053 token = G_TOKEN_ERROR;
2054 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2058 gstring = g_string_append_c (gstring, ch);
2068 if (token == G_TOKEN_FLOAT)
2069 value.v_float = g_strtod (gstring->str, &endptr);
2075 case G_TOKEN_BINARY:
2076 ui64 = g_ascii_strtoull (gstring->str, &endptr, 2);
2079 ui64 = g_ascii_strtoull (gstring->str, &endptr, 8);
2082 ui64 = g_ascii_strtoull (gstring->str, &endptr, 10);
2085 ui64 = g_ascii_strtoull (gstring->str, &endptr, 16);
2089 if (scanner->config->store_int64)
2090 value.v_int64 = ui64;
2094 if (endptr && *endptr)
2096 token = G_TOKEN_ERROR;
2097 if (*endptr == 'e' || *endptr == 'E')
2098 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2100 value.v_error = G_ERR_DIGIT_RADIX;
2102 g_string_free (gstring, TRUE);
2105 } /* number_parsing:... */
2111 if (config->cpair_comment_single &&
2112 ch == config->cpair_comment_single[0])
2114 token = G_TOKEN_COMMENT_SINGLE;
2115 in_comment_single = TRUE;
2116 gstring = g_string_new (NULL);
2117 ch = g_scanner_get_char (scanner, line_p, position_p);
2120 if (ch == config->cpair_comment_single[1])
2122 in_comment_single = FALSE;
2127 gstring = g_string_append_c (gstring, ch);
2128 ch = g_scanner_get_char (scanner, line_p, position_p);
2130 /* ignore a missing newline at EOF for single line comments */
2131 if (in_comment_single &&
2132 config->cpair_comment_single[1] == '\n')
2133 in_comment_single = FALSE;
2135 else if (config->scan_identifier && ch &&
2136 strchr (config->cset_identifier_first, ch))
2138 identifier_precedence:
2140 if (config->cset_identifier_nth && ch &&
2141 strchr (config->cset_identifier_nth,
2142 g_scanner_peek_next_char (scanner)))
2144 token = G_TOKEN_IDENTIFIER;
2145 gstring = g_string_new (NULL);
2146 gstring = g_string_append_c (gstring, ch);
2149 ch = g_scanner_get_char (scanner, line_p, position_p);
2150 gstring = g_string_append_c (gstring, ch);
2151 ch = g_scanner_peek_next_char (scanner);
2153 while (ch && strchr (config->cset_identifier_nth, ch));
2156 else if (config->scan_identifier_1char)
2158 token = G_TOKEN_IDENTIFIER;
2159 value.v_identifier = g_new0 (gchar, 2);
2160 value.v_identifier[0] = ch;
2166 if (config->char_2_token)
2170 token = G_TOKEN_CHAR;
2175 } /* default_case:... */
2178 g_assert (ch == 0 && token != G_TOKEN_NONE); /* paranoid */
2182 if (in_comment_multi || in_comment_single ||
2183 in_string_sq || in_string_dq)
2185 token = G_TOKEN_ERROR;
2188 g_string_free (gstring, TRUE);
2192 if (in_comment_multi || in_comment_single)
2193 value.v_error = G_ERR_UNEXP_EOF_IN_COMMENT;
2194 else /* (in_string_sq || in_string_dq) */
2195 value.v_error = G_ERR_UNEXP_EOF_IN_STRING;
2200 value.v_string = g_string_free (gstring, FALSE);
2204 if (token == G_TOKEN_IDENTIFIER)
2206 if (config->scan_symbols)
2211 scope_id = scanner->scope_id;
2212 key = g_scanner_lookup_internal (scanner, scope_id, value.v_identifier);
2213 if (!key && scope_id && scanner->config->scope_0_fallback)
2214 key = g_scanner_lookup_internal (scanner, 0, value.v_identifier);
2218 g_free (value.v_identifier);
2219 token = G_TOKEN_SYMBOL;
2220 value.v_symbol = key->value;
2224 if (token == G_TOKEN_IDENTIFIER &&
2225 config->scan_identifier_NULL &&
2226 strlen (value.v_identifier) == 4)
2228 gchar *null_upper = "NULL";
2229 gchar *null_lower = "null";
2231 if (scanner->config->case_sensitive)
2233 if (value.v_identifier[0] == null_upper[0] &&
2234 value.v_identifier[1] == null_upper[1] &&
2235 value.v_identifier[2] == null_upper[2] &&
2236 value.v_identifier[3] == null_upper[3])
2237 token = G_TOKEN_IDENTIFIER_NULL;
2241 if ((value.v_identifier[0] == null_upper[0] ||
2242 value.v_identifier[0] == null_lower[0]) &&
2243 (value.v_identifier[1] == null_upper[1] ||
2244 value.v_identifier[1] == null_lower[1]) &&
2245 (value.v_identifier[2] == null_upper[2] ||
2246 value.v_identifier[2] == null_lower[2]) &&
2247 (value.v_identifier[3] == null_upper[3] ||
2248 value.v_identifier[3] == null_lower[3]))
2249 token = G_TOKEN_IDENTIFIER_NULL;