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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
47 #include "gprintfint.h"
48 #include "gstrfuncs.h"
50 #include "gtestutils.h"
53 #include <io.h> /* For _read() */
59 * @title: Lexical Scanner
60 * @short_description: a general purpose lexical scanner
62 * The #GScanner and its associated functions provide a
63 * general purpose lexical scanner.
68 * @scanner: a #GScanner
69 * @message: the message
70 * @error: %TRUE if the message signals an error,
71 * %FALSE if it signals a warning.
73 * Specifies the type of the message handler function.
79 * The set of lowercase ASCII alphabet characters.
80 * Used for specifying valid identifier characters
87 * The set of uppercase ASCII alphabet characters.
88 * Used for specifying valid identifier characters
95 * The set of uppercase ISO 8859-1 alphabet characters
96 * which are not ASCII characters.
97 * Used for specifying valid identifier characters
104 * The set of lowercase ISO 8859-1 alphabet characters
105 * which are not ASCII characters.
106 * Used for specifying valid identifier characters
107 * in #GScannerConfig.
112 * @G_TOKEN_EOF: the end of the file
113 * @G_TOKEN_LEFT_PAREN: a '(' character
114 * @G_TOKEN_LEFT_CURLY: a '{' character
115 * @G_TOKEN_RIGHT_CURLY: a '}' character
116 * @G_TOKEN_RIGHT_PAREN: a ')' character
117 * @G_TOKEN_ERROR: an error occurred
118 * @G_TOKEN_CHAR: a character
119 * @G_TOKEN_BINARY: a binary integer
120 * @G_TOKEN_OCTAL: an octal integer
121 * @G_TOKEN_INT: an integer
122 * @G_TOKEN_HEX: a hex integer
123 * @G_TOKEN_FLOAT: a floating point number
124 * @G_TOKEN_STRING: a string
125 * @G_TOKEN_SYMBOL: a symbol
126 * @G_TOKEN_IDENTIFIER: an identifier
127 * @G_TOKEN_IDENTIFIER_NULL: a null identifier
128 * @G_TOKEN_COMMENT_SINGLE: one line comment
129 * @G_TOKEN_COMMENT_MULTI: multi line comment
131 * The possible types of token returned from each
132 * g_scanner_get_next_token() call.
137 * @v_symbol: token symbol value
138 * @v_identifier: token identifier value
139 * @v_binary: token binary integer value
140 * @v_octal: octal integer value
141 * @v_int: integer value
142 * @v_int64: 64-bit integer value
143 * @v_float: floating point value
144 * @v_hex: hex integer value
145 * @v_string: string value
146 * @v_comment: comment value
147 * @v_char: character value
148 * @v_error: error value
150 * A union holding the value of the token.
155 * @G_ERR_UNKNOWN: unknown error
156 * @G_ERR_UNEXP_EOF: unexpected end of file
157 * @G_ERR_UNEXP_EOF_IN_STRING: unterminated string constant
158 * @G_ERR_UNEXP_EOF_IN_COMMENT: unterminated comment
159 * @G_ERR_NON_DIGIT_IN_CONST: non-digit character in a number
160 * @G_ERR_DIGIT_RADIX: digit beyond radix in a number
161 * @G_ERR_FLOAT_RADIX: non-decimal floating point number
162 * @G_ERR_FLOAT_MALFORMED: malformed floating point number
164 * The possible errors, used in the @v_error field
165 * of #GTokenValue, when the token is a %G_TOKEN_ERROR.
171 * @max_parse_errors: unused
172 * @parse_errors: g_scanner_error() increments this field
173 * @input_name: name of input stream, featured by the default message handler
174 * @qdata: quarked data
175 * @config: link into the scanner configuration
176 * @token: token parsed by the last g_scanner_get_next_token()
177 * @value: value of the last token from g_scanner_get_next_token()
178 * @line: line number of the last token from g_scanner_get_next_token()
179 * @position: char number of the last token from g_scanner_get_next_token()
180 * @next_token: token parsed by the last g_scanner_peek_next_token()
181 * @next_value: value of the last token from g_scanner_peek_next_token()
182 * @next_line: line number of the last token from g_scanner_peek_next_token()
183 * @next_position: char number of the last token from g_scanner_peek_next_token()
184 * @msg_handler: handler function for _warn and _error
186 * The data structure representing a lexical scanner.
188 * You should set @input_name after creating the scanner, since
189 * it is used by the default message handler when displaying
190 * warnings and errors. If you are scanning a file, the filename
191 * would be a good choice.
193 * The @user_data and @max_parse_errors fields are not used.
194 * If you need to associate extra data with the scanner you
195 * can place them here.
197 * If you want to use your own message handler you can set the
198 * @msg_handler field. The type of the message handler function
199 * is declared by #GScannerMsgFunc.
204 * @cset_skip_characters: specifies which characters should be skipped
205 * by the scanner (the default is the whitespace characters: space,
206 * tab, carriage-return and line-feed).
207 * @cset_identifier_first: specifies the characters which can start
208 * identifiers (the default is #G_CSET_a_2_z, "_", and #G_CSET_A_2_Z).
209 * @cset_identifier_nth: specifies the characters which can be used
210 * in identifiers, after the first character (the default is
211 * #G_CSET_a_2_z, "_0123456789", #G_CSET_A_2_Z, #G_CSET_LATINS,
213 * @cpair_comment_single: specifies the characters at the start and
214 * end of single-line comments. The default is "#\n" which means
215 * that single-line comments start with a '#' and continue until
216 * a '\n' (end of line).
217 * @case_sensitive: specifies if symbols are case sensitive (the
218 * default is %FALSE).
219 * @skip_comment_multi: specifies if multi-line comments are skipped
220 * and not returned as tokens (the default is %TRUE).
221 * @skip_comment_single: specifies if single-line comments are skipped
222 * and not returned as tokens (the default is %TRUE).
223 * @scan_comment_multi: specifies if multi-line comments are recognized
224 * (the default is %TRUE).
225 * @scan_identifier: specifies if identifiers are recognized (the
227 * @scan_identifier_1char: specifies if single-character
228 * identifiers are recognized (the default is %FALSE).
229 * @scan_identifier_NULL: specifies if %NULL is reported as
230 * %G_TOKEN_IDENTIFIER_NULL (the default is %FALSE).
231 * @scan_symbols: specifies if symbols are recognized (the default
233 * @scan_binary: specifies if binary numbers are recognized (the
234 * default is %FALSE).
235 * @scan_octal: specifies if octal numbers are recognized (the
237 * @scan_float: specifies if floating point numbers are recognized
238 * (the default is %TRUE).
239 * @scan_hex: specifies if hexadecimal numbers are recognized (the
241 * @scan_hex_dollar: specifies if '$' is recognized as a prefix for
242 * hexadecimal numbers (the default is %FALSE).
243 * @scan_string_sq: specifies if strings can be enclosed in single
244 * quotes (the default is %TRUE).
245 * @scan_string_dq: specifies if strings can be enclosed in double
246 * quotes (the default is %TRUE).
247 * @numbers_2_int: specifies if binary, octal and hexadecimal numbers
248 * are reported as #G_TOKEN_INT (the default is %TRUE).
249 * @int_2_float: specifies if all numbers are reported as %G_TOKEN_FLOAT
250 * (the default is %FALSE).
251 * @identifier_2_string: specifies if identifiers are reported as strings
252 * (the default is %FALSE).
253 * @char_2_token: specifies if characters are reported by setting
254 * <literal>token = ch</literal> or as %G_TOKEN_CHAR (the default
256 * @symbol_2_token: specifies if symbols are reported by setting
257 * <literal>token = v_symbol</literal> or as %G_TOKEN_SYMBOL (the
258 * default is %FALSE).
259 * @scope_0_fallback: specifies if a symbol is searched for in the
260 * default scope in addition to the current scope (the default is %FALSE).
261 * @store_int64: use value.v_int64 rather than v_int
263 * Specifies the #GScanner parser configuration. Most settings can
264 * be changed during the parsing phase and will affect the lexical
265 * parsing of the next unpeeked token.
268 /* --- defines --- */
269 #define to_lower(c) ( \
271 ( (((guchar)(c))>='A' && ((guchar)(c))<='Z') * ('a'-'A') ) | \
272 ( (((guchar)(c))>=192 && ((guchar)(c))<=214) * (224-192) ) | \
273 ( (((guchar)(c))>=216 && ((guchar)(c))<=222) * (248-216) ) | \
277 #define READ_BUFFER_SIZE (4000)
280 /* --- typedefs --- */
281 typedef struct _GScannerKey GScannerKey;
291 /* --- variables --- */
292 static const GScannerConfig g_scanner_config_template =
296 ) /* cset_skip_characters */,
301 ) /* cset_identifier_first */,
309 ) /* cset_identifier_nth */,
310 ( "#\n" ) /* cpair_comment_single */,
312 FALSE /* case_sensitive */,
314 TRUE /* skip_comment_multi */,
315 TRUE /* skip_comment_single */,
316 TRUE /* scan_comment_multi */,
317 TRUE /* scan_identifier */,
318 FALSE /* scan_identifier_1char */,
319 FALSE /* scan_identifier_NULL */,
320 TRUE /* scan_symbols */,
321 FALSE /* scan_binary */,
322 TRUE /* scan_octal */,
323 TRUE /* scan_float */,
325 FALSE /* scan_hex_dollar */,
326 TRUE /* scan_string_sq */,
327 TRUE /* scan_string_dq */,
328 TRUE /* numbers_2_int */,
329 FALSE /* int_2_float */,
330 FALSE /* identifier_2_string */,
331 TRUE /* char_2_token */,
332 FALSE /* symbol_2_token */,
333 FALSE /* scope_0_fallback */,
334 FALSE /* store_int64 */,
338 /* --- prototypes --- */
340 GScannerKey* g_scanner_lookup_internal (GScanner *scanner,
342 const gchar *symbol);
343 static gboolean g_scanner_key_equal (gconstpointer v1,
345 static guint g_scanner_key_hash (gconstpointer v);
346 static void g_scanner_get_token_ll (GScanner *scanner,
348 GTokenValue *value_p,
351 static void g_scanner_get_token_i (GScanner *scanner,
353 GTokenValue *value_p,
357 static guchar g_scanner_peek_next_char (GScanner *scanner);
358 static guchar g_scanner_get_char (GScanner *scanner,
361 static void g_scanner_msg_handler (GScanner *scanner,
366 /* --- functions --- */
368 g_scanner_char_2_num (guchar c,
371 if (c >= '0' && c <= '9')
373 else if (c >= 'A' && c <= 'Z')
375 else if (c >= 'a' && c <= 'z')
388 * @config_templ: the initial scanner settings
390 * Creates a new #GScanner.
392 * The @config_templ structure specifies the initial settings
393 * of the scanner, which are copied into the #GScanner
394 * @config field. If you pass %NULL then the default settings
397 * Returns: the new #GScanner
400 g_scanner_new (const GScannerConfig *config_templ)
405 config_templ = &g_scanner_config_template;
407 scanner = g_new0 (GScanner, 1);
409 scanner->user_data = NULL;
410 scanner->max_parse_errors = 1;
411 scanner->parse_errors = 0;
412 scanner->input_name = NULL;
413 g_datalist_init (&scanner->qdata);
415 scanner->config = g_new0 (GScannerConfig, 1);
417 scanner->config->case_sensitive = config_templ->case_sensitive;
418 scanner->config->cset_skip_characters = config_templ->cset_skip_characters;
419 if (!scanner->config->cset_skip_characters)
420 scanner->config->cset_skip_characters = "";
421 scanner->config->cset_identifier_first = config_templ->cset_identifier_first;
422 scanner->config->cset_identifier_nth = config_templ->cset_identifier_nth;
423 scanner->config->cpair_comment_single = config_templ->cpair_comment_single;
424 scanner->config->skip_comment_multi = config_templ->skip_comment_multi;
425 scanner->config->skip_comment_single = config_templ->skip_comment_single;
426 scanner->config->scan_comment_multi = config_templ->scan_comment_multi;
427 scanner->config->scan_identifier = config_templ->scan_identifier;
428 scanner->config->scan_identifier_1char = config_templ->scan_identifier_1char;
429 scanner->config->scan_identifier_NULL = config_templ->scan_identifier_NULL;
430 scanner->config->scan_symbols = config_templ->scan_symbols;
431 scanner->config->scan_binary = config_templ->scan_binary;
432 scanner->config->scan_octal = config_templ->scan_octal;
433 scanner->config->scan_float = config_templ->scan_float;
434 scanner->config->scan_hex = config_templ->scan_hex;
435 scanner->config->scan_hex_dollar = config_templ->scan_hex_dollar;
436 scanner->config->scan_string_sq = config_templ->scan_string_sq;
437 scanner->config->scan_string_dq = config_templ->scan_string_dq;
438 scanner->config->numbers_2_int = config_templ->numbers_2_int;
439 scanner->config->int_2_float = config_templ->int_2_float;
440 scanner->config->identifier_2_string = config_templ->identifier_2_string;
441 scanner->config->char_2_token = config_templ->char_2_token;
442 scanner->config->symbol_2_token = config_templ->symbol_2_token;
443 scanner->config->scope_0_fallback = config_templ->scope_0_fallback;
444 scanner->config->store_int64 = config_templ->store_int64;
446 scanner->token = G_TOKEN_NONE;
447 scanner->value.v_int64 = 0;
449 scanner->position = 0;
451 scanner->next_token = G_TOKEN_NONE;
452 scanner->next_value.v_int64 = 0;
453 scanner->next_line = 1;
454 scanner->next_position = 0;
456 scanner->symbol_table = g_hash_table_new (g_scanner_key_hash, g_scanner_key_equal);
457 scanner->input_fd = -1;
458 scanner->text = NULL;
459 scanner->text_end = NULL;
460 scanner->buffer = NULL;
461 scanner->scope_id = 0;
463 scanner->msg_handler = g_scanner_msg_handler;
469 g_scanner_free_value (GTokenType *token_p,
470 GTokenValue *value_p)
475 case G_TOKEN_IDENTIFIER:
476 case G_TOKEN_IDENTIFIER_NULL:
477 case G_TOKEN_COMMENT_SINGLE:
478 case G_TOKEN_COMMENT_MULTI:
479 g_free (value_p->v_string);
486 *token_p = G_TOKEN_NONE;
490 g_scanner_destroy_symbol_table_entry (gpointer _key,
494 GScannerKey *key = _key;
496 g_free (key->symbol);
502 * @scanner: a #GScanner
504 * Frees all memory used by the #GScanner.
507 g_scanner_destroy (GScanner *scanner)
509 g_return_if_fail (scanner != NULL);
511 g_datalist_clear (&scanner->qdata);
512 g_hash_table_foreach (scanner->symbol_table,
513 g_scanner_destroy_symbol_table_entry, NULL);
514 g_hash_table_destroy (scanner->symbol_table);
515 g_scanner_free_value (&scanner->token, &scanner->value);
516 g_scanner_free_value (&scanner->next_token, &scanner->next_value);
517 g_free (scanner->config);
518 g_free (scanner->buffer);
523 g_scanner_msg_handler (GScanner *scanner,
527 g_return_if_fail (scanner != NULL);
529 _g_fprintf (stderr, "%s:%d: ",
530 scanner->input_name ? scanner->input_name : "<memory>",
533 _g_fprintf (stderr, "error: ");
534 _g_fprintf (stderr, "%s\n", message);
539 * @scanner: a #GScanner
540 * @format: the message format. See the printf() documentation
541 * @...: the parameters to insert into the format string
543 * Outputs an error message, via the #GScanner message handler.
546 g_scanner_error (GScanner *scanner,
550 g_return_if_fail (scanner != NULL);
551 g_return_if_fail (format != NULL);
553 scanner->parse_errors++;
555 if (scanner->msg_handler)
560 va_start (args, format);
561 string = g_strdup_vprintf (format, args);
564 scanner->msg_handler (scanner, string, TRUE);
572 * @scanner: a #GScanner
573 * @format: the message format. See the printf() documentation
574 * @...: the parameters to insert into the format string
576 * Outputs a warning message, via the #GScanner message handler.
579 g_scanner_warn (GScanner *scanner,
583 g_return_if_fail (scanner != NULL);
584 g_return_if_fail (format != NULL);
586 if (scanner->msg_handler)
591 va_start (args, format);
592 string = g_strdup_vprintf (format, args);
595 scanner->msg_handler (scanner, string, FALSE);
602 g_scanner_key_equal (gconstpointer v1,
605 const GScannerKey *key1 = v1;
606 const GScannerKey *key2 = v2;
608 return (key1->scope_id == key2->scope_id) && (strcmp (key1->symbol, key2->symbol) == 0);
612 g_scanner_key_hash (gconstpointer v)
614 const GScannerKey *key = v;
619 for (c = key->symbol; *c; c++)
620 h = (h << 5) - h + *c;
625 static inline GScannerKey*
626 g_scanner_lookup_internal (GScanner *scanner,
633 key.scope_id = scope_id;
635 if (!scanner->config->case_sensitive)
640 key.symbol = g_new (gchar, strlen (symbol) + 1);
641 for (d = key.symbol, c = symbol; *c; c++, d++)
644 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
649 key.symbol = (gchar*) symbol;
650 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
657 * g_scanner_add_symbol
658 * @scanner: a #GScanner
659 * @symbol: the symbol to add
660 * @value: the value of the symbol
662 * Adds a symbol to the default scope.
664 * Deprecated: 2.2: Use g_scanner_scope_add_symbol() instead.
668 * g_scanner_scope_add_symbol:
669 * @scanner: a #GScanner
670 * @scope_id: the scope id
671 * @symbol: the symbol to add
672 * @value: the value of the symbol
674 * Adds a symbol to the given scope.
677 g_scanner_scope_add_symbol (GScanner *scanner,
684 g_return_if_fail (scanner != NULL);
685 g_return_if_fail (symbol != NULL);
687 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
691 key = g_new (GScannerKey, 1);
692 key->scope_id = scope_id;
693 key->symbol = g_strdup (symbol);
695 if (!scanner->config->case_sensitive)
706 g_hash_table_insert (scanner->symbol_table, key, key);
713 * g_scanner_remove_symbol:
714 * @scanner: a #GScanner
715 * @symbol: the symbol to remove
717 * Removes a symbol from the default scope.
719 * Deprecated: 2.2: Use g_scanner_scope_remove_symbol() instead.
723 * g_scanner_scope_remove_symbol:
724 * @scanner: a #GScanner
725 * @scope_id: the scope id
726 * @symbol: the symbol to remove
728 * Removes a symbol from a scope.
731 g_scanner_scope_remove_symbol (GScanner *scanner,
737 g_return_if_fail (scanner != NULL);
738 g_return_if_fail (symbol != NULL);
740 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
744 g_hash_table_remove (scanner->symbol_table, key);
745 g_free (key->symbol);
751 * g_scanner_freeze_symbol_table:
752 * @scanner: a #GScanner
754 * There is no reason to use this macro, since it does nothing.
756 * Deprecated: 2.2: This macro does nothing.
760 * g_scanner_thaw_symbol_table:
761 * @scanner: a #GScanner
763 * There is no reason to use this macro, since it does nothing.
765 * Deprecated: 2.2: This macro does nothing.
769 * g_scanner_lookup_symbol:
770 * @scanner: a #GScanner
771 * @symbol: the symbol to look up
773 * Looks up a symbol in the current scope and return its value.
774 * If the symbol is not bound in the current scope, %NULL is
777 * Returns: the value of @symbol in the current scope, or %NULL
778 * if @symbol is not bound in the current scope
781 g_scanner_lookup_symbol (GScanner *scanner,
787 g_return_val_if_fail (scanner != NULL, NULL);
792 scope_id = scanner->scope_id;
793 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
794 if (!key && scope_id && scanner->config->scope_0_fallback)
795 key = g_scanner_lookup_internal (scanner, 0, symbol);
804 * g_scanner_scope_lookup_symbol:
805 * @scanner: a #GScanner
806 * @scope_id: the scope id
807 * @symbol: the symbol to look up
809 * Looks up a symbol in a scope and return its value. If the
810 * symbol is not bound in the scope, %NULL is returned.
812 * Returns: the value of @symbol in the given scope, or %NULL
813 * if @symbol is not bound in the given scope.
817 g_scanner_scope_lookup_symbol (GScanner *scanner,
823 g_return_val_if_fail (scanner != NULL, NULL);
828 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
837 * g_scanner_set_scope:
838 * @scanner: a #GScanner
839 * @scope_id: the new scope id
841 * Sets the current scope.
843 * Returns: the old scope id
846 g_scanner_set_scope (GScanner *scanner,
851 g_return_val_if_fail (scanner != NULL, 0);
853 old_scope_id = scanner->scope_id;
854 scanner->scope_id = scope_id;
860 g_scanner_foreach_internal (gpointer _key,
871 func = (GHFunc) d[0];
876 if (key->scope_id == *scope_id)
877 func (key->symbol, key->value, user_data);
881 * g_scanner_foreach_symbol:
882 * @scanner: a #GScanner
883 * @func: the function to call with each symbol
884 * @data: data to pass to the function
886 * Calls a function for each symbol in the default scope.
888 * Deprecated: 2.2: Use g_scanner_scope_foreach_symbol() instead.
892 * g_scanner_scope_foreach_symbol:
893 * @scanner: a #GScanner
894 * @scope_id: the scope id
895 * @func: the function to call for each symbol/value pair
896 * @user_data: user data to pass to the function
898 * Calls the given function for each of the symbol/value pairs
899 * in the given scope of the #GScanner. The function is passed
900 * the symbol and value of each pair, and the given @user_data
904 g_scanner_scope_foreach_symbol (GScanner *scanner,
911 g_return_if_fail (scanner != NULL);
913 d[0] = (gpointer) func;
917 g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
921 * g_scanner_peek_next_token:
922 * @scanner: a #GScanner
924 * Parses the next token, without removing it from the input stream.
925 * The token data is placed in the @next_token, @next_value, @next_line,
926 * and @next_position fields of the #GScanner structure.
928 * Note that, while the token is not removed from the input stream
929 * (i.e. the next call to g_scanner_get_next_token() will return the
930 * same token), it will not be reevaluated. This can lead to surprising
931 * results when changing scope or the scanner configuration after peeking
932 * the next token. Getting the next token after switching the scope or
933 * configuration will return whatever was peeked before, regardless of
934 * any symbols that may have been added or removed in the new scope.
936 * Returns: the type of the token
939 g_scanner_peek_next_token (GScanner *scanner)
941 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
943 if (scanner->next_token == G_TOKEN_NONE)
945 scanner->next_line = scanner->line;
946 scanner->next_position = scanner->position;
947 g_scanner_get_token_i (scanner,
948 &scanner->next_token,
949 &scanner->next_value,
951 &scanner->next_position);
954 return scanner->next_token;
958 * g_scanner_get_next_token:
959 * @scanner: a #GScanner
961 * Parses the next token just like g_scanner_peek_next_token()
962 * and also removes it from the input stream. The token data is
963 * placed in the @token, @value, @line, and @position fields of
964 * the #GScanner structure.
966 * Returns: the type of the token
969 g_scanner_get_next_token (GScanner *scanner)
971 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
973 if (scanner->next_token != G_TOKEN_NONE)
975 g_scanner_free_value (&scanner->token, &scanner->value);
977 scanner->token = scanner->next_token;
978 scanner->value = scanner->next_value;
979 scanner->line = scanner->next_line;
980 scanner->position = scanner->next_position;
981 scanner->next_token = G_TOKEN_NONE;
984 g_scanner_get_token_i (scanner,
990 return scanner->token;
994 * g_scanner_cur_token:
995 * @scanner: a #GScanner
997 * Gets the current token type. This is simply the @token
998 * field in the #GScanner structure.
1000 * Returns: the current token type
1003 g_scanner_cur_token (GScanner *scanner)
1005 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
1007 return scanner->token;
1011 * g_scanner_cur_value:
1012 * @scanner: a #GScanner
1014 * Gets the current token value. This is simply the @value
1015 * field in the #GScanner structure.
1017 * Returns: the current token value
1020 g_scanner_cur_value (GScanner *scanner)
1026 g_return_val_if_fail (scanner != NULL, v);
1028 /* MSC isn't capable of handling return scanner->value; ? */
1036 * g_scanner_cur_line:
1037 * @scanner: a #GScanner
1039 * Returns the current line in the input stream (counting
1040 * from 1). This is the line of the last token parsed via
1041 * g_scanner_get_next_token().
1043 * Returns: the current line
1046 g_scanner_cur_line (GScanner *scanner)
1048 g_return_val_if_fail (scanner != NULL, 0);
1050 return scanner->line;
1054 * g_scanner_cur_position:
1055 * @scanner: a #GScanner
1057 * Returns the current position in the current line (counting
1058 * from 0). This is the position of the last token parsed via
1059 * g_scanner_get_next_token().
1061 * Returns: the current position on the line
1064 g_scanner_cur_position (GScanner *scanner)
1066 g_return_val_if_fail (scanner != NULL, 0);
1068 return scanner->position;
1073 * @scanner: a #GScanner
1075 * Returns %TRUE if the scanner has reached the end of
1076 * the file or text buffer.
1078 * Returns: %TRUE if the scanner has reached the end of
1079 * the file or text buffer
1082 g_scanner_eof (GScanner *scanner)
1084 g_return_val_if_fail (scanner != NULL, TRUE);
1086 return scanner->token == G_TOKEN_EOF || scanner->token == G_TOKEN_ERROR;
1090 * g_scanner_input_file:
1091 * @scanner: a #GScanner
1092 * @input_fd: a file descriptor
1094 * Prepares to scan a file.
1097 g_scanner_input_file (GScanner *scanner,
1100 g_return_if_fail (scanner != NULL);
1101 g_return_if_fail (input_fd >= 0);
1103 if (scanner->input_fd >= 0)
1104 g_scanner_sync_file_offset (scanner);
1106 scanner->token = G_TOKEN_NONE;
1107 scanner->value.v_int64 = 0;
1109 scanner->position = 0;
1110 scanner->next_token = G_TOKEN_NONE;
1112 scanner->input_fd = input_fd;
1113 scanner->text = NULL;
1114 scanner->text_end = NULL;
1116 if (!scanner->buffer)
1117 scanner->buffer = g_new (gchar, READ_BUFFER_SIZE + 1);
1121 * g_scanner_input_text:
1122 * @scanner: a #GScanner
1123 * @text: the text buffer to scan
1124 * @text_len: the length of the text buffer
1126 * Prepares to scan a text buffer.
1129 g_scanner_input_text (GScanner *scanner,
1133 g_return_if_fail (scanner != NULL);
1135 g_return_if_fail (text != NULL);
1139 if (scanner->input_fd >= 0)
1140 g_scanner_sync_file_offset (scanner);
1142 scanner->token = G_TOKEN_NONE;
1143 scanner->value.v_int64 = 0;
1145 scanner->position = 0;
1146 scanner->next_token = G_TOKEN_NONE;
1148 scanner->input_fd = -1;
1149 scanner->text = text;
1150 scanner->text_end = text + text_len;
1152 if (scanner->buffer)
1154 g_free (scanner->buffer);
1155 scanner->buffer = NULL;
1160 g_scanner_peek_next_char (GScanner *scanner)
1162 if (scanner->text < scanner->text_end)
1164 return *scanner->text;
1166 else if (scanner->input_fd >= 0)
1171 buffer = scanner->buffer;
1174 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
1176 while (count == -1 && (errno == EINTR || errno == EAGAIN));
1180 scanner->input_fd = -1;
1186 scanner->text = buffer;
1187 scanner->text_end = buffer + count;
1197 * g_scanner_sync_file_offset:
1198 * @scanner: a #GScanner
1200 * Rewinds the filedescriptor to the current buffer position
1201 * and blows the file read ahead buffer. This is useful for
1202 * third party uses of the scanners filedescriptor, which hooks
1203 * onto the current scanning position.
1206 g_scanner_sync_file_offset (GScanner *scanner)
1208 g_return_if_fail (scanner != NULL);
1210 /* for file input, rewind the filedescriptor to the current
1211 * buffer position and blow the file read ahead buffer. useful
1212 * for third party uses of our file descriptor, which hooks
1213 * onto the current scanning position.
1216 if (scanner->input_fd >= 0 && scanner->text_end > scanner->text)
1220 buffered = scanner->text_end - scanner->text;
1221 if (lseek (scanner->input_fd, - buffered, SEEK_CUR) >= 0)
1223 /* we succeeded, blow our buffer's contents now */
1224 scanner->text = NULL;
1225 scanner->text_end = NULL;
1233 g_scanner_get_char (GScanner *scanner,
1239 if (scanner->text < scanner->text_end)
1240 fchar = *(scanner->text++);
1241 else if (scanner->input_fd >= 0)
1246 buffer = scanner->buffer;
1249 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
1251 while (count == -1 && (errno == EINTR || errno == EAGAIN));
1255 scanner->input_fd = -1;
1260 scanner->text = buffer + 1;
1261 scanner->text_end = buffer + count;
1265 g_scanner_sync_file_offset (scanner);
1266 scanner->text_end = scanner->text;
1267 scanner->input_fd = -1;
1288 * g_scanner_unexp_token:
1289 * @scanner: a #GScanner
1290 * @expected_token: the expected token
1291 * @identifier_spec: a string describing how the scanner's user
1292 * refers to identifiers (%NULL defaults to "identifier").
1293 * This is used if @expected_token is %G_TOKEN_IDENTIFIER or
1294 * %G_TOKEN_IDENTIFIER_NULL.
1295 * @symbol_spec: a string describing how the scanner's user refers
1296 * to symbols (%NULL defaults to "symbol"). This is used if
1297 * @expected_token is %G_TOKEN_SYMBOL or any token value greater
1298 * than %G_TOKEN_LAST.
1299 * @symbol_name: the name of the symbol, if the scanner's current
1300 * token is a symbol.
1301 * @message: a message string to output at the end of the
1302 * warning/error, or %NULL.
1303 * @is_error: if %TRUE it is output as an error. If %FALSE it is
1304 * output as a warning.
1306 * Outputs a message through the scanner's msg_handler,
1307 * resulting from an unexpected token in the input stream.
1308 * Note that you should not call g_scanner_peek_next_token()
1309 * followed by g_scanner_unexp_token() without an intermediate
1310 * call to g_scanner_get_next_token(), as g_scanner_unexp_token()
1311 * evaluates the scanner's current token (not the peeked token)
1312 * to construct part of the message.
1315 g_scanner_unexp_token (GScanner *scanner,
1316 GTokenType expected_token,
1317 const gchar *identifier_spec,
1318 const gchar *symbol_spec,
1319 const gchar *symbol_name,
1320 const gchar *message,
1323 gchar *token_string;
1324 guint token_string_len;
1325 gchar *expected_string;
1326 guint expected_string_len;
1327 gchar *message_prefix;
1328 gboolean print_unexp;
1329 void (*msg_handler) (GScanner*, const gchar*, ...);
1331 g_return_if_fail (scanner != NULL);
1334 msg_handler = g_scanner_error;
1336 msg_handler = g_scanner_warn;
1338 if (!identifier_spec)
1339 identifier_spec = "identifier";
1341 symbol_spec = "symbol";
1343 token_string_len = 56;
1344 token_string = g_new (gchar, token_string_len + 1);
1345 expected_string_len = 64;
1346 expected_string = g_new (gchar, expected_string_len + 1);
1349 switch (scanner->token)
1352 _g_snprintf (token_string, token_string_len, "end of file");
1356 if (scanner->token >= 1 && scanner->token <= 255)
1358 if ((scanner->token >= ' ' && scanner->token <= '~') ||
1359 strchr (scanner->config->cset_identifier_first, scanner->token) ||
1360 strchr (scanner->config->cset_identifier_nth, scanner->token))
1361 _g_snprintf (token_string, token_string_len, "character `%c'", scanner->token);
1363 _g_snprintf (token_string, token_string_len, "character `\\%o'", scanner->token);
1366 else if (!scanner->config->symbol_2_token)
1368 _g_snprintf (token_string, token_string_len, "(unknown) token <%d>", scanner->token);
1372 case G_TOKEN_SYMBOL:
1373 if (expected_token == G_TOKEN_SYMBOL ||
1374 (scanner->config->symbol_2_token &&
1375 expected_token > G_TOKEN_LAST))
1376 print_unexp = FALSE;
1378 _g_snprintf (token_string,
1381 print_unexp ? "" : "invalid ",
1385 _g_snprintf (token_string,
1388 print_unexp ? "" : "invalid ",
1393 print_unexp = FALSE;
1394 expected_token = G_TOKEN_NONE;
1395 switch (scanner->value.v_error)
1397 case G_ERR_UNEXP_EOF:
1398 _g_snprintf (token_string, token_string_len, "scanner: unexpected end of file");
1401 case G_ERR_UNEXP_EOF_IN_STRING:
1402 _g_snprintf (token_string, token_string_len, "scanner: unterminated string constant");
1405 case G_ERR_UNEXP_EOF_IN_COMMENT:
1406 _g_snprintf (token_string, token_string_len, "scanner: unterminated comment");
1409 case G_ERR_NON_DIGIT_IN_CONST:
1410 _g_snprintf (token_string, token_string_len, "scanner: non digit in constant");
1413 case G_ERR_FLOAT_RADIX:
1414 _g_snprintf (token_string, token_string_len, "scanner: invalid radix for floating constant");
1417 case G_ERR_FLOAT_MALFORMED:
1418 _g_snprintf (token_string, token_string_len, "scanner: malformed floating constant");
1421 case G_ERR_DIGIT_RADIX:
1422 _g_snprintf (token_string, token_string_len, "scanner: digit is beyond radix");
1427 _g_snprintf (token_string, token_string_len, "scanner: unknown error");
1433 _g_snprintf (token_string, token_string_len, "character `%c'", scanner->value.v_char);
1436 case G_TOKEN_IDENTIFIER:
1437 case G_TOKEN_IDENTIFIER_NULL:
1438 if (expected_token == G_TOKEN_IDENTIFIER ||
1439 expected_token == G_TOKEN_IDENTIFIER_NULL)
1440 print_unexp = FALSE;
1441 _g_snprintf (token_string,
1444 print_unexp ? "" : "invalid ",
1446 scanner->token == G_TOKEN_IDENTIFIER ? scanner->value.v_string : "null");
1449 case G_TOKEN_BINARY:
1453 if (scanner->config->store_int64)
1454 _g_snprintf (token_string, token_string_len, "number `%" G_GUINT64_FORMAT "'", scanner->value.v_int64);
1456 _g_snprintf (token_string, token_string_len, "number `%lu'", scanner->value.v_int);
1460 _g_snprintf (token_string, token_string_len, "number `%.3f'", scanner->value.v_float);
1463 case G_TOKEN_STRING:
1464 if (expected_token == G_TOKEN_STRING)
1465 print_unexp = FALSE;
1466 _g_snprintf (token_string,
1468 "%s%sstring constant \"%s\"",
1469 print_unexp ? "" : "invalid ",
1470 scanner->value.v_string[0] == 0 ? "empty " : "",
1471 scanner->value.v_string);
1472 token_string[token_string_len - 2] = '"';
1473 token_string[token_string_len - 1] = 0;
1476 case G_TOKEN_COMMENT_SINGLE:
1477 case G_TOKEN_COMMENT_MULTI:
1478 _g_snprintf (token_string, token_string_len, "comment");
1482 /* somehow the user's parsing code is screwed, there isn't much
1483 * we can do about it.
1484 * Note, a common case to trigger this is
1485 * g_scanner_peek_next_token(); g_scanner_unexp_token();
1486 * without an intermediate g_scanner_get_next_token().
1488 g_assert_not_reached ();
1493 switch (expected_token)
1495 gboolean need_valid;
1498 _g_snprintf (expected_string, expected_string_len, "end of file");
1501 if (expected_token >= 1 && expected_token <= 255)
1503 if ((expected_token >= ' ' && expected_token <= '~') ||
1504 strchr (scanner->config->cset_identifier_first, expected_token) ||
1505 strchr (scanner->config->cset_identifier_nth, expected_token))
1506 _g_snprintf (expected_string, expected_string_len, "character `%c'", expected_token);
1508 _g_snprintf (expected_string, expected_string_len, "character `\\%o'", expected_token);
1511 else if (!scanner->config->symbol_2_token)
1513 _g_snprintf (expected_string, expected_string_len, "(unknown) token <%d>", expected_token);
1517 case G_TOKEN_SYMBOL:
1518 need_valid = (scanner->token == G_TOKEN_SYMBOL ||
1519 (scanner->config->symbol_2_token &&
1520 scanner->token > G_TOKEN_LAST));
1521 _g_snprintf (expected_string,
1522 expected_string_len,
1524 need_valid ? "valid " : "",
1526 /* FIXME: should we attempt to lookup the symbol_name for symbol_2_token? */
1529 _g_snprintf (expected_string, expected_string_len, "%scharacter",
1530 scanner->token == G_TOKEN_CHAR ? "valid " : "");
1532 case G_TOKEN_BINARY:
1534 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1535 scanner->token == expected_token ? "valid " : "", tstring);
1539 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1540 scanner->token == expected_token ? "valid " : "", tstring);
1543 tstring = "integer";
1544 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1545 scanner->token == expected_token ? "valid " : "", tstring);
1548 tstring = "hexadecimal";
1549 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1550 scanner->token == expected_token ? "valid " : "", tstring);
1554 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1555 scanner->token == expected_token ? "valid " : "", tstring);
1557 case G_TOKEN_STRING:
1558 _g_snprintf (expected_string,
1559 expected_string_len,
1560 "%sstring constant",
1561 scanner->token == G_TOKEN_STRING ? "valid " : "");
1563 case G_TOKEN_IDENTIFIER:
1564 case G_TOKEN_IDENTIFIER_NULL:
1565 need_valid = (scanner->token == G_TOKEN_IDENTIFIER_NULL ||
1566 scanner->token == G_TOKEN_IDENTIFIER);
1567 _g_snprintf (expected_string,
1568 expected_string_len,
1570 need_valid ? "valid " : "",
1573 case G_TOKEN_COMMENT_SINGLE:
1574 tstring = "single-line";
1575 _g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1576 scanner->token == expected_token ? "valid " : "", tstring);
1578 case G_TOKEN_COMMENT_MULTI:
1579 tstring = "multi-line";
1580 _g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1581 scanner->token == expected_token ? "valid " : "", tstring);
1585 /* this is handled upon printout */
1589 if (message && message[0] != 0)
1590 message_prefix = " - ";
1593 message_prefix = "";
1596 if (expected_token == G_TOKEN_ERROR)
1598 msg_handler (scanner,
1599 "failure around %s%s%s",
1604 else if (expected_token == G_TOKEN_NONE)
1607 msg_handler (scanner,
1608 "unexpected %s%s%s",
1613 msg_handler (scanner,
1622 msg_handler (scanner,
1623 "unexpected %s, expected %s%s%s",
1629 msg_handler (scanner,
1630 "%s, expected %s%s%s",
1637 g_free (token_string);
1638 g_free (expected_string);
1642 g_scanner_get_token_i (GScanner *scanner,
1643 GTokenType *token_p,
1644 GTokenValue *value_p,
1650 g_scanner_free_value (token_p, value_p);
1651 g_scanner_get_token_ll (scanner, token_p, value_p, line_p, position_p);
1653 while (((*token_p > 0 && *token_p < 256) &&
1654 strchr (scanner->config->cset_skip_characters, *token_p)) ||
1655 (*token_p == G_TOKEN_CHAR &&
1656 strchr (scanner->config->cset_skip_characters, value_p->v_char)) ||
1657 (*token_p == G_TOKEN_COMMENT_MULTI &&
1658 scanner->config->skip_comment_multi) ||
1659 (*token_p == G_TOKEN_COMMENT_SINGLE &&
1660 scanner->config->skip_comment_single));
1664 case G_TOKEN_IDENTIFIER:
1665 if (scanner->config->identifier_2_string)
1666 *token_p = G_TOKEN_STRING;
1669 case G_TOKEN_SYMBOL:
1670 if (scanner->config->symbol_2_token)
1671 *token_p = (GTokenType) value_p->v_symbol;
1674 case G_TOKEN_BINARY:
1677 if (scanner->config->numbers_2_int)
1678 *token_p = G_TOKEN_INT;
1685 if (*token_p == G_TOKEN_INT &&
1686 scanner->config->int_2_float)
1688 *token_p = G_TOKEN_FLOAT;
1689 if (scanner->config->store_int64)
1692 /* work around error C2520, see gvaluetransform.c */
1693 value_p->v_float = (__int64)value_p->v_int64;
1695 value_p->v_float = value_p->v_int64;
1699 value_p->v_float = value_p->v_int;
1706 g_scanner_get_token_ll (GScanner *scanner,
1707 GTokenType *token_p,
1708 GTokenValue *value_p,
1712 GScannerConfig *config;
1714 gboolean in_comment_multi;
1715 gboolean in_comment_single;
1716 gboolean in_string_sq;
1717 gboolean in_string_dq;
1722 config = scanner->config;
1723 (*value_p).v_int64 = 0;
1725 if ((scanner->text >= scanner->text_end && scanner->input_fd < 0) ||
1726 scanner->token == G_TOKEN_EOF)
1728 *token_p = G_TOKEN_EOF;
1732 in_comment_multi = FALSE;
1733 in_comment_single = FALSE;
1734 in_string_sq = FALSE;
1735 in_string_dq = FALSE;
1738 do /* while (ch != 0) */
1740 gboolean dotted_float = FALSE;
1742 ch = g_scanner_get_char (scanner, line_p, position_p);
1745 token = G_TOKEN_NONE;
1747 /* this is *evil*, but needed ;(
1748 * we first check for identifier first character, because it
1749 * might interfere with other key chars like slashes or numbers
1751 if (config->scan_identifier &&
1752 ch && strchr (config->cset_identifier_first, ch))
1753 goto identifier_precedence;
1758 token = G_TOKEN_EOF;
1764 if (!config->scan_comment_multi ||
1765 g_scanner_peek_next_char (scanner) != '*')
1767 g_scanner_get_char (scanner, line_p, position_p);
1768 token = G_TOKEN_COMMENT_MULTI;
1769 in_comment_multi = TRUE;
1770 gstring = g_string_new (NULL);
1771 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1773 if (ch == '*' && g_scanner_peek_next_char (scanner) == '/')
1775 g_scanner_get_char (scanner, line_p, position_p);
1776 in_comment_multi = FALSE;
1780 gstring = g_string_append_c (gstring, ch);
1786 if (!config->scan_string_sq)
1788 token = G_TOKEN_STRING;
1789 in_string_sq = TRUE;
1790 gstring = g_string_new (NULL);
1791 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1795 in_string_sq = FALSE;
1799 gstring = g_string_append_c (gstring, ch);
1805 if (!config->scan_string_dq)
1807 token = G_TOKEN_STRING;
1808 in_string_dq = TRUE;
1809 gstring = g_string_new (NULL);
1810 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1814 in_string_dq = FALSE;
1821 ch = g_scanner_get_char (scanner, line_p, position_p);
1831 gstring = g_string_append_c (gstring, '\\');
1835 gstring = g_string_append_c (gstring, '\n');
1839 gstring = g_string_append_c (gstring, '\t');
1843 gstring = g_string_append_c (gstring, '\r');
1847 gstring = g_string_append_c (gstring, '\b');
1851 gstring = g_string_append_c (gstring, '\f');
1863 fchar = g_scanner_peek_next_char (scanner);
1864 if (fchar >= '0' && fchar <= '7')
1866 ch = g_scanner_get_char (scanner, line_p, position_p);
1867 i = i * 8 + ch - '0';
1868 fchar = g_scanner_peek_next_char (scanner);
1869 if (fchar >= '0' && fchar <= '7')
1871 ch = g_scanner_get_char (scanner, line_p, position_p);
1872 i = i * 8 + ch - '0';
1875 gstring = g_string_append_c (gstring, i);
1879 gstring = g_string_append_c (gstring, ch);
1884 gstring = g_string_append_c (gstring, ch);
1891 if (!config->scan_float)
1893 token = G_TOKEN_FLOAT;
1894 dotted_float = TRUE;
1895 ch = g_scanner_get_char (scanner, line_p, position_p);
1896 goto number_parsing;
1899 if (!config->scan_hex_dollar)
1901 token = G_TOKEN_HEX;
1902 ch = g_scanner_get_char (scanner, line_p, position_p);
1903 goto number_parsing;
1906 if (config->scan_octal)
1907 token = G_TOKEN_OCTAL;
1909 token = G_TOKEN_INT;
1910 ch = g_scanner_peek_next_char (scanner);
1911 if (config->scan_hex && (ch == 'x' || ch == 'X'))
1913 token = G_TOKEN_HEX;
1914 g_scanner_get_char (scanner, line_p, position_p);
1915 ch = g_scanner_get_char (scanner, line_p, position_p);
1918 token = G_TOKEN_ERROR;
1919 value.v_error = G_ERR_UNEXP_EOF;
1923 if (g_scanner_char_2_num (ch, 16) < 0)
1925 token = G_TOKEN_ERROR;
1926 value.v_error = G_ERR_DIGIT_RADIX;
1931 else if (config->scan_binary && (ch == 'b' || ch == 'B'))
1933 token = G_TOKEN_BINARY;
1934 g_scanner_get_char (scanner, line_p, position_p);
1935 ch = g_scanner_get_char (scanner, line_p, position_p);
1938 token = G_TOKEN_ERROR;
1939 value.v_error = G_ERR_UNEXP_EOF;
1943 if (g_scanner_char_2_num (ch, 10) < 0)
1945 token = G_TOKEN_ERROR;
1946 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1965 gboolean in_number = TRUE;
1968 if (token == G_TOKEN_NONE)
1969 token = G_TOKEN_INT;
1971 gstring = g_string_new (dotted_float ? "0." : "");
1972 gstring = g_string_append_c (gstring, ch);
1974 do /* while (in_number) */
1978 is_E = token == G_TOKEN_FLOAT && (ch == 'e' || ch == 'E');
1980 ch = g_scanner_peek_next_char (scanner);
1982 if (g_scanner_char_2_num (ch, 36) >= 0 ||
1983 (config->scan_float && ch == '.') ||
1984 (is_E && (ch == '+' || ch == '-')))
1986 ch = g_scanner_get_char (scanner, line_p, position_p);
1991 if (token != G_TOKEN_INT && token != G_TOKEN_OCTAL)
1993 value.v_error = token == G_TOKEN_FLOAT ? G_ERR_FLOAT_MALFORMED : G_ERR_FLOAT_RADIX;
1994 token = G_TOKEN_ERROR;
1999 token = G_TOKEN_FLOAT;
2000 gstring = g_string_append_c (gstring, ch);
2014 gstring = g_string_append_c (gstring, ch);
2019 if (token != G_TOKEN_FLOAT)
2021 token = G_TOKEN_ERROR;
2022 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2026 gstring = g_string_append_c (gstring, ch);
2031 if ((token != G_TOKEN_HEX && !config->scan_float) ||
2032 (token != G_TOKEN_HEX &&
2033 token != G_TOKEN_OCTAL &&
2034 token != G_TOKEN_FLOAT &&
2035 token != G_TOKEN_INT))
2037 token = G_TOKEN_ERROR;
2038 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2043 if (token != G_TOKEN_HEX)
2044 token = G_TOKEN_FLOAT;
2045 gstring = g_string_append_c (gstring, ch);
2050 if (token != G_TOKEN_HEX)
2052 token = G_TOKEN_ERROR;
2053 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2057 gstring = g_string_append_c (gstring, ch);
2067 if (token == G_TOKEN_FLOAT)
2068 value.v_float = g_strtod (gstring->str, &endptr);
2074 case G_TOKEN_BINARY:
2075 ui64 = g_ascii_strtoull (gstring->str, &endptr, 2);
2078 ui64 = g_ascii_strtoull (gstring->str, &endptr, 8);
2081 ui64 = g_ascii_strtoull (gstring->str, &endptr, 10);
2084 ui64 = g_ascii_strtoull (gstring->str, &endptr, 16);
2088 if (scanner->config->store_int64)
2089 value.v_int64 = ui64;
2093 if (endptr && *endptr)
2095 token = G_TOKEN_ERROR;
2096 if (*endptr == 'e' || *endptr == 'E')
2097 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2099 value.v_error = G_ERR_DIGIT_RADIX;
2101 g_string_free (gstring, TRUE);
2104 } /* number_parsing:... */
2110 if (config->cpair_comment_single &&
2111 ch == config->cpair_comment_single[0])
2113 token = G_TOKEN_COMMENT_SINGLE;
2114 in_comment_single = TRUE;
2115 gstring = g_string_new (NULL);
2116 ch = g_scanner_get_char (scanner, line_p, position_p);
2119 if (ch == config->cpair_comment_single[1])
2121 in_comment_single = FALSE;
2126 gstring = g_string_append_c (gstring, ch);
2127 ch = g_scanner_get_char (scanner, line_p, position_p);
2129 /* ignore a missing newline at EOF for single line comments */
2130 if (in_comment_single &&
2131 config->cpair_comment_single[1] == '\n')
2132 in_comment_single = FALSE;
2134 else if (config->scan_identifier && ch &&
2135 strchr (config->cset_identifier_first, ch))
2137 identifier_precedence:
2139 if (config->cset_identifier_nth && ch &&
2140 strchr (config->cset_identifier_nth,
2141 g_scanner_peek_next_char (scanner)))
2143 token = G_TOKEN_IDENTIFIER;
2144 gstring = g_string_new (NULL);
2145 gstring = g_string_append_c (gstring, ch);
2148 ch = g_scanner_get_char (scanner, line_p, position_p);
2149 gstring = g_string_append_c (gstring, ch);
2150 ch = g_scanner_peek_next_char (scanner);
2152 while (ch && strchr (config->cset_identifier_nth, ch));
2155 else if (config->scan_identifier_1char)
2157 token = G_TOKEN_IDENTIFIER;
2158 value.v_identifier = g_new0 (gchar, 2);
2159 value.v_identifier[0] = ch;
2165 if (config->char_2_token)
2169 token = G_TOKEN_CHAR;
2174 } /* default_case:... */
2177 g_assert (ch == 0 && token != G_TOKEN_NONE); /* paranoid */
2181 if (in_comment_multi || in_comment_single ||
2182 in_string_sq || in_string_dq)
2184 token = G_TOKEN_ERROR;
2187 g_string_free (gstring, TRUE);
2191 if (in_comment_multi || in_comment_single)
2192 value.v_error = G_ERR_UNEXP_EOF_IN_COMMENT;
2193 else /* (in_string_sq || in_string_dq) */
2194 value.v_error = G_ERR_UNEXP_EOF_IN_STRING;
2199 value.v_string = g_string_free (gstring, FALSE);
2203 if (token == G_TOKEN_IDENTIFIER)
2205 if (config->scan_symbols)
2210 scope_id = scanner->scope_id;
2211 key = g_scanner_lookup_internal (scanner, scope_id, value.v_identifier);
2212 if (!key && scope_id && scanner->config->scope_0_fallback)
2213 key = g_scanner_lookup_internal (scanner, 0, value.v_identifier);
2217 g_free (value.v_identifier);
2218 token = G_TOKEN_SYMBOL;
2219 value.v_symbol = key->value;
2223 if (token == G_TOKEN_IDENTIFIER &&
2224 config->scan_identifier_NULL &&
2225 strlen (value.v_identifier) == 4)
2227 gchar *null_upper = "NULL";
2228 gchar *null_lower = "null";
2230 if (scanner->config->case_sensitive)
2232 if (value.v_identifier[0] == null_upper[0] &&
2233 value.v_identifier[1] == null_upper[1] &&
2234 value.v_identifier[2] == null_upper[2] &&
2235 value.v_identifier[3] == null_upper[3])
2236 token = G_TOKEN_IDENTIFIER_NULL;
2240 if ((value.v_identifier[0] == null_upper[0] ||
2241 value.v_identifier[0] == null_lower[0]) &&
2242 (value.v_identifier[1] == null_upper[1] ||
2243 value.v_identifier[1] == null_lower[1]) &&
2244 (value.v_identifier[2] == null_upper[2] ||
2245 value.v_identifier[2] == null_lower[2]) &&
2246 (value.v_identifier[3] == null_upper[3] ||
2247 value.v_identifier[3] == null_lower[3]))
2248 token = G_TOKEN_IDENTIFIER_NULL;