Re: g_scanner



On Sun, 20 Sep 1998, David Bryant wrote:

> Hi all,
> 
> I'm trying to use a GScanner to parse an rc-file for my proggie. The
> problem is when parsing negative numbers I want the scanner to report a
> negative-number, rather than a minus-sign followed by a positive-number.
> ie,
> 
> here is my GScannerConfig:
> 
>   GScannerConfig scanner_config =
>   {
>     " \t\n",            /* cset_skip_characters */
>     (G_CSET_a_2_z       /* cset_identifier_first */
>     "_"
>     G_CSET_A_2_Z),
>     (G_CSET_a_2_z       /* cset_identifier_nth */
>     "_"
>     G_CSET_A_2_Z),
>     "#\n",              /* cpair_comment_style */
>     0,                  /* case_sensitive */
>     TRUE,               /* skip_comment_multi */
>     TRUE,               /* skip_comment_single */
>     FALSE,              /* scan_comment_multi */
>     TRUE,               /* scan_identifier */
>     FALSE,              /* scan_identifier_1char */
>     FALSE,              /* scan_identifier_NULL */
>     TRUE,               /* scan_symbols */
>     FALSE,              /* scan_binary */
>     FALSE,              /* scan_octal */
>     TRUE,               /* scan_float */
>     FALSE,              /* scan_hex */
>     FALSE,              /* scan_hex_dollar */
>     FALSE,              /* scan_string_sq */
>     FALSE,              /* scan_string_dq */
>     FALSE,              /* numbers_2_int */
>     TRUE,               /* int_2_float */
>     FALSE,              /* identifier_2_string */
>     TRUE,               /* char_2_token */
>     FALSE               /* symbol_2_token */
>   };
> 
> There doesn't seem to be any documentation on g_scanner_* yet, or is
> there ?

nope, there is no explicit documentation yet, but you can use the gtkrc.c
source as example code (though the symbol_2_token=TRUE approach it takes
is somewhat ugly). other than that, gtk-list is the correct place to ask
questions about GScanner/Glib related stuff (unfortunately i get around to
read gtk-app-devel-list only rarely).

regarding negative numbers, doing that automatically is essentially a nono
for a lexical scanner since negating numbers is nearly always a parsing
issue (e.g. is a '-' an unary operator or an infix operator?).
if GScanner starts out on doing -5 magic, there is no good reason to not
also evaluate things like -5+7 or even 6^7 and return the resulting number,
because the differentiation between lexical scanning and parsing becomes
a grey zone.
this is also the reason why GTokenValue defines v_int as a gulong rather
than a glong.

to get negative numbers working correctly (as unary operator),
i usually use code like:

  register gdouble number;
  register gboolean negate;

  negate = FALSE;
  g_scanner_get_next_token (scanner);
  while (scanner->token == '-')
    {
      negate = !negate;
      g_scanner_get_next_token (scanner);
    }
  if (scanner->token == G_TOKEN_FLOAT)
    number = scanner->value.v_float;
  else if (scanner->token == G_TOKEN_INT)
    number = scanner->value.v_int;

  number = negate ? - number : number;

(as you have int_2_float=TRUE in your scanner config, you can even skip the
if (scanner->token == G_TOKEN_INT) check).


> Thanks.
> David Bryant
> 

---
ciaoTJ



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]