Simplify glib/glib/tests setup
[glib.git] / glib / gscanner.c
blobce56142a20abd7cbedd8c19ef3f86dec04003c8b
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/.
31 * MT safe
34 #include "config.h"
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <stdarg.h>
39 #include <string.h>
40 #include <stdio.h>
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
45 #include "gscanner.h"
47 #include "gprintfint.h"
48 #include "gstrfuncs.h"
49 #include "gstring.h"
50 #include "gtestutils.h"
52 #ifdef G_OS_WIN32
53 #include <io.h> /* For _read() */
54 #endif
57 /**
58 * SECTION:scanner
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.
66 /**
67 * GScannerMsgFunc:
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.
76 /**
77 * G_CSET_a_2_z:
79 * The set of lowercase ASCII alphabet characters.
80 * Used for specifying valid identifier characters
81 * in #GScannerConfig.
84 /**
85 * G_CSET_A_2_Z:
87 * The set of uppercase ASCII alphabet characters.
88 * Used for specifying valid identifier characters
89 * in #GScannerConfig.
92 /**
93 * G_CSET_LATINC:
95 * The set of uppercase ISO 8859-1 alphabet characters
96 * which are not ASCII characters.
97 * Used for specifying valid identifier characters
98 * in #GScannerConfig.
102 * G_CSET_LATINS:
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.
111 * GTokenType:
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_LEFT_BRACE: a '[' character
116 * @G_TOKEN_RIGHT_CURLY: a '}' character
117 * @G_TOKEN_RIGHT_PAREN: a ')' character
118 * @G_TOKEN_RIGHT_BRACE: a ']' character
119 * @G_TOKEN_EQUAL_SIGN: a '=' character
120 * @G_TOKEN_COMMA: a ',' character
121 * @G_TOKEN_NONE: not a token
122 * @G_TOKEN_ERROR: an error occurred
123 * @G_TOKEN_CHAR: a character
124 * @G_TOKEN_BINARY: a binary integer
125 * @G_TOKEN_OCTAL: an octal integer
126 * @G_TOKEN_INT: an integer
127 * @G_TOKEN_HEX: a hex integer
128 * @G_TOKEN_FLOAT: a floating point number
129 * @G_TOKEN_STRING: a string
130 * @G_TOKEN_SYMBOL: a symbol
131 * @G_TOKEN_IDENTIFIER: an identifier
132 * @G_TOKEN_IDENTIFIER_NULL: a null identifier
133 * @G_TOKEN_COMMENT_SINGLE: one line comment
134 * @G_TOKEN_COMMENT_MULTI: multi line comment
136 * The possible types of token returned from each
137 * g_scanner_get_next_token() call.
141 * GTokenValue:
142 * @v_symbol: token symbol value
143 * @v_identifier: token identifier value
144 * @v_binary: token binary integer value
145 * @v_octal: octal integer value
146 * @v_int: integer value
147 * @v_int64: 64-bit integer value
148 * @v_float: floating point value
149 * @v_hex: hex integer value
150 * @v_string: string value
151 * @v_comment: comment value
152 * @v_char: character value
153 * @v_error: error value
155 * A union holding the value of the token.
159 * GErrorType:
160 * @G_ERR_UNKNOWN: unknown error
161 * @G_ERR_UNEXP_EOF: unexpected end of file
162 * @G_ERR_UNEXP_EOF_IN_STRING: unterminated string constant
163 * @G_ERR_UNEXP_EOF_IN_COMMENT: unterminated comment
164 * @G_ERR_NON_DIGIT_IN_CONST: non-digit character in a number
165 * @G_ERR_DIGIT_RADIX: digit beyond radix in a number
166 * @G_ERR_FLOAT_RADIX: non-decimal floating point number
167 * @G_ERR_FLOAT_MALFORMED: malformed floating point number
169 * The possible errors, used in the @v_error field
170 * of #GTokenValue, when the token is a %G_TOKEN_ERROR.
174 * GScanner:
175 * @user_data: unused
176 * @max_parse_errors: unused
177 * @parse_errors: g_scanner_error() increments this field
178 * @input_name: name of input stream, featured by the default message handler
179 * @qdata: quarked data
180 * @config: link into the scanner configuration
181 * @token: token parsed by the last g_scanner_get_next_token()
182 * @value: value of the last token from g_scanner_get_next_token()
183 * @line: line number of the last token from g_scanner_get_next_token()
184 * @position: char number of the last token from g_scanner_get_next_token()
185 * @next_token: token parsed by the last g_scanner_peek_next_token()
186 * @next_value: value of the last token from g_scanner_peek_next_token()
187 * @next_line: line number of the last token from g_scanner_peek_next_token()
188 * @next_position: char number of the last token from g_scanner_peek_next_token()
189 * @msg_handler: handler function for _warn and _error
191 * The data structure representing a lexical scanner.
193 * You should set @input_name after creating the scanner, since
194 * it is used by the default message handler when displaying
195 * warnings and errors. If you are scanning a file, the filename
196 * would be a good choice.
198 * The @user_data and @max_parse_errors fields are not used.
199 * If you need to associate extra data with the scanner you
200 * can place them here.
202 * If you want to use your own message handler you can set the
203 * @msg_handler field. The type of the message handler function
204 * is declared by #GScannerMsgFunc.
208 * GScannerConfig:
209 * @cset_skip_characters: specifies which characters should be skipped
210 * by the scanner (the default is the whitespace characters: space,
211 * tab, carriage-return and line-feed).
212 * @cset_identifier_first: specifies the characters which can start
213 * identifiers (the default is #G_CSET_a_2_z, "_", and #G_CSET_A_2_Z).
214 * @cset_identifier_nth: specifies the characters which can be used
215 * in identifiers, after the first character (the default is
216 * #G_CSET_a_2_z, "_0123456789", #G_CSET_A_2_Z, #G_CSET_LATINS,
217 * #G_CSET_LATINC).
218 * @cpair_comment_single: specifies the characters at the start and
219 * end of single-line comments. The default is "#\n" which means
220 * that single-line comments start with a '#' and continue until
221 * a '\n' (end of line).
222 * @case_sensitive: specifies if symbols are case sensitive (the
223 * default is %FALSE).
224 * @skip_comment_multi: specifies if multi-line comments are skipped
225 * and not returned as tokens (the default is %TRUE).
226 * @skip_comment_single: specifies if single-line comments are skipped
227 * and not returned as tokens (the default is %TRUE).
228 * @scan_comment_multi: specifies if multi-line comments are recognized
229 * (the default is %TRUE).
230 * @scan_identifier: specifies if identifiers are recognized (the
231 * default is %TRUE).
232 * @scan_identifier_1char: specifies if single-character
233 * identifiers are recognized (the default is %FALSE).
234 * @scan_identifier_NULL: specifies if %NULL is reported as
235 * %G_TOKEN_IDENTIFIER_NULL (the default is %FALSE).
236 * @scan_symbols: specifies if symbols are recognized (the default
237 * is %TRUE).
238 * @scan_binary: specifies if binary numbers are recognized (the
239 * default is %FALSE).
240 * @scan_octal: specifies if octal numbers are recognized (the
241 * default is %TRUE).
242 * @scan_float: specifies if floating point numbers are recognized
243 * (the default is %TRUE).
244 * @scan_hex: specifies if hexadecimal numbers are recognized (the
245 * default is %TRUE).
246 * @scan_hex_dollar: specifies if '$' is recognized as a prefix for
247 * hexadecimal numbers (the default is %FALSE).
248 * @scan_string_sq: specifies if strings can be enclosed in single
249 * quotes (the default is %TRUE).
250 * @scan_string_dq: specifies if strings can be enclosed in double
251 * quotes (the default is %TRUE).
252 * @numbers_2_int: specifies if binary, octal and hexadecimal numbers
253 * are reported as #G_TOKEN_INT (the default is %TRUE).
254 * @int_2_float: specifies if all numbers are reported as %G_TOKEN_FLOAT
255 * (the default is %FALSE).
256 * @identifier_2_string: specifies if identifiers are reported as strings
257 * (the default is %FALSE).
258 * @char_2_token: specifies if characters are reported by setting
259 * <literal>token = ch</literal> or as %G_TOKEN_CHAR (the default
260 * is %TRUE).
261 * @symbol_2_token: specifies if symbols are reported by setting
262 * <literal>token = v_symbol</literal> or as %G_TOKEN_SYMBOL (the
263 * default is %FALSE).
264 * @scope_0_fallback: specifies if a symbol is searched for in the
265 * default scope in addition to the current scope (the default is %FALSE).
266 * @store_int64: use value.v_int64 rather than v_int
268 * Specifies the #GScanner parser configuration. Most settings can
269 * be changed during the parsing phase and will affect the lexical
270 * parsing of the next unpeeked token.
273 /* --- defines --- */
274 #define to_lower(c) ( \
275 (guchar) ( \
276 ( (((guchar)(c))>='A' && ((guchar)(c))<='Z') * ('a'-'A') ) | \
277 ( (((guchar)(c))>=192 && ((guchar)(c))<=214) * (224-192) ) | \
278 ( (((guchar)(c))>=216 && ((guchar)(c))<=222) * (248-216) ) | \
279 ((guchar)(c)) \
282 #define READ_BUFFER_SIZE (4000)
285 /* --- typedefs --- */
286 typedef struct _GScannerKey GScannerKey;
288 struct _GScannerKey
290 guint scope_id;
291 gchar *symbol;
292 gpointer value;
296 /* --- variables --- */
297 static const GScannerConfig g_scanner_config_template =
300 " \t\r\n"
301 ) /* cset_skip_characters */,
303 G_CSET_a_2_z
305 G_CSET_A_2_Z
306 ) /* cset_identifier_first */,
308 G_CSET_a_2_z
310 G_CSET_A_2_Z
311 G_CSET_DIGITS
312 G_CSET_LATINS
313 G_CSET_LATINC
314 ) /* cset_identifier_nth */,
315 ( "#\n" ) /* cpair_comment_single */,
317 FALSE /* case_sensitive */,
319 TRUE /* skip_comment_multi */,
320 TRUE /* skip_comment_single */,
321 TRUE /* scan_comment_multi */,
322 TRUE /* scan_identifier */,
323 FALSE /* scan_identifier_1char */,
324 FALSE /* scan_identifier_NULL */,
325 TRUE /* scan_symbols */,
326 FALSE /* scan_binary */,
327 TRUE /* scan_octal */,
328 TRUE /* scan_float */,
329 TRUE /* scan_hex */,
330 FALSE /* scan_hex_dollar */,
331 TRUE /* scan_string_sq */,
332 TRUE /* scan_string_dq */,
333 TRUE /* numbers_2_int */,
334 FALSE /* int_2_float */,
335 FALSE /* identifier_2_string */,
336 TRUE /* char_2_token */,
337 FALSE /* symbol_2_token */,
338 FALSE /* scope_0_fallback */,
339 FALSE /* store_int64 */,
343 /* --- prototypes --- */
344 static inline
345 GScannerKey* g_scanner_lookup_internal (GScanner *scanner,
346 guint scope_id,
347 const gchar *symbol);
348 static gboolean g_scanner_key_equal (gconstpointer v1,
349 gconstpointer v2);
350 static guint g_scanner_key_hash (gconstpointer v);
351 static void g_scanner_get_token_ll (GScanner *scanner,
352 GTokenType *token_p,
353 GTokenValue *value_p,
354 guint *line_p,
355 guint *position_p);
356 static void g_scanner_get_token_i (GScanner *scanner,
357 GTokenType *token_p,
358 GTokenValue *value_p,
359 guint *line_p,
360 guint *position_p);
362 static guchar g_scanner_peek_next_char (GScanner *scanner);
363 static guchar g_scanner_get_char (GScanner *scanner,
364 guint *line_p,
365 guint *position_p);
366 static void g_scanner_msg_handler (GScanner *scanner,
367 gchar *message,
368 gboolean is_error);
371 /* --- functions --- */
372 static inline gint
373 g_scanner_char_2_num (guchar c,
374 guchar base)
376 if (c >= '0' && c <= '9')
377 c -= '0';
378 else if (c >= 'A' && c <= 'Z')
379 c -= 'A' - 10;
380 else if (c >= 'a' && c <= 'z')
381 c -= 'a' - 10;
382 else
383 return -1;
385 if (c < base)
386 return c;
388 return -1;
392 * g_scanner_new:
393 * @config_templ: the initial scanner settings
395 * Creates a new #GScanner.
397 * The @config_templ structure specifies the initial settings
398 * of the scanner, which are copied into the #GScanner
399 * @config field. If you pass %NULL then the default settings
400 * are used.
402 * Returns: the new #GScanner
404 GScanner *
405 g_scanner_new (const GScannerConfig *config_templ)
407 GScanner *scanner;
409 if (!config_templ)
410 config_templ = &g_scanner_config_template;
412 scanner = g_new0 (GScanner, 1);
414 scanner->user_data = NULL;
415 scanner->max_parse_errors = 1;
416 scanner->parse_errors = 0;
417 scanner->input_name = NULL;
418 g_datalist_init (&scanner->qdata);
420 scanner->config = g_new0 (GScannerConfig, 1);
422 scanner->config->case_sensitive = config_templ->case_sensitive;
423 scanner->config->cset_skip_characters = config_templ->cset_skip_characters;
424 if (!scanner->config->cset_skip_characters)
425 scanner->config->cset_skip_characters = "";
426 scanner->config->cset_identifier_first = config_templ->cset_identifier_first;
427 scanner->config->cset_identifier_nth = config_templ->cset_identifier_nth;
428 scanner->config->cpair_comment_single = config_templ->cpair_comment_single;
429 scanner->config->skip_comment_multi = config_templ->skip_comment_multi;
430 scanner->config->skip_comment_single = config_templ->skip_comment_single;
431 scanner->config->scan_comment_multi = config_templ->scan_comment_multi;
432 scanner->config->scan_identifier = config_templ->scan_identifier;
433 scanner->config->scan_identifier_1char = config_templ->scan_identifier_1char;
434 scanner->config->scan_identifier_NULL = config_templ->scan_identifier_NULL;
435 scanner->config->scan_symbols = config_templ->scan_symbols;
436 scanner->config->scan_binary = config_templ->scan_binary;
437 scanner->config->scan_octal = config_templ->scan_octal;
438 scanner->config->scan_float = config_templ->scan_float;
439 scanner->config->scan_hex = config_templ->scan_hex;
440 scanner->config->scan_hex_dollar = config_templ->scan_hex_dollar;
441 scanner->config->scan_string_sq = config_templ->scan_string_sq;
442 scanner->config->scan_string_dq = config_templ->scan_string_dq;
443 scanner->config->numbers_2_int = config_templ->numbers_2_int;
444 scanner->config->int_2_float = config_templ->int_2_float;
445 scanner->config->identifier_2_string = config_templ->identifier_2_string;
446 scanner->config->char_2_token = config_templ->char_2_token;
447 scanner->config->symbol_2_token = config_templ->symbol_2_token;
448 scanner->config->scope_0_fallback = config_templ->scope_0_fallback;
449 scanner->config->store_int64 = config_templ->store_int64;
451 scanner->token = G_TOKEN_NONE;
452 scanner->value.v_int64 = 0;
453 scanner->line = 1;
454 scanner->position = 0;
456 scanner->next_token = G_TOKEN_NONE;
457 scanner->next_value.v_int64 = 0;
458 scanner->next_line = 1;
459 scanner->next_position = 0;
461 scanner->symbol_table = g_hash_table_new (g_scanner_key_hash, g_scanner_key_equal);
462 scanner->input_fd = -1;
463 scanner->text = NULL;
464 scanner->text_end = NULL;
465 scanner->buffer = NULL;
466 scanner->scope_id = 0;
468 scanner->msg_handler = g_scanner_msg_handler;
470 return scanner;
473 static inline void
474 g_scanner_free_value (GTokenType *token_p,
475 GTokenValue *value_p)
477 switch (*token_p)
479 case G_TOKEN_STRING:
480 case G_TOKEN_IDENTIFIER:
481 case G_TOKEN_IDENTIFIER_NULL:
482 case G_TOKEN_COMMENT_SINGLE:
483 case G_TOKEN_COMMENT_MULTI:
484 g_free (value_p->v_string);
485 break;
487 default:
488 break;
491 *token_p = G_TOKEN_NONE;
494 static void
495 g_scanner_destroy_symbol_table_entry (gpointer _key,
496 gpointer _value,
497 gpointer _data)
499 GScannerKey *key = _key;
501 g_free (key->symbol);
502 g_free (key);
506 * g_scanner_destroy:
507 * @scanner: a #GScanner
509 * Frees all memory used by the #GScanner.
511 void
512 g_scanner_destroy (GScanner *scanner)
514 g_return_if_fail (scanner != NULL);
516 g_datalist_clear (&scanner->qdata);
517 g_hash_table_foreach (scanner->symbol_table,
518 g_scanner_destroy_symbol_table_entry, NULL);
519 g_hash_table_destroy (scanner->symbol_table);
520 g_scanner_free_value (&scanner->token, &scanner->value);
521 g_scanner_free_value (&scanner->next_token, &scanner->next_value);
522 g_free (scanner->config);
523 g_free (scanner->buffer);
524 g_free (scanner);
527 static void
528 g_scanner_msg_handler (GScanner *scanner,
529 gchar *message,
530 gboolean is_error)
532 g_return_if_fail (scanner != NULL);
534 _g_fprintf (stderr, "%s:%d: ",
535 scanner->input_name ? scanner->input_name : "<memory>",
536 scanner->line);
537 if (is_error)
538 _g_fprintf (stderr, "error: ");
539 _g_fprintf (stderr, "%s\n", message);
543 * g_scanner_error:
544 * @scanner: a #GScanner
545 * @format: the message format. See the printf() documentation
546 * @...: the parameters to insert into the format string
548 * Outputs an error message, via the #GScanner message handler.
550 void
551 g_scanner_error (GScanner *scanner,
552 const gchar *format,
553 ...)
555 g_return_if_fail (scanner != NULL);
556 g_return_if_fail (format != NULL);
558 scanner->parse_errors++;
560 if (scanner->msg_handler)
562 va_list args;
563 gchar *string;
565 va_start (args, format);
566 string = g_strdup_vprintf (format, args);
567 va_end (args);
569 scanner->msg_handler (scanner, string, TRUE);
571 g_free (string);
576 * g_scanner_warn:
577 * @scanner: a #GScanner
578 * @format: the message format. See the printf() documentation
579 * @...: the parameters to insert into the format string
581 * Outputs a warning message, via the #GScanner message handler.
583 void
584 g_scanner_warn (GScanner *scanner,
585 const gchar *format,
586 ...)
588 g_return_if_fail (scanner != NULL);
589 g_return_if_fail (format != NULL);
591 if (scanner->msg_handler)
593 va_list args;
594 gchar *string;
596 va_start (args, format);
597 string = g_strdup_vprintf (format, args);
598 va_end (args);
600 scanner->msg_handler (scanner, string, FALSE);
602 g_free (string);
606 static gboolean
607 g_scanner_key_equal (gconstpointer v1,
608 gconstpointer v2)
610 const GScannerKey *key1 = v1;
611 const GScannerKey *key2 = v2;
613 return (key1->scope_id == key2->scope_id) && (strcmp (key1->symbol, key2->symbol) == 0);
616 static guint
617 g_scanner_key_hash (gconstpointer v)
619 const GScannerKey *key = v;
620 gchar *c;
621 guint h;
623 h = key->scope_id;
624 for (c = key->symbol; *c; c++)
625 h = (h << 5) - h + *c;
627 return h;
630 static inline GScannerKey*
631 g_scanner_lookup_internal (GScanner *scanner,
632 guint scope_id,
633 const gchar *symbol)
635 GScannerKey *key_p;
636 GScannerKey key;
638 key.scope_id = scope_id;
640 if (!scanner->config->case_sensitive)
642 gchar *d;
643 const gchar *c;
645 key.symbol = g_new (gchar, strlen (symbol) + 1);
646 for (d = key.symbol, c = symbol; *c; c++, d++)
647 *d = to_lower (*c);
648 *d = 0;
649 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
650 g_free (key.symbol);
652 else
654 key.symbol = (gchar*) symbol;
655 key_p = g_hash_table_lookup (scanner->symbol_table, &key);
658 return key_p;
662 * g_scanner_add_symbol:
663 * @scanner: a #GScanner
664 * @symbol: the symbol to add
665 * @value: the value of the symbol
667 * Adds a symbol to the default scope.
669 * Deprecated: 2.2: Use g_scanner_scope_add_symbol() instead.
673 * g_scanner_scope_add_symbol:
674 * @scanner: a #GScanner
675 * @scope_id: the scope id
676 * @symbol: the symbol to add
677 * @value: the value of the symbol
679 * Adds a symbol to the given scope.
681 void
682 g_scanner_scope_add_symbol (GScanner *scanner,
683 guint scope_id,
684 const gchar *symbol,
685 gpointer value)
687 GScannerKey *key;
689 g_return_if_fail (scanner != NULL);
690 g_return_if_fail (symbol != NULL);
692 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
694 if (!key)
696 key = g_new (GScannerKey, 1);
697 key->scope_id = scope_id;
698 key->symbol = g_strdup (symbol);
699 key->value = value;
700 if (!scanner->config->case_sensitive)
702 gchar *c;
704 c = key->symbol;
705 while (*c != 0)
707 *c = to_lower (*c);
708 c++;
711 g_hash_table_insert (scanner->symbol_table, key, key);
713 else
714 key->value = value;
718 * g_scanner_remove_symbol:
719 * @scanner: a #GScanner
720 * @symbol: the symbol to remove
722 * Removes a symbol from the default scope.
724 * Deprecated: 2.2: Use g_scanner_scope_remove_symbol() instead.
728 * g_scanner_scope_remove_symbol:
729 * @scanner: a #GScanner
730 * @scope_id: the scope id
731 * @symbol: the symbol to remove
733 * Removes a symbol from a scope.
735 void
736 g_scanner_scope_remove_symbol (GScanner *scanner,
737 guint scope_id,
738 const gchar *symbol)
740 GScannerKey *key;
742 g_return_if_fail (scanner != NULL);
743 g_return_if_fail (symbol != NULL);
745 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
747 if (key)
749 g_hash_table_remove (scanner->symbol_table, key);
750 g_free (key->symbol);
751 g_free (key);
756 * g_scanner_freeze_symbol_table:
757 * @scanner: a #GScanner
759 * There is no reason to use this macro, since it does nothing.
761 * Deprecated: 2.2: This macro does nothing.
765 * g_scanner_thaw_symbol_table:
766 * @scanner: a #GScanner
768 * There is no reason to use this macro, since it does nothing.
770 * Deprecated: 2.2: This macro does nothing.
774 * g_scanner_lookup_symbol:
775 * @scanner: a #GScanner
776 * @symbol: the symbol to look up
778 * Looks up a symbol in the current scope and return its value.
779 * If the symbol is not bound in the current scope, %NULL is
780 * returned.
782 * Returns: the value of @symbol in the current scope, or %NULL
783 * if @symbol is not bound in the current scope
785 gpointer
786 g_scanner_lookup_symbol (GScanner *scanner,
787 const gchar *symbol)
789 GScannerKey *key;
790 guint scope_id;
792 g_return_val_if_fail (scanner != NULL, NULL);
794 if (!symbol)
795 return NULL;
797 scope_id = scanner->scope_id;
798 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
799 if (!key && scope_id && scanner->config->scope_0_fallback)
800 key = g_scanner_lookup_internal (scanner, 0, symbol);
802 if (key)
803 return key->value;
804 else
805 return NULL;
809 * g_scanner_scope_lookup_symbol:
810 * @scanner: a #GScanner
811 * @scope_id: the scope id
812 * @symbol: the symbol to look up
814 * Looks up a symbol in a scope and return its value. If the
815 * symbol is not bound in the scope, %NULL is returned.
817 * Returns: the value of @symbol in the given scope, or %NULL
818 * if @symbol is not bound in the given scope.
821 gpointer
822 g_scanner_scope_lookup_symbol (GScanner *scanner,
823 guint scope_id,
824 const gchar *symbol)
826 GScannerKey *key;
828 g_return_val_if_fail (scanner != NULL, NULL);
830 if (!symbol)
831 return NULL;
833 key = g_scanner_lookup_internal (scanner, scope_id, symbol);
835 if (key)
836 return key->value;
837 else
838 return NULL;
842 * g_scanner_set_scope:
843 * @scanner: a #GScanner
844 * @scope_id: the new scope id
846 * Sets the current scope.
848 * Returns: the old scope id
850 guint
851 g_scanner_set_scope (GScanner *scanner,
852 guint scope_id)
854 guint old_scope_id;
856 g_return_val_if_fail (scanner != NULL, 0);
858 old_scope_id = scanner->scope_id;
859 scanner->scope_id = scope_id;
861 return old_scope_id;
864 static void
865 g_scanner_foreach_internal (gpointer _key,
866 gpointer _value,
867 gpointer _user_data)
869 GScannerKey *key;
870 gpointer *d;
871 GHFunc func;
872 gpointer user_data;
873 guint *scope_id;
875 d = _user_data;
876 func = (GHFunc) d[0];
877 user_data = d[1];
878 scope_id = d[2];
879 key = _value;
881 if (key->scope_id == *scope_id)
882 func (key->symbol, key->value, user_data);
886 * g_scanner_foreach_symbol:
887 * @scanner: a #GScanner
888 * @func: the function to call with each symbol
889 * @data: data to pass to the function
891 * Calls a function for each symbol in the default scope.
893 * Deprecated: 2.2: Use g_scanner_scope_foreach_symbol() instead.
897 * g_scanner_scope_foreach_symbol:
898 * @scanner: a #GScanner
899 * @scope_id: the scope id
900 * @func: the function to call for each symbol/value pair
901 * @user_data: user data to pass to the function
903 * Calls the given function for each of the symbol/value pairs
904 * in the given scope of the #GScanner. The function is passed
905 * the symbol and value of each pair, and the given @user_data
906 * parameter.
908 void
909 g_scanner_scope_foreach_symbol (GScanner *scanner,
910 guint scope_id,
911 GHFunc func,
912 gpointer user_data)
914 gpointer d[3];
916 g_return_if_fail (scanner != NULL);
918 d[0] = (gpointer) func;
919 d[1] = user_data;
920 d[2] = &scope_id;
922 g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
926 * g_scanner_peek_next_token:
927 * @scanner: a #GScanner
929 * Parses the next token, without removing it from the input stream.
930 * The token data is placed in the @next_token, @next_value, @next_line,
931 * and @next_position fields of the #GScanner structure.
933 * Note that, while the token is not removed from the input stream
934 * (i.e. the next call to g_scanner_get_next_token() will return the
935 * same token), it will not be reevaluated. This can lead to surprising
936 * results when changing scope or the scanner configuration after peeking
937 * the next token. Getting the next token after switching the scope or
938 * configuration will return whatever was peeked before, regardless of
939 * any symbols that may have been added or removed in the new scope.
941 * Returns: the type of the token
943 GTokenType
944 g_scanner_peek_next_token (GScanner *scanner)
946 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
948 if (scanner->next_token == G_TOKEN_NONE)
950 scanner->next_line = scanner->line;
951 scanner->next_position = scanner->position;
952 g_scanner_get_token_i (scanner,
953 &scanner->next_token,
954 &scanner->next_value,
955 &scanner->next_line,
956 &scanner->next_position);
959 return scanner->next_token;
963 * g_scanner_get_next_token:
964 * @scanner: a #GScanner
966 * Parses the next token just like g_scanner_peek_next_token()
967 * and also removes it from the input stream. The token data is
968 * placed in the @token, @value, @line, and @position fields of
969 * the #GScanner structure.
971 * Returns: the type of the token
973 GTokenType
974 g_scanner_get_next_token (GScanner *scanner)
976 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
978 if (scanner->next_token != G_TOKEN_NONE)
980 g_scanner_free_value (&scanner->token, &scanner->value);
982 scanner->token = scanner->next_token;
983 scanner->value = scanner->next_value;
984 scanner->line = scanner->next_line;
985 scanner->position = scanner->next_position;
986 scanner->next_token = G_TOKEN_NONE;
988 else
989 g_scanner_get_token_i (scanner,
990 &scanner->token,
991 &scanner->value,
992 &scanner->line,
993 &scanner->position);
995 return scanner->token;
999 * g_scanner_cur_token:
1000 * @scanner: a #GScanner
1002 * Gets the current token type. This is simply the @token
1003 * field in the #GScanner structure.
1005 * Returns: the current token type
1007 GTokenType
1008 g_scanner_cur_token (GScanner *scanner)
1010 g_return_val_if_fail (scanner != NULL, G_TOKEN_EOF);
1012 return scanner->token;
1016 * g_scanner_cur_value:
1017 * @scanner: a #GScanner
1019 * Gets the current token value. This is simply the @value
1020 * field in the #GScanner structure.
1022 * Returns: the current token value
1024 GTokenValue
1025 g_scanner_cur_value (GScanner *scanner)
1027 GTokenValue v;
1029 v.v_int64 = 0;
1031 g_return_val_if_fail (scanner != NULL, v);
1033 /* MSC isn't capable of handling return scanner->value; ? */
1035 v = scanner->value;
1037 return v;
1041 * g_scanner_cur_line:
1042 * @scanner: a #GScanner
1044 * Returns the current line in the input stream (counting
1045 * from 1). This is the line of the last token parsed via
1046 * g_scanner_get_next_token().
1048 * Returns: the current line
1050 guint
1051 g_scanner_cur_line (GScanner *scanner)
1053 g_return_val_if_fail (scanner != NULL, 0);
1055 return scanner->line;
1059 * g_scanner_cur_position:
1060 * @scanner: a #GScanner
1062 * Returns the current position in the current line (counting
1063 * from 0). This is the position of the last token parsed via
1064 * g_scanner_get_next_token().
1066 * Returns: the current position on the line
1068 guint
1069 g_scanner_cur_position (GScanner *scanner)
1071 g_return_val_if_fail (scanner != NULL, 0);
1073 return scanner->position;
1077 * g_scanner_eof:
1078 * @scanner: a #GScanner
1080 * Returns %TRUE if the scanner has reached the end of
1081 * the file or text buffer.
1083 * Returns: %TRUE if the scanner has reached the end of
1084 * the file or text buffer
1086 gboolean
1087 g_scanner_eof (GScanner *scanner)
1089 g_return_val_if_fail (scanner != NULL, TRUE);
1091 return scanner->token == G_TOKEN_EOF || scanner->token == G_TOKEN_ERROR;
1095 * g_scanner_input_file:
1096 * @scanner: a #GScanner
1097 * @input_fd: a file descriptor
1099 * Prepares to scan a file.
1101 void
1102 g_scanner_input_file (GScanner *scanner,
1103 gint input_fd)
1105 g_return_if_fail (scanner != NULL);
1106 g_return_if_fail (input_fd >= 0);
1108 if (scanner->input_fd >= 0)
1109 g_scanner_sync_file_offset (scanner);
1111 scanner->token = G_TOKEN_NONE;
1112 scanner->value.v_int64 = 0;
1113 scanner->line = 1;
1114 scanner->position = 0;
1115 scanner->next_token = G_TOKEN_NONE;
1117 scanner->input_fd = input_fd;
1118 scanner->text = NULL;
1119 scanner->text_end = NULL;
1121 if (!scanner->buffer)
1122 scanner->buffer = g_new (gchar, READ_BUFFER_SIZE + 1);
1126 * g_scanner_input_text:
1127 * @scanner: a #GScanner
1128 * @text: the text buffer to scan
1129 * @text_len: the length of the text buffer
1131 * Prepares to scan a text buffer.
1133 void
1134 g_scanner_input_text (GScanner *scanner,
1135 const gchar *text,
1136 guint text_len)
1138 g_return_if_fail (scanner != NULL);
1139 if (text_len)
1140 g_return_if_fail (text != NULL);
1141 else
1142 text = NULL;
1144 if (scanner->input_fd >= 0)
1145 g_scanner_sync_file_offset (scanner);
1147 scanner->token = G_TOKEN_NONE;
1148 scanner->value.v_int64 = 0;
1149 scanner->line = 1;
1150 scanner->position = 0;
1151 scanner->next_token = G_TOKEN_NONE;
1153 scanner->input_fd = -1;
1154 scanner->text = text;
1155 scanner->text_end = text + text_len;
1157 if (scanner->buffer)
1159 g_free (scanner->buffer);
1160 scanner->buffer = NULL;
1164 static guchar
1165 g_scanner_peek_next_char (GScanner *scanner)
1167 if (scanner->text < scanner->text_end)
1169 return *scanner->text;
1171 else if (scanner->input_fd >= 0)
1173 gint count;
1174 gchar *buffer;
1176 buffer = scanner->buffer;
1179 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
1181 while (count == -1 && (errno == EINTR || errno == EAGAIN));
1183 if (count < 1)
1185 scanner->input_fd = -1;
1187 return 0;
1189 else
1191 scanner->text = buffer;
1192 scanner->text_end = buffer + count;
1194 return *buffer;
1197 else
1198 return 0;
1202 * g_scanner_sync_file_offset:
1203 * @scanner: a #GScanner
1205 * Rewinds the filedescriptor to the current buffer position
1206 * and blows the file read ahead buffer. This is useful for
1207 * third party uses of the scanners filedescriptor, which hooks
1208 * onto the current scanning position.
1210 void
1211 g_scanner_sync_file_offset (GScanner *scanner)
1213 g_return_if_fail (scanner != NULL);
1215 /* for file input, rewind the filedescriptor to the current
1216 * buffer position and blow the file read ahead buffer. useful
1217 * for third party uses of our file descriptor, which hooks
1218 * onto the current scanning position.
1221 if (scanner->input_fd >= 0 && scanner->text_end > scanner->text)
1223 gint buffered;
1225 buffered = scanner->text_end - scanner->text;
1226 if (lseek (scanner->input_fd, - buffered, SEEK_CUR) >= 0)
1228 /* we succeeded, blow our buffer's contents now */
1229 scanner->text = NULL;
1230 scanner->text_end = NULL;
1232 else
1233 errno = 0;
1237 static guchar
1238 g_scanner_get_char (GScanner *scanner,
1239 guint *line_p,
1240 guint *position_p)
1242 guchar fchar;
1244 if (scanner->text < scanner->text_end)
1245 fchar = *(scanner->text++);
1246 else if (scanner->input_fd >= 0)
1248 gint count;
1249 gchar *buffer;
1251 buffer = scanner->buffer;
1254 count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
1256 while (count == -1 && (errno == EINTR || errno == EAGAIN));
1258 if (count < 1)
1260 scanner->input_fd = -1;
1261 fchar = 0;
1263 else
1265 scanner->text = buffer + 1;
1266 scanner->text_end = buffer + count;
1267 fchar = *buffer;
1268 if (!fchar)
1270 g_scanner_sync_file_offset (scanner);
1271 scanner->text_end = scanner->text;
1272 scanner->input_fd = -1;
1276 else
1277 fchar = 0;
1279 if (fchar == '\n')
1281 (*position_p) = 0;
1282 (*line_p)++;
1284 else if (fchar)
1286 (*position_p)++;
1289 return fchar;
1293 * g_scanner_unexp_token:
1294 * @scanner: a #GScanner
1295 * @expected_token: the expected token
1296 * @identifier_spec: a string describing how the scanner's user
1297 * refers to identifiers (%NULL defaults to "identifier").
1298 * This is used if @expected_token is %G_TOKEN_IDENTIFIER or
1299 * %G_TOKEN_IDENTIFIER_NULL.
1300 * @symbol_spec: a string describing how the scanner's user refers
1301 * to symbols (%NULL defaults to "symbol"). This is used if
1302 * @expected_token is %G_TOKEN_SYMBOL or any token value greater
1303 * than %G_TOKEN_LAST.
1304 * @symbol_name: the name of the symbol, if the scanner's current
1305 * token is a symbol.
1306 * @message: a message string to output at the end of the
1307 * warning/error, or %NULL.
1308 * @is_error: if %TRUE it is output as an error. If %FALSE it is
1309 * output as a warning.
1311 * Outputs a message through the scanner's msg_handler,
1312 * resulting from an unexpected token in the input stream.
1313 * Note that you should not call g_scanner_peek_next_token()
1314 * followed by g_scanner_unexp_token() without an intermediate
1315 * call to g_scanner_get_next_token(), as g_scanner_unexp_token()
1316 * evaluates the scanner's current token (not the peeked token)
1317 * to construct part of the message.
1319 void
1320 g_scanner_unexp_token (GScanner *scanner,
1321 GTokenType expected_token,
1322 const gchar *identifier_spec,
1323 const gchar *symbol_spec,
1324 const gchar *symbol_name,
1325 const gchar *message,
1326 gint is_error)
1328 gchar *token_string;
1329 guint token_string_len;
1330 gchar *expected_string;
1331 guint expected_string_len;
1332 gchar *message_prefix;
1333 gboolean print_unexp;
1334 void (*msg_handler) (GScanner*, const gchar*, ...);
1336 g_return_if_fail (scanner != NULL);
1338 if (is_error)
1339 msg_handler = g_scanner_error;
1340 else
1341 msg_handler = g_scanner_warn;
1343 if (!identifier_spec)
1344 identifier_spec = "identifier";
1345 if (!symbol_spec)
1346 symbol_spec = "symbol";
1348 token_string_len = 56;
1349 token_string = g_new (gchar, token_string_len + 1);
1350 expected_string_len = 64;
1351 expected_string = g_new (gchar, expected_string_len + 1);
1352 print_unexp = TRUE;
1354 switch (scanner->token)
1356 case G_TOKEN_EOF:
1357 _g_snprintf (token_string, token_string_len, "end of file");
1358 break;
1360 default:
1361 if (scanner->token >= 1 && scanner->token <= 255)
1363 if ((scanner->token >= ' ' && scanner->token <= '~') ||
1364 strchr (scanner->config->cset_identifier_first, scanner->token) ||
1365 strchr (scanner->config->cset_identifier_nth, scanner->token))
1366 _g_snprintf (token_string, token_string_len, "character `%c'", scanner->token);
1367 else
1368 _g_snprintf (token_string, token_string_len, "character `\\%o'", scanner->token);
1369 break;
1371 else if (!scanner->config->symbol_2_token)
1373 _g_snprintf (token_string, token_string_len, "(unknown) token <%d>", scanner->token);
1374 break;
1376 /* fall through */
1377 case G_TOKEN_SYMBOL:
1378 if (expected_token == G_TOKEN_SYMBOL ||
1379 (scanner->config->symbol_2_token &&
1380 expected_token > G_TOKEN_LAST))
1381 print_unexp = FALSE;
1382 if (symbol_name)
1383 _g_snprintf (token_string,
1384 token_string_len,
1385 "%s%s `%s'",
1386 print_unexp ? "" : "invalid ",
1387 symbol_spec,
1388 symbol_name);
1389 else
1390 _g_snprintf (token_string,
1391 token_string_len,
1392 "%s%s",
1393 print_unexp ? "" : "invalid ",
1394 symbol_spec);
1395 break;
1397 case G_TOKEN_ERROR:
1398 print_unexp = FALSE;
1399 expected_token = G_TOKEN_NONE;
1400 switch (scanner->value.v_error)
1402 case G_ERR_UNEXP_EOF:
1403 _g_snprintf (token_string, token_string_len, "scanner: unexpected end of file");
1404 break;
1406 case G_ERR_UNEXP_EOF_IN_STRING:
1407 _g_snprintf (token_string, token_string_len, "scanner: unterminated string constant");
1408 break;
1410 case G_ERR_UNEXP_EOF_IN_COMMENT:
1411 _g_snprintf (token_string, token_string_len, "scanner: unterminated comment");
1412 break;
1414 case G_ERR_NON_DIGIT_IN_CONST:
1415 _g_snprintf (token_string, token_string_len, "scanner: non digit in constant");
1416 break;
1418 case G_ERR_FLOAT_RADIX:
1419 _g_snprintf (token_string, token_string_len, "scanner: invalid radix for floating constant");
1420 break;
1422 case G_ERR_FLOAT_MALFORMED:
1423 _g_snprintf (token_string, token_string_len, "scanner: malformed floating constant");
1424 break;
1426 case G_ERR_DIGIT_RADIX:
1427 _g_snprintf (token_string, token_string_len, "scanner: digit is beyond radix");
1428 break;
1430 case G_ERR_UNKNOWN:
1431 default:
1432 _g_snprintf (token_string, token_string_len, "scanner: unknown error");
1433 break;
1435 break;
1437 case G_TOKEN_CHAR:
1438 _g_snprintf (token_string, token_string_len, "character `%c'", scanner->value.v_char);
1439 break;
1441 case G_TOKEN_IDENTIFIER:
1442 case G_TOKEN_IDENTIFIER_NULL:
1443 if (expected_token == G_TOKEN_IDENTIFIER ||
1444 expected_token == G_TOKEN_IDENTIFIER_NULL)
1445 print_unexp = FALSE;
1446 _g_snprintf (token_string,
1447 token_string_len,
1448 "%s%s `%s'",
1449 print_unexp ? "" : "invalid ",
1450 identifier_spec,
1451 scanner->token == G_TOKEN_IDENTIFIER ? scanner->value.v_string : "null");
1452 break;
1454 case G_TOKEN_BINARY:
1455 case G_TOKEN_OCTAL:
1456 case G_TOKEN_INT:
1457 case G_TOKEN_HEX:
1458 if (scanner->config->store_int64)
1459 _g_snprintf (token_string, token_string_len, "number `%" G_GUINT64_FORMAT "'", scanner->value.v_int64);
1460 else
1461 _g_snprintf (token_string, token_string_len, "number `%lu'", scanner->value.v_int);
1462 break;
1464 case G_TOKEN_FLOAT:
1465 _g_snprintf (token_string, token_string_len, "number `%.3f'", scanner->value.v_float);
1466 break;
1468 case G_TOKEN_STRING:
1469 if (expected_token == G_TOKEN_STRING)
1470 print_unexp = FALSE;
1471 _g_snprintf (token_string,
1472 token_string_len,
1473 "%s%sstring constant \"%s\"",
1474 print_unexp ? "" : "invalid ",
1475 scanner->value.v_string[0] == 0 ? "empty " : "",
1476 scanner->value.v_string);
1477 token_string[token_string_len - 2] = '"';
1478 token_string[token_string_len - 1] = 0;
1479 break;
1481 case G_TOKEN_COMMENT_SINGLE:
1482 case G_TOKEN_COMMENT_MULTI:
1483 _g_snprintf (token_string, token_string_len, "comment");
1484 break;
1486 case G_TOKEN_NONE:
1487 /* somehow the user's parsing code is screwed, there isn't much
1488 * we can do about it.
1489 * Note, a common case to trigger this is
1490 * g_scanner_peek_next_token(); g_scanner_unexp_token();
1491 * without an intermediate g_scanner_get_next_token().
1493 g_assert_not_reached ();
1494 break;
1498 switch (expected_token)
1500 gboolean need_valid;
1501 gchar *tstring;
1502 case G_TOKEN_EOF:
1503 _g_snprintf (expected_string, expected_string_len, "end of file");
1504 break;
1505 default:
1506 if (expected_token >= 1 && expected_token <= 255)
1508 if ((expected_token >= ' ' && expected_token <= '~') ||
1509 strchr (scanner->config->cset_identifier_first, expected_token) ||
1510 strchr (scanner->config->cset_identifier_nth, expected_token))
1511 _g_snprintf (expected_string, expected_string_len, "character `%c'", expected_token);
1512 else
1513 _g_snprintf (expected_string, expected_string_len, "character `\\%o'", expected_token);
1514 break;
1516 else if (!scanner->config->symbol_2_token)
1518 _g_snprintf (expected_string, expected_string_len, "(unknown) token <%d>", expected_token);
1519 break;
1521 /* fall through */
1522 case G_TOKEN_SYMBOL:
1523 need_valid = (scanner->token == G_TOKEN_SYMBOL ||
1524 (scanner->config->symbol_2_token &&
1525 scanner->token > G_TOKEN_LAST));
1526 _g_snprintf (expected_string,
1527 expected_string_len,
1528 "%s%s",
1529 need_valid ? "valid " : "",
1530 symbol_spec);
1531 /* FIXME: should we attempt to lookup the symbol_name for symbol_2_token? */
1532 break;
1533 case G_TOKEN_CHAR:
1534 _g_snprintf (expected_string, expected_string_len, "%scharacter",
1535 scanner->token == G_TOKEN_CHAR ? "valid " : "");
1536 break;
1537 case G_TOKEN_BINARY:
1538 tstring = "binary";
1539 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1540 scanner->token == expected_token ? "valid " : "", tstring);
1541 break;
1542 case G_TOKEN_OCTAL:
1543 tstring = "octal";
1544 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1545 scanner->token == expected_token ? "valid " : "", tstring);
1546 break;
1547 case G_TOKEN_INT:
1548 tstring = "integer";
1549 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1550 scanner->token == expected_token ? "valid " : "", tstring);
1551 break;
1552 case G_TOKEN_HEX:
1553 tstring = "hexadecimal";
1554 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1555 scanner->token == expected_token ? "valid " : "", tstring);
1556 break;
1557 case G_TOKEN_FLOAT:
1558 tstring = "float";
1559 _g_snprintf (expected_string, expected_string_len, "%snumber (%s)",
1560 scanner->token == expected_token ? "valid " : "", tstring);
1561 break;
1562 case G_TOKEN_STRING:
1563 _g_snprintf (expected_string,
1564 expected_string_len,
1565 "%sstring constant",
1566 scanner->token == G_TOKEN_STRING ? "valid " : "");
1567 break;
1568 case G_TOKEN_IDENTIFIER:
1569 case G_TOKEN_IDENTIFIER_NULL:
1570 need_valid = (scanner->token == G_TOKEN_IDENTIFIER_NULL ||
1571 scanner->token == G_TOKEN_IDENTIFIER);
1572 _g_snprintf (expected_string,
1573 expected_string_len,
1574 "%s%s",
1575 need_valid ? "valid " : "",
1576 identifier_spec);
1577 break;
1578 case G_TOKEN_COMMENT_SINGLE:
1579 tstring = "single-line";
1580 _g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1581 scanner->token == expected_token ? "valid " : "", tstring);
1582 break;
1583 case G_TOKEN_COMMENT_MULTI:
1584 tstring = "multi-line";
1585 _g_snprintf (expected_string, expected_string_len, "%scomment (%s)",
1586 scanner->token == expected_token ? "valid " : "", tstring);
1587 break;
1588 case G_TOKEN_NONE:
1589 case G_TOKEN_ERROR:
1590 /* this is handled upon printout */
1591 break;
1594 if (message && message[0] != 0)
1595 message_prefix = " - ";
1596 else
1598 message_prefix = "";
1599 message = "";
1601 if (expected_token == G_TOKEN_ERROR)
1603 msg_handler (scanner,
1604 "failure around %s%s%s",
1605 token_string,
1606 message_prefix,
1607 message);
1609 else if (expected_token == G_TOKEN_NONE)
1611 if (print_unexp)
1612 msg_handler (scanner,
1613 "unexpected %s%s%s",
1614 token_string,
1615 message_prefix,
1616 message);
1617 else
1618 msg_handler (scanner,
1619 "%s%s%s",
1620 token_string,
1621 message_prefix,
1622 message);
1624 else
1626 if (print_unexp)
1627 msg_handler (scanner,
1628 "unexpected %s, expected %s%s%s",
1629 token_string,
1630 expected_string,
1631 message_prefix,
1632 message);
1633 else
1634 msg_handler (scanner,
1635 "%s, expected %s%s%s",
1636 token_string,
1637 expected_string,
1638 message_prefix,
1639 message);
1642 g_free (token_string);
1643 g_free (expected_string);
1646 static void
1647 g_scanner_get_token_i (GScanner *scanner,
1648 GTokenType *token_p,
1649 GTokenValue *value_p,
1650 guint *line_p,
1651 guint *position_p)
1655 g_scanner_free_value (token_p, value_p);
1656 g_scanner_get_token_ll (scanner, token_p, value_p, line_p, position_p);
1658 while (((*token_p > 0 && *token_p < 256) &&
1659 strchr (scanner->config->cset_skip_characters, *token_p)) ||
1660 (*token_p == G_TOKEN_CHAR &&
1661 strchr (scanner->config->cset_skip_characters, value_p->v_char)) ||
1662 (*token_p == G_TOKEN_COMMENT_MULTI &&
1663 scanner->config->skip_comment_multi) ||
1664 (*token_p == G_TOKEN_COMMENT_SINGLE &&
1665 scanner->config->skip_comment_single));
1667 switch (*token_p)
1669 case G_TOKEN_IDENTIFIER:
1670 if (scanner->config->identifier_2_string)
1671 *token_p = G_TOKEN_STRING;
1672 break;
1674 case G_TOKEN_SYMBOL:
1675 if (scanner->config->symbol_2_token)
1676 *token_p = (GTokenType) value_p->v_symbol;
1677 break;
1679 case G_TOKEN_BINARY:
1680 case G_TOKEN_OCTAL:
1681 case G_TOKEN_HEX:
1682 if (scanner->config->numbers_2_int)
1683 *token_p = G_TOKEN_INT;
1684 break;
1686 default:
1687 break;
1690 if (*token_p == G_TOKEN_INT &&
1691 scanner->config->int_2_float)
1693 *token_p = G_TOKEN_FLOAT;
1694 if (scanner->config->store_int64)
1696 #ifdef _MSC_VER
1697 /* work around error C2520, see gvaluetransform.c */
1698 value_p->v_float = (__int64)value_p->v_int64;
1699 #else
1700 value_p->v_float = value_p->v_int64;
1701 #endif
1703 else
1704 value_p->v_float = value_p->v_int;
1707 errno = 0;
1710 static void
1711 g_scanner_get_token_ll (GScanner *scanner,
1712 GTokenType *token_p,
1713 GTokenValue *value_p,
1714 guint *line_p,
1715 guint *position_p)
1717 GScannerConfig *config;
1718 GTokenType token;
1719 gboolean in_comment_multi;
1720 gboolean in_comment_single;
1721 gboolean in_string_sq;
1722 gboolean in_string_dq;
1723 GString *gstring;
1724 GTokenValue value;
1725 guchar ch;
1727 config = scanner->config;
1728 (*value_p).v_int64 = 0;
1730 if ((scanner->text >= scanner->text_end && scanner->input_fd < 0) ||
1731 scanner->token == G_TOKEN_EOF)
1733 *token_p = G_TOKEN_EOF;
1734 return;
1737 in_comment_multi = FALSE;
1738 in_comment_single = FALSE;
1739 in_string_sq = FALSE;
1740 in_string_dq = FALSE;
1741 gstring = NULL;
1743 do /* while (ch != 0) */
1745 gboolean dotted_float = FALSE;
1747 ch = g_scanner_get_char (scanner, line_p, position_p);
1749 value.v_int64 = 0;
1750 token = G_TOKEN_NONE;
1752 /* this is *evil*, but needed ;(
1753 * we first check for identifier first character, because it
1754 * might interfere with other key chars like slashes or numbers
1756 if (config->scan_identifier &&
1757 ch && strchr (config->cset_identifier_first, ch))
1758 goto identifier_precedence;
1760 switch (ch)
1762 case 0:
1763 token = G_TOKEN_EOF;
1764 (*position_p)++;
1765 /* ch = 0; */
1766 break;
1768 case '/':
1769 if (!config->scan_comment_multi ||
1770 g_scanner_peek_next_char (scanner) != '*')
1771 goto default_case;
1772 g_scanner_get_char (scanner, line_p, position_p);
1773 token = G_TOKEN_COMMENT_MULTI;
1774 in_comment_multi = TRUE;
1775 gstring = g_string_new (NULL);
1776 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1778 if (ch == '*' && g_scanner_peek_next_char (scanner) == '/')
1780 g_scanner_get_char (scanner, line_p, position_p);
1781 in_comment_multi = FALSE;
1782 break;
1784 else
1785 gstring = g_string_append_c (gstring, ch);
1787 ch = 0;
1788 break;
1790 case '\'':
1791 if (!config->scan_string_sq)
1792 goto default_case;
1793 token = G_TOKEN_STRING;
1794 in_string_sq = TRUE;
1795 gstring = g_string_new (NULL);
1796 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1798 if (ch == '\'')
1800 in_string_sq = FALSE;
1801 break;
1803 else
1804 gstring = g_string_append_c (gstring, ch);
1806 ch = 0;
1807 break;
1809 case '"':
1810 if (!config->scan_string_dq)
1811 goto default_case;
1812 token = G_TOKEN_STRING;
1813 in_string_dq = TRUE;
1814 gstring = g_string_new (NULL);
1815 while ((ch = g_scanner_get_char (scanner, line_p, position_p)) != 0)
1817 if (ch == '"')
1819 in_string_dq = FALSE;
1820 break;
1822 else
1824 if (ch == '\\')
1826 ch = g_scanner_get_char (scanner, line_p, position_p);
1827 switch (ch)
1829 guint i;
1830 guint fchar;
1832 case 0:
1833 break;
1835 case '\\':
1836 gstring = g_string_append_c (gstring, '\\');
1837 break;
1839 case 'n':
1840 gstring = g_string_append_c (gstring, '\n');
1841 break;
1843 case 't':
1844 gstring = g_string_append_c (gstring, '\t');
1845 break;
1847 case 'r':
1848 gstring = g_string_append_c (gstring, '\r');
1849 break;
1851 case 'b':
1852 gstring = g_string_append_c (gstring, '\b');
1853 break;
1855 case 'f':
1856 gstring = g_string_append_c (gstring, '\f');
1857 break;
1859 case '0':
1860 case '1':
1861 case '2':
1862 case '3':
1863 case '4':
1864 case '5':
1865 case '6':
1866 case '7':
1867 i = 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';
1873 fchar = g_scanner_peek_next_char (scanner);
1874 if (fchar >= '0' && fchar <= '7')
1876 ch = g_scanner_get_char (scanner, line_p, position_p);
1877 i = i * 8 + ch - '0';
1880 gstring = g_string_append_c (gstring, i);
1881 break;
1883 default:
1884 gstring = g_string_append_c (gstring, ch);
1885 break;
1888 else
1889 gstring = g_string_append_c (gstring, ch);
1892 ch = 0;
1893 break;
1895 case '.':
1896 if (!config->scan_float)
1897 goto default_case;
1898 token = G_TOKEN_FLOAT;
1899 dotted_float = TRUE;
1900 ch = g_scanner_get_char (scanner, line_p, position_p);
1901 goto number_parsing;
1903 case '$':
1904 if (!config->scan_hex_dollar)
1905 goto default_case;
1906 token = G_TOKEN_HEX;
1907 ch = g_scanner_get_char (scanner, line_p, position_p);
1908 goto number_parsing;
1910 case '0':
1911 if (config->scan_octal)
1912 token = G_TOKEN_OCTAL;
1913 else
1914 token = G_TOKEN_INT;
1915 ch = g_scanner_peek_next_char (scanner);
1916 if (config->scan_hex && (ch == 'x' || ch == 'X'))
1918 token = G_TOKEN_HEX;
1919 g_scanner_get_char (scanner, line_p, position_p);
1920 ch = g_scanner_get_char (scanner, line_p, position_p);
1921 if (ch == 0)
1923 token = G_TOKEN_ERROR;
1924 value.v_error = G_ERR_UNEXP_EOF;
1925 (*position_p)++;
1926 break;
1928 if (g_scanner_char_2_num (ch, 16) < 0)
1930 token = G_TOKEN_ERROR;
1931 value.v_error = G_ERR_DIGIT_RADIX;
1932 ch = 0;
1933 break;
1936 else if (config->scan_binary && (ch == 'b' || ch == 'B'))
1938 token = G_TOKEN_BINARY;
1939 g_scanner_get_char (scanner, line_p, position_p);
1940 ch = g_scanner_get_char (scanner, line_p, position_p);
1941 if (ch == 0)
1943 token = G_TOKEN_ERROR;
1944 value.v_error = G_ERR_UNEXP_EOF;
1945 (*position_p)++;
1946 break;
1948 if (g_scanner_char_2_num (ch, 10) < 0)
1950 token = G_TOKEN_ERROR;
1951 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
1952 ch = 0;
1953 break;
1956 else
1957 ch = '0';
1958 /* fall through */
1959 case '1':
1960 case '2':
1961 case '3':
1962 case '4':
1963 case '5':
1964 case '6':
1965 case '7':
1966 case '8':
1967 case '9':
1968 number_parsing:
1970 gboolean in_number = TRUE;
1971 gchar *endptr;
1973 if (token == G_TOKEN_NONE)
1974 token = G_TOKEN_INT;
1976 gstring = g_string_new (dotted_float ? "0." : "");
1977 gstring = g_string_append_c (gstring, ch);
1979 do /* while (in_number) */
1981 gboolean is_E;
1983 is_E = token == G_TOKEN_FLOAT && (ch == 'e' || ch == 'E');
1985 ch = g_scanner_peek_next_char (scanner);
1987 if (g_scanner_char_2_num (ch, 36) >= 0 ||
1988 (config->scan_float && ch == '.') ||
1989 (is_E && (ch == '+' || ch == '-')))
1991 ch = g_scanner_get_char (scanner, line_p, position_p);
1993 switch (ch)
1995 case '.':
1996 if (token != G_TOKEN_INT && token != G_TOKEN_OCTAL)
1998 value.v_error = token == G_TOKEN_FLOAT ? G_ERR_FLOAT_MALFORMED : G_ERR_FLOAT_RADIX;
1999 token = G_TOKEN_ERROR;
2000 in_number = FALSE;
2002 else
2004 token = G_TOKEN_FLOAT;
2005 gstring = g_string_append_c (gstring, ch);
2007 break;
2009 case '0':
2010 case '1':
2011 case '2':
2012 case '3':
2013 case '4':
2014 case '5':
2015 case '6':
2016 case '7':
2017 case '8':
2018 case '9':
2019 gstring = g_string_append_c (gstring, ch);
2020 break;
2022 case '-':
2023 case '+':
2024 if (token != G_TOKEN_FLOAT)
2026 token = G_TOKEN_ERROR;
2027 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2028 in_number = FALSE;
2030 else
2031 gstring = g_string_append_c (gstring, ch);
2032 break;
2034 case 'e':
2035 case 'E':
2036 if ((token != G_TOKEN_HEX && !config->scan_float) ||
2037 (token != G_TOKEN_HEX &&
2038 token != G_TOKEN_OCTAL &&
2039 token != G_TOKEN_FLOAT &&
2040 token != G_TOKEN_INT))
2042 token = G_TOKEN_ERROR;
2043 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2044 in_number = FALSE;
2046 else
2048 if (token != G_TOKEN_HEX)
2049 token = G_TOKEN_FLOAT;
2050 gstring = g_string_append_c (gstring, ch);
2052 break;
2054 default:
2055 if (token != G_TOKEN_HEX)
2057 token = G_TOKEN_ERROR;
2058 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2059 in_number = FALSE;
2061 else
2062 gstring = g_string_append_c (gstring, ch);
2063 break;
2066 else
2067 in_number = FALSE;
2069 while (in_number);
2071 endptr = NULL;
2072 if (token == G_TOKEN_FLOAT)
2073 value.v_float = g_strtod (gstring->str, &endptr);
2074 else
2076 guint64 ui64 = 0;
2077 switch (token)
2079 case G_TOKEN_BINARY:
2080 ui64 = g_ascii_strtoull (gstring->str, &endptr, 2);
2081 break;
2082 case G_TOKEN_OCTAL:
2083 ui64 = g_ascii_strtoull (gstring->str, &endptr, 8);
2084 break;
2085 case G_TOKEN_INT:
2086 ui64 = g_ascii_strtoull (gstring->str, &endptr, 10);
2087 break;
2088 case G_TOKEN_HEX:
2089 ui64 = g_ascii_strtoull (gstring->str, &endptr, 16);
2090 break;
2091 default: ;
2093 if (scanner->config->store_int64)
2094 value.v_int64 = ui64;
2095 else
2096 value.v_int = ui64;
2098 if (endptr && *endptr)
2100 token = G_TOKEN_ERROR;
2101 if (*endptr == 'e' || *endptr == 'E')
2102 value.v_error = G_ERR_NON_DIGIT_IN_CONST;
2103 else
2104 value.v_error = G_ERR_DIGIT_RADIX;
2106 g_string_free (gstring, TRUE);
2107 gstring = NULL;
2108 ch = 0;
2109 } /* number_parsing:... */
2110 break;
2112 default:
2113 default_case:
2115 if (config->cpair_comment_single &&
2116 ch == config->cpair_comment_single[0])
2118 token = G_TOKEN_COMMENT_SINGLE;
2119 in_comment_single = TRUE;
2120 gstring = g_string_new (NULL);
2121 ch = g_scanner_get_char (scanner, line_p, position_p);
2122 while (ch != 0)
2124 if (ch == config->cpair_comment_single[1])
2126 in_comment_single = FALSE;
2127 ch = 0;
2128 break;
2131 gstring = g_string_append_c (gstring, ch);
2132 ch = g_scanner_get_char (scanner, line_p, position_p);
2134 /* ignore a missing newline at EOF for single line comments */
2135 if (in_comment_single &&
2136 config->cpair_comment_single[1] == '\n')
2137 in_comment_single = FALSE;
2139 else if (config->scan_identifier && ch &&
2140 strchr (config->cset_identifier_first, ch))
2142 identifier_precedence:
2144 if (config->cset_identifier_nth && ch &&
2145 strchr (config->cset_identifier_nth,
2146 g_scanner_peek_next_char (scanner)))
2148 token = G_TOKEN_IDENTIFIER;
2149 gstring = g_string_new (NULL);
2150 gstring = g_string_append_c (gstring, ch);
2153 ch = g_scanner_get_char (scanner, line_p, position_p);
2154 gstring = g_string_append_c (gstring, ch);
2155 ch = g_scanner_peek_next_char (scanner);
2157 while (ch && strchr (config->cset_identifier_nth, ch));
2158 ch = 0;
2160 else if (config->scan_identifier_1char)
2162 token = G_TOKEN_IDENTIFIER;
2163 value.v_identifier = g_new0 (gchar, 2);
2164 value.v_identifier[0] = ch;
2165 ch = 0;
2168 if (ch)
2170 if (config->char_2_token)
2171 token = ch;
2172 else
2174 token = G_TOKEN_CHAR;
2175 value.v_char = ch;
2177 ch = 0;
2179 } /* default_case:... */
2180 break;
2182 g_assert (ch == 0 && token != G_TOKEN_NONE); /* paranoid */
2184 while (ch != 0);
2186 if (in_comment_multi || in_comment_single ||
2187 in_string_sq || in_string_dq)
2189 token = G_TOKEN_ERROR;
2190 if (gstring)
2192 g_string_free (gstring, TRUE);
2193 gstring = NULL;
2195 (*position_p)++;
2196 if (in_comment_multi || in_comment_single)
2197 value.v_error = G_ERR_UNEXP_EOF_IN_COMMENT;
2198 else /* (in_string_sq || in_string_dq) */
2199 value.v_error = G_ERR_UNEXP_EOF_IN_STRING;
2202 if (gstring)
2204 value.v_string = g_string_free (gstring, FALSE);
2205 gstring = NULL;
2208 if (token == G_TOKEN_IDENTIFIER)
2210 if (config->scan_symbols)
2212 GScannerKey *key;
2213 guint scope_id;
2215 scope_id = scanner->scope_id;
2216 key = g_scanner_lookup_internal (scanner, scope_id, value.v_identifier);
2217 if (!key && scope_id && scanner->config->scope_0_fallback)
2218 key = g_scanner_lookup_internal (scanner, 0, value.v_identifier);
2220 if (key)
2222 g_free (value.v_identifier);
2223 token = G_TOKEN_SYMBOL;
2224 value.v_symbol = key->value;
2228 if (token == G_TOKEN_IDENTIFIER &&
2229 config->scan_identifier_NULL &&
2230 strlen (value.v_identifier) == 4)
2232 gchar *null_upper = "NULL";
2233 gchar *null_lower = "null";
2235 if (scanner->config->case_sensitive)
2237 if (value.v_identifier[0] == null_upper[0] &&
2238 value.v_identifier[1] == null_upper[1] &&
2239 value.v_identifier[2] == null_upper[2] &&
2240 value.v_identifier[3] == null_upper[3])
2241 token = G_TOKEN_IDENTIFIER_NULL;
2243 else
2245 if ((value.v_identifier[0] == null_upper[0] ||
2246 value.v_identifier[0] == null_lower[0]) &&
2247 (value.v_identifier[1] == null_upper[1] ||
2248 value.v_identifier[1] == null_lower[1]) &&
2249 (value.v_identifier[2] == null_upper[2] ||
2250 value.v_identifier[2] == null_lower[2]) &&
2251 (value.v_identifier[3] == null_upper[3] ||
2252 value.v_identifier[3] == null_lower[3]))
2253 token = G_TOKEN_IDENTIFIER_NULL;
2258 *token_p = token;
2259 *value_p = value;