comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Dynamic Variant Record Creation
Date: Tue, 16 Mar 2010 11:57:53 -0700 (PDT)
Date: 2010-03-16T11:57:53-07:00	[thread overview]
Message-ID: <c3fa9a0f-99c7-4fff-9310-7e4d769065db@s25g2000prd.googlegroups.com> (raw)
In-Reply-To: Xns9D3D861E55DA0WarrensBlatherings@188.40.43.213

On Mar 16, 10:11 am, Warren <ve3...@gmail.com> wrote:
> I am trying to solve a lexer Ada design problem, using a
> variant record (Token_Unit) definition:
>
>     type Token_Type is (
>         LEX_000,
>         LEX_BINARY,
>         LEX_SECTION,
>         '!',
>         '"',
>         '#',
>         '$',
>         ...etc..
>         '^',
>         '_',
>         '`',
>         LEX_SFUN3,
>         LEX_SFUN2,
>         LEX_SFUN1,
>         LEX_SFUNN,
>         LEX_FUN3,
>         LEX_FUN2,
>         LEX_FUN1,
>         LEX_FUNN,
>         ...etc...
>         LEX_EOF
>     );
>
>     type Token_Unit(Token : Token_Type := LEX_EOF) is
>         record
>             case Token is
>                 when LEX_SFUN3 | LEX_SFUN2 | LEX_SFUN1 | LEX_SFUNN
>                    | LEX_FUN3  | LEX_FUN2  | LEX_FUN1  | LEX_FUNN =>
>                     Func :      Func_Type;
>                 when LEX_IDENT | LEX_SIDENT | LEX_LNUMBER | LEX_NUMBER
>                    | LEX_HEX   | LEX_STRLIT | LEX_INLREM  | LEX_FLOAT
>                    | LEX_REM =>
>                     ID :        String_ID;                                                                                                                                                            
>                 when others =>
>                     null;
>             end case;
>         end record;
>
>     procedure Get_Token(Object : in out Lexer; Token : out Token_Unit) is
>
>         procedure Emit_Token(T : Token_Type) is
>             T : Token_Type := Token_Type'Val(Character'Pos(Ch));
>         begin
>             Token := ( Token => T );
>         end;
>
>         ...        
>
>     end;
>
> In the above, I need to create a Token_Unit from            
> a single character (there is no "id" in this case).

And how is the compiler supposed to know that there is no "id"?

When you use a record aggregate, you need to specify values for every
component of the record.  If the record has a variant part, then the
compiler won't know what components have to be specified unless it
knows what the discriminant is going to be; and it doesn't know what
the discriminants are going to be unless the value of the discriminant
is static.  That's why you get the error you're getting:

> But the issue is this:    
>
> *.adb:339:33: value for discriminant "token" must be static
> *.adb:339:33: "T" is not static constant or named number (RM 4.9(5))

You can't get what you want with an aggregate, but Georg's solution
will give you some of what you want, at least.  However, this will
leave all the components (besides the discriminants) uninitialized,
and you'll need to assign values into them.

By the way, now that Ada 2005 has the <> construct for aggregates,
it's just occurred to me that maybe 4.3.1(17) can be relaxed a bit, to
make it legal to specify a record aggregate with a nonstatic
discriminant for a variant record, *if* the only component
associations for aggregates are components that are not in variant
parts *and* there is an others=><> in the aggregate (or something
along those lines).  I don't know whether it's worthwhile, though.
(It wouldn't help too much in this exact example, since there are no
non-variant components, but if it were expanded to include, say, a
source file name, line number, and column number for each Token_Unit,
then there would be some benefit.)  I'll consider making a proposal
for this, depending on how loud a groan Randy, Bob, etc., make when
they read this idea...  :)

                                     -- Adam




  parent reply	other threads:[~2010-03-16 18:57 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-16 17:11 Dynamic Variant Record Creation Warren
2010-03-16 18:31 ` Georg Bauhaus
2010-03-16 18:57 ` Adam Beneschan [this message]
2010-03-16 20:01   ` Warren
2010-03-16 20:09     ` Jeffrey R. Carter
2010-03-16 20:24       ` Warren
2010-03-16 20:40         ` Robert A Duff
2010-03-16 20:44           ` Warren
2010-03-16 20:31     ` Robert A Duff
2010-03-16 20:59       ` Warren
2010-03-16 21:55         ` Jeffrey R. Carter
2010-03-17 14:40           ` Warren
2010-03-18 12:57           ` Warren
2010-03-16 21:58         ` Robert A Duff
2010-03-17 14:22           ` Charmed Snark
2010-03-17 14:49             ` Robert A Duff
2010-03-17 16:30               ` Warren
2010-03-16 21:15       ` Adam Beneschan
2010-03-16 23:24       ` Adam Beneschan
2010-03-16 20:15   ` Robert A Duff
2010-03-16 21:00     ` Warren
2010-03-16 23:39   ` Randy Brukardt
2010-03-16 23:43     ` Randy Brukardt
2010-03-17  0:15     ` Robert A Duff
2010-03-17 14:28       ` Warren
2010-03-18  0:02       ` Randy Brukardt
2010-03-17  4:20     ` Adam Beneschan
2010-03-18  0:13       ` Randy Brukardt
2010-03-18 13:00         ` Warren
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox